KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > management > agents > JmxAgent


1 /*
2  * $Id: JmxAgent.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10 package org.mule.management.agents;
11
12 import java.util.ArrayList JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import javax.management.InstanceAlreadyExistsException JavaDoc;
18 import javax.management.MBeanRegistrationException JavaDoc;
19 import javax.management.MBeanServer JavaDoc;
20 import javax.management.MBeanServerFactory JavaDoc;
21 import javax.management.MalformedObjectNameException JavaDoc;
22 import javax.management.NotCompliantMBeanException JavaDoc;
23 import javax.management.ObjectName JavaDoc;
24 import javax.management.remote.JMXConnectorServer JavaDoc;
25 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
26 import javax.management.remote.JMXServiceURL JavaDoc;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.mule.MuleManager;
31 import org.mule.MuleRuntimeException;
32 import org.mule.config.i18n.Message;
33 import org.mule.config.i18n.Messages;
34 import org.mule.impl.internal.notifications.ModelNotification;
35 import org.mule.impl.internal.notifications.ModelNotificationListener;
36 import org.mule.impl.internal.notifications.NotificationException;
37 import org.mule.management.mbeans.ComponentService;
38 import org.mule.management.mbeans.ComponentServiceMBean;
39 import org.mule.management.mbeans.ConnectorService;
40 import org.mule.management.mbeans.ConnectorServiceMBean;
41 import org.mule.management.mbeans.EndpointService;
42 import org.mule.management.mbeans.EndpointServiceMBean;
43 import org.mule.management.mbeans.ModelService;
44 import org.mule.management.mbeans.ModelServiceMBean;
45 import org.mule.management.mbeans.MuleConfigurationService;
46 import org.mule.management.mbeans.MuleConfigurationServiceMBean;
47 import org.mule.management.mbeans.MuleService;
48 import org.mule.management.mbeans.MuleServiceMBean;
49 import org.mule.management.mbeans.StatisticsService;
50 import org.mule.management.support.AutoDiscoveryJmxSupportFactory;
51 import org.mule.management.support.JmxSupport;
52 import org.mule.management.support.JmxSupportFactory;
53 import org.mule.providers.AbstractConnector;
54 import org.mule.umo.UMOException;
55 import org.mule.umo.lifecycle.InitialisationException;
56 import org.mule.umo.manager.UMOAgent;
57 import org.mule.umo.manager.UMOManager;
58 import org.mule.umo.manager.UMOServerNotification;
59 import org.mule.umo.provider.UMOConnector;
60 import org.mule.umo.provider.UMOMessageReceiver;
61 import org.mule.util.StringUtils;
62
63 /**
64  * <code>JmxAgent</code> registers MUle Jmx management beans with an MBean
65  * server.
66  *
67  * @version $Revision: 3798 $
68  */

