KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > jmx > CommonsModelerHelper


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: CommonsModelerHelper.java 638 2006-06-13 08:04:28Z sauthieg $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.jmx;
27
28 import java.net.URL JavaDoc;
29
30 import javax.management.ObjectName JavaDoc;
31
32 import org.apache.commons.modeler.Registry;
33 import org.objectweb.easybeans.log.JLog;
34 import org.objectweb.easybeans.log.JLogFactory;
35
36 /**
37  * @author Florent Benoit
38  */

39 public final class CommonsModelerHelper {
40
41     /**
42      * Registry of commons modeler.
43      */

44     private static Registry registry = null;
45
46     /**
47      * Logger.
48      */

49     private static JLog logger = JLogFactory.getLog(CommonsModelerHelper.class);
50
51     /**
52      * Utility class, no public constructor.
53      */

54     private CommonsModelerHelper() {
55
56     }
57
58     /**
59      * Load the registry of managed object descriptions.
60      * @throws CommonsModelerException if the MBeans cannot be registered.
61      */

62     public static synchronized void initRegistry() throws CommonsModelerException {
63
64         if (registry == null) {
65             // Load registry
66
registry = Registry.getRegistry(null, null);
67
68             ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
69
70             // Load descriptors
71
try {
72                 // EasyBeans specific MBeans :
73
registry.loadDescriptors("org.objectweb.easybeans.deployer.management", classLoader);
74
75                 // JSR 77 MBeans description
76
registry.loadDescriptors("org.objectweb.easybeans.jsr77", classLoader);
77
78                 // EasyBeans additionnal attributes/operations
79
extendsManagedBeansDescription("org.objectweb.easybeans.container.management", classLoader);
80                 extendsManagedBeansDescription("org.objectweb.easybeans.container.session.stateful.management", classLoader);
81                 extendsManagedBeansDescription("org.objectweb.easybeans.container.session.stateless.management", classLoader);
82                 extendsManagedBeansDescription("org.objectweb.easybeans.server.management", classLoader);
83             } catch (Exception JavaDoc e) {
84                 throw new CommonsModelerException("Cannot load descriptors of commons modeler", e);
85             }
86
87             if (logger.isDebugEnabled()) {
88                 String JavaDoc[] managedBeans = registry.findManagedBeans();
89                 logger.debug("List of all MBeans descriptors");
90                 for (String JavaDoc managedBean : managedBeans) {
91                     logger.debug("Found managedBean {0}.", managedBean);
92                 }
93                 logger.debug("End of list of all MBeans descriptors");
94             }
95         }
96     }
97
98     /**
99      * Load <code>mbeans-descriptors-ext.xml</code> extension files.
100      *
101      * @param packageLoc
102      * package name
103      * @param classLoader
104      * loader where resources can be found
105      * @throws Exception
106      * if the Resource is unavailable or if the update fails.
107      */

108     private static void extendsManagedBeansDescription(final String JavaDoc packageLoc,
109             final ClassLoader JavaDoc classLoader) throws Exception JavaDoc {
110         String JavaDoc resource = packageLoc.replace('.', '/');
111         URL JavaDoc url = classLoader.getResource(resource + "/mbeans-descriptors-ext.xml");
112         CommonsModelerExtension.updateDescriptors(registry, url.openStream());
113     }
114
115     /**
116      * Gets the registry.
117      *
118      * @return registry object.
119      * @throws CommonsModelerException if registry is not initialized.
120      */

121     public static Registry getRegistry() throws CommonsModelerException {
122         initRegistry();
123         return registry;
124     }
125
126     /**
127      * Registers an MBean.
128      * @param bean the instance to be managed.
129      * @param objectName the ON to use.
130      * @throws CommonsModelerException if the MBean is not registered.
131      */

132     public static void registerModelerMBean(final Object JavaDoc bean,
133                                      final String JavaDoc objectName) throws CommonsModelerException {
134         initRegistry();
135         try {
136             registry.registerComponent(bean, objectName, null);
137         } catch (Exception JavaDoc e) {
138             throw new CommonsModelerException("Cannot register MBean with name '" + objectName + "'.", e);
139         }
140     }
141
142     /**
143      * Unregister the given ObjectName.
144      * @param on the ObjectName.
145      */

146     public static void unregisterModelerMBean(final ObjectName JavaDoc on) {
147         if (registry != null) {
148             registry.unregisterComponent(on);
149         }
150     }
151 }
152
Popular Tags