KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > net > SocketFactoryFactory


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Marc Wick.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.common.net;
26
27 import java.io.File JavaDoc;
28 import java.io.FileInputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.security.GeneralSecurityException JavaDoc;
31 import java.security.KeyStore JavaDoc;
32
33 import javax.net.ServerSocketFactory;
34 import javax.net.SocketFactory;
35 import javax.net.ssl.SSLServerSocketFactory;
36 import javax.net.ssl.SSLSocketFactory;
37
38 import com.sun.net.ssl.KeyManager;
39 import com.sun.net.ssl.KeyManagerFactory;
40 import com.sun.net.ssl.SSLContext;
41 import com.sun.net.ssl.TrustManager;
42 import com.sun.net.ssl.TrustManagerFactory;
43
44 /**
45  * This class defines a SocketFactory
46  *
47  * @author <a HREF="mailto:marc.wick@monte-bre.ch">Marc Wick </a>
48  * @version 1.0
49  */

50 public class SocketFactoryFactory
51 {
52
53   /**
54    * create a server socket factory with the specified configuration
55    *
56    * @param config - the ssl configuration
57    * @return - the socket factory
58    * @throws SSLException - could not create factory
59    */

60   public static ServerSocketFactory createServerFactory(SSLConfiguration config)
61       throws SSLException
62   {
63     try
64     {
65
66       if (config == null)
67         // nothing todo return default SocketFactory
68
return ServerSocketFactory.getDefault();
69
70       SSLContext context = createSSLContext(config);
71       // Finally, we get a SocketFactory
72
SSLServerSocketFactory ssf = context.getServerSocketFactory();
73
74       if (!config.isClientAuthenticationRequired())
75         return ssf;
76
77       return new AuthenticatedServerSocketFactory(ssf);
78     }
79     catch (Exception JavaDoc e)
80     {
81       throw new SSLException(e);
82     }
83   }
84
85   /**
86    * create a socket factory with the specified configuration
87    *
88    * @param config - the ssl configuration
89    * @return - the socket factory
90    * @throws Exception - could not create factory
91    */

92   public static SocketFactory createFactory(SSLConfiguration config)
93       throws Exception JavaDoc
94   {
95     if (config == null)
96       // nothing todo return default SocketFactory
97
return SocketFactory.getDefault();
98
99     SSLContext context = createSSLContext(config);
100
101     // Finally, we get a SocketFactory
102
SSLSocketFactory ssf = context.getSocketFactory();
103
104     if (!config.isClientAuthenticationRequired())
105       return ssf;
106
107     return new AuthenticatedSocketFactory(ssf);
108   }
109
110   /**
111    * create a ssl context
112    *
113    * @param config - ssl config
114    * @return - the ssl context
115    * @throws Exception - problems initializing the content
116    */

117   public static SSLContext createSSLContext(SSLConfiguration config)
118       throws Exception JavaDoc
119   {
120
121     KeyManager[] kms = getKeyManagers(config.getKeyStore(), config
122         .getKeyStorePassword(), config.getKeyStoreKeyPassword());
123
124     TrustManager[] tms = getTrustManagers(config.getTrustStore(), config
125         .getTrustStorePassword());
126
127     // Now construct a SSLContext using these KeyManagers. We
128
// specify a null SecureRandom, indicating that the
129
// defaults should be used.
130
SSLContext context = SSLContext.getInstance("SSL");
131     context.init(kms, tms, null);
132     return context;
133   }
134
135   protected static KeyManager[] getKeyManagers(File JavaDoc keyStore,
136       String JavaDoc keyStorePassword, String JavaDoc keyPassword) throws IOException JavaDoc,
137       GeneralSecurityException JavaDoc
138   {
139     // First, get the default KeyManagerFactory.
140
String JavaDoc alg = KeyManagerFactory.getDefaultAlgorithm();
141     KeyManagerFactory kmFact = KeyManagerFactory.getInstance(alg);
142
143     // Next, set up the KeyStore to use. We need to load the file into
144
// a KeyStore instance.
145
FileInputStream JavaDoc fis = new FileInputStream JavaDoc(keyStore);
146     KeyStore JavaDoc ks = KeyStore.getInstance("jks");
147
148     char[] passwd = null;
149     if (keyStorePassword != null)
150     {
151       passwd = keyStorePassword.toCharArray();
152     }
153     ks.load(fis, passwd);
154     fis.close();
155
156     // Now we initialize the TrustManagerFactory with this KeyStore
157
kmFact.init(ks, keyPassword.toCharArray());
158
159     // And now get the TrustManagers
160
KeyManager[] kms = kmFact.getKeyManagers();
161     return kms;
162   }
163
164   protected static TrustManager[] getTrustManagers(File JavaDoc trustStore,
165       String JavaDoc trustStorePassword) throws IOException JavaDoc, GeneralSecurityException JavaDoc
166   {
167     // First, get the default TrustManagerFactory.
168
String JavaDoc alg = TrustManagerFactory.getDefaultAlgorithm();
169     TrustManagerFactory tmFact = TrustManagerFactory.getInstance(alg);
170
171     // Next, set up the TrustStore to use. We need to load the file into
172
// a KeyStore instance.
173
FileInputStream JavaDoc fis = new FileInputStream JavaDoc(trustStore);
174     KeyStore JavaDoc ks = KeyStore.getInstance("jks");
175     ks.load(fis, trustStorePassword.toCharArray());
176     fis.close();
177
178     // Now we initialize the TrustManagerFactory with this KeyStore
179
tmFact.init(ks);
180
181     // And now get the TrustManagers
182
TrustManager[] tms = tmFact.getTrustManagers();
183     return tms;
184   }
185 }
186
Popular Tags