KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > joram > client > jms > tcp > ReliableSSLTcpClient


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2005 - ScalAgent Distributed Technologies
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Nicolas Tachker (ScalAgent)
21  * Contributor(s):
22  */

23 package org.objectweb.joram.client.jms.tcp;
24
25 import fr.dyade.aaa.util.*;
26
27 import java.io.*;
28 import java.net.*;
29 import java.util.*;
30
31 import java.io.FileInputStream JavaDoc;
32 import java.security.KeyStore JavaDoc;
33 import java.security.SecureRandom JavaDoc;
34 import javax.net.SocketFactory;
35 import javax.net.ssl.KeyManagerFactory;
36 import javax.net.ssl.TrustManagerFactory;
37 import javax.net.ssl.TrustManager;
38 import javax.net.ssl.SSLContext;
39 import javax.jms.*;
40
41 import org.objectweb.joram.client.jms.FactoryParameters;
42 import org.objectweb.joram.shared.JoramTracing;
43 import org.objectweb.util.monolog.api.BasicLevel;
44
45 public class ReliableSSLTcpClient extends ReliableTcpClient {
46
47   private final static String JavaDoc CIPHER = "org.objectweb.joram.cipherList";
48   private final static String JavaDoc KS = "org.objectweb.joram.keystore";
49   private final static String JavaDoc KS_PASS = "org.objectweb.joram.keystorepass";
50   private final static String JavaDoc KS_TYPE = "org.objectweb.joram.keystoretype";
51   private final static String JavaDoc SSLCONTEXT = "org.objectweb.joram.sslCtx";
52
53   public ReliableSSLTcpClient() {
54     super();
55   }
56
57   protected Socket createSocket(String JavaDoc hostName, int port)
58     throws Exception JavaDoc {
59     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
60       JoramTracing.dbgClient.log(
61         BasicLevel.DEBUG,
62         "ReliableSSLTcpClient.createSocket(" +
63         hostName+"," + port + ")");
64
65     SocketFactory socketFactory = createSocketFactory();
66     return socketFactory.createSocket(hostName, port);
67   }
68
69   private static SocketFactory createSocketFactory()
70     throws Exception JavaDoc {
71     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
72       JoramTracing.dbgClient.log(
73         BasicLevel.DEBUG, "ReliableSSLTcpClient.createSocketFactory()");
74
75     char[] keyStorePass = System.getProperty(KS_PASS,"jorampass").toCharArray();
76     String JavaDoc keystoreFile = System.getProperty(KS,"./joram_ks");
77     String JavaDoc sslContext = System.getProperty(SSLCONTEXT,"SSL");
78     String JavaDoc ksType = System.getProperty(KS_TYPE,"JKS");
79
80     if (JoramTracing.dbgClient.isLoggable(BasicLevel.DEBUG))
81       JoramTracing.dbgClient.log(
82         BasicLevel.DEBUG, "SSLTcpProxyService.createSocketFactory : keystoreFile=" +
83         keystoreFile);
84     
85     KeyStore JavaDoc keystore = KeyStore.getInstance(ksType);
86     keystore.load(new FileInputStream JavaDoc(keystoreFile),keyStorePass);
87     
88     KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
89     kmf.init(keystore,keyStorePass);
90     
91     TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
92     tmf.init(keystore);
93     TrustManager[] trustManagers = tmf.getTrustManagers();
94     
95     SSLContext ctx = SSLContext.getInstance(sslContext);
96     SecureRandom JavaDoc securerandom = SecureRandom.getInstance("SHA1PRNG");
97 // SecureRandom securerandom = null;
98
ctx.init(kmf.getKeyManagers(),trustManagers,securerandom);
99     
100     return (SocketFactory) ctx.getSocketFactory();
101   }
102 }
103
Popular Tags