001: /**
002: * EasyBeans
003: * Copyright (C) 2006-2007 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: MBeansHelper.java 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.jmx;
025:
026: import java.util.Hashtable;
027: import java.util.Map;
028:
029: import javax.management.MalformedObjectNameException;
030: import javax.management.ObjectName;
031:
032: import org.ow2.easybeans.api.jmx.EZBManagementIdentifier;
033: import org.ow2.easybeans.util.loader.ClassUtils;
034: import org.ow2.util.log.Log;
035: import org.ow2.util.log.LogFactory;
036:
037: /**
038: * Singleton object.Creates the MBeans and register them.
039: * @author florent
040: *
041: */
042: public final class MBeansHelper {
043: /**
044: * Logger.
045: */
046: private static Log logger = LogFactory.getLog(MBeansHelper.class);
047:
048: /**
049: * Singleton instance.
050: */
051: private static MBeansHelper instance = null;
052:
053: /**
054: * The Identifier in charge of creating the right ObjectName for a given
055: * instance.
056: */
057: private Map<Class, EZBManagementIdentifier> identifiers = null;
058:
059: /**
060: * Is Management activated ?
061: */
062: private boolean activate;
063:
064: /**
065: * Domain name to enforce if set.
066: */
067: private static String domainName = null;
068:
069: /**
070: * Server name to enforce if set.
071: */
072: private static String serverName = null;
073:
074: /**
075: * Singleton class, no public constructor.
076: */
077: private MBeansHelper() {
078: identifiers = new Hashtable<Class, EZBManagementIdentifier>();
079: }
080:
081: /**
082: * @return Returns the Singleton MBeansHelper instance.
083: */
084: public static MBeansHelper getInstance() {
085: if (instance == null) {
086: instance = new MBeansHelper();
087: }
088: return instance;
089: }
090:
091: /**
092: * Activate the MBeans registration.
093: * @param activate <code>true</code> if mbeans should be
094: * registered, <code>false</code> otherwise.
095: */
096: public void activate(final boolean activate) {
097: this .activate = activate;
098: }
099:
100: /**
101: * Register the instance as a ModelMBean using the delegate.
102: * @param <T> instance Type
103: * @param instance Object instance to be managed
104: * @throws MBeansException if registration fails.
105: */
106: public <T> void registerMBean(final T instance)
107: throws MBeansException {
108:
109: if (activate) {
110: String on = getObjectName(instance);
111:
112: // register
113: try {
114: CommonsModelerHelper.registerModelerMBean(instance, on);
115: } catch (CommonsModelerException e) {
116: throw new MBeansException("Cannot register MBean", e);
117: }
118: }
119: }
120:
121: /**
122: * Unregister the given Object.
123: * @param <T> instance Type
124: * @param instance Instance to be deregistered.
125: * @throws MBeansException if unregistration fails.
126: */
127: public <T> void unregisterMBean(final T instance)
128: throws MBeansException {
129:
130: if (activate) {
131: String on = getObjectName(instance);
132:
133: // unregister
134: try {
135: CommonsModelerHelper
136: .unregisterModelerMBean(new ObjectName(on));
137: } catch (MalformedObjectNameException e) {
138: logger.warn("Cannot unregister MBean '" + on + "' : "
139: + e.getMessage());
140: } catch (NullPointerException e) {
141: logger.warn("Cannot unregister MBean '" + on + "' : "
142: + e.getMessage());
143: }
144: }
145: }
146:
147: /**
148: * @param <T> instance Type
149: * @param instance Object instance to be managed
150: * @return Returns the instance ObjectName.
151: * @throws MBeansException if registration fails.
152: */
153: public <T> String getObjectName(final T instance)
154: throws MBeansException {
155:
156: EZBManagementIdentifier<T> identifier = getIdentifier(instance);
157:
158: // gather the ObjectName
159: if (identifier != null) {
160: StringBuilder sb = new StringBuilder();
161:
162: sb.append(identifier.getDomain());
163: sb.append(":");
164: sb.append(identifier.getTypeProperty());
165: String additionnal = identifier
166: .getAdditionnalProperties(instance);
167: if (additionnal != null && (!"".equals(additionnal))) {
168: sb.append(",");
169: sb.append(additionnal);
170: }
171: sb.append(",");
172: sb.append("name=");
173: sb.append(identifier.getNamePropertyValue(instance));
174: return sb.toString();
175: }
176:
177: return null;
178:
179: }
180:
181: /**
182: * @param <T> instance type
183: * @param instance instance to be managed.
184: * @return Returns an {@link EZBManagementIdentifier} for the given Resource type.
185: * @throws MBeansException if the Identifier cannot be returned.
186: */
187: @SuppressWarnings("unchecked")
188: private <T> EZBManagementIdentifier<T> getIdentifier(
189: final T instance) throws MBeansException {
190:
191: // try to use the cached identifier
192: Class clz = instance.getClass();
193: if (identifiers.containsKey(clz)) {
194: return identifiers.get(clz);
195: }
196:
197: // the identifier was not cached
198: String mbeanClassname = instance.getClass().getName();
199: String mbeanPackage = mbeanClassname.substring(0,
200: mbeanClassname.lastIndexOf(".") + 1);
201:
202: // looking for a class named :
203: // <mbean-package>.management.<mbean-model-name>Identifier
204: // ex : org.ow2.easybeans.container.management.JContainer3Identifier
205: String identifierClassname = mbeanPackage + "management."
206: + clz.getSimpleName() + "Identifier";
207:
208: // Instantiate it ...
209: try {
210: Class<? extends EZBManagementIdentifier> cls = ClassUtils
211: .forName(identifierClassname).asSubclass(
212: EZBManagementIdentifier.class);
213: EZBManagementIdentifier<T> id = cls.newInstance();
214:
215: // Sets domain if there is a need to override value
216: if (domainName != null) {
217: id.setDomain(domainName);
218: }
219:
220: // Sets Server if there is a need to override value
221: if (serverName != null) {
222: id.setServerName(serverName);
223: }
224:
225: // cache the identifier
226: identifiers.put(clz, id);
227:
228: return id;
229: } catch (ClassNotFoundException e) {
230: throw new MBeansException("Identifier Class not found", e);
231: } catch (InstantiationException e) {
232: throw new MBeansException(
233: "Identifier Class not instantiated", e);
234: } catch (IllegalAccessException e) {
235: throw new MBeansException(
236: "Identifier Class not instantiated", e);
237: }
238: }
239:
240: /**
241: * Gets the domain name used by this helper.
242: * @return the domain name used by this helper.
243: */
244: public static String getDomainName() {
245: return MBeansHelper.domainName;
246: }
247:
248: /**
249: * Sets the domain name to be used by this helper.
250: * @param domainName the domain name to be used by this helper
251: */
252: public static void setDomainName(final String domainName) {
253: MBeansHelper.domainName = domainName;
254: }
255:
256: /**
257: * Gets the server name used by this helper.
258: * @return the domain name used by this helper.
259: */
260: public static String getServerName() {
261: return MBeansHelper.serverName;
262: }
263:
264: /**
265: * Sets the server name used by this helper.
266: * @param serverName the server name used by this helper.
267: */
268: public static void setServerName(final String serverName) {
269: MBeansHelper.serverName = serverName;
270: }
271:
272: }
|