View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.management.agent;
8   
9   import org.mule.AbstractAgent;
10  import org.mule.api.MuleException;
11  import org.mule.api.lifecycle.InitialisationException;
12  import org.mule.config.i18n.CoreMessages;
13  import org.mule.module.management.support.AutoDiscoveryJmxSupportFactory;
14  import org.mule.module.management.support.JmxSupport;
15  import org.mule.module.management.support.JmxSupportFactory;
16  
17  import java.util.Iterator;
18  import java.util.Set;
19  
20  import javax.management.InstanceNotFoundException;
21  import javax.management.MBeanRegistrationException;
22  import javax.management.MBeanServer;
23  import javax.management.MBeanServerFactory;
24  import javax.management.MalformedObjectNameException;
25  import javax.management.ObjectInstance;
26  import javax.management.ObjectName;
27  
28  import org.apache.log4j.jmx.HierarchyDynamicMBean;
29  
30  /**
31   * <code>Log4jAgent</code> exposes the configuration of the Log4J instance running
32   * in Mule for Jmx management
33   */
34  public class Log4jAgent extends AbstractAgent
35  {
36      private MBeanServer mBeanServer;
37      public static final String JMX_OBJECT_NAME = "log4j:type=Hierarchy";
38  
39      private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
40      private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
41  
42      public Log4jAgent()
43      {
44          super("jmx-log4j");
45      }
46  
47      @Override
48      public String getDescription() {
49          return "JMX Log4J Agent";
50      }
51  
52      public void initialise() throws InitialisationException
53      {
54          try
55          {
56              mBeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
57              final ObjectName objectName = jmxSupport.getObjectName(JMX_OBJECT_NAME);
58              // unregister existing Log4j MBean first if required
59              unregisterMBeansIfNecessary();
60              mBeanServer.registerMBean(new HierarchyDynamicMBean(), objectName);
61          }
62          catch (Exception e)
63          {
64              throw new InitialisationException(CoreMessages.failedToStart("Log4j Agent"), e, this);
65          }
66      }
67  
68      /**
69       * Unregister log4j MBeans if there are any left over the old deployment
70       */
71      protected void unregisterMBeansIfNecessary()
72          throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException
73      {
74          if (mBeanServer.isRegistered(jmxSupport.getObjectName(JMX_OBJECT_NAME)))
75          {
76              // unregister all log4jMBeans and loggers
77              Set log4jMBeans = mBeanServer.queryMBeans(jmxSupport.getObjectName("log4j*:*"), null);
78              for (Iterator it = log4jMBeans.iterator(); it.hasNext();)
79              {
80                  ObjectInstance objectInstance = (ObjectInstance)it.next();
81                  ObjectName theName = objectInstance.getObjectName();
82                  mBeanServer.unregisterMBean(theName);
83              }
84          }
85      }
86  
87      public void start() throws MuleException
88      {
89          // nothing to do
90      }
91  
92      public void stop() throws MuleException
93      {
94          // nothing to do
95      }
96  
97      public void dispose()
98      {
99          try
100         {
101             unregisterMBeansIfNecessary();
102         }
103         catch (Exception ex)
104         {
105             // ignore
106         }
107     }
108 
109 }