View Javadoc

1   /*
2    * $Id: Log4jAgent.java 7976 2007-08-21 14:26:13Z 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.management.agents;
12  
13  import org.mule.config.i18n.CoreMessages;
14  import org.mule.management.support.AutoDiscoveryJmxSupportFactory;
15  import org.mule.management.support.JmxSupport;
16  import org.mule.management.support.JmxSupportFactory;
17  import org.mule.umo.UMOException;
18  import org.mule.umo.lifecycle.InitialisationException;
19  import org.mule.umo.manager.UMOAgent;
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 implements UMOAgent
39  {
40  
41      private String name = "Log4j Agent";
42      private MBeanServer mBeanServer;
43      public static final String JMX_OBJECT_NAME = "log4j:type=Hierarchy";
44  
45      private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
46      private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
47  
48  
49      /*
50       * (non-Javadoc)
51       * 
52       * @see org.mule.umo.manager.UMOAgent#getName()
53       */
54      public String getName()
55      {
56          return this.name;
57      }
58  
59      /*
60       * (non-Javadoc)
61       * 
62       * @see org.mule.umo.manager.UMOAgent#setName(java.lang.String)
63       */
64      public void setName(String name)
65      {
66          this.name = name;
67      }
68  
69      /*
70       * (non-Javadoc)
71       * 
72       * @see org.mule.umo.manager.UMOAgent#getDescription()
73       */
74      public String getDescription()
75      {
76          return "Log4j JMX Agent";
77      }
78  
79      /*
80       * (non-Javadoc)
81       * 
82       * @see org.mule.umo.lifecycle.Initialisable#initialise()
83       */
84      public void initialise() throws InitialisationException
85      {
86          try
87          {
88              mBeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
89              final ObjectName objectName = jmxSupport.getObjectName(JMX_OBJECT_NAME);
90              // unregister existing Log4j MBean first if required
91              unregisterMBeansIfNecessary();
92              mBeanServer.registerMBean(new HierarchyDynamicMBean(), objectName);
93          }
94          catch (Exception e)
95          {
96              throw new InitialisationException(CoreMessages.failedToStart("JMX Agent"), e, this);
97          }
98      }
99  
100     /**
101      * Unregister all log4j MBeans if there are any left over the old deployment
102      */
103     protected void unregisterMBeansIfNecessary()
104         throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException
105     {
106         if (mBeanServer.isRegistered(jmxSupport.getObjectName(JMX_OBJECT_NAME)))
107         {
108             // unregister all log4jMBeans and loggers
109             Set log4jMBeans = mBeanServer.queryMBeans(jmxSupport.getObjectName("log4j*:*"), null);
110             for (Iterator it = log4jMBeans.iterator(); it.hasNext();)
111             {
112                 ObjectInstance objectInstance = (ObjectInstance)it.next();
113                 ObjectName theName = objectInstance.getObjectName();
114                 mBeanServer.unregisterMBean(theName);
115             }
116         }
117     }
118 
119     /*
120      * (non-Javadoc)
121      * 
122      * @see org.mule.umo.lifecycle.Startable#start()
123      */
124     public void start() throws UMOException
125     {
126         // nothing to do
127     }
128 
129     /*
130      * (non-Javadoc)
131      * 
132      * @see org.mule.umo.lifecycle.Stoppable#stop()
133      */
134     public void stop() throws UMOException
135     {
136         // nothing to do
137     }
138 
139     /*
140      * (non-Javadoc)
141      * 
142      * @see org.mule.umo.lifecycle.Disposable#dispose()
143      */
144     public void dispose()
145     {
146         // nothing to do
147     }
148 
149     /*
150      * (non-Javadoc)
151      * 
152      * @see org.mule.umo.manager.UMOAgent#registered()
153      */
154     public void registered()
155     {
156         // nothing to do
157     }
158 
159     /*
160      * (non-Javadoc)
161      * 
162      * @see org.mule.umo.manager.UMOAgent#unregistered()
163      */
164     public void unregistered()
165     {
166         // nothing to do
167     }
168 
169 }