Coverage Report - org.mule.module.management.agent.JmxServerNotificationAgent
 
Classes in this File Line Coverage Branch Coverage Complexity
JmxServerNotificationAgent
0%
0/36
0%
0/12
1.6
JmxServerNotificationAgent$BroadcastNotificationService
0%
0/1
N/A
1.6
JmxServerNotificationAgent$BroadcastNotificationServiceMBean
N/A
N/A
1.6
JmxServerNotificationAgent$NotificationListener
0%
0/13
0%
0/4
1.6
JmxServerNotificationAgent$NotificationListenerMBean
N/A
N/A
1.6
 
 1  
 /*
 2  
  * $Id: JmxServerNotificationAgent.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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  0
     private boolean registerListenerMbean = true;
 44  
     private ObjectName listenerObjectName;
 45  
     private ObjectName broadcasterObjectName;
 46  
 
 47  0
     private JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
 48  
     private JmxSupport jmxSupport;
 49  
 
 50  
 
 51  
     public JmxServerNotificationAgent()
 52  
     {
 53  0
         super(DEFAULT_AGENT_NAME);
 54  0
     }
 55  
 
 56  
     /**
 57  
      * {@inheritDoc}
 58  
      */
 59  
     protected void doInitialise() throws InitialisationException
 60  
     {
 61  
         try
 62  
         {
 63  0
             jmxSupport = jmxSupportFactory.getJmxSupport();
 64  0
             mBeanServer = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);
 65  0
             broadcasterObjectName = ObjectName.getInstance(jmxSupport.getDomainName(muleContext) + ":" + BROADCASTER_JMX_OBJECT_NAME);
 66  0
             broadcastNotificationMbean = new BroadcastNotificationService();
 67  0
             mBeanServer.registerMBean(broadcastNotificationMbean, broadcasterObjectName);
 68  0
             if (registerListenerMbean)
 69  
             {
 70  0
                 listenerObjectName = ObjectName.getInstance(jmxSupport.getDomainName(muleContext) + ":" + LISTENER_JMX_OBJECT_NAME);
 71  0
                 NotificationListener mbean = new NotificationListener();
 72  0
                 broadcastNotificationMbean.addNotificationListener(mbean, null, null);
 73  0
                 mBeanServer.registerMBean(mbean, listenerObjectName);
 74  
             }
 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 (listenerObjectName != null && mBeanServer.isRegistered(listenerObjectName))
 89  
         {
 90  
             try
 91  
             {
 92  0
                 mBeanServer.unregisterMBean(listenerObjectName);
 93  
             }
 94  0
             catch (Exception e)
 95  
             {
 96  0
                 logger.warn(e.getMessage(), e);
 97  0
             }
 98  
         }
 99  
 
 100  0
         if (broadcasterObjectName != null && mBeanServer.isRegistered(broadcasterObjectName))
 101  
         {
 102  
             try
 103  
             {
 104  0
                 mBeanServer.unregisterMBean(broadcasterObjectName);
 105  
             }
 106  0
             catch (Exception e)
 107  
             {
 108  0
                 logger.warn(e.getMessage(), e);
 109  0
             }
 110  0
         }        super.dispose();
 111  0
     }
 112  
 
 113  
     /**
 114  
      * {@inheritDoc}
 115  
      */
 116  
     protected void logEvent(ServerNotification e)
 117  
     {
 118  0
         broadcastNotificationMbean.sendNotification(new Notification(e.getClass().getName(), e, e.getTimestamp(), e.toString()));
 119  0
     }
 120  
 
 121  
     /**
 122  
      * Should be a 1 line description of the agent.
 123  
      *
 124  
      * @return description
 125  
      */
 126  
     public String getDescription()
 127  
     {
 128  0
         return DEFAULT_AGENT_NAME + (registerListenerMbean ? " (Listener MBean registered)" : "");
 129  
     }
 130  
 
 131  
 
 132  
     /**
 133  
      * Getter for property 'jmxSupportFactory'.
 134  
      *
 135  
      * @return Value for property 'jmxSupportFactory'.
 136  
      */
 137  
     public JmxSupportFactory getJmxSupportFactory()
 138  
     {
 139  0
         return jmxSupportFactory;
 140  
     }
 141  
 
 142  
     /**
 143  
      * Setter for property 'jmxSupportFactory'.
 144  
      *
 145  
      * @param jmxSupportFactory Value to set for property 'jmxSupportFactory'.
 146  
      */
 147  
     public void setJmxSupportFactory(JmxSupportFactory jmxSupportFactory)
 148  
     {
 149  0
         this.jmxSupportFactory = jmxSupportFactory;
 150  0
     }
 151  
 
 152  
     public static interface BroadcastNotificationServiceMBean extends NotificationEmitter
 153  
     {
 154  
         // no methods
 155  
     }
 156  
 
 157  0
     public static class BroadcastNotificationService extends NotificationBroadcasterSupport implements BroadcastNotificationServiceMBean
 158  
     {
 159  
         // no methods
 160  
     }
 161  
 
 162  
     public static interface NotificationListenerMBean
 163  
     {
 164  
         /**
 165  
          * Getter for property 'notificsationList'.
 166  
          *
 167  
          * @return Value for property 'notificsationList'.
 168  
          */
 169  
         List getNotificationsList();
 170  
 
 171  
         /**
 172  
          * Getter for property 'listSize'.
 173  
          *
 174  
          * @return Value for property 'listSize'.
 175  
          */
 176  
         int getListSize();
 177  
 
 178  
         /**
 179  
          * Setter for property 'listSize'.
 180  
          *
 181  
          * @param listSize Value to set for property 'listSize'.
 182  
          */
 183  
         void setListSize(int listSize);
 184  
     }
 185  
 
 186  0
     public static class NotificationListener implements NotificationListenerMBean, javax.management.NotificationListener
 187  
     {
 188  0
         private int listSize = 100;
 189  
 
 190  
         private List notifs;
 191  
 
 192  
         /**
 193  
          * {@inheritDoc}
 194  
          */
 195  
         public void handleNotification(Notification notification, Object o)
 196  
         {
 197  0
             if (getList().size() == listSize)
 198  
             {
 199  0
                 getList().remove(listSize - 1);
 200  
             }
 201  0
             getList().add(0, notification);
 202  0
         }
 203  
 
 204  
         /**
 205  
          * {@inheritDoc}
 206  
          */
 207  
         public List getNotificationsList()
 208  
         {
 209  0
             return notifs;
 210  
         }
 211  
 
 212  
         /**
 213  
          * {@inheritDoc}
 214  
          */
 215  
         public int getListSize()
 216  
         {
 217  0
             return listSize;
 218  
         }
 219  
 
 220  
         /**
 221  
          * {@inheritDoc}
 222  
          */
 223  
         public void setListSize(int listSize)
 224  
         {
 225  0
             this.listSize = listSize;
 226  0
         }
 227  
 
 228  
         /**
 229  
          * Getter for property 'list'.
 230  
          *
 231  
          * @return Value for property 'list'.
 232  
          */
 233  
         protected List getList()
 234  
         {
 235  0
             if (notifs == null)
 236  
             {
 237  0
                 notifs = new ArrayList(listSize);
 238  
             }
 239  0
             return notifs;
 240  
         }
 241  
 
 242  
     }
 243  
 
 244  
 }