69 public class JmxAgent implements UMOAgent
70 {
71
72     /**
73      * Logger used by this class.
74      */

75     protected static Log logger = LogFactory.getLog(JmxAgent.class);
76
77     /**
78      * Should MBeanServer be discovered.
79      */

80     protected boolean locateServer = true;
81
82     private String JavaDoc name = "JMX Agent";
83     private boolean createServer = true;
84     private String JavaDoc connectorServerUrl;
85     private MBeanServer JavaDoc mBeanServer;
86     private JMXConnectorServer JavaDoc connectorServer;
87     private Map JavaDoc connectorServerProperties = null;
88     private boolean enableStatistics = true;
89     private List JavaDoc registeredMBeans = new ArrayList JavaDoc();
90     private boolean serverCreated = false;
91     private boolean initialized = false;
92
93     private JmxSupportFactory jmxSupportFactory = new AutoDiscoveryJmxSupportFactory();
94     private JmxSupport jmxSupport;
95
96     /** {@inheritDoc}
97     *
98     * @see org.mule.umo.manager.UMOAgent#getName()
99     */

100     public String JavaDoc getName()
101     {
102         return this.name;
103     }
104
105     /** {@inheritDoc}
106      *
107      * @see org.mule.umo.manager.UMOAgent#setName(java.lang.String)
108      */

109     public void setName(String JavaDoc name)
110     {
111         this.name = name;
112     }
113
114     /** {@inheritDoc}
115      *
116      * @see org.mule.umo.manager.UMOAgent#getDescription()
117      */

118     public String JavaDoc getDescription()
119     {
120         if (connectorServerUrl != null)
121         {
122             return "JMX Agent: " + connectorServerUrl;
123         }
124         else
125         {
126             return "JMX Agent";
127         }
128     }
129
130     /** {@inheritDoc}
131      * (non-Javadoc)
132      *
133      * @see org.mule.umo.lifecycle.Initialisable#initialise()
134      */

135     public void initialise() throws InitialisationException
136     {
137         if (initialized) {
138             return;
139         }
140         if (!locateServer && !createServer) {
141             throw new InitialisationException(new Message(Messages.JMX_CREATE_OR_LOCATE_SHOULD_BE_SET), this);
142         }
143         if (mBeanServer == null && locateServer) {
144             List JavaDoc l = MBeanServerFactory.findMBeanServer(null);
145             if (l != null && l.size() > 0) {
146                 mBeanServer = (MBeanServer JavaDoc) l.get(0);
147             }
148         }
149         if (mBeanServer == null && createServer) {
150             mBeanServer = MBeanServerFactory.createMBeanServer();
151             serverCreated = true;
152         }
153         if (mBeanServer == null) {
154             throw new InitialisationException(new Message(Messages.JMX_CANT_LOCATE_CREATE_SERVER), this);
155         }
156         if (connectorServerUrl != null) {
157             try {
158                 JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc(connectorServerUrl);
159                 connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, connectorServerProperties, mBeanServer);
160             } catch (Exception JavaDoc e) {
161                 throw new InitialisationException(new Message(Messages.FAILED_TO_CREATE_X, "Jmx Connector"), e, this);
162             }
163         }
164
165         jmxSupport = jmxSupportFactory.newJmxSupport();
166
167         // We need to register all the services once the server has initialised
168
ModelNotificationListener l = new ModelNotificationListener() {
169             public void onNotification(UMOServerNotification notification)
170             {
171                 if (notification.getAction() == ModelNotification.MODEL_STARTED) {
172                     try {
173                         registerStatisticsService();
174                         registerMuleService();
175                         registerConfigurationService();
176                         registerModelService();
177                         registerComponentServices();
178                         registerEndpointServices();
179                         registerConnectorServices();
180                     } catch (Exception JavaDoc e) {
181                         throw new MuleRuntimeException(new Message(Messages.X_FAILED_TO_INITIALISE, "MBeans"), e);
182                     }
183                 }
184             }
185         };
186
187         try
188         {
189             UMOManager manager = MuleManager.getInstance();
190             if (StringUtils.isBlank(manager.getId()))
191             {
192                 // TODO i18n the message properly
193
throw new IllegalArgumentException JavaDoc(
194                         "Manager ID is mandatory when running with JmxAgent. Give your Mule configuration a valid ID.");
195             }
196             MuleManager.getInstance().registerListener(l);
197         } catch (NotificationException e) {
198             throw new InitialisationException(e, this);
199         }
200         initialized = true;
201     }
202
203     /** {@inheritDoc}
204      * (non-Javadoc)
205      *
206      * @see org.mule.umo.lifecycle.Startable#start()
207      */

208     public void start() throws UMOException
209     {
210         if (connectorServer != null) {
211             try {
212                 logger.info("Starting JMX agent connector Server");
213                 connectorServer.start();
214             } catch (Exception JavaDoc e) {
215                 throw new JmxManagementException(new Message(Messages.FAILED_TO_START_X, "Jmx Connector"), e);
216             }
217         }
218     }
219
220     /** {@inheritDoc}
221      * (non-Javadoc)
222      *
223      * @see org.mule.umo.lifecycle.Stoppable#stop()
224      */

