KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ioc > connector > jrmps > JRMPSClientSocketFactory


1 /* JFox, the OpenSource J2EE Application Server
2  *
3  * Distributable under GNU LGPL license by gun.org
4  * more details please visit http://www.huihoo.org/jfox
5  */

6 package org.jfox.ioc.connector.jrmps;
7
8 import java.io.IOException JavaDoc;
9 import java.io.Serializable JavaDoc;
10 import java.net.Socket JavaDoc;
11 import java.rmi.server.RMIClientSocketFactory JavaDoc;
12 import java.security.KeyStore JavaDoc;
13 import javax.net.ssl.SSLContext;
14 import javax.net.ssl.TrustManagerFactory;
15
16
17 /**
18  * @author <a HREF="mailto:young_yy@hotmail.com">Young Yang</a>
19  */

20
21 public class JRMPSClientSocketFactory implements RMIClientSocketFactory JavaDoc, Serializable JavaDoc {
22
23     private String JavaDoc SSLTrustKeyFile = "jfox.truststore";
24     private String JavaDoc password = "jfox.org";
25
26
27     public JRMPSClientSocketFactory(String JavaDoc SSLKeyFile, String JavaDoc password) {
28         this.SSLTrustKeyFile = SSLKeyFile;
29         this.password = password;
30     }
31
32     public synchronized Socket JavaDoc createSocket(String JavaDoc host, int port) throws IOException JavaDoc {
33         try {
34             KeyStore JavaDoc keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
35             keyStore.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(SSLTrustKeyFile), password.toCharArray());
36
37 // KeyManagerFactory keyMgr = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
38
// keyMgr.init(keyStore, password.toCharArray());
39

40             TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
41             tmf.init(keyStore);
42
43             SSLContext sslCtx = SSLContext.getInstance("TLS");
44             sslCtx.init(null, tmf.getTrustManagers(), null);
45             return sslCtx.getSocketFactory().createSocket(host, port);
46         }
47         catch(Exception JavaDoc e) {
48             e.printStackTrace();
49             return null;
50         }
51     }
52
53     public static void main(String JavaDoc[] args) {
54
55     }
56 }
57
58
Popular Tags