1 10 package org.mule.management.agents; 11 12 import java.util.ArrayList ; 13 import java.util.List ; 14 15 import javax.management.MBeanServer ; 16 import javax.management.MBeanServerFactory ; 17 import javax.management.Notification ; 18 import javax.management.NotificationBroadcasterSupport ; 19 import javax.management.NotificationEmitter ; 20 import javax.management.ObjectName ; 21 22 import org.mule.config.i18n.Message; 23 import org.mule.config.i18n.Messages; 24 import org.mule.impl.internal.admin.AbstractNotificationLoggerAgent; 25 import org.mule.management.support.AutoDiscoveryJmxSupportFactory; 26 import org.mule.management.support.JmxSupport; 27 import org.mule.management.support.JmxSupportFactory; 28 import org.mule.umo.lifecycle.InitialisationException; 29 import org.mule.umo.manager.UMOServerNotification; 30 31 37 public class JmxServerNotificationAgent extends AbstractNotificationLoggerAgent 38 { 39 40 public static final String LISTENER_JMX_OBJECT_NAME = "type=org.mule.Notification,name=MuleNotificationListener"; 41 public static final String BROADCASTER_JMX_OBJECT_NAME = "type=org.mule.Notification,name=MuleNotificationBroadcaster"; 42 public static final String DEFAULT_AGENT_NAME = "Jmx Notification Agent"; 43 44 private MBeanServer mBeanServer; 45 private BroadcastNotificationService broadcastNotificationMbean; 46 private boolean registerListenerMbean = true; 47 private ObjectName listenerObjectName; 48 private ObjectName broadcasterObjectName; 49 50 private JmxSupportFactory jmxSupportFactory = new AutoDiscoveryJmxSupportFactory(); 51 private JmxSupport jmxSupport; 52 53 54 public JmxServerNotificationAgent() 55 { 56 setName(DEFAULT_AGENT_NAME); 58 } 59 60 63 protected void doInitialise() throws InitialisationException 64 { 65 try 66 { 67 jmxSupport = jmxSupportFactory.newJmxSupport(); 68 mBeanServer = (MBeanServer ) MBeanServerFactory.findMBeanServer(null).get(0); 69 broadcasterObjectName = ObjectName.getInstance(jmxSupport.getDomainName() + ":" + BROADCASTER_JMX_OBJECT_NAME); 70 broadcastNotificationMbean = new BroadcastNotificationService(); 71 mBeanServer.registerMBean(broadcastNotificationMbean, broadcasterObjectName); 72 if (registerListenerMbean) 73 { 74 listenerObjectName = ObjectName.getInstance(jmxSupport.getDomainName() + ":" + LISTENER_JMX_OBJECT_NAME); 75 NotificationListener mbean = new NotificationListener(); 76 broadcastNotificationMbean.addNotificationListener(mbean, null, null); 77 mBeanServer.registerMBean(mbean, listenerObjectName); 78 } 79 } catch (Exception e) 80 { 81 throw new InitialisationException(new Message(Messages.FAILED_TO_START_X, "JMX Server Notification Agent"), e, this); 82 } 83 } 84 85 86 89 public void dispose() 90 { 91 try 92 { 93 if (listenerObjectName != null) 94 { 95 mBeanServer.unregisterMBean(listenerObjectName); 96 } 97 } catch (Exception e) 98 { 99 logger.warn(e.getMessage(), e); 100 } 101 try 102 { 103 mBeanServer.unregisterMBean(broadcasterObjectName); 104 } catch (Exception e) 105 { 106 logger.warn(e.getMessage(), e); 107 } 108 super.dispose(); 109 } 110 111 114 protected void logEvent(UMOServerNotification e) 115 { 116 broadcastNotificationMbean.sendNotification(new Notification (e.getClass().getName(), e, e.getTimestamp(), e.toString())); 117 } 118 119 124 public String getDescription() 125 { 126 return DEFAULT_AGENT_NAME + (registerListenerMbean ? " (Listener MBean registered)" : ""); 127 } 128 129 130 135 public JmxSupportFactory getJmxSupportFactory() 136 { 137 return jmxSupportFactory; 138 } 139 140 145 public void setJmxSupportFactory(JmxSupportFactory jmxSupportFactory) 146 { 147 this.jmxSupportFactory = jmxSupportFactory; 148 } 149 150 public static interface BroadcastNotificationServiceMBean extends NotificationEmitter 151 { 152 } 154 155 public static class BroadcastNotificationService extends NotificationBroadcasterSupport implements BroadcastNotificationServiceMBean 156 { 157 } 159 160 public static interface NotificationListenerMBean 161 { 162 167 List getNotificationsList(); 168 169 174 int getListSize(); 175 176 181 void setListSize(int listSize); 182 } 183 184 public static class NotificationListener implements NotificationListenerMBean, javax.management.NotificationListener 185 { 186 private int listSize = 100; 187 188 private List notifs; 189 190 193 public void handleNotification(Notification notification, Object o) 194 { 195 if (getList().size() == listSize) 196 { 197 getList().remove(listSize - 1); 198 } 199 getList().add(0, notification); 200 } 201 202 205 public List getNotificationsList() 206 { 207 return notifs; 208 } 209 210 213 public int getListSize() 214 { 215 return listSize; 216 } 217 218 221 public void setListSize(int listSize) 222 { 223 this.listSize = listSize; 224 } 225 226 231 protected List getList() 232 { 233 if (notifs == null) 234 { 235 notifs = new ArrayList (listSize); 236 } 237 return notifs; 238 } 239 240 } 241 242 } 243 | Popular Tags |