1
2
3
4
5
6
7
8
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
36
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
51
52
53
54 public String getName()
55 {
56 return this.name;
57 }
58
59
60
61
62
63
64 public void setName(String name)
65 {
66 this.name = name;
67 }
68
69
70
71
72
73
74 public String getDescription()
75 {
76 return "Log4j JMX Agent";
77 }
78
79
80
81
82
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
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
102
103 protected void unregisterMBeansIfNecessary()
104 throws MalformedObjectNameException, InstanceNotFoundException, MBeanRegistrationException
105 {
106 if (mBeanServer.isRegistered(jmxSupport.getObjectName(JMX_OBJECT_NAME)))
107 {
108
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
121
122
123
124 public void start() throws UMOException
125 {
126
127 }
128
129
130
131
132
133
134 public void stop() throws UMOException
135 {
136
137 }
138
139
140
141
142
143
144 public void dispose()
145 {
146
147 }
148
149
150
151
152
153
154 public void registered()
155 {
156
157 }
158
159
160
161
162
163
164 public void unregistered()
165 {
166
167 }
168
169 }