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