View Javadoc

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