1
2
3
4
5
6
7
8
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
36
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
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
71
72 protected void unregisterMBeansIfNecessary()
73 throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException
74 {
75 if (mBeanServer.isRegistered(jmxSupport.getObjectName(JMX_OBJECT_NAME)))
76 {
77
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
91 }
92
93 public void stop() throws MuleException
94 {
95
96 }
97
98
99
100
101
102
103 public void dispose()
104 {
105
106 }
107
108
109
110
111
112
113 public void registered()
114 {
115
116 }
117
118
119
120
121
122
123 public void unregistered()
124 {
125
126 }
127
128 }