001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: jonas@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JMXRemoteHelper.java 9268 2006-07-31 16:02:56Z pelletib $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jmx.commons;
025:
026: import java.io.IOException;
027: import java.net.MalformedURLException;
028: import java.util.HashMap;
029: import java.util.Map;
030: import java.util.Set;
031:
032: import javax.management.InstanceAlreadyExistsException;
033: import javax.management.MBeanRegistrationException;
034: import javax.management.MBeanServerConnection;
035: import javax.management.MalformedObjectNameException;
036: import javax.management.NotCompliantMBeanException;
037: import javax.management.ObjectInstance;
038: import javax.management.ObjectName;
039: import javax.management.remote.JMXConnector;
040: import javax.management.remote.JMXConnectorFactory;
041: import javax.management.remote.JMXConnectorServer;
042: import javax.management.remote.JMXConnectorServerFactory;
043: import javax.management.remote.JMXServiceURL;
044:
045: import org.objectweb.jonas.cluster.daemon.ClusterDaemonTools;
046:
047: /**
048: * This helper class allow to start a JMX remote connector allowing to connect remote applications.
049: * This could be for example a JSR88 provider.
050: * @author Florent Benoit
051: */
052: public final class JMXRemoteHelper {
053:
054: /**
055: * JMX connector (server side).
056: */
057: private static JMXConnectorServer jmxConnectorServer = null;
058:
059: /**
060: * Default URL.
061: */
062: private static final String DEFAULT_URL = "service:jmx:rmi:///jndi/rmi://localhost:1099/jonasConnector";
063:
064: /**
065: * ObjectName for the connector.
066: */
067: private static final String DEFAULT_NAME_CONNECTOR = "connectors:name=JMXRemoteConnector";
068:
069: /**
070: * MBean server connection
071: */
072: private static MBeanServerConnection mbscnx = null;
073:
074: /**
075: * Utility class, no public constructor.
076: */
077: private JMXRemoteHelper() {
078:
079: }
080:
081: /**
082: * Build a new JMX Remote connector
083: * @param url JMX remote url.
084: * @throws JMXRemoteException if jmx connector can't be built.
085: */
086: private static void init(String url) throws JMXRemoteException {
087:
088: // Create connector
089: Map environment = new HashMap();
090: JMXServiceURL jmxServiceURL = null;
091: try {
092: jmxServiceURL = new JMXServiceURL(url);
093: } catch (MalformedURLException e) {
094: throw new JMXRemoteException(
095: "Cannot create jmxservice url with url '" + url
096: + "'.", e);
097: }
098: environment.put("jmx.remote.jndi.rebind", "true");
099: try {
100: jmxConnectorServer = JMXConnectorServerFactory
101: .newJMXConnectorServer(jmxServiceURL, environment,
102: null);
103: } catch (IOException e) {
104: throw new JMXRemoteException(
105: "Cannot create new JMX Connector", e);
106: }
107:
108: }
109:
110: /**
111: * Connect to a JMX Remote connector
112: * @param url JMX remote url.
113: * @return MBeanServerConnection MBean server connection
114: * @throws JMXRemoteException if jmx connector can't be connected.
115: */
116: public static MBeanServerConnection connect(String url)
117: throws JMXRemoteException {
118:
119: if (mbscnx != null)
120: return mbscnx;
121:
122: JMXServiceURL jmxServiceURL = null;
123: try {
124: jmxServiceURL = new JMXServiceURL(url);
125: } catch (MalformedURLException e) {
126: throw new JMXRemoteException(
127: "Cannot create jmxservice url with url '" + url
128: + "'.", e);
129: }
130: try {
131: JMXConnector jmxConnector = JMXConnectorFactory
132: .connect(jmxServiceURL);
133: mbscnx = jmxConnector.getMBeanServerConnection();
134: } catch (IOException e) {
135: throw new JMXRemoteException(
136: "Cannot connect to JMX remote with '" + url + "'.",
137: e);
138: }
139: return mbscnx;
140: }
141:
142: /**
143: * Start a JMX connector (used to do remote administration).
144: * @param url JMX remote url
145: * @param connectorName connector name
146: * @throws JMXRemoteException if the connector can't be started.
147: */
148: public static synchronized void startConnector(String url,
149: String connectorName) throws JMXRemoteException {
150: // Create connector if null
151: if (jmxConnectorServer == null) {
152: if (url == null) {
153: init(DEFAULT_URL);
154: } else {
155: init(url);
156: }
157: }
158:
159: // Create MBean for this connector
160: ObjectName connectorServerName = null;
161: String objName = null;
162: try {
163: if (connectorName == null) {
164: objName = DEFAULT_NAME_CONNECTOR;
165: } else {
166: objName = connectorName;
167: }
168: connectorServerName = new ObjectName(objName);
169: } catch (MalformedObjectNameException e) {
170: throw new JMXRemoteException(
171: "Cannot create ObjectName with name '" + objName
172: + "'", e);
173: } catch (NullPointerException e) {
174: throw new JMXRemoteException(
175: "Cannot create ObjectName with name '" + objName
176: + "'", e);
177: }
178:
179: // register it
180: try {
181: MBeanServerHelper.getMBeanServer().registerMBean(
182: jmxConnectorServer, connectorServerName);
183: } catch (InstanceAlreadyExistsException e) {
184: throw new JMXRemoteException(
185: "Cannot register Mbean with the name '"
186: + connectorServerName + "'", e);
187: } catch (MBeanRegistrationException e) {
188: throw new JMXRemoteException(
189: "Cannot register Mbean with the name '"
190: + connectorServerName + "'", e);
191: } catch (NotCompliantMBeanException e) {
192: throw new JMXRemoteException(
193: "Cannot register Mbean with the name '"
194: + connectorServerName + "'", e);
195: }
196:
197: // Start connector
198: try {
199: jmxConnectorServer.start();
200: } catch (IOException e) {
201: throw new JMXRemoteException(
202: "Cannot start the jmx connector", e);
203: }
204: }
205:
206: /**
207: * Get ObjectInstance
208: * @param cnx MBean server connection
209: * @param pattern MBean name
210: * @return ObjectInstance
211: */
212: public static ObjectInstance getInstance(MBeanServerConnection cnx,
213: String pattern) {
214:
215: Set oiset = null;
216: ObjectInstance oi = null;
217: try {
218: ObjectName on = new ObjectName(pattern);
219: oiset = cnx.queryMBeans(on, null);
220:
221: } catch (Exception ex) {
222: ex.printStackTrace();
223: }
224: if (!oiset.isEmpty())
225: oi = (ObjectInstance) oiset.iterator().next();
226:
227: return oi;
228: }
229: }
|