KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > management > agents > JmxServerNotificationAgent


1 /*
2  * $Id: JmxServerNotificationAgent.java 4219 2006-12-09 10:15:14Z lajos $
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 MuleSource MPL
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 java.util.ArrayList JavaDoc;
13 import java.util.List JavaDoc;
14
15 import javax.management.MBeanServer JavaDoc;
16 import javax.management.MBeanServerFactory JavaDoc;
17 import javax.management.Notification JavaDoc;
18 import javax.management.NotificationBroadcasterSupport JavaDoc;
19 import javax.management.NotificationEmitter JavaDoc;
20 import javax.management.ObjectName JavaDoc;
21
22 import org.mule.config.i18n.Message;
23 import org.mule.config.i18n.Messages;
24 import org.mule.impl.internal.admin.AbstractNotificationLoggerAgent;
25 import org.mule.management.support.AutoDiscoveryJmxSupportFactory;
26 import org.mule.management.support.JmxSupport;
27 import org.mule.management.support.JmxSupportFactory;
28 import org.mule.umo.lifecycle.InitialisationException;
29 import org.mule.umo.manager.UMOServerNotification;
30
31 /**
32  * An agent that propergates Mule Server notifications to Jmx.
33  *
34  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
35  * @version $Revision: 4219 $
36  */

37 public class JmxServerNotificationAgent extends AbstractNotificationLoggerAgent
38 {
39
40     public static final String JavaDoc LISTENER_JMX_OBJECT_NAME = "type=org.mule.Notification,name=MuleNotificationListener";
41     public static final String JavaDoc BROADCASTER_JMX_OBJECT_NAME = "type=org.mule.Notification,name=MuleNotificationBroadcaster";
42     public static final String JavaDoc DEFAULT_AGENT_NAME = "Jmx Notification Agent";
43
44     private MBeanServer JavaDoc mBeanServer;
45     private BroadcastNotificationService broadcastNotificationMbean;
46     private boolean registerListenerMbean = true;
47     private ObjectName JavaDoc listenerObjectName;
48     private ObjectName JavaDoc broadcasterObjectName;
49
50     private JmxSupportFactory jmxSupportFactory = new AutoDiscoveryJmxSupportFactory();
51     private JmxSupport jmxSupport;
52
53
54     public JmxServerNotificationAgent()
55     {
56         // set default name, overridable by config
57
setName(DEFAULT_AGENT_NAME);
58     }
59
60     /**
61      * {@inheritDoc}
62      */

63     protected void doInitialise() throws InitialisationException
64     {
65         try
66         {
67             jmxSupport = jmxSupportFactory.newJmxSupport();
68             mBeanServer = (MBeanServer JavaDoc) MBeanServerFactory.findMBeanServer(null).get(0);
69             broadcasterObjectName = ObjectName.getInstance(jmxSupport.getDomainName() + ":" + BROADCASTER_JMX_OBJECT_NAME);
70             broadcastNotificationMbean = new BroadcastNotificationService();
71             mBeanServer.registerMBean(broadcastNotificationMbean, broadcasterObjectName);
72             if (registerListenerMbean)
73             {
74                 listenerObjectName = ObjectName.getInstance(jmxSupport.getDomainName() + ":" + LISTENER_JMX_OBJECT_NAME);
75                 NotificationListener mbean = new NotificationListener();
76                 broadcastNotificationMbean.addNotificationListener(mbean, null, null);
77                 mBeanServer.registerMBean(mbean, listenerObjectName);
78             }
79         } catch (Exception JavaDoc e)
80         {
81             throw new InitialisationException(new Message(Messages.FAILED_TO_START_X, "JMX Server Notification Agent"), e, this);
82         }
83     }
84
85
86     /**
87      * {@inheritDoc}
88      */

89     public void dispose()
90     {
91         try
92         {
93             if (listenerObjectName != null)
94             {
95                 mBeanServer.unregisterMBean(listenerObjectName);
96             }
97         } catch (Exception JavaDoc e)
98         {
99             logger.warn(e.getMessage(), e);
100         }
101         try
102         {
103             mBeanServer.unregisterMBean(broadcasterObjectName);
104         } catch (Exception JavaDoc e)
105         {
106             logger.warn(e.getMessage(), e);
107         }
108         super.dispose();
109     }
110
111     /**
112      * {@inheritDoc}
113      */

114     protected void logEvent(UMOServerNotification e)
115     {
116         broadcastNotificationMbean.sendNotification(new Notification JavaDoc(e.getClass().getName(), e, e.getTimestamp(), e.toString()));
117     }
118
119     /**
120      * Should be a 1 line description of the agent.
121      *
122      * @return description
123      */

124     public String JavaDoc getDescription()
125     {
126         return DEFAULT_AGENT_NAME + (registerListenerMbean ? " (Listener MBean registered)" : "");
127     }
128
129
130     /**
131      * Getter for property 'jmxSupportFactory'.
132      *
133      * @return Value for property 'jmxSupportFactory'.
134      */

135     public JmxSupportFactory getJmxSupportFactory()
136     {
137         return jmxSupportFactory;
138     }
139
140     /**
141      * Setter for property 'jmxSupportFactory'.
142      *
143      * @param jmxSupportFactory Value to set for property 'jmxSupportFactory'.
144      */

145     public void setJmxSupportFactory(JmxSupportFactory jmxSupportFactory)
146     {
147         this.jmxSupportFactory = jmxSupportFactory;
148     }
149
150     public static interface BroadcastNotificationServiceMBean extends NotificationEmitter JavaDoc
151     {
152         // no methods
153
}
154
155     public static class BroadcastNotificationService extends NotificationBroadcasterSupport JavaDoc implements BroadcastNotificationServiceMBean
156     {
157         // no methods
158
}
159
160     public static interface NotificationListenerMBean
161     {
162         /**
163          * Getter for property 'notificsationList'.
164          *
165          * @return Value for property 'notificsationList'.
166          */

167         List JavaDoc getNotificationsList();
168
169         /**
170          * Getter for property 'listSize'.
171          *
172          * @return Value for property 'listSize'.
173          */

174         int getListSize();
175
176         /**
177          * Setter for property 'listSize'.
178          *
179          * @param listSize Value to set for property 'listSize'.
180          */

181         void setListSize(int listSize);
182     }
183
184     public static class NotificationListener implements NotificationListenerMBean, javax.management.NotificationListener JavaDoc
185     {
186         private int listSize = 100;
187
188         private List JavaDoc notifs;
189
190         /**
191          * {@inheritDoc}
192          */

193         public void handleNotification(Notification JavaDoc notification, Object JavaDoc o)
194         {
195             if (getList().size() == listSize)
196             {
197                 getList().remove(listSize - 1);
198             }
199             getList().add(0, notification);
200         }
201
202         /**
203          * {@inheritDoc}
204          */

205         public List JavaDoc getNotificationsList()
206         {
207             return notifs;
208         }
209
210         /**
211          * {@inheritDoc}
212          */

213         public int getListSize()
214         {
215             return listSize;
216         }
217
218         /**
219          * {@inheritDoc}
220          */

221         public void setListSize(int listSize)
222         {
223             this.listSize = listSize;
224         }
225
226         /**
227          * Getter for property 'list'.
228          *
229          * @return Value for property 'list'.
230          */

231         protected List JavaDoc getList()
232         {
233             if (notifs == null)
234             {
235                 notifs = new ArrayList JavaDoc(listSize);
236             }
237             return notifs;
238         }
239
240     }
241
242 }
243
Popular Tags