KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > util > AppserverConnectionFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.j2ee.sun.util;
20
21 import java.io.IOException JavaDoc;
22 import javax.management.Attribute JavaDoc;
23 import com.sun.appserv.management.client.AppserverConnectionSource;
24 import com.sun.appserv.management.client.TLSParams;
25 import com.sun.appserv.management.client.TrustAnyTrustManager;
26 import com.sun.appserv.management.config.ConfigDottedNames;
27
28
29 /**
30  * Factory for creating the appropriate JMX connection to the Aplication Server
31  * for management operations through the AMX API.
32  */

33 public class AppserverConnectionFactory {
34     
35     /** Dotted name for the jmx connector port **/
36     private static final String JavaDoc JMX_CONNECTOR_PORT_DOTTED_NAME =
37         "server.admin-service.jmx-connector.system.port";
38     
39     /** Dotted name for the security enabled token **/
40     private static final String JavaDoc SECURITY_ENABLED_DOTTED_NAME =
41         "server.admin-service.jmx-connector.system.security-enabled";
42     
43     /**
44      * Constructor for AppserverConnectionFactory made private to avoid
45      * anyone trying to instantiate an instance of this static class. This is
46      * simply a good coding practice.
47      */

48     private AppserverConnectionFactory() {
49     }
50     
51     
52     /**
53      * Returns an AMX connection for connecting to the Sun Java System
54      * Application Server over SSL.
55      *
56      * @param host the name of the host on which the appserver is running
57      * @param port the port on which the admin server is running
58      * @param username username for logging into the admin server
59      * @param password password for logging into the admin server
60      *
61      * @return an AppserverConnectionSource
62      */

63     public static AppserverConnectionSource getAppserverConnection(
64             final String JavaDoc host, final int port, final String JavaDoc username,
65             final String JavaDoc password, final boolean isSecure) throws IOException JavaDoc {
66         return getAppserverConnection(host, port, username, password,
67             getDefaultTLSParams(isSecure), false);
68     }
69     
70     
71     /**
72      * Returns an AMX connection for connecting to the Sun Java System
73      * Application Server.
74      *
75      * @param host the name of the host on which the appserver is running
76      * @param port the port on which the admin server is running
77      * @param username username for logging into the admin server
78      * @param password password for logging into the admin server
79      * @param forceNew boolean true/false to determine whether to force the
80      * creation of a new AppserverConnectionSource
81      *
82      * @return an AppserverConnectionSource
83      */

84     public static AppserverConnectionSource getAppserverConnection(
85             final String JavaDoc host, final int port, final String JavaDoc username,
86             final String JavaDoc password, final boolean isSecure, boolean forceNew)
87                 throws IOException JavaDoc {
88         return getAppserverConnection(host, port, username, password,
89             getDefaultTLSParams(isSecure), forceNew);
90     }
91
92     
93     /**
94      * Returns an AMX connection for connecting to the Sun Java System
95      * Application Server over SSL.
96      *
97      * @param host the name of the host on which the appserver is running
98      * @param port the port on which the admin server is running
99      * @param username username for logging into the admin server
100      * @param password password for logging into the admin server
101      * @param tlsParams SSL parameters for secure connection
102      * @param forceNew boolean true/false to determine whether to force the
103      * creation of a new AppserverConnectionSource
104      *
105      * @return an AppserverConnectionSource
106      */

107     public static AppserverConnectionSource getAppserverConnection(
108             final String JavaDoc host, final int port, final String JavaDoc username,
109             final String JavaDoc password, final TLSParams tlsParams, boolean forceNew)
110                 throws IOException JavaDoc {
111         return getRMIAppserverConnectionSource(host, port, username, password,
112                     tlsParams);
113     }
114   
115     
116     /**
117      * Returns an AMX connection over HTTP for connecting to the Sun Java System
118      * Application Server over SSL.
119      *
120      * @param host the name of the host on which the appserver is running
121      * @param port the port on which the admin server is running
122      * @param username username for logging into the admin server
123      * @param password password for logging into the admin server
124      *
125      * @return an AppserverConnectionSource
126      */

127     public static AppserverConnectionSource getHTTPAppserverConnection(
128             final String JavaDoc host, final int port, final String JavaDoc username,
129             final String JavaDoc password, final boolean isSecure) throws IOException JavaDoc {
130         return getHTTPAppserverConnectionSource(host, port, username, password,
131             getDefaultTLSParams(isSecure));
132     }
133     
134     
135     /**
136      * Returns the AppserverConnectionSource connected to the RMI port running
137      * on DAS. It's retrieved by creating an HTTP AppserverConnectionSource
138      * that then uses dotted names for getting the port on which the JMX
139      * Connector is running. After getting the appropriate RMI port, the
140      * AppserverConnectionSource running over RMI is created.
141      *
142      * @param host the name of the host on which the appserver is running
143      * @param port the port on which the admin server is running
144      * @param username username for logging into the admin server
145      * @param password password for logging into the admin server
146      * @param tlsParams SSL parameters for secure connection
147      *
148      * @return the AppserverConnectionSource connected over RMI
149      */

150     static AppserverConnectionSource getRMIAppserverConnectionSource(
151             final String JavaDoc host, final int port, final String JavaDoc username,
152             final String JavaDoc password, final TLSParams tlsParams)
153                 throws IOException JavaDoc {
154         AppserverConnectionSource httpConn =
155                 getHTTPAppserverConnectionSource(host, port, username, password,
156                     tlsParams);
157         return new AppserverConnectionSource(
158             AppserverConnectionSource.PROTOCOL_RMI, host,
159             getJMXConnectorPort(httpConn), username, password, tlsParams, null);
160     }
161     
162     /**
163      * Returns the AppserverConnectionSource running over HTTP. Currently, there
164      * is no notification mechanism for HTTP due to its inherent stateless
165      * nature. This method is used solely for getting the application server's
166      * JMX Connector port that is running over RMI. AppserverConnectionSource
167      * running over RMI is currently to only protocol supported for AMX.
168      *
169      * @param host the name of the host on which the appserver is running
170      * @param port the port on which the admin server is running
171      * @param username username for logging into the admin server
172      * @param password password for logging into the admin server
173      * @param tlsParams SSL parameters for secure connection
174      *
175      * @return the AppserverConnectionSource connected over HTTP
176      */

177     static AppserverConnectionSource getHTTPAppserverConnectionSource(
178             final String JavaDoc host, final int port, final String JavaDoc username,
179             final String JavaDoc password, final TLSParams tlsParams) {
180         return new AppserverConnectionSource(
181             AppserverConnectionSource.PROTOCOL_HTTP, host, port, username,
182             password, tlsParams, null);
183     }
184     
185     /**
186      * Returns the ConfigDottedNames object from an AppserverConnectionSource.
187      *
188      * @param conn an AppserverConnectionSource
189      *
190      * @return the ConfigDottedNames AMX object
191      */

192     static ConfigDottedNames getConfigDottedNames(
193             final AppserverConnectionSource conn) throws IOException JavaDoc {
194         ConfigDottedNames names = null;
195         names = conn.getDomainRoot().getConfigDottedNames();
196         return names;
197     }
198     
199     /**
200      * Gets the port on which the JMXConnector is listening. This
201      * operation is necessary in order for our continuing interaction with
202      * our admin infrastructure to be through RMI, which is currently the only
203      * protocol upon which the MBean API supports.
204      *
205      * @param conn an AppserverConnectionSource
206      *
207      * @return the port on which the JMX Connector is listening.
208      */

209     static int getJMXConnectorPort(
210             final AppserverConnectionSource conn) throws IOException JavaDoc {
211        Attribute JavaDoc attr =
212             (Attribute JavaDoc)getAttributeFromConfigDottedNames(conn,
213                 JMX_CONNECTOR_PORT_DOTTED_NAME);
214        return Integer.parseInt((String JavaDoc)attr.getValue());
215     }
216     
217     /**
218      * Returns true/false depending on whether or not the appserver is running
219      * in secure mode.
220      *
221      * @param conn an AppserverConnectionSource
222      *
223      * @return true/false determining whether or not security is enabled
224      */

225     static boolean isAppserverConnectionSecurityEnabled(
226             final AppserverConnectionSource conn) throws IOException JavaDoc {
227         Attribute JavaDoc attr =
228             (Attribute JavaDoc)getAttributeFromConfigDottedNames(conn,
229                 SECURITY_ENABLED_DOTTED_NAME);
230        return Boolean.getBoolean((String JavaDoc)attr.getValue());
231     }
232     
233     /**
234      * Gets the Ojbect associated with the dotted name given for extraction of
235      * the value for name passed.
236      *
237      * @param conn an AppserverConnectionSource
238      *
239      * @return a java.lang.Object containing dotted name value
240      */

241     static Object JavaDoc getAttributeFromConfigDottedNames(
242             final AppserverConnectionSource conn, final String JavaDoc dottedName)
243                 throws IOException JavaDoc{
244         return getConfigDottedNames(conn).dottedNameGet(dottedName);
245     }
246
247     
248     /**
249      * Gets the default TLS params configuration.
250      *
251      * @param isSecure Boolean value specifying whether the sever is secure or
252      * not.
253      * @return Either null if server is not secure or an instance of a default
254      * TrustAnyTrustManager.
255      */

256     private static TLSParams getDefaultTLSParams(final boolean isSecure) {
257         return (isSecure)
258             ? new TLSParams(TrustAnyTrustManager.getInstanceArray(), null)
259             : null;
260     }
261     
262     
263 }
264
265
266
267
268
269
270
Popular Tags