View Javadoc

1   /*
2    * $Id: Log4jAgent.java 22391 2011-07-12 12:00:48Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.Set;
22  
23  import javax.management.InstanceNotFoundException;
24  import javax.management.MBeanRegistrationException;
25  import javax.management.MBeanServer;
26  import javax.management.MBeanServerFactory;
27  import javax.management.MalformedObjectNameException;
28  import javax.management.ObjectInstance;
29  import javax.management.ObjectName;
30  
31  import org.apache.log4j.jmx.HierarchyDynamicMBean;
32  
33  /**
34   * <code>Log4jAgent</code> exposes the configuration of the Log4J instance running
35   * in Mule for Jmx management
36   */
37  public class Log4jAgent extends AbstractAgent
38  {
39      private MBeanServer mBeanServer;
40      public static final String JMX_OBJECT_NAME = "log4j:type=Hierarchy";
41  
42      private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
43      private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
44  
45      public Log4jAgent()
46      {
47          super("jmx-log4j");
48      }
49  
50      @Override
51      public String getDescription() {
52          return "JMX Log4J Agent";
53      }
54  
55      @Override
56      public void initialise() throws InitialisationException
57      {
58          try
59          {
60              mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
61              final ObjectName objectName = jmxSupport.getObjectName(JMX_OBJECT_NAME);
62              // unregister existing Log4j MBean first if required
63              unregisterMBeansIfNecessary();
64              mBeanServer.registerMBean(new HierarchyDynamicMBean(), objectName);
65          }
66          catch (Exception e)
67          {
68              throw new InitialisationException(CoreMessages.failedToStart("Log4j Agent"), e, this);
69          }
70      }
71  
72      /**
73       * Unregister log4j MBeans if there are any left over the old deployment
74       */
75      protected void unregisterMBeansIfNecessary()
76          throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException
77      {
78          if (mBeanServer.isRegistered(jmxSupport.getObjectName(JMX_OBJECT_NAME)))
79          {
80              // unregister all log4jMBeans and loggers
81              Set<ObjectInstance> log4jMBeans = mBeanServer.queryMBeans(jmxSupport.getObjectName("log4j*:*"), null);
82              for (ObjectInstance objectInstance : log4jMBeans)
83              {
84                  ObjectName theName = objectInstance.getObjectName();
85                  mBeanServer.unregisterMBean(theName);
86              }
87          }
88      }
89  
90      @Override
91      public void start() throws MuleException
92      {
93          // nothing to do
94      }
95  
96      @Override
97      public void stop() throws MuleException
98      {
99          // nothing to do
100     }
101 
102     @Override
103     public void dispose()
104     {
105         try
106         {
107             unregisterMBeansIfNecessary();
108         }
109         catch (Exception ex)
110         {
111             // ignore
112         }
113     }
114 }