KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: Log4jAgent.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
11 package org.mule.management.agents;
12
13 import java.util.Iterator JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import javax.management.InstanceNotFoundException JavaDoc;
17 import javax.management.MBeanRegistrationException JavaDoc;
18 import javax.management.MBeanServer JavaDoc;
19 import javax.management.MBeanServerFactory JavaDoc;
20 import javax.management.MalformedObjectNameException JavaDoc;
21 import javax.management.ObjectInstance JavaDoc;
22 import javax.management.ObjectName JavaDoc;
23
24 import org.apache.log4j.jmx.HierarchyDynamicMBean;
25 import org.mule.config.i18n.Message;
26 import org.mule.config.i18n.Messages;
27 import org.mule.umo.UMOException;
28 import org.mule.umo.lifecycle.InitialisationException;
29 import org.mule.umo.manager.UMOAgent;
30
31 /**
32  * <code>Log4jAgent</code> exposes the configuration of the Log4J instance running
33  * in Mule for Jmx management
34  */

35 public class Log4jAgent implements UMOAgent
36 {
37
38     private String JavaDoc name = "Log4j Agent";
39     private MBeanServer JavaDoc mBeanServer;
40     public static final String JavaDoc JMX_OBJECT_NAME = "log4j:type=Hierarchy";
41
42     /*
43      * (non-Javadoc)
44      *
45      * @see org.mule.umo.manager.UMOAgent#getName()
46      */

47     public String JavaDoc getName()
48     {
49         return this.name;
50     }
51
52     /*
53      * (non-Javadoc)
54      *
55      * @see org.mule.umo.manager.UMOAgent#setName(java.lang.String)
56      */

57     public void setName(String JavaDoc name)
58     {
59         this.name = name;
60     }
61
62     /*
63      * (non-Javadoc)
64      *
65      * @see org.mule.umo.manager.UMOAgent#getDescription()
66      */

67     public String JavaDoc getDescription()
68     {
69         return "Log4j JMX Agent";
70     }
71
72     /*
73      * (non-Javadoc)
74      *
75      * @see org.mule.umo.lifecycle.Initialisable#initialise()
76      */

77     public void initialise() throws InitialisationException
78     {
79         try
80         {
81             mBeanServer = (MBeanServer JavaDoc)MBeanServerFactory.findMBeanServer(null).get(0);
82             final ObjectName JavaDoc objectName = ObjectName.getInstance(JMX_OBJECT_NAME);
83             // unregister existing Log4j MBean first if required
84
unregisterMBeansIfNecessary();
85             mBeanServer.registerMBean(new HierarchyDynamicMBean(), objectName);
86         }
87         catch (Exception JavaDoc e)
88         {
89             throw new InitialisationException(new Message(Messages.FAILED_TO_START_X, "JMX Agent"), e, this);
90         }
91     }
92
93     /**
94      * Unregister all log4j MBeans if there are any left over the old deployment
95      */

96     protected void unregisterMBeansIfNecessary()
97         throws MalformedObjectNameException JavaDoc, InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc
98     {
99         if (mBeanServer.isRegistered(ObjectName.getInstance(JMX_OBJECT_NAME)))
100         {
101             // unregister all log4jMBeans and loggers
102
Set JavaDoc log4jMBeans = mBeanServer.queryMBeans(ObjectName.getInstance("log4j*:*"), null);
103             for (Iterator JavaDoc it = log4jMBeans.iterator(); it.hasNext();)
104             {
105                 ObjectInstance JavaDoc objectInstance = (ObjectInstance JavaDoc)it.next();
106                 ObjectName JavaDoc theName = objectInstance.getObjectName();
107                 mBeanServer.unregisterMBean(theName);
108             }
109         }
110     }
111
112     /*
113      * (non-Javadoc)
114      *
115      * @see org.mule.umo.lifecycle.Startable#start()
116      */

117     public void start() throws UMOException
118     {
119         // nothing to do
120
}
121
122     /*
123      * (non-Javadoc)
124      *
125      * @see org.mule.umo.lifecycle.Stoppable#stop()
126      */

127     public void stop() throws UMOException
128     {
129         // nothing to do
130
}
131
132     /*
133      * (non-Javadoc)
134      *
135      * @see org.mule.umo.lifecycle.Disposable#dispose()
136      */

137     public void dispose()
138     {
139         // nothing to do
140
}
141
142     /*
143      * (non-Javadoc)
144      *
145      * @see org.mule.umo.manager.UMOAgent#registered()
146      */

147     public void registered()
148     {
149         // nothing to do
150
}
151
152     /*
153      * (non-Javadoc)
154      *
155      * @see org.mule.umo.manager.UMOAgent#unregistered()
156      */

157     public void unregistered()
158     {
159         // nothing to do
160
}
161
162 }
163
Popular Tags