KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > standardmbean > StandardMBeanImpl


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.mx.standardmbean;
23
24 import java.lang.reflect.Method JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 import javax.management.Attribute JavaDoc;
28 import javax.management.AttributeList JavaDoc;
29 import javax.management.AttributeNotFoundException JavaDoc;
30 import javax.management.InvalidAttributeValueException JavaDoc;
31 import javax.management.JMException JavaDoc;
32 import javax.management.MBeanException JavaDoc;
33 import javax.management.MBeanInfo JavaDoc;
34 import javax.management.NotCompliantMBeanException JavaDoc;
35 import javax.management.ReflectionException JavaDoc;
36
37 import org.jboss.logging.Logger;
38 import org.jboss.mx.loading.LoaderRepository;
39 import org.jboss.mx.metadata.StandardMetaData;
40 import org.jboss.mx.server.ExceptionHandler;
41
42 /**
43  * A helper class to allow standard mbeans greater control over their
44  * management interface.<p>
45  *
46  * Extending this class actually makes the mbean a dynamic mbean, but
47  * with the convenience of a standard mbean.
48  *
49  * @todo make this more dynamic, somehow delegate to an XMBean?
50  *
51  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
52  * @author <a HREF="mailto:thomas.diesler@jboss.com">Thomas Diesler</a>.
53  * @version $Revision: 37459 $
54  */

55 public class StandardMBeanImpl implements StandardMBeanDelegate
56 {
57    private static final Logger log = Logger.getLogger(StandardMBeanImpl.class);
58
59    /**
60     * The implementation object
61     */

62    private Object JavaDoc implementation;
63
64    /**
65     * The management interface
66     */

67    private Class JavaDoc mbeanInterface;
68
69    /**
70     * The cached mbeaninfo
71     */

72    private MBeanInfo JavaDoc cachedMBeanInfo;
73
74    // Static -----------------------------------------------------
75

76    // Constructors ------------------------------------------------
77

78    /**
79     * Construct a DynamicMBean from the given implementation object
80     * and the passed management interface class.
81     *
82     * @param implementation the object implementing the mbean
83     * @param mbeanInterface the management interface of the mbean
84     * @exception IllegalArgumentException for a null implementation
85     * @exception NotCompliantMBeanException if the management interface
86     * does not follow the JMX design patterns or the implementation
87     * does not implement the interface
88     */

89    public StandardMBeanImpl(Object JavaDoc implementation, Class JavaDoc mbeanInterface)
90       throws NotCompliantMBeanException JavaDoc
91    {
92       this.implementation = implementation;
93       this.mbeanInterface = mbeanInterface;
94    }
95
96    /**
97     * Construct a DynamicMBean from this object
98     * and the passed management interface class.<p>
99     *
100     * Used in subclassing
101     *
102     * @param mbeanInterface the management interface of the mbean
103     * @exception NotCompliantMBeanException if the management interface
104     * does not follow the JMX design patterns or this
105     * does not implement the interface
106     */

107    protected StandardMBeanImpl(Class JavaDoc mbeanInterface)
108       throws NotCompliantMBeanException JavaDoc
109    {
110       this.implementation = this;
111       this.mbeanInterface = mbeanInterface;
112    }
113
114    /**
115     * Retrieve the implementation object
116     *
117     * @return the implementation
118     */

119    public Object JavaDoc getImplementation()
120    {
121       return implementation;
122    }
123
124    /**
125     * Replace the implementation object
126     *
127     * @todo make this work after the mbean is registered
128     * @param implementation the new implementation
129     * @exception IllegalArgumentException for a null parameter
130     * @exception NotCompliantMBeanException if the new implementation
131     * does not implement the interface supplied at
132     * construction
133     */

134    public void setImplementation(Object JavaDoc implementation)
135         throws NotCompliantMBeanException JavaDoc
136    {
137       if (implementation == null)
138          throw new IllegalArgumentException JavaDoc("Null implementation");
139       this.implementation = implementation;
140    }
141
142    /**
143     * Retrieve the implementation class
144     *
145     * @return the class of the implementation
146     */

147    public Class JavaDoc getImplementationClass()
148    {
149       return implementation.getClass();
150    }
151
152    /**
153     * Retrieve the management interface
154     *
155     * @return the management interface
156     */

157    public final Class JavaDoc getMBeanInterface()
158    {
159       return mbeanInterface;
160    }
161
162    public Object JavaDoc getAttribute(String JavaDoc attribute)
163       throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
164    {
165       try
166       {
167          Method JavaDoc method = implementation.getClass().getMethod("get" + attribute, null);
168          return method.invoke(implementation, new Object JavaDoc[0]);
169       }
170       catch (Exception JavaDoc e)
171       {
172          JMException JavaDoc result = ExceptionHandler.handleException(e);
173          if (result instanceof AttributeNotFoundException JavaDoc)
174             throw (AttributeNotFoundException JavaDoc)result;
175          if (result instanceof MBeanException JavaDoc)
176             throw (MBeanException JavaDoc)result;
177          if (result instanceof ReflectionException JavaDoc)
178             throw (ReflectionException JavaDoc)result;
179          throw new MBeanException JavaDoc(e, "Cannot get attribute: " + attribute);
180       }
181    }
182
183    public void setAttribute(Attribute JavaDoc attribute)
184       throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc
185    {
186       try
187       {
188          Class JavaDoc[] clArr = null;
189          if (attribute.getValue() != null)
190          {
191             clArr = new Class JavaDoc[]{attribute.getValue().getClass()};
192          }
193          Method JavaDoc method = implementation.getClass().getMethod("set" + attribute.getName(), clArr);
194          method.invoke(implementation, new Object JavaDoc[]{attribute.getValue()});
195       }
196       catch (Exception JavaDoc e)
197       {
198          JMException JavaDoc result = ExceptionHandler.handleException(e);
199          if (result instanceof AttributeNotFoundException JavaDoc)
200             throw (AttributeNotFoundException JavaDoc)result;
201          if (result instanceof InvalidAttributeValueException JavaDoc)
202             throw (InvalidAttributeValueException JavaDoc)result;
203          if (result instanceof MBeanException JavaDoc)
204             throw (MBeanException JavaDoc)result;
205          if (result instanceof ReflectionException JavaDoc)
206             throw (ReflectionException JavaDoc)result;
207          throw new MBeanException JavaDoc(e, "Cannot set attribute: " + attribute);
208       }
209    }
210
211    public AttributeList JavaDoc getAttributes(String JavaDoc[] attributes)
212    {
213       try
214       {
215          AttributeList JavaDoc attrList = new AttributeList JavaDoc(attributes.length);
216          for (int i = 0; i < attributes.length; i++)
217          {
218             String JavaDoc name = attributes[i];
219             Object JavaDoc value = getAttribute(name);
220             attrList.add(new Attribute JavaDoc(name, value));
221          }
222          return attrList;
223       }
224       catch (Exception JavaDoc e)
225       {
226          JMException JavaDoc result = ExceptionHandler.handleException(e);
227          // Why is this not throwing the same exceptions as getAttribute(String)
228
throw new RuntimeException JavaDoc("Cannot get attributes", result);
229       }
230    }
231
232
233    public AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributes)
234    {
235       try
236       {
237          AttributeList JavaDoc attrList = new AttributeList JavaDoc(attributes.size());
238          Iterator JavaDoc it = attributes.iterator();
239          while (it.hasNext())
240          {
241             Attribute JavaDoc attr = (Attribute JavaDoc) it.next();
242             setAttribute(attr);
243             String JavaDoc name = attr.getName();
244             Object JavaDoc value = getAttribute(name);
245             attrList.add(new Attribute JavaDoc(name, value));
246          }
247          return attrList;
248       }
249       catch (Exception JavaDoc e)
250       {
251          JMException JavaDoc result = ExceptionHandler.handleException(e);
252          // Why is this not throwing the same exceptions as setAttribute(Attribute)
253
throw new RuntimeException JavaDoc("Cannot set attributes", result);
254       }
255    }
256
257    public Object JavaDoc invoke(String JavaDoc actionName, Object JavaDoc[] params, String JavaDoc[] signature)
258       throws MBeanException JavaDoc, ReflectionException JavaDoc
259    {
260       try
261       {
262          Class JavaDoc[] sigcl = new Class JavaDoc[signature.length];
263          for (int i = 0; i < signature.length; i++)
264          {
265             sigcl[i] = loadClass(signature[i]);
266          }
267          Method JavaDoc method = implementation.getClass().getMethod(actionName, sigcl);
268          return method.invoke(implementation, params);
269       }
270       catch (Exception JavaDoc e)
271       {
272          JMException JavaDoc result = ExceptionHandler.handleException(e);
273          if (result instanceof MBeanException JavaDoc)
274             throw (MBeanException JavaDoc)result;
275          if (result instanceof ReflectionException JavaDoc)
276             throw (ReflectionException JavaDoc)result;
277          throw new MBeanException JavaDoc(e, "Cannot invoke: " + actionName);
278       }
279    }
280
281    /**
282     * Load a class from the classloader that loaded this MBean
283     */

