1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.module.management.agent; |
8 | |
|
9 | |
import org.mule.agent.AbstractNotificationLoggerAgent; |
10 | |
import org.mule.api.context.notification.ServerNotification; |
11 | |
import org.mule.api.lifecycle.InitialisationException; |
12 | |
import org.mule.config.i18n.CoreMessages; |
13 | |
import org.mule.module.management.support.AutoDiscoveryJmxSupportFactory; |
14 | |
import org.mule.module.management.support.JmxSupport; |
15 | |
import org.mule.module.management.support.JmxSupportFactory; |
16 | |
|
17 | |
import java.util.ArrayList; |
18 | |
import java.util.List; |
19 | |
|
20 | |
import javax.management.MBeanServer; |
21 | |
import javax.management.MBeanServerFactory; |
22 | |
import javax.management.Notification; |
23 | |
import javax.management.NotificationBroadcasterSupport; |
24 | |
import javax.management.NotificationEmitter; |
25 | |
import javax.management.ObjectName; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class JmxServerNotificationAgent extends AbstractNotificationLoggerAgent |
32 | |
{ |
33 | |
|
34 | |
public static final String LISTENER_JMX_OBJECT_NAME = "type=org.mule.Notification,name=MuleNotificationListener"; |
35 | |
public static final String BROADCASTER_JMX_OBJECT_NAME = "type=org.mule.Notification,name=MuleNotificationBroadcaster"; |
36 | |
public static final String DEFAULT_AGENT_NAME = "Jmx Notification Agent"; |
37 | |
|
38 | |
private MBeanServer mBeanServer; |
39 | |
private BroadcastNotificationService broadcastNotificationMbean; |
40 | 0 | private boolean registerListenerMbean = true; |
41 | |
private ObjectName listenerObjectName; |
42 | |
private ObjectName broadcasterObjectName; |
43 | |
|
44 | 0 | private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance(); |
45 | |
private JmxSupport jmxSupport; |
46 | |
|
47 | |
|
48 | |
public JmxServerNotificationAgent() |
49 | |
{ |
50 | 0 | super(DEFAULT_AGENT_NAME); |
51 | 0 | } |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
protected void doInitialise() throws InitialisationException |
57 | |
{ |
58 | |
try |
59 | |
{ |
60 | 0 | jmxSupport = jmxSupportFactory.getJmxSupport(); |
61 | 0 | mBeanServer = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0); |
62 | 0 | broadcasterObjectName = ObjectName.getInstance(jmxSupport.getDomainName(muleContext) + ":" + BROADCASTER_JMX_OBJECT_NAME); |
63 | 0 | broadcastNotificationMbean = new BroadcastNotificationService(); |
64 | 0 | mBeanServer.registerMBean(broadcastNotificationMbean, broadcasterObjectName); |
65 | 0 | if (registerListenerMbean) |
66 | |
{ |
67 | 0 | listenerObjectName = ObjectName.getInstance(jmxSupport.getDomainName(muleContext) + ":" + LISTENER_JMX_OBJECT_NAME); |
68 | 0 | NotificationListener mbean = new NotificationListener(); |
69 | 0 | broadcastNotificationMbean.addNotificationListener(mbean, null, null); |
70 | 0 | mBeanServer.registerMBean(mbean, listenerObjectName); |
71 | |
} |
72 | |
} |
73 | 0 | catch (Exception e) |
74 | |
{ |
75 | 0 | throw new InitialisationException(CoreMessages.failedToStart("JMX Server Notification Agent"), e, this); |
76 | 0 | } |
77 | 0 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public void dispose() |
84 | |
{ |
85 | 0 | if (listenerObjectName != null && mBeanServer.isRegistered(listenerObjectName)) |
86 | |
{ |
87 | |
try |
88 | |
{ |
89 | 0 | mBeanServer.unregisterMBean(listenerObjectName); |
90 | |
} |
91 | 0 | catch (Exception e) |
92 | |
{ |
93 | 0 | logger.warn(e.getMessage(), e); |
94 | 0 | } |
95 | |
} |
96 | |
|
97 | 0 | if (broadcasterObjectName != null && mBeanServer.isRegistered(broadcasterObjectName)) |
98 | |
{ |
99 | |
try |
100 | |
{ |
101 | 0 | mBeanServer.unregisterMBean(broadcasterObjectName); |
102 | |
} |
103 | 0 | catch (Exception e) |
104 | |
{ |
105 | 0 | logger.warn(e.getMessage(), e); |
106 | 0 | } |
107 | 0 | } super.dispose(); |
108 | 0 | } |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
protected void logEvent(ServerNotification e) |
114 | |
{ |
115 | 0 | broadcastNotificationMbean.sendNotification(new Notification(e.getClass().getName(), e, e.getTimestamp(), e.toString())); |
116 | 0 | } |
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
public String getDescription() |
124 | |
{ |
125 | 0 | return DEFAULT_AGENT_NAME + (registerListenerMbean ? " (Listener MBean registered)" : ""); |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
public JmxSupportFactory getJmxSupportFactory() |
135 | |
{ |
136 | 0 | return jmxSupportFactory; |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public void setJmxSupportFactory(JmxSupportFactory jmxSupportFactory) |
145 | |
{ |
146 | 0 | this.jmxSupportFactory = jmxSupportFactory; |
147 | 0 | } |
148 | |
|
149 | |
public static interface BroadcastNotificationServiceMBean extends NotificationEmitter |
150 | |
{ |
151 | |
|
152 | |
} |
153 | |
|
154 | 0 | public static class BroadcastNotificationService extends NotificationBroadcasterSupport implements BroadcastNotificationServiceMBean |
155 | |
{ |
156 | |
|
157 | |
} |
158 | |
|
159 | |
public static interface NotificationListenerMBean |
160 | |
{ |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
List getNotificationsList(); |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
int getListSize(); |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
void setListSize(int listSize); |
181 | |
} |
182 | |
|
183 | 0 | public static class NotificationListener implements NotificationListenerMBean, javax.management.NotificationListener |
184 | |
{ |
185 | 0 | private int listSize = 100; |
186 | |
|
187 | |
private List notifs; |
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
public void handleNotification(Notification notification, Object o) |
193 | |
{ |
194 | 0 | if (getList().size() == listSize) |
195 | |
{ |
196 | 0 | getList().remove(listSize - 1); |
197 | |
} |
198 | 0 | getList().add(0, notification); |
199 | 0 | } |
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
public List getNotificationsList() |
205 | |
{ |
206 | 0 | return notifs; |
207 | |
} |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
public int getListSize() |
213 | |
{ |
214 | 0 | return listSize; |
215 | |
} |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
public void setListSize(int listSize) |
221 | |
{ |
222 | 0 | this.listSize = listSize; |
223 | 0 | } |
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
protected List getList() |
231 | |
{ |
232 | 0 | if (notifs == null) |
233 | |
{ |
234 | 0 | notifs = new ArrayList(listSize); |
235 | |
} |
236 | 0 | return notifs; |
237 | |
} |
238 | |
|
239 | |
} |
240 | |
|
241 | |
} |