View Javadoc

1   /*
2    * $Id: JmxServerNotificationAgent.java 22391 2011-07-12 12:00:48Z dirk.olmes $
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      @Override
57      protected void doInitialise() throws InitialisationException
58      {
59          try
60          {
61              jmxSupport = jmxSupportFactory.getJmxSupport();
62              mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
63              broadcasterObjectName = ObjectName.getInstance(jmxSupport.getDomainName(muleContext) + ":" + BROADCASTER_JMX_OBJECT_NAME);
64              broadcastNotificationMbean = new BroadcastNotificationService();
65              mBeanServer.registerMBean(broadcastNotificationMbean, broadcasterObjectName);
66              if (registerListenerMbean)
67              {
68                  listenerObjectName = ObjectName.getInstance(jmxSupport.getDomainName(muleContext) + ":" + LISTENER_JMX_OBJECT_NAME);
69                  NotificationListener mbean = new NotificationListener();
70                  broadcastNotificationMbean.addNotificationListener(mbean, null, null);
71                  mBeanServer.registerMBean(mbean, listenerObjectName);
72              }
73          }
74          catch (Exception e)
75          {
76              throw new InitialisationException(CoreMessages.failedToStart("JMX Server Notification Agent"), e, this);
77          }
78      }
79  
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      public void dispose()
86      {
87          if (listenerObjectName != null && mBeanServer.isRegistered(listenerObjectName))
88          {
89              try
90              {
91                  mBeanServer.unregisterMBean(listenerObjectName);
92              }
93              catch (Exception e)
94              {
95                  logger.warn(e.getMessage(), e);
96              }
97          }
98  
99          if (broadcasterObjectName != null && mBeanServer.isRegistered(broadcasterObjectName))
100         {
101             try
102             {
103                 mBeanServer.unregisterMBean(broadcasterObjectName);
104             }
105             catch (Exception e)
106             {
107                 logger.warn(e.getMessage(), e);
108             }
109         }        super.dispose();
110     }
111 
112     /**
113      * {@inheritDoc}
114      */
115     @Override
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     @Override
127     public String getDescription()
128     {
129         return DEFAULT_AGENT_NAME + (registerListenerMbean ? " (Listener MBean registered)" : "");
130     }
131 
132 
133     /**
134      * Getter for property 'jmxSupportFactory'.
135      *
136      * @return Value for property 'jmxSupportFactory'.
137      */
138     public JmxSupportFactory getJmxSupportFactory()
139     {
140         return jmxSupportFactory;
141     }
142 
143     /**
144      * Setter for property 'jmxSupportFactory'.
145      *
146      * @param jmxSupportFactory Value to set for property 'jmxSupportFactory'.
147      */
148     public void setJmxSupportFactory(JmxSupportFactory jmxSupportFactory)
149     {
150         this.jmxSupportFactory = jmxSupportFactory;
151     }
152 
153     public static interface BroadcastNotificationServiceMBean extends NotificationEmitter
154     {
155         // no methods
156     }
157 
158     public static class BroadcastNotificationService extends NotificationBroadcasterSupport implements BroadcastNotificationServiceMBean
159     {
160         // no methods
161     }
162 
163     public static interface NotificationListenerMBean
164     {
165         /**
166          * Getter for property 'notificsationList'.
167          *
168          * @return Value for property 'notificsationList'.
169          */
170         List getNotificationsList();
171 
172         /**
173          * Getter for property 'listSize'.
174          *
175          * @return Value for property 'listSize'.
176          */
177         int getListSize();
178 
179         /**
180          * Setter for property 'listSize'.
181          *
182          * @param listSize Value to set for property 'listSize'.
183          */
184         void setListSize(int listSize);
185     }
186 
187     public static class NotificationListener implements NotificationListenerMBean, javax.management.NotificationListener
188     {
189         private int listSize = 100;
190 
191         private List notifs;
192 
193         /**
194          * {@inheritDoc}
195          */
196         @Override
197         public void handleNotification(Notification notification, Object o)
198         {
199             if (getList().size() == listSize)
200             {
201                 getList().remove(listSize - 1);
202             }
203             getList().add(0, notification);
204         }
205 
206         /**
207          * {@inheritDoc}
208          */
209         public List getNotificationsList()
210         {
211             return notifs;
212         }
213 
214         /**
215          * {@inheritDoc}
216          */
217         @Override
218         public int getListSize()
219         {
220             return listSize;
221         }
222 
223         /**
224          * {@inheritDoc}
225          */
226         @Override
227         public void setListSize(int listSize)
228         {
229             this.listSize = listSize;
230         }
231 
232         /**
233          * Getter for property 'list'.
234          *
235          * @return Value for property 'list'.
236          */
237         protected List getList()
238         {
239             if (notifs == null)
240             {
241                 notifs = new ArrayList(listSize);
242             }
243             return notifs;
244         }
245 
246     }
247 
248 }