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