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