KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > appserv > management > helper > Connect


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/helper/Connect.java,v 1.4 2005/12/25 03:50:35 tcfujii Exp $
26  * $Revision: 1.4 $
27  * $Date: 2005/12/25 03:50:35 $
28  */

29 package com.sun.appserv.management.helper;
30
31 import java.io.File JavaDoc;
32 import java.io.FileInputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34
35 import javax.net.ssl.HandshakeCompletedListener;
36
37 import com.sun.appserv.management.base.UploadDownloadMgr;
38
39 import com.sun.appserv.management.DomainRoot;
40 import com.sun.appserv.management.client.TLSParams;
41 import com.sun.appserv.management.client.AppserverConnectionSource;
42 import com.sun.appserv.management.client.HandshakeCompletedListenerImpl;
43 import com.sun.appserv.management.client.TrustStoreTrustManager;
44
45 /**
46     Miscellaneous helper routines for connecting to the Appserver.
47     @since AppServer 9.0
48  */

49 public class Connect
50 {
51     private Connect() {}
52     
53     /**
54         Connect to an Appserver using TLS (SSL) using the default TrustStore and
55         default TrustStore password.
56         <p>
57         If the server certificate is unknown, prompting will occur via System.out if
58         'promptForNewCertificate' is true; otherwise the connection will be rejected.
59         <p>
60         If a new server certificate is found, and the user enters "yes", then it
61         is added to the default truststore.
62         
63         @param host hostname or IP address
64         @param port port to which to connect
65         @param user admin user name
66         @param userPassword password for specified user
67         @param promptForUnknownCertificate prompts via System.out if the server
68             certificate is unrecognized, otherwise rejects the connection
69      */

70         public static AppserverConnectionSource
71     connectTLS(
72         final String JavaDoc host,
73         final int port,
74         final String JavaDoc user,
75         final String JavaDoc userPassword,
76         final boolean promptForUnknownCertificate )
77         throws IOException JavaDoc
78     {
79         final TLSParams tlsParams = createTLSParams( null, promptForUnknownCertificate );
80         
81         return new AppserverConnectionSource( AppserverConnectionSource.PROTOCOL_RMI,
82                 host, port, user, userPassword, tlsParams, null);
83     }
84     
85         
86         public static AppserverConnectionSource
87     connectNoTLS(
88         final String JavaDoc host,
89         final int port,
90         final String JavaDoc user,
91         final String JavaDoc userPassword)
92         throws IOException JavaDoc
93     {
94         return new AppserverConnectionSource(
95                 host, port, user, userPassword, null);
96     }
97     
98     
99     /**
100         The File will use the name
101         {@link AppserverConnectionSource#DEFAULT_TRUST_STORE_NAME}
102         in the user's home directory.
103         
104         @return a File for the default truststore. May not yet exist.
105      */

106         public static File JavaDoc
107     getDefaultTrustStore()
108     {
109         final String JavaDoc homeDir = System.getProperty( "user.home" );
110         final String JavaDoc sep = System.getProperty( "file.separator" );
111         
112         return new File JavaDoc ( homeDir + sep + AppserverConnectionSource.DEFAULT_TRUST_STORE_NAME );
113     }
114     
115     /**
116         Get TLSParams for the default truststore, assuming the default password.
117         
118         @param trustStorePassword a truststore, or null for the default one
119         @param promptForNewCertificate
120      */

121         public static TLSParams
122     createTLSParams(
123         final String JavaDoc trustStorePassword,
124         final boolean promptForNewCertificate)
125     {
126         return createTLSParams( null, trustStorePassword , promptForNewCertificate);
127     }
128     
129
130     /**
131         Get TLSParams for the specified truststore and password.
132         <p>
133         A {@link HandshakeCompletedListener} is installed which may be obtained
134         by calling {@link TLSParams#getHandshakeCompletedListener}.
135         <p>
136         If a new server certificate is found, and the user enters "yes" in response
137         to prompting, then the certificate is added to the truststore.
138         
139         @param trustStore a truststore, or null for the default one
140         @param trustStorePasswordIn the truststore password, if null the default one will be used
141         @param promptForUnknownCertificate prompts via System.out if the server
142             certificate is unrecognized
143      */

144         public static TLSParams
145     createTLSParams(
146         final File JavaDoc trustStore,
147         final String JavaDoc trustStorePasswordIn,
148         final boolean promptForUnknownCertificate )
149     {
150         final File JavaDoc trustStoreFile = (trustStore == null) ? getDefaultTrustStore() : trustStore;
151         final char[] trustStorePassword = ((trustStorePasswordIn == null) ?
152             AppserverConnectionSource.DEFAULT_TRUST_STORE_PASSWORD : trustStorePasswordIn).toCharArray();
153                     
154         final HandshakeCompletedListener handshakeCompletedListener =
155             new HandshakeCompletedListenerImpl();
156             
157         final TrustStoreTrustManager trustMgr =
158             new TrustStoreTrustManager( trustStoreFile, trustStorePassword);
159             
160         trustMgr.setPrompt( promptForUnknownCertificate );
161
162         final TLSParams tlsParams = new TLSParams( trustMgr, handshakeCompletedListener );
163
164         return( tlsParams );
165     }
166 }
167
168
169
170
171
172
173
174
Popular Tags