KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > monitor > MonitoringHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /**
25  * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
26  *
27  * Copyright 2001-2002 by iPlanet/Sun Microsystems, Inc.,
28  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
29  * All rights reserved.
30  */

31 package com.sun.enterprise.admin.monitor;
32
33 import java.util.ArrayList JavaDoc;
34 import java.util.HashMap JavaDoc;
35 import java.util.logging.Level JavaDoc;
36 import java.util.logging.Logger JavaDoc;
37
38 import javax.management.InstanceAlreadyExistsException JavaDoc;
39 import javax.management.InstanceNotFoundException JavaDoc;
40 import javax.management.MBeanRegistrationException JavaDoc;
41 import javax.management.NotCompliantMBeanException JavaDoc;
42 import javax.management.ObjectName JavaDoc;
43
44 import com.sun.enterprise.admin.common.constant.AdminConstants;
45 import com.sun.enterprise.admin.server.core.AdminService;
46
47 //i18n import
48
import com.sun.enterprise.util.i18n.StringManager;
49
50 /**
51  * Helper class to manage monitoring data provider object. This class has helper
52  * methods to register and unregister monitoring MBeans and implementors
53  * of monitorable interface (IMonitorable).
54  */

55 public class MonitoringHelper {
56
57     // i18n StringManager
58
private static StringManager localStrings =
59         StringManager.getManager( MonitoringHelper.class );
60
61     /**
62      * A reference to logger object
63      */

64     static Logger JavaDoc logger = Logger.getLogger(AdminConstants.kLoggerName);
65
66     /**
67      * Register a monitoring MBean for a ejb that is part of specified
68      * module in the specified application.
69      *
70      * @param appName name of the application
71      * @param moduleName name of the module
72      * @param ejbName name of the ejb
73      * @param type type of ejb - entity, stateless or stateful session, mdb
74      * @param mbean the monitorable mbean
75      *
76      * @returns name under which the mbean is registered in MBeanServer
77      *
78      * @throws InstanceAlreadyExistsException A MBean for specified parameter(s)
79      * is already under the control of the MBean server.
80      * @throws MBeanRegistrationException preRegister (MBeanRegistration
81      * interface) method of the MBean has thrown an exception. The MBean
82      * will not be registered.
83      */

84     public static ObjectName JavaDoc registerEJBMonitoringMBean(String JavaDoc appName,
85             String JavaDoc moduleName, String JavaDoc ejbName, MonitoredObjectType type,
86             BaseMonitorMBean mbean)
87             throws InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc {
88         BaseMonitorMBean app = getLevelOneMBeanForSure(
89                 MonitoredObjectType.APPLICATION, appName);
90         BaseMonitorMBean module = getMBeanForSure(app,
91                 MonitoredObjectType.EJBMODULE, moduleName);
92         module.addChild(ejbName, type, mbean);
93         return mbean.getObjectName();
94     }
95
96     /**
97      * Get name of the monitoring MBean (in MBeanServer) for a ejb that is part
98      * of specified module in the specified application.
99      *
100      * @param appName name of the application
101      * @param moduleName name of the module
102      * @param ejbName name of the ejb
103      *
104      * @throws InstanceNotFoundException There is no MBean registered for
105      * specified bean, module and application in the MBean server.
106      */

107     public static ObjectName JavaDoc getEJBMonitoringMBeanName(String JavaDoc appName,
108             String JavaDoc moduleName, String JavaDoc ejbName)
109             throws InstanceNotFoundException JavaDoc {
110         return getEJBMonitoringMBean(appName, moduleName, ejbName).getObjectName();
111     }
112
113     /**
114      * Unregister a monitoring MBean for a ejb that is part of specified
115      * module in the specified application.
116      *
117      * @param appName name of the application
118      * @param moduleName name of the module
119      * @param ejbName name of the ejb
120      *
121      * @throws InstanceNotFoundException There is no MBean registered for
122      * specified bean, module and application in the MBean server.
123      * @throws MBeanRegistrationException - The preDeregister (MBeanRegistration
124      * interface) method of the MBean has thrown an exception.
125      */

126     public static void unregisterEJBMonitoringMBean(String JavaDoc appName,
127             String JavaDoc moduleName, String JavaDoc ejbName)
128             throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc {
129         BaseMonitorMBean module = getEJBModuleMBean(appName, moduleName);
130         BaseMonitorMBean ejb = module.getFirstChildByName(ejbName);
131         module.removeChild(ejb);
132         checkAndPurgeUnusedAppAndModule(appName, module);
133     }
134
135     /**
136      * Register a monitoring MBean for a ejb that is part of specified
137      * stand alone ejb module.
138      *
139      * @param standaloneModuleName name of the stand alone ejb module.
140      * @param ejbName name of the ejb
141      * @param type type of ejb - entity, stateless or stateful session, mdb
142      * @param mbean the monitorable mbean
143      *
144      * @returns name under which the mbean is registered in MBeanServer
145      *
146      * @throws InstanceAlreadyExistsException A MBean for specified parameter(s)
147      * is already under the control of the MBean server.
148      * @throws MBeanRegistrationException preRegister (MBeanRegistration
149      * interface) method of the MBean has thrown an exception. The MBean
150      * will not be registered.
151      */

152     public static ObjectName JavaDoc registerEJBMonitoringMBean(String JavaDoc standaloneModuleName,
153             String JavaDoc ejbName, MonitoredObjectType type, BaseMonitorMBean mbean)
154             throws InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc {
155         BaseMonitorMBean standaloneModule = getLevelOneMBeanForSure(
156                 MonitoredObjectType.STANDALONE_EJBMODULE, standaloneModuleName);
157         standaloneModule.addChild(ejbName, type, mbean);
158         return mbean.getObjectName();
159     }
160
161     /**
162      * Get name of the monitoring MBean (in MBeanServer) for a ejb that is part
163      * of specified stand alone ejb module.
164      *
165      * @param standaloneModuleName name of the stand alone ejb module.
166      * @param ejbName name of the ejb
167      *
168      * @throws InstanceNotFoundException There is no MBean registered for
169      * specified bean, and stand alone ejb module in the MBean server.
170      */

171     public static ObjectName JavaDoc getEJBMonitoringMBeanName(String JavaDoc standaloneModuleName,
172             String JavaDoc ejbName)
173             throws InstanceNotFoundException JavaDoc {
174         return getEJBMonitoringMBean(standaloneModuleName, ejbName).getObjectName();
175     }
176
177     /**
178      * Unregister a monitoring MBean for a ejb that is part of specified
179      * stand alone ejb module.
180      *
181      * @param standaloneModuleName name of the stand alone ejb module.
182      * @param ejbName name of the ejb
183      *
184      * @throws InstanceNotFoundException There is no MBean registered for
185      * specified bean, and stand alone ejb module in the MBean server.
186      * @throws MBeanRegistrationException - The preDeregister (MBeanRegistration
187      * interface) method of the MBean has thrown an exception.
188      */

189     public static void unregisterEJBMonitoringMBean(String JavaDoc standaloneModuleName,
190             String JavaDoc ejbName)
191             throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc {
192         BaseMonitorMBean module = getEJBModuleMBean(standaloneModuleName);
193         BaseMonitorMBean ejb = module.getFirstChildByName(ejbName);
194         module.removeChild(ejb);
195         checkAndPurgeUnusedLevelOneMBean(module);
196     }
197
198     /**
199      * @throws InstanceNotFoundException There is no MBean registered for
200      * specified bean, and stand alone ejb module in the MBean server.
201      * @throws InstanceAlreadyExistsException A MBean for specified parameter(s)
202      * is already under the control of the MBean server.
203      * @throws MBeanRegistrationException preRegister (MBeanRegistration
204      * interface) method of the MBean has thrown an exception. The MBean
205      * will not be registered.
206      */

207     public static void registerEJBMethodMonitoringMBean(ObjectName JavaDoc ejbMBeanName,
208             String JavaDoc methodName, BaseMonitorMBean mbean)
209             throws InstanceNotFoundException JavaDoc, InstanceAlreadyExistsException JavaDoc,
210             MBeanRegistrationException JavaDoc {
211         BaseMonitorMBean ejb = getMonitorMBean(ejbMBeanName);
212         ejb.addChild(methodName, MonitoredObjectType.BEAN_METHOD, mbean);
213     }
214
215     /**
216      * Register a monitoring MBean under specified name. name is dotted
217      * representation that denotes the position of MBean within the current
218      * context (this instance). [TBD: Dotted notation]
219      *
220      * @param name name of the MBean in dotted notation
221      * @param mbean monitoring mbean object
222      *
223      * @return name under which the mbean is registered in MBeanServer
224      *
225      * @throws InstanceAlreadyExistsException A MBean for specified parameter(s)
226      * is already under the control of the MBean server.
227      * @throws MBeanRegistrationException preRegister (MBeanRegistration
228      * interface) method of the MBean has thrown an exception. The MBean
229      * will not be registered.
230      * @deprecated
231      */

232     public static ObjectName JavaDoc registerMonitoringMBean(String JavaDoc name,
233             BaseMonitorMBean mbean)
234             throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc {
235         return null;
236     }
237
238     /**
239      * Unregister a monitoring MBean under specified name. name is dotted
240      * representation that denotes the position of MBean within the current
241      * context (this instance). [TBD: Dotted notation]
242      *
243      * @param name name of the MBean in dotted notation
244      *
245      * @throws InstanceNotFoundException The MBean specified is not registered
246      * in the MBean server.
247      * @throws MBeanRegistrationException - The preDeregister (MBeanRegistration
248      * interface) method of the MBean has thrown an exception.
249      * @deprecated
250      */

251     public static void unregisterMonitoringMBean(String JavaDoc name)
252             throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc {
253     }
254
255     /**
256      * Name of system orb. This string can not be used for naming any user orbs.
257      */

258     public static final String JavaDoc SYSTEM_ORB_NAME = "system";
259
260     /**
261      * Register a mbean to monitor system ORB. System ORB is created at server
262      * startup.
263      * @param mbean the mbean to be registered.
264      * @throws IllegalStateException if the method is called when admin service
265      * has not been initialized (for example, a call by application client
266      * container, which does not ever initialize admin service)
267      * @throws InstanceAlreadyExistsException A MBean for system orb
268      * is already under the control of the MBean server.
269      * @throws MBeanRegistrationException preRegister (MBeanRegistration
270      * interface) method of the MBean has thrown an exception. The MBean
271      * will not be registered.
272      */

273     public static void registerSystemORBMonitoringMBean(BaseMonitorMBean mbean)
274             throws InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc {
275         registerORBMonitoringMBean(SYSTEM_ORB_NAME, mbean);
276     }
277
278     /**
279      * Register a mbean to monitor user created ORB.
280      * @param hint a hint for the name of mbean. The hint will be used as name
281      * if it is valid and has not been used earlier. A hint is valid if it
282      * is not null and does not contain any of following characters -
283      * comma, space, equals sign or colon. If hint is invalid a name derived
284      * from the constant DEFAULT_USER_ORB_HINT will be used for the mbean.
285      * @throws IllegalArgumentException if the specified hint is reserved for
286      * use by system. The reserved name is value of constant SYSTEM_ORB_NAME
287      * @throws IllegalStateException if the method is called when admin service
288      * has not been initialized (for example, a call by application client
289      * container, which does not ever initialize admin service)
290      * @throws InstanceAlreadyExistsException A MBean for system orb
291      * is already under the control of the MBean server.
292      * @throws MBeanRegistrationException preRegister (MBeanRegistration
293      * interface) method of the MBean has thrown an exception. The MBean
294      * will not be registered.
295      */

296     public static void registerUserORBMonitoringMBean(String JavaDoc hint,
297             BaseMonitorMBean mbean)
298             throws InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc {
299         if (SYSTEM_ORB_NAME.equalsIgnoreCase(hint)) {
300             String JavaDoc msg = localStrings.getString(
301                     "admin.monitor.system_orb_name_used", hint);
302             throw new IllegalArgumentException JavaDoc(msg);
303         }
304         registerORBMonitoringMBean(getUserOrbMBeanName(hint), mbean);
305     }
306
307     /**
308      * Register a ORB monitoring MBean. ORB monitoring MBeans are registered
309      * under the node iiop-service (which is directly under root node) in the
310      * tree of monitoring mbeans.
311      * @param name name of the mbean to be registered
312      * @param mbean the mbean to be registered
313      * @throws IllegalStateException if the method is called when admin service
314      * has not been initialized (for example, a call by application client
315      * container, which does not ever initialize admin service)
316      * @throws InstanceAlreadyExistsException A MBean for user orb
317      * is already under the control of the MBean server.
318      * @throws MBeanRegistrationException preRegister (MBeanRegistration
319      * interface) method of the MBean has thrown an exception. The MBean
320      * will not be registered.
321      */

322     private static void registerORBMonitoringMBean(String JavaDoc name,
323             BaseMonitorMBean mbean)
324             throws InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc {
325         if (AdminService.getAdminService() == null) {
326             String JavaDoc msg = localStrings.getString(
327                     "admin.monitor.admin_service_not_inited");
328             throw new IllegalStateException JavaDoc(msg);
329         }
330         BaseMonitorMBean iiop = getLevelOneMBeanForSure(
331                 MonitoredObjectType.IIOP_SERVICE,
332                 MonitoredObjectType.IIOP_SERVICE.getTypeName());
333         iiop.addChild(name, MonitoredObjectType.ORB, mbean);
334         logger.log(Level.FINEST, ORB_MBEAN_REGISTERED, name);
335     }
336
337     /**
338      * A map to keep track of user orb names already in use.
339      */

340     private static final HashMap JavaDoc userOrbNames = new HashMap JavaDoc();
341
342     /**
343      * An int that is used to make user orb names unique.
344      */

345     private static int orbNameUniquifier = 0;
346
347     /**
348      * Default prefix for user orbs.
349      */

350     public static final String JavaDoc DEFAULT_USER_ORB_HINT = "user";
351
352     /**
353      * Get a unique name for user orb. It tries to uniquify the name by adding
354      * integers to the end of the hint.
355      * @param hint hint for orb name, if the name is not in use, it is used as
356      * is, otherwise integers are appended to it till it is unique. If hint
357      * is null, empty or any form (lowercase or uppercase) of constant
358      * DEFAULT_USER_ORB_HINT, the method uses constant DEFAULT_USER_ORB_HINT
359      * as hint.
360      * @return a name that is derived from the specified hint and is not in use
361      */

362     private static String JavaDoc getUserOrbMBeanName(String JavaDoc hint) {
363         if (hint == null || hint.trim().equals("")
364                  || hint.equalsIgnoreCase(DEFAULT_USER_ORB_HINT)
365                  || (hint.indexOf(COMMA) != -1)
366                  || (hint.indexOf(SPACE) != -1)
367                  || (hint.indexOf(COLON) != -1)
368                  || (hint.indexOf(EQUALS) != -1)) {
369             logger.log(Level.FINEST, INVALID_USER_ORB_NAME_HINT, hint);
370             hint = DEFAULT_USER_ORB_HINT + (++orbNameUniquifier);
371         }
372         synchronized (userOrbNames) {
373             while (userOrbNames.containsKey(hint)) {
374                 logger.log(Level.FINEST, USER_ORB_MBEAN_NAME_USED, hint);
375                 hint = hint + (++orbNameUniquifier);
376             }
377             userOrbNames.put(hint, hint);
378         }
379         return hint;
380     }
381
382     /**
383      * Register a monitorable object under specified name. A MBean is created
384      * from the monitorable and then registered to MBean server. name is dotted
385      * representation that denotes the position of MBean within the current
386      * context (this instance). [TBD: Dotted notation]
387      *
388      * @param name name of the MBean in dotted notation
389      * @param monitorable monitorable object
390      *
391      * @return name under which the mbean is registered in MBeanServer
392      *
393      * @throws InstanceAlreadyExistsException A MBean for specified parameter(s)
394      * is already under the control of the MBean server.
395      * @throws MBeanRegistrationException preRegister (MBeanRegistration
396      * interface) method of the MBean has thrown an exception. The MBean
397      * will not be registered.
398      * @deprecated
399      */

400     public static ObjectName JavaDoc registerMonitorable(String JavaDoc name,
401             IMonitorable monitorable)
402             throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc {
403         return null;
404     }
405
406     /**
407      * Unregister a monitorable under specified name. A MBean is created
408      * from the monitorable and then registered to MBean server. name is dotted
409      * representation that denotes the position of MBean within the current
410      * context (this instance). [TBD: Dotted notation]
411      *
412      * @param name name of the MBean in dotted notation
413      *
414      * @throws InstanceNotFoundException The MBean specified is not registered
415      * in the MBean server.
416      * @throws MBeanRegistrationException - The preDeregister (MBeanRegistration
417      * interface) method of the MBean has thrown an exception.
418      * @deprecated
419      */

420     public static void unregisterMonitorable(String JavaDoc name)
421             throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc {
422     }
423
424     /**
425      * Get monitoring mBean registered in MBean Server with specified name.
426      * @param name registered name of the MBean in MBeanServer
427      * @throws InstanceNotFoundException if the specified name is not registered
428      * in the MBean server.
429      */

430     public static BaseMonitorMBean getMonitorMBean(ObjectName JavaDoc name)
431             throws InstanceNotFoundException JavaDoc {
432         BaseMonitorMBean mbean = getMonitorMBeanOrNull(name);
433         if (mbean == null) {
434             String JavaDoc msg = localStrings.getString( "admin.monitor.mbean_with_name_not_found", name );
435             throw new InstanceNotFoundException JavaDoc( msg );
436         }
437         return mbean;
438     }
439
440     /**
441      * Get monitoring mBean registered in MBean Server with specified name. If
442      * the mbean with specified name is not found this method returns null.
443      * @param name registered name of the MBean in MBeanServer
444      * @return mbean of specified name or null if it is not found
445      */

446     private static BaseMonitorMBean getMonitorMBeanOrNull(ObjectName JavaDoc name) {
447         BaseMonitorMBean mbean =
448                 (BaseMonitorMBean)BaseMonitorMBean.objectNameMap.get(name);
449         return mbean;
450     }
451
452     /**
453      * Get monitoring MBean (in MBeanServer) for a ejb that is part
454      * of specified module in the specified application.
455      *
456      * @param appName name of the application
457      * @param moduleName name of the module
458      * @param ejbName name of the ejb
459      *
460      * @throws InstanceNotFoundException There is no MBean registered for
461      * specified bean, module and application in the MBean server.
462      */

463     private static BaseMonitorMBean getEJBMonitoringMBean(String JavaDoc appName,
464             String JavaDoc moduleName, String JavaDoc ejbName)
465             throws InstanceNotFoundException JavaDoc {
466         BaseMonitorMBean mbean = null;
467         BaseMonitorMBean app = getLevelOneMBean(MonitoredObjectType.APPLICATION,
468                 appName);
469         if (app != null) {
470             BaseMonitorMBean module = app.getChildOrNull(
471                     MonitoredObjectType.EJBMODULE, moduleName);
472             if (module != null) {
473                 ArrayList JavaDoc list = module.getChildList(ejbName);
474                 if (!list.isEmpty()) {
475                     mbean = (BaseMonitorMBean)list.get(0);
476                 }
477             }
478         }
479         if (mbean == null) {
480             String JavaDoc msg = localStrings.getString( "admin.monitor.no_mbean_for_appname_modulename_ejbname", appName, moduleName, ejbName );
481             throw new InstanceNotFoundException JavaDoc( msg );
482         }
483         return mbean;
484     }
485
486     /**
487      * Get MBean for specified application and ejb module. If the MBean for
488      * application or module does not exist, it returns null.
489      * @param appName name of the application
490      * @param moduleName name of the ejb module
491      * @return MBean for the ejb module if it exists, null otherwise
492      */

493     private static BaseMonitorMBean getEJBModuleMBean(String JavaDoc appName,
494             String JavaDoc moduleName) {
495         BaseMonitorMBean module = null;
496         BaseMonitorMBean app = getLevelOneMBean(MonitoredObjectType.APPLICATION,
497                 appName);
498         if (app != null) {
499             module = app.getChildOrNull(
500                     MonitoredObjectType.EJBMODULE, moduleName);
501         }
502         return module;
503     }
504
505     /**
506      * Get monitoring MBean (in MBeanServer) for a ejb that is part
507      * of specified stand alone ejb module.
508      *
509      * @param standaloneModuleName name of the stand alone ejb module
510      * @param ejbName name of the ejb
511      *
512      * @throws InstanceNotFoundException There is no MBean registered for
513      * specified bean, and stand alone ejb module in the MBean server.
514      */

515     private static BaseMonitorMBean getEJBMonitoringMBean(
516             String JavaDoc standaloneModuleName, String JavaDoc ejbName)
517             throws InstanceNotFoundException JavaDoc {
518         BaseMonitorMBean mbean = null;
519         BaseMonitorMBean module = getLevelOneMBean(
520                 MonitoredObjectType.STANDALONE_EJBMODULE, standaloneModuleName);
521         if (module != null) {
522             ArrayList JavaDoc list = module.getChildList(ejbName);
523             if (!list.isEmpty()) {
524                 mbean = (BaseMonitorMBean)list.get(0);
525             }
526         }
527         if (mbean == null) {
528             String JavaDoc msg = localStrings.getString( "admin.monitor.no_mbean_for_standalonemodulename_ejbname", standaloneModuleName, ejbName );
529             throw new InstanceNotFoundException JavaDoc( msg );
530         }
531         return mbean;
532     }
533
534     /**
535      * Get MBean for specified stand alone ejb module. If the MBean for
536      * stand alone ejb module does not exist, it returns null.
537      * @param standaloneModuleName name of the stand alone ejb module
538      * @return MBean for the stand alone ejb module if it exists, null otherwise
539      */

540     private static BaseMonitorMBean getEJBModuleMBean(
541             String JavaDoc standaloneModuleName) {
542         return getLevelOneMBean(
543                 MonitoredObjectType.STANDALONE_EJBMODULE, standaloneModuleName);
544     }
545
546     /**
547      * Get a monitoring mbean from top level (immediate child of root monitor
548      * mbean). If the mbean does not exist, it returns null.
549      * @param type type of the mbean
550      * @param name name of the mbean
551      * @return mbean representing specified type and name, if it exists, null
552      * otherwise.
553      */

554     private static BaseMonitorMBean getLevelOneMBean(
555             MonitoredObjectType type, String JavaDoc name) {
556         GenericMonitorMBean root = GenericMonitorMBean.getRoot();
557         return root.getChildOrNull(type, name);
558     }
559
560     /**
561      * Get a monitoring mbean from top level (immediate child of root monitor
562      * mbean). If the mbean does not exist, a generic mbean is created and
563      * added.
564      * @param type type of the mbean
565      * @param name name of the mbean
566      * @return mbean representing specified type and name
567      */

568     private static BaseMonitorMBean getLevelOneMBeanForSure(
569             MonitoredObjectType type, String JavaDoc name) {
570         GenericMonitorMBean root = GenericMonitorMBean.getRoot();
571         return getMBeanForSure(root, type, name);
572     }
573
574     /**
575      * Get a MBean of specified type and name contained within specified
576      * parent mbean. If a MBean representing the child does not exist, an
577      * instance of generic mbean is created and added to the parent.
578      * @param parent the parent MBean
579      * @param type type of the child MBean
580      * @param name name of the child MBean
581      * @return an MBean representing specified child.
582      */

583     private static BaseMonitorMBean getMBeanForSure(BaseMonitorMBean parent,
584             MonitoredObjectType type, String JavaDoc name) {
585         BaseMonitorMBean mbean = parent.getChildOrNull(type, name);
586         if (mbean == null) {
587             mbean = new GenericMonitorMBean();
588             try {
589                 parent.addChild(name, type, mbean);
590             } catch (InstanceAlreadyExistsException JavaDoc iae) {
591                 // Narrow window between getChildOrNull() and addChild() call
592
// allows this possibility. So get the child again
593
mbean = parent.getChildOrNull(type, name);
594                 if (mbean == null) {
595                     // If this is null, don't deal with it (removed?). If this
596
// exception shows up, synchronize calls to getChildOrNull()
597
// and addChild() on parent
598
String JavaDoc msg = localStrings.getString( "admin.monitor.unable_getting_mbean", type, name, parent.getObjectName() );
599                     throw new RuntimeException JavaDoc( msg );
600                 }
601             } catch (MBeanRegistrationException JavaDoc mbr) {
602                 // This is thrown by errors during execution of method in
603
// MBeanRegistration interface. GenericMonitorMBean does not
604
// implement this interace, so if it happens throw a
605
// RuntimeException
606
String JavaDoc msg = localStrings.getString( "admin.monitor.rootcause_unable_to_register_mbean", type, name, mbr.getMessage() );
607                 throw new RuntimeException JavaDoc( msg );
608             }
609         }
610         return mbean;
611     }
612
613     /**
614      * Register a monitoring MBean for transaction service.
615      *
616      * @param mbean the monitorable mbean
617      *
618      * @returns name under which the mbean is registered in MBeanServer
619      *
620      * @throws InstanceAlreadyExistsException A MBean for specified parameter(s)
621      * is already under the control of the MBean server.
622      * @throws MBeanRegistrationException preRegister (MBeanRegistration
623      * interface) method of the MBean has thrown an exception. The MBean
624      * will not be registered.
625      */

626     public static ObjectName JavaDoc registerTxnMonitoringMBean(BaseMonitorMBean mbean)
627             throws InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc {
628         GenericMonitorMBean.getRoot().addChild(MonitoredObjectType.TXNMGR.getTypeName(), MonitoredObjectType.TXNMGR, mbean);
629         return mbean.getObjectName();
630     }
631     
632     public static ObjectName JavaDoc registerJdbcPoolMonitoringMBean(String JavaDoc poolName, MonitoredObjectType type, BaseMonitorMBean mbean)
633             throws InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc
634     {
635         if(AdminService.getAdminService() != null){
636         BaseMonitorMBean resMBean = getLevelOneMBeanForSure(MonitoredObjectType.RESOURCES,"resources");
637        resMBean.addChild(poolName, type, mbean);
638        return mbean.getObjectName();
639         }
640         return null;
641
642     }
643
644     public void unregisterJdbcPoolMonitoringMBean(MonitoredObjectType type,String JavaDoc poolName)
645         throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc
646     {
647
648       BaseMonitorMBean resMBean = getLevelOneMBean(MonitoredObjectType.RESOURCES,"resources");
649        if(resMBean != null)
650            resMBean.removeChild(type,poolName);
651     }
652
653     /**
654      * Purge module mbean if it does not have any child mbean. If module mbean
655      * is purged then also purge application mbean if there are no more modules.
656      * @param appName name of the application
657      * @param moduleMBean module mbean
658      */

659     private static void checkAndPurgeUnusedAppAndModule(String JavaDoc appName,
660             BaseMonitorMBean moduleMBean) {
661         if (moduleMBean == null) {
662             return;
663         }
664         BaseMonitorMBean appMBean = getLevelOneMBean(
665                 MonitoredObjectType.APPLICATION, appName);
666         boolean purged = checkAndPurgeMBean(appMBean, moduleMBean);
667         if (purged) {
668             checkAndPurgeUnusedLevelOneMBean(appMBean);
669         }
670         return;
671     }
672
673     /**
674      * Purge unused mbean at the top level (below root) in the monitoring mbean
675      * tree. Any mbean that does not have children is considered unused.
676      * @param levelOneMBean the mbean that needs to checked and purged
677      */

678     private static void checkAndPurgeUnusedLevelOneMBean(
679             BaseMonitorMBean levelOneMBean) {
680         if (levelOneMBean == null) {
681             return;
682         }
683         GenericMonitorMBean root = GenericMonitorMBean.getRoot();
684         boolean purged = checkAndPurgeMBean(root, levelOneMBean);
685         return;
686     }
687
688     /**
689      * Purge an mbean if the mbean does not have any child mbean. This method
690      * tries to do
691      * @param parent the parent mbean of the mbean that needs to purged.
692      * @param mbean the mbean that needs to be purged
693      * @returns true if the mbean was purged, false otherwise
694      */

695     private static boolean checkAndPurgeMBean(BaseMonitorMBean parent,
696             BaseMonitorMBean mbean) {
697         boolean purged = false;
698         if (mbean != null && parent != null) {
699             ArrayList JavaDoc childList = mbean.getChildList();
700             if (childList == null || childList.size() == 0) {
701                 try {
702                     parent.removeChild(mbean);
703                     purged = true;
704                 } catch (Throwable JavaDoc t) {
705                     // Failure in deletion of mbean is not fatal, it will have
706
// undesirable effect of not removing the module mbean even
707
// when the application is undeployed, but won't cause any
708
// harm. So just log the error.
709
logger.log(Level.FINE, PURGE_MBEAN_FAILED,
710                             mbean.getNodeType() + "/" + mbean.getNodeName());
711                     logger.log(Level.FINEST, PURGE_MBEAN_FAILED_TRACE, t);
712                 }
713             }
714         }
715         return purged;
716     }
717
718     /*
719      * Invalid characters for MBean name follow.
720      */

721     private static final char COMMA = ',';
722     private static final char SPACE = ' ';
723     private static final char COLON = ':';
724     private static final char EQUALS = '=';
725
726     /*
727      * Constants for logging keys.
728      */

729     private static final String JavaDoc PURGE_MBEAN_FAILED =
730             "monitor.purge_mbean_failed";
731     private static final String JavaDoc PURGE_MBEAN_FAILED_TRACE =
732             "monitor.purge_mbean_failed_trace";
733     private static final String JavaDoc USER_ORB_MBEAN_NAME_USED =
734             "monitor.user_orb_mbean_name_used";
735     private static final String JavaDoc ORB_MBEAN_REGISTERED =
736             "monitor.orb_mbean_registered";
737     private static final String JavaDoc INVALID_USER_ORB_NAME_HINT =
738             "monitor.invalid_user_orb_name_hint";
739 }
740
Popular Tags