KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > jmx > JMXRemoteHelper


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JMXRemoteHelper.java 1060 2006-08-09 15:48:46Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.jmx;
27
28 import static org.objectweb.easybeans.jmx.MBeanServerHelper.getMBeanServerServer;
29
30 import java.io.IOException JavaDoc;
31 import java.net.MalformedURLException JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import javax.management.InstanceAlreadyExistsException JavaDoc;
36 import javax.management.InstanceNotFoundException JavaDoc;
37 import javax.management.MBeanRegistrationException JavaDoc;
38 import javax.management.MalformedObjectNameException JavaDoc;
39 import javax.management.NotCompliantMBeanException JavaDoc;
40 import javax.management.ObjectName JavaDoc;
41 import javax.management.remote.JMXConnectorServer JavaDoc;
42 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
43 import javax.management.remote.JMXServiceURL JavaDoc;
44
45 import org.objectweb.easybeans.component.itf.RegistryComponent;
46 import org.objectweb.easybeans.log.JLog;
47 import org.objectweb.easybeans.log.JLogFactory;
48
49 /**
50  * This helper class allow to start a JMX remote connector allowing to connect remote applications.
51  * This could be for example a JSR88 provider.
52  * @author Florent Benoit
53  */

54 public final class JMXRemoteHelper {
55
56     /**
57      * Logger.
58      */

59     private static JLog logger = JLogFactory.getLog(JMXRemoteHelper.class);
60
61     /**
62      * JMX connector (server side).
63      */

64     private static JMXConnectorServer JavaDoc jmxConnectorServer = null;
65
66     /**
67      * Prefix for the URL.
68      */

69     private static final String JavaDoc PREFIX_URL = "service:jmx:rmi:///jndi/";
70
71     /**
72      * Default RMI host.
73      */

74     private static final String JavaDoc DEFAULT_RMI = "rmi://localhost:1099";
75
76     /**
77      * Suffix for the URL.
78      */

79     private static final String JavaDoc SUFFIX_URL = "/EasyBeansConnector";
80
81     /**
82      * ObjectName for the connector.
83      */

84     private static final String JavaDoc DEFAULT_NAME_CONNECTOR = "connectors:name=JMXRemoteConnector";
85
86
87     /**
88      * Utility class, no public constructor.
89      */

90     private JMXRemoteHelper() {
91
92     }
93
94
95     /**
96      * Build a new JMX Remote connector.
97      * @param registryComponent to get the provider URL.
98      * @throws JMXRemoteException if jmx connector can't be built.
99      */

100     private static void init(final RegistryComponent registryComponent) throws JMXRemoteException {
101
102         // Create connector
103
Map JavaDoc<String JavaDoc, String JavaDoc> environment = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
104         JMXServiceURL JavaDoc jmxServiceURL = null;
105
106         // Build URL
107
StringBuilder JavaDoc sb = new StringBuilder JavaDoc(PREFIX_URL);
108         if (registryComponent != null) {
109             sb.append(registryComponent.getProviderURL());
110         } else {
111             sb.append(DEFAULT_RMI);
112         }
113         sb.append(SUFFIX_URL);
114         String JavaDoc url = sb.toString();
115
116         try {
117             jmxServiceURL = new JMXServiceURL JavaDoc(url);
118         } catch (MalformedURLException JavaDoc e) {
119             throw new JMXRemoteException("Cannot create jmxservice url with url '" + url + "'.", e);
120         }
121         environment.put("jmx.remote.jndi.rebind", "true");
122         try {
123             jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, environment, null);
124         } catch (IOException JavaDoc e) {
125             throw new JMXRemoteException("Cannot create new JMX Connector", e);
126         }
127         logger.info("Creating JMXRemote connector with URl ''{0}''", url);
128
129     }
130
131     /**
132      * Start a JMX connector (used to do remote administration).
133      * @param registryComponent to get the provider URL.
134      * @throws JMXRemoteException if the connector can't be started.
135      */

136     public static synchronized void startConnector(final RegistryComponent registryComponent) throws JMXRemoteException {
137         // Create connector if null
138
if (jmxConnectorServer == null) {
139             init(registryComponent);
140         }
141
142         ObjectName JavaDoc connectorServerName = getConnectorObjectName();
143         // register it
144
try {
145             getMBeanServerServer().registerMBean(jmxConnectorServer, connectorServerName);
146         } catch (InstanceAlreadyExistsException JavaDoc e) {
147             throw new JMXRemoteException("Cannot register Mbean with the name '" + connectorServerName + "'", e);
148         } catch (MBeanRegistrationException JavaDoc e) {
149             throw new JMXRemoteException("Cannot register Mbean with the name '" + connectorServerName + "'", e);
150         } catch (NotCompliantMBeanException JavaDoc e) {
151             throw new JMXRemoteException("Cannot register Mbean with the name '" + connectorServerName + "'", e);
152         }
153
154         // Start connector
155
try {
156             jmxConnectorServer.start();
157         } catch (IOException JavaDoc e) {
158             throw new JMXRemoteException("Cannot start the jmx connector", e);
159         }
160     }
161
162     /**
163      * Start a JMX connector (used to do remote administration).
164      * @throws JMXRemoteException if the connector can't be started.
165      */

166     public static synchronized void stopConnector() throws JMXRemoteException {
167         // Do nothing if there is no Connector
168
if (jmxConnectorServer != null) {
169             ObjectName JavaDoc connectorServerName = getConnectorObjectName();
170             try {
171                 // Stop
172
jmxConnectorServer.stop();
173                 // Unregister
174
getMBeanServerServer().unregisterMBean(connectorServerName);
175                 // unget the ref
176
jmxConnectorServer = null;
177             } catch (InstanceNotFoundException JavaDoc e) {
178                 throw new JMXRemoteException("Cannot unregister JMX Connector with name '" + connectorServerName + "'", e);
179             } catch (MBeanRegistrationException JavaDoc e) {
180                 throw new JMXRemoteException("Cannot unregister JMX Connector with name '" + connectorServerName + "'", e);
181             } catch (IOException JavaDoc e) {
182                 throw new JMXRemoteException("Cannot Stop JMX Connector with name '" + connectorServerName + "'", e);
183             }
184         }
185     }
186
187
188     /**
189      * @return Returns the Connector ObjectName.
190      * @throws JMXRemoteException if unable to create the ObjectName.
191      */

192     private static ObjectName JavaDoc getConnectorObjectName() throws JMXRemoteException {
193         ObjectName JavaDoc connectorServerName = null;
194         String JavaDoc objName = null;
195         try {
196             objName = DEFAULT_NAME_CONNECTOR;
197             connectorServerName = new ObjectName JavaDoc(objName);
198         } catch (MalformedObjectNameException JavaDoc e) {
199             throw new JMXRemoteException("Cannot create ObjectName with name '" + objName + "'", e);
200         } catch (NullPointerException JavaDoc e) {
201             throw new JMXRemoteException("Cannot create ObjectName with name '" + objName + "'", e);
202         }
203         return connectorServerName;
204     }
205 }
206
Popular Tags