225     public void stop() throws UMOException
226     {
227         if (connectorServer != null) {
228             try {
229                 connectorServer.stop();
230             } catch (Exception JavaDoc e) {
231                 throw new JmxManagementException(new Message(Messages.FAILED_TO_STOP_X, "Jmx Connector"), e);
232             }
233         }
234     }
235
236     /** {@inheritDoc}
237      * (non-Javadoc)
238      *
239      * @see org.mule.umo.lifecycle.Disposable#dispose()
240      */

241     public void dispose()
242     {
243         if (mBeanServer != null) {
244             for (Iterator JavaDoc iterator = registeredMBeans.iterator(); iterator.hasNext();) {
245                 ObjectName JavaDoc objectName = (ObjectName JavaDoc) iterator.next();
246                 try {
247                     mBeanServer.unregisterMBean(objectName);
248                 } catch (Exception JavaDoc e) {
249                     logger.warn("Failed to unregister MBean: " + objectName + ". Error is: " + e.getMessage());
250                 }
251             }
252             if (serverCreated) {
253                 MBeanServerFactory.releaseMBeanServer(mBeanServer);
254             }
255             mBeanServer = null;
256         }
257     }
258
259     /** {@inheritDoc}
260      * (non-Javadoc)
261      *
262      * @see org.mule.umo.manager.UMOAgent#registered()
263      */

264     public void registered()
265     {
266         // nothing to do
267
}
268
269     /** {@inheritDoc}
270      * (non-Javadoc)
271      *
272      * @see org.mule.umo.manager.UMOAgent#unregistered()
273      */

