KickJava   Java API By Example, From Geeks To Geeks.

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


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: MBeansHelper.java 1041 2006-08-07 17:01:48Z sauthieg $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.jmx;
27
28 import java.util.Hashtable JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.management.MalformedObjectNameException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33
34 import org.objectweb.easybeans.api.jmx.EZBManagementIdentifier;
35 import org.objectweb.easybeans.log.JLog;
36 import org.objectweb.easybeans.log.JLogFactory;
37 import org.objectweb.easybeans.util.loader.ClassUtils;
38
39 /**
40  * Singleton object.Creates the MBeans and register them.
41  * @author florent
42  *
43  */

44 public final class MBeansHelper {
45     /**
46      * Logger.
47      */

48     private static JLog logger = JLogFactory.getLog(MBeansHelper.class);
49
50     /**
51      * Singleton instance.
52      */

53     private static MBeansHelper instance = null;
54
55     /**
56      * The Identifier in charge of creating the right ObjectName for a given
57      * instance.
58      */

59     private Map JavaDoc<Class JavaDoc, EZBManagementIdentifier> identifiers = null;
60
61     /**
62      * Is Management activated ?
63      */

64     private boolean activate;
65
66     /**
67      * Singleton class, no public constructor.
68      */

69     private MBeansHelper() {
70         identifiers = new Hashtable JavaDoc<Class JavaDoc, EZBManagementIdentifier>();
71     }
72
73     /**
74      * @return Returns the Singleton MBeansHelper instance.
75      */

76     public static MBeansHelper getInstance() {
77         if (instance == null) {
78             instance = new MBeansHelper();
79         }
80         return instance;
81     }
82
83     /**
84      * Activate the MBeans registration.
85      * @param activate <code>true</code> if mbeans should be
86      * registered, <code>false</code> otherwise.
87      */

88     public void activate(final boolean activate) {
89         this.activate = activate;
90     }
91
92     /**
93      * Register the instance as a ModelMBean using the delegate.
94      * @param <T> instance Type
95      * @param instance Object instance to be managed
96      * @throws MBeansException if registration fails.
97      */

98     public <T> void registerMBean(final T instance) throws MBeansException {
99
100         if (activate) {
101             String JavaDoc on = getObjectName(instance);
102
103             // register
104
try {
105                 CommonsModelerHelper.registerModelerMBean(instance, on);
106             } catch (CommonsModelerException e) {
107                 throw new MBeansException("Cannot register MBean", e);
108             }
109         }
110     }
111
112     /**
113      * Unregister the given Object.
114      * @param <T> instance Type
115      * @param instance Instance to be deregistered.
116      * @throws MBeansException if unregistration fails.
117      */

118     public <T> void unregisterMBean(final T instance) throws MBeansException {
119
120         if (activate) {
121             String JavaDoc on = getObjectName(instance);
122
123             // unregister
124
try {
125                 CommonsModelerHelper.unregisterModelerMBean(new ObjectName JavaDoc(on));
126             } catch (MalformedObjectNameException JavaDoc e) {
127                 logger.warn("Cannot unregister MBean '" + on + "' : " + e.getMessage());
128             } catch (NullPointerException JavaDoc e) {
129                 logger.warn("Cannot unregister MBean '" + on + "' : " + e.getMessage());
130             }
131         }
132     }
133
134     /**
135      * @param <T> instance Type
136      * @param instance Object instance to be managed
137      * @return Returns the instance ObjectName.
138      * @throws MBeansException if registration fails.
139      */

140     public <T> String JavaDoc getObjectName(final T instance) throws MBeansException {
141
142         EZBManagementIdentifier<T> identifier = getIdentifier(instance);
143
144         // gather the ObjectName
145
if (identifier != null) {
146             StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
147
148             sb.append(identifier.getDomain());
149             sb.append(":");
150             sb.append(identifier.getTypeProperty());
151             String JavaDoc additionnal = identifier.getAdditionnalProperties(instance);
152             if (additionnal != null && (!"".equals(additionnal))) {
153                 sb.append(",");
154                 sb.append(additionnal);
155             }
156             sb.append(",");
157             sb.append("name=");
158             sb.append(identifier.getNamePropertyValue(instance));
159             return sb.toString();
160         }
161
162         return null;
163
164     }
165
166     /**
167      * @param <T> instance type
168      * @param instance instance to be managed.
169      * @return Returns an {@link EZBManagementIdentifier} for the given Resource type.
170      * @throws MBeansException if the Identifier cannot be returned.
171      */

172     @SuppressWarnings JavaDoc("unchecked")
173     private <T> EZBManagementIdentifier<T> getIdentifier(final T instance) throws MBeansException {
174
175         // try to use the cached identifier
176
Class JavaDoc clz = instance.getClass();
177         if (identifiers.containsKey(clz)) {
178             return identifiers.get(clz);
179         }
180
181         // the identifier was not cached
182
String JavaDoc mbeanClassname = instance.getClass().getName();
183         String JavaDoc mbeanPackage = mbeanClassname.substring(0, mbeanClassname
184                 .lastIndexOf(".") + 1);
185
186         // looking for a class named :
187
// <mbean-package>.management.<mbean-model-name>Identifier
188
// ex : org.objectweb.easybeans.container.management.JContainer3Identifier
189
String JavaDoc identifierClassname = mbeanPackage + "management." + clz.getSimpleName() + "Identifier";
190
191         // Instantiate it ...
192
try {
193             Class JavaDoc<? extends EZBManagementIdentifier> cls = ClassUtils.forName(
194                     identifierClassname).asSubclass(EZBManagementIdentifier.class);
195             EZBManagementIdentifier<T> id = cls.newInstance();
196
197             // cache the identifier
198
identifiers.put(clz, id);
199
200             return id;
201         } catch (ClassNotFoundException JavaDoc e) {
202             throw new MBeansException("Identifier Class not found", e);
203         } catch (InstantiationException JavaDoc e) {
204             throw new MBeansException("Identifier Class not instantiated", e);
205         } catch (IllegalAccessException JavaDoc e) {
206             throw new MBeansException("Identifier Class not instantiated", e);
207         }
208     }
209
210 }
211
Popular Tags