001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: CommonsModelerHelper.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.jmx;
025:
026: import java.net.URL;
027:
028: import javax.management.ObjectName;
029:
030: import org.apache.commons.modeler.Registry;
031: import org.ow2.util.log.Log;
032: import org.ow2.util.log.LogFactory;
033:
034: /**
035: * @author Florent Benoit
036: */
037: public final class CommonsModelerHelper {
038:
039: /**
040: * Registry of commons modeler.
041: */
042: private static Registry registry = null;
043:
044: /**
045: * Logger.
046: */
047: private static Log logger = LogFactory
048: .getLog(CommonsModelerHelper.class);
049:
050: /**
051: * Utility class, no public constructor.
052: */
053: private CommonsModelerHelper() {
054:
055: }
056:
057: /**
058: * Load the registry of managed object descriptions.
059: * @throws CommonsModelerException if the MBeans cannot be registered.
060: */
061: public static synchronized void initRegistry()
062: throws CommonsModelerException {
063:
064: if (registry == null) {
065: // Load registry
066: registry = Registry.getRegistry(null, null);
067:
068: ClassLoader classLoader = Thread.currentThread()
069: .getContextClassLoader();
070:
071: // Load descriptors
072: try {
073: // EasyBeans specific MBeans :
074: registry.loadDescriptors(
075: "org.ow2.easybeans.deployer.management",
076: classLoader);
077:
078: // JSR 77 MBeans description
079: registry.loadDescriptors("org.ow2.easybeans.jsr77",
080: classLoader);
081:
082: // EasyBeans additionnal attributes/operations
083: extendsManagedBeansDescription(
084: "org.ow2.easybeans.container.management",
085: classLoader);
086: extendsManagedBeansDescription(
087: "org.ow2.easybeans.container.session.stateful.management",
088: classLoader);
089: extendsManagedBeansDescription(
090: "org.ow2.easybeans.container.session.stateless.management",
091: classLoader);
092: extendsManagedBeansDescription(
093: "org.ow2.easybeans.server.management",
094: classLoader);
095: } catch (Exception e) {
096: throw new CommonsModelerException(
097: "Cannot load descriptors of commons modeler", e);
098: }
099:
100: if (logger.isDebugEnabled()) {
101: String[] managedBeans = registry.findManagedBeans();
102: logger.debug("List of all MBeans descriptors");
103: for (String managedBean : managedBeans) {
104: logger.debug("Found managedBean {0}.", managedBean);
105: }
106: logger.debug("End of list of all MBeans descriptors");
107: }
108: }
109: }
110:
111: /**
112: * Load <code>mbeans-descriptors-ext.xml</code> extension files.
113: *
114: * @param packageLoc
115: * package name
116: * @param classLoader
117: * loader where resources can be found
118: * @throws Exception
119: * if the Resource is unavailable or if the update fails.
120: */
121: private static void extendsManagedBeansDescription(
122: final String packageLoc, final ClassLoader classLoader)
123: throws Exception {
124: String resource = packageLoc.replace('.', '/');
125: URL url = classLoader.getResource(resource
126: + "/mbeans-descriptors-ext.xml");
127: CommonsModelerExtension.updateDescriptors(registry, url
128: .openStream());
129: }
130:
131: /**
132: * Gets the registry.
133: *
134: * @return registry object.
135: * @throws CommonsModelerException if registry is not initialized.
136: */
137: public static Registry getRegistry() throws CommonsModelerException {
138: initRegistry();
139: return registry;
140: }
141:
142: /**
143: * Registers an MBean.
144: * @param bean the instance to be managed.
145: * @param objectName the ON to use.
146: * @throws CommonsModelerException if the MBean is not registered.
147: */
148: public static void registerModelerMBean(final Object bean,
149: final String objectName) throws CommonsModelerException {
150: initRegistry();
151: try {
152: registry.registerComponent(bean, objectName, null);
153: } catch (Exception e) {
154: throw new CommonsModelerException(
155: "Cannot register MBean with name '" + objectName
156: + "'.", e);
157: }
158: }
159:
160: /**
161: * Unregister the given ObjectName.
162: * @param on the ObjectName.
163: */
164: public static void unregisterModelerMBean(final ObjectName on) {
165: if (registry != null) {
166: registry.unregisterComponent(on);
167: }
168: }
169: }
|