View Javadoc

1   /*
2    * $Id: Log4jAgent.java 11517 2008-03-31 21:34:19Z dirk.olmes $
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 CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.management.agent;
12  
13  import org.mule.AbstractAgent;
14  import org.mule.api.MuleException;
15  import org.mule.api.lifecycle.InitialisationException;
16  import org.mule.config.i18n.CoreMessages;
17  import org.mule.module.management.support.AutoDiscoveryJmxSupportFactory;
18  import org.mule.module.management.support.JmxSupport;
19  import org.mule.module.management.support.JmxSupportFactory;
20  
21  import java.util.Iterator;
22  import java.util.Set;
23  
24  import javax.management.InstanceNotFoundException;
25  import javax.management.MBeanRegistrationException;
26  import javax.management.MBeanServer;
27  import javax.management.MBeanServerFactory;
28  import javax.management.MalformedObjectNameException;
29  import javax.management.ObjectInstance;
30  import javax.management.ObjectName;
31  
32  import org.apache.log4j.jmx.HierarchyDynamicMBean;
33  
34  /**
35   * <code>Log4jAgent</code> exposes the configuration of the Log4J instance running
36   * in Mule for Jmx management
37   */
38  public class Log4jAgent extends AbstractAgent
39  {
40      private MBeanServer mBeanServer;
41      public static final String JMX_OBJECT_NAME = "log4j:type=Hierarchy";
42  
43      private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
44      private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
45  
46  
47      public Log4jAgent()
48      {
49          super("jmx-log4j");
50      }
51      
52  
53      public void initialise() throws InitialisationException
54      {
55          try
56          {
57              mBeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
58              final ObjectName objectName = jmxSupport.getObjectName(JMX_OBJECT_NAME);
59              // unregister existing Log4j MBean first if required
60              unregisterMBeansIfNecessary();
61              mBeanServer.registerMBean(new HierarchyDynamicMBean(), objectName);
62          }
63          catch (Exception e)
64          {
65              throw new InitialisationException(CoreMessages.failedToStart("JMX Agent"), e, this);
66          }
67      }
68  
69      /**
70       * Unregister all log4j MBeans if there are any left over the old deployment
71       */
72      protected void unregisterMBeansIfNecessary()
73          throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException
74      {
75          if (mBeanServer.isRegistered(jmxSupport.getObjectName(JMX_OBJECT_NAME)))
76          {
77              // unregister all log4jMBeans and loggers
78              Set log4jMBeans = mBeanServer.queryMBeans(jmxSupport.getObjectName("log4j*:*"), null);
79              for (Iterator it = log4jMBeans.iterator(); it.hasNext();)
80              {
81                  ObjectInstance objectInstance = (ObjectInstance)it.next();
82                  ObjectName theName = objectInstance.getObjectName();
83                  mBeanServer.unregisterMBean(theName);
84              }
85          }
86      }
87  
88      public void start() throws MuleException
89      {
90          // nothing to do
91      }
92  
93      public void stop() throws MuleException
94      {
95          // nothing to do
96      }
97  
98      /*
99       * (non-Javadoc)
100      * 
101      * @see org.mule.api.lifecycle.Disposable#dispose()
102      */
103     public void dispose()
104     {
105         // nothing to do
106     }
107 
108     /*
109      * (non-Javadoc)
110      * 
111      * @see org.mule.api.context.Agent#registered()
112      */
113     public void registered()
114     {
115         // nothing to do
116     }
117 
118     /*
119      * (non-Javadoc)
120      * 
121      * @see org.mule.api.context.Agent#unregistered()
122      */
123     public void unregistered()
124     {
125         // nothing to do
126     }
127 
128 }