KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > JmxConnectorLifecycle


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 package com.sun.enterprise.admin.server.core;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.net.InetAddress JavaDoc;
33 import java.net.MalformedURLException JavaDoc;
34 import java.net.UnknownHostException JavaDoc;
35 import java.io.IOException JavaDoc;
36 import javax.management.MBeanServer JavaDoc;
37 import javax.management.MBeanServerConnection JavaDoc;
38 //import javax.management.MBeanServerFactory;
39
import com.sun.enterprise.admin.common.MBeanServerFactory;
40 import javax.management.remote.JMXConnectorServer JavaDoc;
41 import javax.management.remote.JMXServiceURL JavaDoc;
42 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
43 import javax.management.remote.JMXAuthenticator JavaDoc;
44
45 import com.sun.enterprise.admin.jmx.remote.DefaultConfiguration;
46 import com.sun.enterprise.admin.server.core.jmx.auth.ASJMXAuthenticator;
47 import com.sun.enterprise.admin.server.core.jmx.auth.ASLoginDriverImpl;
48
49 import java.rmi.server.RMIServerSocketFactory JavaDoc;
50 import java.rmi.server.RMIClientSocketFactory JavaDoc;
51 import javax.rmi.ssl.SslRMIClientSocketFactory JavaDoc;
52
53 import com.sun.enterprise.admin.common.constant.AdminConstants;
54 import com.sun.appserv.server.ServerLifecycle;
55 import com.sun.appserv.server.ServerLifecycleException;
56 import com.sun.enterprise.server.ServerContext;
57 import com.sun.enterprise.config.serverbeans.JmxConnector;
58 import com.sun.enterprise.config.serverbeans.AdminService;
59 import com.sun.enterprise.config.serverbeans.ServerBeansFactory;
60 import com.sun.enterprise.config.ConfigException;
61 import com.sun.enterprise.server.pluggable.PluggableFeatureFactory;
62 import com.sun.enterprise.util.i18n.StringManager;
63
64 import javax.rmi.ssl.SslRMIClientSocketFactory JavaDoc;
65 import com.sun.enterprise.util.SystemPropertyConstants;
66 /* Following classes come from /m/jws/jmx-remote which gets built before
67  * /m/jws/appserv-core */

68 /* start */
69 import com.sun.enterprise.admin.jmx.remote.server.rmi.JmxConnectorServerDriver;
70 import com.sun.enterprise.admin.jmx.remote.server.rmi.RemoteJmxProtocol;
71 /* end */
72 import com.sun.enterprise.admin.server.core.jmx.nonssl.RMIMultiHomedServerSocketFactory;
73 import com.sun.enterprise.admin.server.core.jmx.ssl.AdminSslServerSocketFactory;
74 import com.sun.enterprise.admin.server.core.jmx.ssl.AsTlsClientEnvSetter;
75 import com.sun.appserv.management.client.AdminRMISSLClientSocketFactory; /* From MBean API */
76 import javax.rmi.ssl.SslRMIServerSocketFactory JavaDoc;
77 /**
78  *
79  * @author kedar
80  */

