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 | 0 | private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance(); |
44 | 0 | private JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport(); |
45 | |
|
46 | |
public Log4jAgent() |
47 | |
{ |
48 | 0 | super("jmx-log4j"); |
49 | 0 | } |
50 | |
|
51 | |
@Override |
52 | |
public String getDescription() { |
53 | 0 | return "JMX Log4J Agent"; |
54 | |
} |
55 | |
|
56 | |
public void initialise() throws InitialisationException |
57 | |
{ |
58 | |
try |
59 | |
{ |
60 | 0 | mBeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0); |
61 | 0 | final ObjectName objectName = jmxSupport.getObjectName(JMX_OBJECT_NAME); |
62 | |
|
63 | 0 | unregisterMBeansIfNecessary(); |
64 | 0 | mBeanServer.registerMBean(new HierarchyDynamicMBean(), objectName); |
65 | |
} |
66 | 0 | catch (Exception e) |
67 | |
{ |
68 | 0 | throw new InitialisationException(CoreMessages.failedToStart("Log4j Agent"), e, this); |
69 | 0 | } |
70 | 0 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
protected void unregisterMBeansIfNecessary() |
76 | |
throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException |
77 | |
{ |
78 | 0 | if (mBeanServer.isRegistered(jmxSupport.getObjectName(JMX_OBJECT_NAME))) |
79 | |
{ |
80 | |
|
81 | 0 | Set log4jMBeans = mBeanServer.queryMBeans(jmxSupport.getObjectName("log4j*:*"), null); |
82 | 0 | for (Iterator it = log4jMBeans.iterator(); it.hasNext();) |
83 | |
{ |
84 | 0 | ObjectInstance objectInstance = (ObjectInstance)it.next(); |
85 | 0 | ObjectName theName = objectInstance.getObjectName(); |
86 | 0 | mBeanServer.unregisterMBean(theName); |
87 | 0 | } |
88 | |
} |
89 | 0 | } |
90 | |
|
91 | |
public void start() throws MuleException |
92 | |
{ |
93 | |
|
94 | 0 | } |
95 | |
|
96 | |
public void stop() throws MuleException |
97 | |
{ |
98 | |
|
99 | 0 | } |
100 | |
|
101 | |
public void dispose() |
102 | |
{ |
103 | |
try |
104 | |
{ |
105 | 0 | unregisterMBeansIfNecessary(); |
106 | |
} |
107 | 0 | catch (Exception ex) |
108 | |
{ |
109 | |
|
110 | 0 | } |
111 | 0 | } |
112 | |
|
113 | |
} |