KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > common > net > SocketFactoryFactory


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Marc Wick.
20  * Contributor(s): ______________________.
21  */

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

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

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

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

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