Coverage Report - org.mule.module.management.agent.JmxServerNotificationAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
JmxServerNotificationAgent
71%
25/35
33%
2/6
1.533
JmxServerNotificationAgent$BroadcastNotificationService
100%
1/1
N/A
1.533
JmxServerNotificationAgent$BroadcastNotificationServiceMBean
N/A
N/A
1.533
JmxServerNotificationAgent$NotificationListener
62%
8/13
75%
3/4
1.533
JmxServerNotificationAgent$NotificationListenerMBean
N/A
N/A
1.533
 
 1  
 /*
 2  
  * $Id: JmxServerNotificationAgent.java 11234 2008-03-06 23:44:34Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
 5  
  *
 6  
  * The software in this package is published under the terms of the CPAL v1.0
 7  
  * license, a copy of which has been included with this distribution in the
 8  
  * LICENSE.txt file.
 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  
  * An agent that propergates Mule Server notifications to Jmx.
 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  
      * {@inheritDoc}
 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  
      * {@inheritDoc}
 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  
      * {@inheritDoc}
 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  
      * Should be a 1 line description of the agent.
 117  
      *
 118  
      * @return description
 119  
      */
 120  
     public String getDescription()
 121  
     {
 122  0
         return DEFAULT_AGENT_NAME + (registerListenerMbean ? " (Listener MBean registered)" : "");
 123  
     }
 124  
 
 125  
 
 126  
     /**
 127  
      * Getter for property 'jmxSupportFactory'.
 128  
      *
 129  
      * @return Value for property 'jmxSupportFactory'.
 130  
      */
 131  
     public JmxSupportFactory getJmxSupportFactory()
 132  
     {
 133  0
         return jmxSupportFactory;
 134  
     }
 135  
 
 136  
     /**
 137  
      * Setter for property 'jmxSupportFactory'.
 138  
      *
 139  
      * @param jmxSupportFactory Value to set for property 'jmxSupportFactory'.
 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  
         // no methods
 149  
     }
 150  
 
 151  6
     public static class BroadcastNotificationService extends NotificationBroadcasterSupport implements BroadcastNotificationServiceMBean
 152  
     {
 153  
         // no methods
 154  
     }
 155  
 
 156  
     public static interface NotificationListenerMBean
 157  
     {
 158  
         /**
 159  
          * Getter for property 'notificsationList'.
 160  
          *
 161  
          * @return Value for property 'notificsationList'.
 162  
          */
 163  
         List getNotificationsList();
 164  
 
 165  
         /**
 166  
          * Getter for property 'listSize'.
 167  
          *
 168  
          * @return Value for property 'listSize'.
 169  
          */
 170  
         int getListSize();
 171  
 
 172  
         /**
 173  
          * Setter for property 'listSize'.
 174  
          *
 175  
          * @param listSize Value to set for property 'listSize'.
 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  
          * {@inheritDoc}
 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  
          * {@inheritDoc}
 200  
          */
 201  
         public List getNotificationsList()
 202  
         {
 203  0
             return notifs;
 204  
         }
 205  
 
 206  
         /**
 207  
          * {@inheritDoc}
 208  
          */
 209  
         public int getListSize()
 210  
         {
 211  0
             return listSize;
 212  
         }
 213  
 
 214  
         /**
 215  
          * {@inheritDoc}
 216  
          */
 217  
         public void setListSize(int listSize)
 218  
         {
 219  0
             this.listSize = listSize;
 220  0
         }
 221  
 
 222  
         /**
 223  
          * Getter for property 'list'.
 224  
          *
 225  
          * @return Value for property 'list'.
 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  
 }