81 public class JmxConnectorLifecycle implements ServerLifecycle {
82
83     public static final Logger JavaDoc sLogger =
84     Logger.getLogger(AdminConstants.kLoggerName);
85     private static final StringManager sm = StringManager.getManager(JmxConnectorLifecycle.class);
86     private ServerContext initContext = null;
87     private JmxConnector connectorConfig = null;
88     private JMXConnectorServer JavaDoc cs = null;
89     private JMXConnectorServer JavaDoc jconsolecs = null;
90     private JmxConnectorServerDriver driver;
91     private boolean isEnabled = false;
92     /** Creates a new instance of JmxConnectorLifecycle.
93      */

94     public JmxConnectorLifecycle() {
95         sLogger.log(Level.FINE, "rjmx.lc.init");
96     }
97
98     public void onInitialization(ServerContext sc) throws ServerLifecycleException {
99         try {
100             initContext = sc;
101             initConnectorConfig();
102             handleIsEnabled(connectorConfig.getPort());
103             handleSupportedProtocol();
104             if (isEnabled) {
105                 driver = new JmxConnectorServerDriver();
106                 configureJmxConnectorServerDriver();
107             }
108         }
109         catch(Exception JavaDoc e) {
110             throw new ServerLifecycleException(e.getMessage(), e);
111         }
112     }
113
114     public void onStartup(ServerContext sc) throws ServerLifecycleException {
115         setupClientSide();
116         try {
117             if (isEnabled) {
118                 this.cs = driver.startConnectorServer();
119                 this.jconsolecs = driver.startJconsoleConnectorServer();
120             }
121             else {
122                 final String JavaDoc msg = "JmxConnectorLifeCycle.onStartup: Connector Server not enabled at port: " + connectorConfig.getPort();
123                 sLogger.fine(msg);
124             }
125         }
126         catch (Exception JavaDoc e) {
127             throw new ServerLifecycleException(e.getMessage(), e);
128         }
129     }
130
131     public void onReady(ServerContext sc) throws ServerLifecycleException {
132     }
133     public void onShutdown() throws ServerLifecycleException {
134         try {
135             if (isEnabled) {
136                 driver.stopConnectorServer(cs);
137                 driver.stopConnectorServer(jconsolecs);
138             }
139             else {
140                 final String JavaDoc msg = "JmxConnectorLifeCycle.onShutdown: Connector Server not enabled at port: " + connectorConfig.getPort() + ", its shutdown is not required";
141                 sLogger.fine(msg);
142             }
143         }
144         catch (final Exception JavaDoc e) {
145             throw new ServerLifecycleException(e.getMessage());
146         }
147     }
148
149     private MBeanServer JavaDoc getAssociatedMBS() {
150         /*
151         final String returnAllMBS = null;
152         final ArrayList list = MBeanServerFactory.findMBeanServer(returnAllMBS);
153         if (list.isEmpty())
154                 throw new RuntimeException("Initialize the MBeanServers first...");
155         return (MBeanServer)list.get(0); //for now
156          */

157         return ( MBeanServerFactory.getMBeanServer() );
158     }
159
160     private Map JavaDoc getEnvironment() {
161         final Map JavaDoc env = new HashMap JavaDoc();
162         return ( env );
163     }
164
165     public void onTermination() throws ServerLifecycleException {
166     }
167
168     private void initConnectorConfig() throws ConfigException {
169         //This is the AdminService config bean
170
AdminService as = ServerBeansFactory.getConfigBean(initContext.getConfigContext()).
171                   getAdminService();
172         connectorConfig = as.getJmxConnectorByName(as.getSystemJmxConnectorName());
173         if (connectorConfig.isEnabled()) {
174             this.isEnabled = true;
175         }
176     }
177     
178     private JMXAuthenticator JavaDoc createJMXAuthenticator() {
179         final ASJMXAuthenticator authenticator = new ASJMXAuthenticator();
180         // TODO: If domain.xml is not present, can not configure authentication
181
authenticator.setRealmName(connectorConfig.getAuthRealmName());
182         authenticator.setLoginDriver(new ASLoginDriverImpl());
183         return authenticator;
184     }
185
186     private void configureJmxConnectorServerDriver() throws ServerLifecycleException {
187         driver.setAuthentication(true);
188         driver.setAuthenticator(createJMXAuthenticator());
189         driver.setLogger(this.sLogger);
190         driver.setMBeanServer(this.getAssociatedMBS());
191         driver.setRmiRegistrySecureFlag(new Boolean JavaDoc(System.getProperty(RmiTweaks.SECURE_RMI_REGISTRY)).booleanValue());
192         try {
193             driver.setProtocol(RemoteJmxProtocol.instance(connectorConfig.getProtocol()));
194             driver.setPort(Integer.parseInt(connectorConfig.getPort()));
195             handleSsl();
196         }
197         catch (final Exception JavaDoc e) {
198             throw new ServerLifecycleException(e.getMessage());
199         }
200     }
201     
202     /** Handles the enabled flag on system-jmx-connector. On PE, it is okay that this
203      * flag is set to false. But for SE/EE, it has to be true as the inter server
204      * communication depends on it.
205      */

206     private void handleIsEnabled(final String JavaDoc port) throws ServerLifecycleException {
207         /* Implementation note: This could have been handled using the Pluggable
208          * Feature Factory, but since there isn't much pluggable behavior, I am going to
209          * rely on the system property. In general, this should not be done.
210          */

211         if (isEE() && !isEnabled) {
212             //EE and not enabled is not fine
213
final String JavaDoc msg = sm.getString("rjmx.lc.disabled_ee_na", port);
214             throw new ServerLifecycleException(msg);
215         }
216         if (!isEE() && !isEnabled) {
217             //PE and not enabled is fine - log and move on
218
sLogger.log(Level.INFO, "rjmx.lc.not_enabled", port);
219         }
220         //other 2 cases are implicitly handled
221
}
222     
223     private boolean isEE() {
224         boolean isEE = false;
225         final String JavaDoc eepffc = "com.sun.enterprise.ee.server.pluggable.EEPluggableFeatureImpl";
226         final String JavaDoc pn = PluggableFeatureFactory.PLUGGABLE_FEATURES_PROPERTY_NAME;
227         final String JavaDoc pv = System.getProperty(pn);
228         if (eepffc.equals(pv)) {
229             isEE = true;
230         }
231         return ( isEE );
232     }
233     private void handleSupportedProtocol() throws ServerLifecycleException {
234         final String JavaDoc pfc = connectorConfig.getProtocol();
235         if (RemoteJmxProtocol.RMIJRMP != RemoteJmxProtocol.instance(pfc)) {
236             final String JavaDoc port = connectorConfig.getPort();
237             final String JavaDoc setP = connectorConfig.getProtocol();
238             final String JavaDoc supportedP = RemoteJmxProtocol.RMIJRMP.getName();
239             final String JavaDoc msg = sm.getString("rjmx.lc.unsupported_protocol", port, setP, supportedP);
240             throw new ServerLifecycleException (msg);
241         }
242     }
243     private void handleSsl() {
244         final boolean ssl = connectorConfig.isSecurityEnabled();
245         
246         RMIServerSocketFactory JavaDoc sf = null;
247         if (ssl) {
248             driver.setSsl(ssl);
249             final com.sun.enterprise.config.serverbeans.Ssl sslc = connectorConfig.getSsl();
250             sf = new AdminSslServerSocketFactory(sslc, connectorConfig.getAddress());
251             RMIClientSocketFactory JavaDoc cf = new AdminRMISSLClientSocketFactory();
252             driver.setRmiClientSocketFactory(cf);
253         } else sf = new RMIMultiHomedServerSocketFactory(connectorConfig.getAddress());
254         driver.setRmiServerSocketFactory(sf);
255     }
256     
257     /** A method to set up the client side of the TLS connection. Here is the scenario: When
258      * the system jmx connector is set up with TLS enabled, all the other server instances
259      * need to have the RMIClientSocketFactory related environment. This method ensures that.
260      * Since this method is called when the server end is being brought up with the startup, it
261      * ensures that the setup happens early. This is also required in case of cascading where
262      * the server instances have the TLS setup on the jmx-connectors that are started in their
263      * life cycle. Even if the jmx connectors are not set up with TLS, it is okay to
264      * setup the client side.
265      */

266     private void setupClientSide() {
267         new AsTlsClientEnvSetter().setup();
268     }
269
270     private static class RmiTweaks {
271         final static String JavaDoc SECURE_RMI_REGISTRY = "com.sun.aas.jsr160.SecureRmiRegistry";
272     }
273 }
274
Popular Tags