274     public void unregistered()
275     {
276         // nothing to do
277
}
278
279     protected void registerStatisticsService() throws NotCompliantMBeanException JavaDoc, MBeanRegistrationException JavaDoc,
280             InstanceAlreadyExistsException JavaDoc, MalformedObjectNameException JavaDoc
281     {
282         ObjectName JavaDoc on = jmxSupport.getObjectName(jmxSupport.getDomainName() + ":type=org.mule.Statistics,name=AllStatistics");
283         StatisticsService mBean = new StatisticsService();
284         mBean.setManager(MuleManager.getInstance());
285         mBean.setEnabled(isEnableStatistics());
286         logger.debug("Registering statistics with name: " + on);
287         mBeanServer.registerMBean(mBean, on);
288         registeredMBeans.add(on);
289     }
290
291     protected void registerModelService() throws NotCompliantMBeanException JavaDoc, MBeanRegistrationException JavaDoc,
292             InstanceAlreadyExistsException JavaDoc, MalformedObjectNameException JavaDoc
293     {
294         ModelServiceMBean serviceMBean = new ModelService();
295         String JavaDoc rawName = serviceMBean.getName() + "(" + serviceMBean.getType() + ")";
296         String JavaDoc name = jmxSupport.escape(rawName);
297         ObjectName JavaDoc on = jmxSupport.getObjectName(jmxSupport.getDomainName() + ":type=org.mule.Model,name=" + name);
298         logger.debug("Registering model with name: " + on);
299         mBeanServer.registerMBean(serviceMBean, on);
300         registeredMBeans.add(on);
301     }
302
303     protected void registerMuleService() throws NotCompliantMBeanException JavaDoc, MBeanRegistrationException JavaDoc,
304             InstanceAlreadyExistsException JavaDoc, MalformedObjectNameException JavaDoc
305     {
306         ObjectName JavaDoc on = jmxSupport.getObjectName(jmxSupport.getDomainName() + ":type=org.mule.ManagementContext,name=MuleServerInfo");
307         MuleServiceMBean serviceMBean = new MuleService();
308         logger.debug("Registering mule with name: " + on);
309         mBeanServer.registerMBean(serviceMBean, on);
310         registeredMBeans.add(on);
311     }
312
313     protected void registerConfigurationService() throws NotCompliantMBeanException JavaDoc, MBeanRegistrationException JavaDoc,
314             InstanceAlreadyExistsException JavaDoc, MalformedObjectNameException JavaDoc
315     {
316         ObjectName JavaDoc on = jmxSupport.getObjectName(jmxSupport.getDomainName() + ":type=org.mule.Configuration,name=GlobalConfiguration");
317         MuleConfigurationServiceMBean serviceMBean = new MuleConfigurationService();
318         logger.debug("Registering configuration with name: " + on);
319         mBeanServer.registerMBean(serviceMBean, on);
320         registeredMBeans.add(on);
321     }
322
323     protected void registerComponentServices() throws NotCompliantMBeanException JavaDoc, MBeanRegistrationException JavaDoc,
324             InstanceAlreadyExistsException JavaDoc, MalformedObjectNameException JavaDoc
325     {
326         Iterator JavaDoc iter = MuleManager.getInstance().getModel().getComponentNames();
327         String JavaDoc rawName;
328         while (iter.hasNext()) {
329             rawName = iter.next().toString();
330             final String JavaDoc name = jmxSupport.escape(rawName);
331             ObjectName JavaDoc on = jmxSupport.getObjectName(jmxSupport.getDomainName() + ":type=org.mule.Component,name=" + name);
332             ComponentServiceMBean serviceMBean = new ComponentService(rawName);
333             logger.debug("Registering component with name: " + on);
334             mBeanServer.registerMBean(serviceMBean, on);
335             registeredMBeans.add(on);
336         }
337     }
338
339     protected void registerEndpointServices() throws NotCompliantMBeanException JavaDoc, MBeanRegistrationException JavaDoc,
340             InstanceAlreadyExistsException JavaDoc, MalformedObjectNameException JavaDoc
341     {
342         Iterator JavaDoc iter = MuleManager.getInstance().getConnectors().values().iterator();
343         UMOConnector connector;
344         while (iter.hasNext()) {
345             connector = (UMOConnector) iter.next();
346             if (connector instanceof AbstractConnector) {
347                 for (Iterator JavaDoc iterator = ((AbstractConnector) connector).getReceivers().values().iterator(); iterator.hasNext();) {
348                     EndpointServiceMBean mBean = new EndpointService((UMOMessageReceiver) iterator.next());
349                     final String JavaDoc rawName = mBean.getName();
350                     final String JavaDoc name = jmxSupport.escape(rawName);
351                     if (logger.isInfoEnabled()) {
352                         logger.info("Attempting to register service with name: " + jmxSupport.getDomainName()
353                                 + ":type=org.mule.umo.UMOEndpoint,name=" + name);
354                     }
355                     ObjectName JavaDoc on = jmxSupport.getObjectName(
356                                                     jmxSupport.getDomainName() +
357                                                     ":type=org.mule.Endpoint,component=" +
358                                                     jmxSupport.escape(mBean.getComponentName()) +
359                                                     ",name=" + name);
360                     mBeanServer.registerMBean(mBean, on);
361                     registeredMBeans.add(on);
362                     logger.info("Registered Endpoint Service with name: " + on);
363                 }
364             } else {
365                 logger.warn("Connector: " + connector
366                         + " is not an istance of AbstractConnector, cannot obtain Endpoint MBeans from it");
367             }
368
369         }
370     }
371
372     protected void registerConnectorServices() throws
373                                                 MalformedObjectNameException JavaDoc,
374                                                 NotCompliantMBeanException JavaDoc,
375                                                 MBeanRegistrationException JavaDoc,
376                                                 InstanceAlreadyExistsException JavaDoc
377     {
378         Iterator JavaDoc iter = MuleManager.getInstance().getConnectors().values().iterator();
379         while (iter.hasNext()) {
380             UMOConnector connector = (UMOConnector) iter.next();
381             ConnectorServiceMBean mBean = new ConnectorService(connector);
382             final String JavaDoc rawName = mBean.getName();
383             final String JavaDoc name = jmxSupport.escape(rawName);
384             final String JavaDoc stringName = jmxSupport.getDomainName() + ":type=org.mule.Connector,name=" + name;
385             if (logger.isDebugEnabled()) {
386                 logger.debug("Attempting to register service with name: " + stringName);
387             }
388             ObjectName JavaDoc oName = jmxSupport.getObjectName(stringName);
389             mBeanServer.registerMBean(mBean, oName);
390             registeredMBeans.add(oName);
391             logger.info("Registered Connector Service with name " + oName);
392         }
393     }
394
395     /**
396      * @return Returns the createServer.
397      */

