KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > net > SSLJiveServerSocketFactory


1 /**
2  * $RCSfile: SSLJiveServerSocketFactory.java,v $
3  * $Revision: 1.3 $
4  * $Date: 2005/04/11 21:04:00 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.net;
13
14 import org.jivesoftware.util.Log;
15
16 import javax.net.ssl.KeyManagerFactory;
17 import javax.net.ssl.SSLContext;
18 import javax.net.ssl.SSLServerSocketFactory;
19 import javax.net.ssl.TrustManagerFactory;
20 import java.io.IOException JavaDoc;
21 import java.net.InetAddress JavaDoc;
22 import java.net.ServerSocket JavaDoc;
23 import java.security.KeyStore JavaDoc;
24
25 /**
26  * Securue socket factory wrapper allowing simple setup of all security
27  * SSL related parameters.
28  *
29  * @author Iain Shigeoka
30  */

31 public class SSLJiveServerSocketFactory extends SSLServerSocketFactory {
32
33     public static SSLServerSocketFactory getInstance(String JavaDoc algorithm,
34                                                      KeyStore JavaDoc keystore,
35                                                      KeyStore JavaDoc truststore) throws
36             IOException JavaDoc {
37
38         try {
39             SSLContext sslcontext = SSLContext.getInstance(algorithm);
40             SSLServerSocketFactory factory;
41             KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
42             keyFactory.init(keystore, SSLConfig.getKeyPassword().toCharArray());
43             TrustManagerFactory trustFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
44             trustFactory.init(truststore);
45
46             sslcontext.init(keyFactory.getKeyManagers(),
47                     trustFactory.getTrustManagers(),
48                     new java.security.SecureRandom JavaDoc());
49             factory = sslcontext.getServerSocketFactory();
50             return new SSLJiveServerSocketFactory(factory);
51         }
52         catch (Exception JavaDoc e) {
53             Log.error(e);
54             throw new IOException JavaDoc(e.getMessage());
55         }
56     }
57
58     private SSLServerSocketFactory factory;
59
60     private SSLJiveServerSocketFactory(SSLServerSocketFactory factory) {
61         this.factory = factory;
62     }
63
64     public ServerSocket JavaDoc createServerSocket(int i) throws IOException JavaDoc {
65         return factory.createServerSocket(i);
66     }
67
68     public ServerSocket JavaDoc createServerSocket(int i, int i1) throws IOException JavaDoc {
69         return factory.createServerSocket(i, i1);
70     }
71
72     public ServerSocket JavaDoc createServerSocket(int i, int i1, InetAddress JavaDoc inetAddress) throws IOException JavaDoc {
73         return factory.createServerSocket(i, i1, inetAddress);
74     }
75
76     public String JavaDoc[] getDefaultCipherSuites() {
77         return factory.getDefaultCipherSuites();
78     }
79
80     public String JavaDoc[] getSupportedCipherSuites() {
81         return factory.getSupportedCipherSuites();
82     }
83 }
84
Popular Tags