284    private Class JavaDoc loadClass(String JavaDoc className) throws ClassNotFoundException JavaDoc
285    {
286       Class JavaDoc clazz = LoaderRepository.getNativeClassForName(className);
287       if (clazz == null) {
288          ClassLoader JavaDoc cl = getClass().getClassLoader();
289          clazz = cl.loadClass(className);
290       }
291       return clazz;
292    }
293
294    public MBeanInfo JavaDoc getMBeanInfo()
295    {
296       MBeanInfo JavaDoc info = getCachedMBeanInfo();
297       if (info == null)
298       {
299          try
300          {
301             info = buildMBeanInfo();
302             cacheMBeanInfo(info);
303          }
304          catch (NotCompliantMBeanException JavaDoc e)
305          {
306             log.error("Unexcepted exception", e);
307             throw new IllegalStateException JavaDoc("Unexcepted exception " + e.toString());
308          }
309
310       }
311       return info;
312    }
313
314    /**
315     * Retrieve the cached mbean info
316     *
317     * @return the cached mbean info
318     */

319    public MBeanInfo JavaDoc getCachedMBeanInfo()
320    {
321       return cachedMBeanInfo;
322    }
323
324    /**
325     * Sets the cached mbean info
326     *
327     * @todo make this work after the mbean is registered
328     * @param info the mbeaninfo to cache, can be null to erase the cache
329     */

330    public void cacheMBeanInfo(MBeanInfo JavaDoc info)
331    {
332       cachedMBeanInfo = info;
333    }
334
335    /**
336     * Builds a default MBeanInfo for this MBean, using the Management Interface specified for this MBean.
337     *
338     * While building the MBeanInfo, this method calls the customization hooks that make it possible for subclasses to
339     * supply their custom descriptions, parameter names, etc...
340     */

341    public MBeanInfo JavaDoc buildMBeanInfo()
342       throws NotCompliantMBeanException JavaDoc
343    {
344       if (implementation == null)
345          throw new IllegalArgumentException JavaDoc("Null implementation");
346
347       StandardMetaData metaData = new StandardMetaData(implementation, mbeanInterface);
348       this.mbeanInterface = metaData.getMBeanInterface();
349       return metaData.build();
350    }
351    
352    // Inner Classes -----------------------------------------------
353
}
354
Popular Tags