KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > metadata > MBeanCapability


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.metadata;
23
24 import javax.management.DynamicMBean JavaDoc;
25 import javax.management.NotCompliantMBeanException JavaDoc;
26
27 /**
28  * Holds the type of an MBean class.
29  *
30  * The introspection algorithm used is the following:
31  *
32  * 1. If MyClass is an instance of the DynamicMBean interface, then the return value of its getMBeanInfo method will
33  * list the attributes and operations of the resource. In other words, MyClass is a dynamic MBean.
34  *
35  * 2. If the MyClass MBean is an instance of a MyClassMBean interface, then only the methods listed in, or inherited by,
36  * the interface are considered among all the methods of, or inherited by, the MBean. The design patterns are then used to
37  * identify the attributes and operations from the method names in the MyClassMBean interface and its ancestors.
38  * In other words, MyClass is a standard MBean.
39  *
40  * 3. If MyClass is an instance of the DynamicMBean interface, then MyClassMBean is ignored.
41  * If MyClassMBean is not a public interface, it is not a JMX manageable resource.
42  * If the MBean is an instance of neither MyClassMBean nor DynamicMBean, the inheritance tree of MyClass is examined,
43  * looking for the nearest superclass that implements its own MBean interface.
44  *
45  * a. If there is an ancestor called SuperClass that is an instance of SuperClassMBean, the design patterns
46  * are used to derive the attributes and operations from SuperClassMBean. In this case, the MBean MyClass then
47  * has the same management interface as the MBean SuperClass. If SuperClassMBean is not a public interface,
48  * it is not a JMX manageable resource.
49  *
50  * b. When there is no superclass with its own MBean interface, MyClass is not a JMX manageable resource.
51  *
52  * @author <a HREF="mailto:trevor@protocool.com">Trevor Squires</a>.
53  * @author thomas.diesler@jboss.org
54  */

55 public class MBeanCapability
56 {
57    public static final int DYNAMIC_MBEAN = 0x321;
58    public static final int STANDARD_MBEAN = 0x123;
59    public static final int NOT_AN_MBEAN = 0xc0de;
60
61    protected int mbeanType = NOT_AN_MBEAN;
62
63    private MBeanCapability(int type)
64    {
65       mbeanType = type;
66    }
67
68    public int getMBeanType()
69    {
70       return mbeanType;
71    }
72
73    public static MBeanCapability of(Class JavaDoc mbeanClass) throws NotCompliantMBeanException JavaDoc
74    {
75       if (null == mbeanClass)
76       {
77          throw new IllegalArgumentException JavaDoc("MBean class cannot be null");
78       }
79
80       // If MyClass is an instance of the DynamicMBean interface, MyClass is a dynamic MBean.
81
if (DynamicMBean JavaDoc.class.isAssignableFrom(mbeanClass))
82       {
83          return new MBeanCapability(DYNAMIC_MBEAN);
84       }
85
86       // If the MyClass MBean is an instance of a MyClassMBean interface, MyClass is a standard MBean
87
Class JavaDoc [] interfaces = mbeanClass.getInterfaces();
88       for (int i = 0; i < interfaces.length; i++)
89       {
90          Class JavaDoc anInterface = interfaces[i];
91          if (anInterface.getName().equals(mbeanClass.getName() + "MBean"))
92          {
93             return new MBeanCapability(STANDARD_MBEAN);
94          }
95       }
96
97       // If there is an ancestor called SuperClass that is an instance of SuperClassMBean
98
Class JavaDoc superClass = mbeanClass.getSuperclass();
99       if (superClass != null)
100          return of(superClass);
101
102       throw new NotCompliantMBeanException JavaDoc("Class does not expose a management interface: " + mbeanClass.getName());
103    }
104
105 }
106
Popular Tags