KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > management > mbeans > ComponentService


1 /*
2  * $Id: ComponentService.java 3798 2006-11-04 04:07:14Z aperepel $
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
11 package org.mule.management.mbeans;
12
13 import javax.management.MBeanRegistration JavaDoc;
14 import javax.management.MBeanServer JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.mule.MuleManager;
20 import org.mule.impl.model.AbstractComponent;
21 import org.mule.impl.model.seda.SedaComponent;
22 import org.mule.management.stats.ComponentStatistics;
23 import org.mule.umo.UMOComponent;
24 import org.mule.umo.UMOException;
25 import org.mule.umo.UMOSession;
26
27 /**
28  * <code>ComponentService</code> exposes service information about a Mule Managed
29  * component
30  *
31  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
32  * @version $Revision: 3798 $
33  */

34 public class ComponentService implements ComponentServiceMBean, MBeanRegistration JavaDoc, ComponentStatsMBean
35 {
36
37     /**
38      * logger used by this class
39      */

40     private static Log LOGGER = LogFactory.getLog(ComponentService.class);
41
42     private MBeanServer JavaDoc server;
43
44     private String JavaDoc name;
45
46     private ObjectName JavaDoc statsName;
47
48     private ObjectName JavaDoc objectName;
49
50     private ComponentStatistics statistics;
51
52     public ComponentService(String JavaDoc name)
53     {
54         this.name = name;
55         this.statistics = getComponent().getStatistics();
56
57     }
58
59     public int getQueueSize()
60     {
61         UMOComponent c = getComponent();
62         if (c instanceof SedaComponent)
63         {
64             return ((SedaComponent)c).getQueueSize();
65         }
66         else
67         {
68             return -1;
69         }
70     }
71
72     /**
73      * Pauses event processing for theComponent. Unlike stop(), a paused component
74      * will still consume messages from the underlying transport, but those messages
75      * will be queued until the component is resumed. <p/> In order to persist these
76      * queued messages you can set the 'recoverableMode' property on the
77      * Muleconfiguration to true. this causes all internal queues to store their
78      * state.
79      *
80      * @throws org.mule.umo.UMOException if the component failed to pause.
81      * @see org.mule.config.MuleConfiguration
82      */

83     public void pause() throws UMOException
84     {
85         getComponent().pause();
86     }
87
88     /**
89      * Resumes the Component that has been paused. If the component is not paused
90      * nothing is executed.
91      *
92      * @throws org.mule.umo.UMOException if the component failed to resume
93      */

94     public void resume() throws UMOException
95     {
96         getComponent().resume();
97     }
98
99     public boolean isPaused()
100     {
101         return getComponent().isPaused();
102     }
103
104     public boolean isStopped()
105     {
106         return getComponent().isStopped();
107     }
108
109     public void stop() throws UMOException
110     {
111         getComponent().stop();
112     }
113
114     public void forceStop() throws UMOException
115     {
116         getComponent().forceStop();
117     }
118
119     public boolean isStopping()
120     {
121         return getComponent().isStopping();
122     }
123
124     public void dispose() throws UMOException
125     {
126         getComponent().dispose();
127     }
128
129     public void start() throws UMOException
130     {
131         getComponent().start();
132     }
133
134     /*
135      * (non-Javadoc)
136      *
137      * @see org.mule.management.mbeans.ComponentServiceMBean#getStatistics()
138      */

139     public ObjectName JavaDoc getStatistics()
140     {
141         return statsName;
142     }
143
144     /*
145      * (non-Javadoc)
146      *
147      * @see javax.management.MBeanRegistration#preRegister(javax.management.MBeanServer,
148      * javax.management.ObjectName)
149      */

150     public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws Exception JavaDoc
151     {
152         this.server = server;
153         this.objectName = name;
154         return name;
155     }
156
157     /*
158      * (non-Javadoc)
159      *
160      * @see javax.management.MBeanRegistration#postRegister(java.lang.Boolean)
161      */

162     public void postRegister(Boolean JavaDoc registrationDone)
163     {
164         try
165         {
166             if (getComponent().getStatistics() != null)
167             {
168                 statsName = new ObjectName JavaDoc(objectName.getDomain() + ":type=org.mule.Statistics,component="
169                                            + getName());
170                 // unregister old version if exists
171
if (this.server.isRegistered(statsName))
172                 {
173                     this.server.unregisterMBean(statsName);
174                 }
175
176                 this.server.registerMBean(new ComponentStats(getComponent().getStatistics()), this.statsName);
177             }
178         }
179         catch (Exception JavaDoc e)
180         {
181             LOGGER.error("Error post-registering the MBean", e);
182         }
183     }
184
185     /*
186      * (non-Javadoc)
187      *
188      * @see javax.management.MBeanRegistration#preDeregister()
189      */

190     public void preDeregister() throws Exception JavaDoc
191     {
192         try
193         {
194             if (this.server.isRegistered(statsName))
195             {
196                 this.server.unregisterMBean(statsName);
197             }
198         }
199         catch (Exception JavaDoc ex)
200         {
201             LOGGER.error("Error unregistering ComponentService child " + statsName.getCanonicalName(), ex);
202         }
203     }
204
205     /*
206      * (non-Javadoc)
207      *
208      * @see javax.management.MBeanRegistration#postDeregister()
209      */

210     public void postDeregister()
211     {
212         // nothing to do
213
}
214
215     private AbstractComponent getComponent()
216     {
217         UMOSession session = MuleManager.getInstance().getModel().getComponentSession(getName());
218         if (session == null)
219         {
220             return null;
221         }
222         else
223         {
224             return (AbstractComponent)session.getComponent();
225         }
226     }
227
228     // ///// Component stats impl /////////
229

230     /**
231      *
232      */

233     public void clearStatistics()
234     {
235         statistics.clear();
236     }
237
238     /**
239      * @return
240      */

241     public long getAsyncEventsReceived()
242     {
243         return statistics.getAsyncEventsReceived();
244     }
245
246     /**
247      * @return
248      */

249     public long getAsyncEventsSent()
250     {
251         return statistics.getAsyncEventsSent();
252     }
253
254     /**
255      * @return
256      */

257     public long getAverageExecutionTime()
258     {
259         return statistics.getAverageExecutionTime();
260     }
261
262     /**
263      * @return
264      */

265     public long getAverageQueueSize()
266     {
267         return statistics.getAverageQueueSize();
268     }
269
270     /**
271      * @return
272      */

273     public long getExecutedEvents()
274     {
275         return statistics.getExecutedEvents();
276     }
277
278     /**
279      * @return
280      */

281     public long getExecutionErrors()
282     {
283         return statistics.getExecutionErrors();
284     }
285
286     /**
287      * @return
288      */

289     public long getFatalErrors()
290     {
291         return statistics.getFatalErrors();
292     }
293
294     /**
295      * @return
296      */

297     public long getMaxExecutionTime()
298     {
299         return statistics.getMaxExecutionTime();
300     }
301
302     /**
303      * @return
304      */

305     public long getMaxQueueSize()
306     {
307         return statistics.getMaxQueueSize();
308     }
309
310     /**
311      * @return
312      */

313     public long getMinExecutionTime()
314     {
315         return statistics.getMinExecutionTime();
316     }
317
318     /**
319      * @return
320      */

321     public String JavaDoc getName()
322     {
323         return name;
324     }
325
326     /**
327      * @return
328      */

329     public long getQueuedEvents()
330     {
331         return statistics.getQueuedEvents();
332     }
333
334     /**
335      * @return
336      */

337     public long getReplyToEventsSent()
338     {
339         return statistics.getReplyToEventsSent();
340     }
341
342     /**
343      * @return
344      */

345     public long getSyncEventsReceived()
346     {
347         return statistics.getSyncEventsReceived();
348     }
349
350     /**
351      * @return
352      */

353     public long getSyncEventsSent()
354     {
355         return statistics.getSyncEventsSent();
356     }
357
358     /**
359      * @return
360      */

361     public long getTotalEventsReceived()
362     {
363         return statistics.getTotalEventsReceived();
364     }
365
366     /**
367      * @return
368      */

369     public long getTotalEventsSent()
370     {
371         return statistics.getTotalEventsSent();
372     }
373
374     /**
375      * @return
376      */

377     public long getTotalExecutionTime()
378     {
379         return statistics.getTotalExecutionTime();
380     }
381 }
382
Popular Tags