PKIX path
building failed: sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target
Solution:
- 1. Issue related to path of jks file
- 2. please add the certificate to jks.
import java.io.FileInputStream; import java.io.PrintStream; import java.net.ServerSocket; import java.net.Socket; import java.security.KeyStore; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLServerSocketFactory; public class SocketServerTest{ public static void main(String args[]) throws Exception { char[] storepass = "testpass".toCharArray(); char[] keypass = "testpass".toCharArray(); String storename = "ServerKeystore"; SSLContext context = SSLContext.getInstance("TLS"); KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); FileInputStream fin = new FileInputStream(storename); KeyStore ks = KeyStore.getInstance("JKS"); ks.load(fin, storepass); kmf.init(ks, keypass); context.init(kmf.getKeyManagers(), null, null); SSLServerSocketFactory ssf = context.getServerSocketFactory(); ServerSocket ss = ssf.createServerSocket(9999); while (true) { Socket s = ss.accept(); PrintStream out = new PrintStream(s.getOutputStream()); out.print("Server Data Pushed to Client"); out.flush(); out.close(); s.close(); } } }
Ref:
http://docs.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#WhySSL
import java.io.IOException; import java.io.StringReader; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; public class SimpleXmlPullApp { public static void main (String args[]) throws XmlPullParserException, IOException { XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); XmlPullParser xpp = factory.newPullParser(); xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) ); int eventType = xpp.getEventType(); while (eventType != XmlPullParser.END_DOCUMENT) { if(eventType == XmlPullParser.START_DOCUMENT) { System.out.println("Start document"); } else if(eventType == XmlPullParser.START_TAG) { System.out.println("Start tag "+xpp.getName()); } else if(eventType == XmlPullParser.END_TAG) { System.out.println("End tag "+xpp.getName()); } else if(eventType == XmlPullParser.TEXT) { System.out.println("Text "+xpp.getText()); } eventType = xpp.next(); } System.out.println("End document"); } }
Start document Start tag foo Text Hello World! End tag foo End document
ThreadFactory
that creates daemon threads. See this answer hereExecutorService pool = Executors.newSingleThreadExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = Executors.defaultThreadFactory().newThread(runnable);
thread.setDaemon(true);
return thread;
}
});
package org.ohms.threads.locks; import java.util.Random; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * * Producer Consumer Problem...Solved with Locks * * @author * */ public class PCproblemWithLocks { private static int storedBox=-1;; public static void main(String[] args) throws InterruptedException { int totalProduction=1000; Lock lock=new ReentrantLock(); Condition condition= lock.newCondition(); Buffer buffer= new Buffer(); Thread producer= new Thread(new Producer(totalProduction, buffer,lock,condition)); Thread consumer= new Thread(new Consumer(totalProduction, buffer,lock,condition)); System.out.println("====================================="); producer.start(); consumer.start(); producer.join(); consumer.join(); System.out.println("Completed ....."); } /** * used to store the values or a common * object where two threads used. * * @author * */ static class Buffer { void put(int newValue) { storedBox = newValue; } int get() { return storedBox; } void clear() { storedBox=-1; } } /** * Used to store the Value * in the StoredBox(int variable defined above) * * @author * */ static class Producer implements Runnable { private int counter; private Buffer buffer; private Lock lock; private Condition condition; public Producer(int count,Buffer buffer,Lock lock,Condition condition) { this.counter=count; this.buffer=buffer; this.lock=lock; this.condition=condition; System.out.println("Producer Started placing the Values"); } @Override public void run() { try { this.lock.lock(); System.out.println(" Producer Lock Acquired "); for (int i = 0; i < this.counter; i++) { if(this.buffer.get()!=-1) { //System.out.println("Waiting to put>>>>>>"); condition.await(); System.out.println(" Producer Lock Acquired "); } int randomValue=i;//new Random().nextInt(100); this.buffer.put(randomValue); System.out.println("Produced -->"+randomValue); condition.signalAll(); System.out.println(" Producer Lock Released "); } } catch (InterruptedException e) { e.printStackTrace(); }finally{ this.lock.unlock(); } } } /** * used to retrieve/read the stored value * in the StoredValue Box * * @author Kumar * */ static class Consumer implements Runnable { private int counter; private Buffer buffer; private Lock lock; private Condition condition; public Consumer(int count,Buffer buffer,Lock lock,Condition condition) { this.counter=count; this.buffer=buffer; this.lock=lock; this.condition=condition; System.out.println("Consumer started Reading the Values "); } @Override public void run() { this.lock.lock(); System.out.println(" Consumer Lock Acquired "); try { for (int i = 0; i < this.counter; i++) { if(this.buffer.get()==-1) { //System.out.println("Waiting to get <<<<<<<<<<"); condition.await(); System.out.println(" Consumer Lock Acquired "); } System.out.println("Consumed -->"+this.buffer.get()); this.buffer.clear(); condition.signalAll(); System.out.println(" Consumer Lock Released "); } } catch (InterruptedException e) { e.printStackTrace(); }finally{ this.lock.unlock(); } } } }run the above program to know the usage of the Locks and condition in java .