KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > ssl > RistrettoSSLSocketFactory


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.ssl;
37
38 import java.io.IOException JavaDoc;
39 import java.net.InetAddress JavaDoc;
40 import java.net.Socket JavaDoc;
41 import java.security.KeyManagementException JavaDoc;
42 import java.security.NoSuchAlgorithmException JavaDoc;
43
44 import javax.net.ssl.KeyManager;
45 import javax.net.ssl.SSLContext;
46 import javax.net.ssl.SSLSocketFactory;
47 import javax.net.ssl.TrustManager;
48
49 /**
50  * Factory to create a new SSL socket or on top of an existing plain socket.
51  *
52  *
53  * @author Timo Stich <tstich@users.sourceforge.net>
54  */

55
56 public class RistrettoSSLSocketFactory {
57     
58     private static RistrettoSSLSocketFactory myInstance;
59     
60     private SSLSocketFactory socketFactory;
61     
62     /**
63      * Gets the instance of the RistrettoSSLSocketFactory.
64      *
65      * @return the singleton instance of the factory.
66      */

67     public static RistrettoSSLSocketFactory getInstance() {
68         if( myInstance == null ) {
69             myInstance = new RistrettoSSLSocketFactory();
70         }
71         
72         return myInstance;
73     }
74     
75     protected RistrettoSSLSocketFactory() {
76         try {
77             SSLContext sslContext = SSLContext.getInstance("TLS");
78
79             sslContext.init(null, new TrustManager[] { new DefaultTrustManager() }, new java.security.SecureRandom JavaDoc());
80
81             socketFactory = sslContext.getSocketFactory();
82         } catch (NoSuchAlgorithmException JavaDoc e) {
83             e.printStackTrace(System.out);
84         } catch (KeyManagementException JavaDoc e) {
85             e.printStackTrace(System.out);
86         }
87     }
88     
89     /**
90      * Set the TrustManager of the used SSLContext.
91      *
92      * @param tm the Trustmanager used by the SSLContext.
93      */

94     public void setTrustManager(TrustManager tm) {
95         try {
96             SSLContext sslContext = SSLContext.getInstance("TLS");
97             
98             sslContext.init(null, new TrustManager[] { tm }, new java.security.SecureRandom JavaDoc());
99
100             socketFactory = sslContext.getSocketFactory();
101         } catch (NoSuchAlgorithmException JavaDoc e) {
102             e.printStackTrace(System.out);
103         } catch (KeyManagementException JavaDoc e) {
104             e.printStackTrace(System.out);
105         }
106     }
107     
108     /**
109      * Set the KeyManager of the SSLContext.
110      *
111      * @param km the KeyManager used by the SSLContext
112      */

113     public void setKeyManager(KeyManager km) {
114         try {
115             SSLContext sslContext = SSLContext.getInstance("TLS");
116
117             sslContext.init(new KeyManager[] { km }, null, new java.security.SecureRandom JavaDoc());
118
119             socketFactory = sslContext.getSocketFactory();
120         } catch (NoSuchAlgorithmException JavaDoc e) {
121             e.printStackTrace(System.out);
122         } catch (KeyManagementException JavaDoc e) {
123             e.printStackTrace(System.out);
124         }
125     }
126
127     /**
128      * Creates a new SSL Socket connected to the specified address and port.
129      *
130      * @param address the address to connect to
131      * @param port the port to connect to
132      * @return a new SSL Socket
133      * @throws IOException
134      */

135     public Socket JavaDoc createSocket(InetAddress JavaDoc address, int port) throws IOException JavaDoc {
136         return socketFactory.createSocket(address, port);
137     }
138
139     /**
140      * Creates a new SSL Socket connected to the specified address and port.
141      *
142      * @param address the address to connect to
143      * @param port the port to connect to
144      * @param localAddress the local InetAddress
145      * @param localPort the local port
146      * @return a new SSL Socket
147      * @throws IOException
148      */

149     public Socket JavaDoc createSocket(InetAddress JavaDoc address, int port, InetAddress JavaDoc localAddress, int localPort) throws IOException JavaDoc {
150         return socketFactory.createSocket(address, port, localAddress, localPort);
151     }
152
153     /**
154      * Creates a new SSL Socket connected to the specified name address and port.
155      *
156      * @param host the name address to connect to
157      * @param port the port to connect to
158      * @return a new SSL Socket
159      * @throws IOException
160      */

161     public Socket JavaDoc createSocket(String JavaDoc host, int port) throws IOException JavaDoc {
162         return socketFactory.createSocket(host, port);
163     }
164     
165     /**
166      * Creates a new SSL Socket connected to the specified name address and port.
167      *
168      * @param host the name address to connect to
169      * @param port the port to connect to
170      * @param localHost the local InetAddress
171      * @param localPort the local port
172      * @return a new SSL Socket
173      * @throws IOException
174      */

175     public Socket JavaDoc createSocket(String JavaDoc host, int port, InetAddress JavaDoc localHost, int localPort) throws IOException JavaDoc {
176         return socketFactory.createSocket(host, port, localHost, localPort);
177     }
178     
179     /**
180      * Creates a SSL Socket on top of the given Socket.
181      *
182      * @param socket plain socket on which the SSL Socket is built
183      * @param host the local port
184      * @param port the port to connect to
185      * @param autoClose shall the socket be closed when the SSL socket is closed?
186      * @return a new SSL Socket
187      * @throws IOException
188      */

189     public Socket JavaDoc createSocket(Socket JavaDoc socket, String JavaDoc host, int port, boolean autoClose) throws IOException JavaDoc {
190         return socketFactory.createSocket(socket, host, port, autoClose);
191     }
192 }
193
Popular Tags