Coverage Report - org.mule.management.agents.JmxServerNotificationAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
JmxServerNotificationAgent
0%
0/38
0%
0/4
1.667
JmxServerNotificationAgent$BroadcastNotificationService
0%
0/1
N/A
1.667
JmxServerNotificationAgent$BroadcastNotificationServiceMBean
N/A
N/A
1.667
JmxServerNotificationAgent$NotificationListener
0%
0/13
0%
0/2
1.667
JmxServerNotificationAgent$NotificationListenerMBean
N/A
N/A
1.667
 
 1  
 /*
 2  
  * $Id: JmxServerNotificationAgent.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 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.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  
  * 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  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  
         // set default name, overridable by config
 55  0
         setName(DEFAULT_AGENT_NAME);
 56  0
     }
 57  
 
 58  
     /**
 59  
      * {@inheritDoc}
 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  
      * {@inheritDoc}
 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  
      * {@inheritDoc}
 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  
      * Should be a 1 line description of the agent.
 122  
      *
 123  
      * @return description
 124  
      */
 125  
     public String getDescription()
 126  
     {
 127  0
         return DEFAULT_AGENT_NAME + (registerListenerMbean ? " (Listener MBean registered)" : "");
 128  
     }
 129  
 
 130  
 
 131  
     /**
 132  
      * Getter for property 'jmxSupportFactory'.
 133  
      *
 134  
      * @return Value for property 'jmxSupportFactory'.
 135  
      */
 136  
     public JmxSupportFactory getJmxSupportFactory()
 137  
     {
 138  0
         return jmxSupportFactory;
 139  
     }
 140  
 
 141  
     /**
 142  
      * Setter for property 'jmxSupportFactory'.
 143  
      *
 144  
      * @param jmxSupportFactory Value to set for property 'jmxSupportFactory'.
 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  
         // no methods
 154  
     }
 155  
 
 156  0
     public static class BroadcastNotificationService extends NotificationBroadcasterSupport implements BroadcastNotificationServiceMBean
 157  
     {
 158  
         // no methods
 159  
     }
 160  
 
 161  
     public static interface NotificationListenerMBean
 162  
     {
 163  
         /**
 164  
          * Getter for property 'notificsationList'.
 165  
          *
 166  
          * @return Value for property 'notificsationList'.
 167  
          */
 168  
         List getNotificationsList();
 169  
 
 170  
         /**
 171  
          * Getter for property 'listSize'.
 172  
          *
 173  
          * @return Value for property 'listSize'.
 174  
          */
 175  
         int getListSize();
 176  
 
 177  
         /**
 178  
          * Setter for property 'listSize'.
 179  
          *
 180  
          * @param listSize Value to set for property 'listSize'.
 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  
          * {@inheritDoc}
 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  
          * {@inheritDoc}
 205  
          */
 206  
         public List getNotificationsList()
 207  
         {
 208  0
             return notifs;
 209  
         }
 210  
 
 211  
         /**
 212  
          * {@inheritDoc}
 213  
          */
 214  
         public int getListSize()
 215  
         {
 216  0
             return listSize;
 217  
         }
 218  
 
 219  
         /**
 220  
          * {@inheritDoc}
 221  
          */
 222  
         public void setListSize(int listSize)
 223  
         {
 224  0
             this.listSize = listSize;
 225  0
         }
 226  
 
 227  
         /**
 228  
          * Getter for property 'list'.
 229  
          *
 230  
          * @return Value for property 'list'.
 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  
 }