View Javadoc

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      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          } catch (Exception e)
76          {
77              throw new InitialisationException(CoreMessages.failedToStart("JMX Server Notification Agent"), e, this);
78          }
79      }
80  
81  
82      /**
83       * {@inheritDoc}
84       */
85      public void dispose()
86      {
87          try
88          {
89              if (listenerObjectName != null)
90              {
91                  mBeanServer.unregisterMBean(listenerObjectName);
92              }
93          } catch (Exception e)
94          {
95              logger.warn(e.getMessage(), e);
96          }
97          try
98          {
99              mBeanServer.unregisterMBean(broadcasterObjectName);
100         } catch (Exception e)
101         {
102             logger.warn(e.getMessage(), e);
103         }
104         super.dispose();
105     }
106 
107     /**
108      * {@inheritDoc}
109      */
110     protected void logEvent(ServerNotification e)
111     {
112         broadcastNotificationMbean.sendNotification(new Notification(e.getClass().getName(), e, e.getTimestamp(), e.toString()));
113     }
114 
115     /**
116      * Should be a 1 line description of the agent.
117      *
118      * @return description
119      */
120     public String getDescription()
121     {
122         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         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         this.jmxSupportFactory = jmxSupportFactory;
144     }
145 
146     public static interface BroadcastNotificationServiceMBean extends NotificationEmitter
147     {
148         // no methods
149     }
150 
151     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     public static class NotificationListener implements NotificationListenerMBean, javax.management.NotificationListener
181     {
182         private int listSize = 100;
183 
184         private List notifs;
185 
186         /**
187          * {@inheritDoc}
188          */
189         public void handleNotification(Notification notification, Object o)
190         {
191             if (getList().size() == listSize)
192             {
193                 getList().remove(listSize - 1);
194             }
195             getList().add(0, notification);
196         }
197 
198         /**
199          * {@inheritDoc}
200          */
201         public List getNotificationsList()
202         {
203             return notifs;
204         }
205 
206         /**
207          * {@inheritDoc}
208          */
209         public int getListSize()
210         {
211             return listSize;
212         }
213 
214         /**
215          * {@inheritDoc}
216          */
217         public void setListSize(int listSize)
218         {
219             this.listSize = listSize;
220         }
221 
222         /**
223          * Getter for property 'list'.
224          *
225          * @return Value for property 'list'.
226          */
227         protected List getList()
228         {
229             if (notifs == null)
230             {
231                 notifs = new ArrayList(listSize);
232             }
233             return notifs;
234         }
235 
236     }
237 
238 }