398     public boolean isCreateServer()
399     {
400         return createServer;
401     }
402
403     /**
404      * @param createServer The createServer to set.
405      */

406     public void setCreateServer(boolean createServer)
407     {
408         this.createServer = createServer;
409     }
410
411     /**
412      * @return Returns the locateServer.
413      */

414     public boolean isLocateServer()
415     {
416         return locateServer;
417     }
418
419     /**
420      * @param locateServer The locateServer to set.
421      */

422     public void setLocateServer(boolean locateServer)
423     {
424         this.locateServer = locateServer;
425     }
426
427     /**
428      * @return Returns the connectorServerUrl.
429      */

430     public String JavaDoc getConnectorServerUrl()
431     {
432         return connectorServerUrl;
433     }
434
435     /**
436      * @param connectorServerUrl The connectorServerUrl to set.
437      */

438     public void setConnectorServerUrl(String JavaDoc connectorServerUrl)
439     {
440         this.connectorServerUrl = connectorServerUrl;
441     }
442
443     /**
444      * @return Returns the enableStatistics.
445      */

446     public boolean isEnableStatistics()
447     {
448         return enableStatistics;
449     }
450
451     /**
452      * @param enableStatistics The enableStatistics to set.
453      */

454     public void setEnableStatistics(boolean enableStatistics)
455     {
456         this.enableStatistics = enableStatistics;
457     }
458
459     /**
460      * @return Returns the mBeanServer.
461      */

462     public MBeanServer JavaDoc getMBeanServer()
463     {
464         return mBeanServer;
465     }
466
467     /**
468      * @param mBeanServer The mBeanServer to set.
469      */

470     public void setMBeanServer(MBeanServer JavaDoc mBeanServer)
471     {
472         this.mBeanServer = mBeanServer;
473     }
474
475     /**
476      * Getter for property 'connectorServerProperties'.
477      *
478      * @return Value for property 'connectorServerProperties'.
479      */

480     public Map JavaDoc getConnectorServerProperties() {
481         return connectorServerProperties;
482     }
483
484     /**
485      * Setter for property 'connectorServerProperties'.
486      *
487      * @param connectorServerProperties Value to set for property 'connectorServerProperties'.
488      */

489     public void setConnectorServerProperties(Map JavaDoc connectorServerProperties) {
490         this.connectorServerProperties = connectorServerProperties;
491     }
492
493
494     /**
495      * Getter for property 'jmxSupportFactory'.
496      *
497      * @return Value for property 'jmxSupportFactory'.
498      */

499     public JmxSupportFactory getJmxSupportFactory()
500     {
501         return jmxSupportFactory;
502     }
503
504     /**
505      * Setter for property 'jmxSupportFactory'.
506      *
507      * @param jmxSupportFactory Value to set for property 'jmxSupportFactory'.
508      */

509     public void setJmxSupportFactory(JmxSupportFactory jmxSupportFactory)
510     {
511         this.jmxSupportFactory = jmxSupportFactory;
512     }
513 }
514
Popular Tags