View Javadoc

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