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