KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > jmx > ssl > AdminSslServerSocketFactory


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
24 /*
25  * AdminRmiSslServerSocketFactory.java
26  * Indentation Information:
27  * 0. Please (try to) preserve these settings.
28  * 1. No tabs are used, all spaces.
29  * 2. In vi/vim -
30  * :set tabstop=4 :set shiftwidth=4 :set softtabstop=4
31  * 3. In S1 Studio -
32  * 1. Tools->Options->Editor Settings->Java Editor->Tab Size = 4
33  * 2. Tools->Options->Indentation Engines->Java Indentation Engine->Expand Tabs to Spaces = True.
34  * 3. Tools->Options->Indentation Engines->Java Indentation Engine->Number of Spaces per Tab = 4.
35  * Unit Testing Information:
36  * 0. Is Standard Unit Test Written (y/n):
37  * 1. Unit Test Location: (The instructions should be in the Unit Test Class itself).
38  */

39
40 package com.sun.enterprise.admin.server.core.jmx.ssl;
41
42 import java.net.ServerSocket JavaDoc;
43 import java.net.InetAddress JavaDoc;
44 import javax.net.ssl.SSLServerSocket;
45 import javax.net.ssl.SSLServerSocketFactory;
46 import java.io.IOException JavaDoc;
47 import java.rmi.server.RMIServerSocketFactory JavaDoc;
48 import javax.net.ssl.KeyManager;
49 import javax.net.ssl.SSLContext;
50 import javax.net.ssl.TrustManager;
51 import javax.net.ssl.X509KeyManager;
52 import com.sun.enterprise.config.serverbeans.Ssl;
53
54 /* for PE/SE/EE pluggable security infrastructure */
55 import com.sun.enterprise.server.pluggable.SecuritySupport;
56 import com.sun.enterprise.security.SecurityUtil;
57 import com.sun.enterprise.security.SSLUtils;
58 import com.sun.enterprise.security.ssl.J2EEKeyManager;
59 import java.security.SecureRandom JavaDoc;
60
61
62 /* for PE/SE/EE plugaable security infrastructure */
63
64 /** This is the custom RMI server socket factory that uses the same keystore, truststore,
65  * certificate databases all the time. This factory will be used to create
66  * the server side sockets when rmi connector server is configured to use SSL. Please
67  * read the package.html. Note that this code depends upon the pluggability of the proper
68  * security infrastructure.
69  * @author Kedar.Mhaswade@sun.com
70  * @since Sun Java System Application Server 8.1
71  */

72 public class AdminSslServerSocketFactory implements RMIServerSocketFactory JavaDoc {
73     private final Ssl sslc;
74
75     private static final String JavaDoc DEFAULT_ADDRESS = "0.0.0.0";
76     private String JavaDoc address = DEFAULT_ADDRESS;
77     
78     public AdminSslServerSocketFactory(final Ssl sslc, String JavaDoc address) {
79         if (sslc == null)
80             throw new IllegalArgumentException JavaDoc("Internal: null ssl configuration");
81         this.sslc = sslc;
82         this.address = address;
83     }
84     
85     /** Implementation of the only method in {@link RMIServerSocketFactory}. This
86      * method is called for creating the server socket.
87      * @return instance of ServerSocket
88      */

89     public ServerSocket JavaDoc createServerSocket(final int port) throws IOException JavaDoc {
90         try {
91             /* My belief is that one of the bootstrap classes for
92             * initializing the SSL Context and the proper (pluggable)
93             * Key and Trust Managers are in place. We just need to leverage that.
94             */

95             // first get the SSLContext - returned as a new one - http://java.sun.com/j2se/1.4.2/docs/guide/security/jsse/JSSERefGuide.html#AppA
96
final SSLContext ctx = SSLContext.getInstance("TLSv1");
97             // get the key and trust managers
98
final KeyManager[] kms = SSLUtils.getKeyManagers();
99             J2EEKeyManager[] jkms = new J2EEKeyManager[kms.length];
100             for (int i = 0; i < kms.length; i++) {
101                 jkms[i] = new J2EEKeyManager((X509KeyManager)kms[i], sslc.getCertNickname());
102             }
103             final TrustManager[] tms = null; //not needed really untill we support client auth
104
final SecureRandom JavaDoc sr = null; // need to handle better?
105
// now initialize the SSLContext gotten above and return
106
ctx.init(jkms, tms, sr);
107             final SSLServerSocketFactory sf = ctx.getServerSocketFactory();
108             
109             InetAddress JavaDoc bindAddress = null;
110             ServerSocket JavaDoc sss = null;
111             if (address.equals(DEFAULT_ADDRESS))
112                 sss = sf.createServerSocket(port);
113             else {
114                 bindAddress = InetAddress.getByName(address);
115                 sss = sf.createServerSocket(port, 0, bindAddress);
116             }
117             debug(sss);
118             return ( sss );
119         }
120         catch (final Exception JavaDoc e) {
121             throw new IOException JavaDoc(e.getMessage());
122         }
123     }
124     private void debug (final ServerSocket JavaDoc sss) {
125         // prints the debug information - suppress after beta
126
final String JavaDoc prefix = "RMI/TLS Server Debug Message: " ;
127         final boolean DEBUG = Boolean.getBoolean("Debug");
128         if (sss != null) {
129             if (DEBUG) {
130                 System.out.println(prefix + "ServerSocket local port = " + sss.getLocalPort());
131                 System.out.println(prefix + "ServerSocket host address = " + sss.getInetAddress().getHostAddress());
132                 System.out.println(prefix + "ServerSocket bound status = " + sss.isBound());
133             }
134         }
135         else {
136             System.out.println(prefix + " Catastrophe: no server socket");
137         }
138     }
139 }
140
Popular Tags