KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > client > AdminRMISSLClientSocketFactory


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  * $Header: /cvs/glassfish/admin-core/mbeanapi/src/java/com/sun/appserv/management/client/AdminRMISSLClientSocketFactory.java,v 1.3 2005/12/25 03:49:04 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:49:04 $
28  */

29 package com.sun.appserv.management.client;
30
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.Serializable JavaDoc;
35 import java.net.Socket JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 import javax.net.ssl.SSLContext;
39 import javax.net.ssl.TrustManager;
40 import javax.net.ssl.X509TrustManager;
41 import javax.net.ssl.KeyManager;
42 import javax.net.ssl.KeyManagerFactory;
43 import javax.net.ssl.SSLSocket;
44 import javax.net.ssl.SSLSocketFactory;
45 import javax.net.ssl.TrustManagerFactory;
46 import javax.net.ssl.HandshakeCompletedListener;
47
48 import java.security.KeyStore JavaDoc;
49 import java.security.KeyStoreException JavaDoc;
50 import java.security.cert.CertificateException JavaDoc;
51 import java.security.NoSuchAlgorithmException JavaDoc;
52
53 import java.rmi.server.RMIClientSocketFactory JavaDoc;
54
55 import com.sun.appserv.management.client.TrustAnyTrustManager;
56
57
58 /**
59     <b>Not for public use</b>
60     <p>
61     RMISSLClientSocketFactory which allows the configuration of security parameters
62     by an RMI client; by default is not possible to configure these parameters.
63     This is the "stub" class that gets downloaded to the client.
64     <p>
65     Looks for RMISSLClientSocketFactoryEnv, and if found, uses it to get
66     configuration.
67  */

68 public class AdminRMISSLClientSocketFactory
69     implements RMIClientSocketFactory JavaDoc, Serializable JavaDoc
70 {
71     static final long serialVersionUID = 5096547838871926785L;
72     
73     private transient SSLSocketFactory mFactory = null;
74     
75     private transient MyEnvImpl mEnvImpl = null;
76     
77         public
78     AdminRMISSLClientSocketFactory()
79     {
80         mEnvImpl = new MyEnvImpl();
81     }
82     
83         private static TrustManager[]
84     getTrustAny()
85     {
86         final TrustManager[] trustManagers = new TrustManager[ 1 ];
87         trustManagers[ 0 ] = TrustAnyTrustManager.getInstance();
88         return( trustManagers );
89     }
90     
91     /**
92         If the 'env' class is not available eg a client that does not have the client jar,
93         then use this implementation. Lack of the env class implies that the client cannot
94         configure anything and must rely on system properties, which this class
95         handles.
96      */

97     private final class MyEnvImpl
98         implements AdminRMISSLClientSocketFactoryEnv
99     {
100         private transient boolean mTrace = false;
101         private MyEnvImpl() {}
102     
103             public TrustManager[]
104         getTrustManagers( )
105         {
106             final TrustStoreTrustManager mgr = TrustStoreTrustManager.getSystemInstance();
107             
108             return new TrustManager[] { mgr };
109         }
110         
111         public HandshakeCompletedListener getHandshakeCompletedListener( ) { return null; }
112         
113         public void setTrace( final boolean trace ) { mTrace = trace; }
114         public boolean getTrace() { return mTrace; }
115         
116         public Object JavaDoc getValue( final String JavaDoc key ) { return null; }
117     }
118     
119     
120     /**
121         Note that the environment is useless if it gets downloaded, since it would never be
122         possible for a client to configure it! However, that is OK as default
123         behavior will be used.
124      */

125         private synchronized AdminRMISSLClientSocketFactoryEnv
126     getEnv()
127     {
128         return( AdminRMISSLClientSocketFactoryEnvImpl.getInstance() );
129     }
130     
131         private final void
132     trace( Object JavaDoc o )
133     {
134         if ( getEnv().getTrace() )
135         {
136             final String JavaDoc name = this.getClass().getName();
137             Logger.getLogger( name ).info( toString() + ": " + o.toString() );
138         }
139     }
140     
141         private static char[]
142     toCharArray( final String JavaDoc s )
143     {
144         return( s == null ? null : s.toCharArray() );
145     }
146     
147     
148         private final SSLSocketFactory
149     createSocketFactory( final AdminRMISSLClientSocketFactoryEnv env )
150         throws IOException JavaDoc
151     {
152         SSLSocketFactory factory = null;
153         
154         try
155         {
156             final TrustManager[] trustManagers = env.getTrustManagers( );
157             
158             final SSLContext sslContext = SSLContext.getInstance( "TLSv1" );
159             sslContext.init( null, trustManagers, null );
160             factory = sslContext.getSocketFactory();
161         }
162         catch (Exception JavaDoc e)
163         {
164             e.printStackTrace();
165             throw (IOException JavaDoc) new IOException JavaDoc().initCause(e);
166         }
167         
168         return( factory );
169     }
170     
171     
172     
173         public synchronized Socket JavaDoc
174     createSocket(
175         final String JavaDoc host,
176         final int port)
177         throws IOException JavaDoc
178     {
179         final String JavaDoc target = host + ":" + port;
180         
181         trace( "createSocket: " + target );
182         
183         final AdminRMISSLClientSocketFactoryEnv env = getEnv();
184         
185         if ( mFactory == null)
186         {
187             mFactory = createSocketFactory( env );
188             trace( "createSocket: created socket factory" );
189         }
190         
191         //trace( "creating socket: " + target );
192
final SSLSocket sslSocket = (SSLSocket)mFactory.createSocket( host, port );
193         
194         final HandshakeCompletedListener listener = env.getHandshakeCompletedListener( );
195         if ( listener != null )
196         {
197             trace( "createSocket: added HandshakeCompletedListener: " + listener );
198             sslSocket.addHandshakeCompletedListener( listener );
199         }
200         
201         
202         trace( "created socket: " + target );
203         return( sslSocket );
204     }
205 }
206
207
208
209
210
211
212
213
214
215
216
217
Popular Tags