KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > web > security > SSLSocketFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.web.security;
24
25 import java.io.*;
26 import java.net.*;
27 import java.security.KeyStore JavaDoc;
28 import java.security.SecureRandom JavaDoc;
29
30 import javax.net.ssl.HandshakeCompletedListener;
31 import javax.net.ssl.HandshakeCompletedEvent;
32 import javax.net.ssl.KeyManager;
33 import javax.net.ssl.SSLContext;
34 import javax.net.ssl.SSLServerSocket;
35 import javax.net.ssl.SSLServerSocketFactory;
36 import javax.net.ssl.TrustManager;
37 import org.apache.catalina.net.ServerSocketFactory;
38 import com.sun.enterprise.log.Log;
39 import com.sun.enterprise.util.LocalStringManagerImpl;
40 import com.sun.enterprise.security.SSLUtils;
41 import com.sun.enterprise.ServerConfiguration;
42 import com.sun.web.server.*;
43 import com.sun.enterprise.server.J2EEServer;
44 import java.util.logging.*;
45 import com.sun.logging.*;
46
47
48 /**
49  * SSL server socket factory.
50  *
51  * @author Harish Prabandham
52  * @author Vivek Nagar
53  * @author Harpreet Singh
54  */

55
56 public class SSLSocketFactory implements org.apache.catalina.net.ServerSocketFactory {
57
58     static Logger _logger=LogDomains.getLogger(LogDomains.WEB_LOGGER);
59
60     private static final boolean clientAuth = false;
61
62     private static LocalStringManagerImpl localStrings =
63     new LocalStringManagerImpl(SSLSocketFactory.class);
64
65     private SSLContext context = null;
66     private javax.net.ssl.SSLServerSocketFactory factory = null;
67     private String JavaDoc cipherSuites[];
68     private static SecureRandom JavaDoc sr = J2EEServer.secureRandom;
69     private static KeyManager[] keyManagers = null;
70     private static TrustManager[] trustManagers = null;
71
72
73     /**
74      * Create the SSL socket factory. Initialize the key managers and
75      * trust managers which are passed to the SSL context.
76      */

77     public SSLSocketFactory () {
78     try {
79         if(keyManagers == null || trustManagers == null) {
80         SSLUtils.initStoresAtStartup();
81         }
82         context = SSLContext.getInstance("TLS");
83         context.init(keyManagers, trustManagers, sr);
84
85         factory = context.getServerSocketFactory();
86         cipherSuites = factory.getSupportedCipherSuites();
87         
88             for(int i=0; i < cipherSuites.length; ++i) {
89                 if (_logger.isLoggable(Level.FINEST)) {
90                     _logger.log(Level.FINEST,"Suite: " + cipherSuites[i]);
91                 }
92         }
93             
94     } catch(Exception JavaDoc e) {
95       _logger.log(Level.SEVERE,
96                       "web_security.excep_sslsockfact", e.getMessage());
97     }
98     }
99
100     /**
101      * Create the socket at the specified port.
102      * @param the port number.
103      * @return the SSL server socket.
104      */

105     public ServerSocket createSocket (int port)
106     throws IOException
107     {
108     SSLServerSocket socket =
109         (SSLServerSocket) factory.createServerSocket(port);
110     init(socket);
111     return socket;
112     }
113
114     /**
115      * Specify whether the server will require client authentication.
116      * @param the SSL server socket.
117      */

118     private void init(SSLServerSocket socket) {
119     // Some initialization goes here.....
120
// socket.setEnabledCipherSuites(cipherSuites);
121
socket.setNeedClientAuth(clientAuth);
122     }
123
124     /**
125      * Create the socket at the specified port.
126      * @param the port number.
127      * @return the SSL server socket.
128      */

129     public ServerSocket createSocket (int port, int backlog)
130     throws IOException
131     {
132     SSLServerSocket socket = (SSLServerSocket)
133         factory.createServerSocket(port, backlog);
134     init(socket);
135     return socket;
136     }
137
138     /**
139      * Create the socket at the specified port.
140      * @param the port number.
141      * @return the SSL server socket.
142      */

143     public ServerSocket createSocket (int port, int backlog, InetAddress ifAddress)
144     throws IOException
145     {
146     SSLServerSocket socket = (SSLServerSocket)
147         factory.createServerSocket(port, backlog, ifAddress);
148     init(socket);
149     return socket;
150     }
151
152     public static void setManagers(KeyManager[] kmgrs, TrustManager[] tmgrs) {
153         keyManagers = kmgrs;
154         trustManagers = tmgrs;
155     }
156 }
157
Popular Tags