KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jmx > mbeanserver > MXBeanSupport


1 /*
2  * @(#)MXBeanSupport.java 1.24 07/09/11
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.jmx.mbeanserver;
9
10 import static com.sun.jmx.mbeanserver.Util.*;
11
12 import java.util.Iterator JavaDoc;
13 import java.util.Set JavaDoc;
14
15 import javax.management.InstanceAlreadyExistsException JavaDoc;
16 import javax.management.JMX JavaDoc;
17 import javax.management.MBeanServer JavaDoc;
18 import javax.management.NotCompliantMBeanException JavaDoc;
19 import javax.management.ObjectName JavaDoc;
20
21 /**
22  * Base class for MXBeans.
23  *
24  * @since 1.6
25  */

26 public class MXBeanSupport extends MBeanSupport<ConvertingMethod> {
27
28     /**
29        <p>Construct an MXBean that wraps the given resource using the
30        given MXBean interface.</p>
31
32        @param resource the underlying resource for the new MXBean.
33
34        @param mxbeanInterface the interface to be used to determine
35        the MXBean's management interface.
36
37        @param <T> a type parameter that allows the compiler to check
38        that {@code resource} implements {@code mxbeanInterface},
39        provided that {@code mxbeanInterface} is a class constant like
40        {@code SomeMXBean.class}.
41
42        @throws IllegalArgumentException if {@code resource} is null or
43        if it does not implement the class {@code mxbeanInterface} or if
44        that class is not a valid MXBean interface.
45     */

46     public <T> MXBeanSupport(T resource, Class JavaDoc<T> mxbeanInterface)
47             throws NotCompliantMBeanException JavaDoc {
48         super(resource, mxbeanInterface);
49     }
50
51     @Override JavaDoc
52     MBeanIntrospector<ConvertingMethod> getMBeanIntrospector() {
53     return MXBeanIntrospector.getInstance();
54     }
55
56     @Override JavaDoc
57     Object JavaDoc getCookie() {
58     return mxbeanLookup;
59     }
60
61     static Class JavaDoc<?> findMXBeanInterface(Class JavaDoc<?> resourceClass)
62         throws IllegalArgumentException JavaDoc {
63     if (resourceClass == null)
64         throw new IllegalArgumentException JavaDoc("Null resource class");
65         final Set JavaDoc<Class JavaDoc> intfs = transitiveInterfaces(resourceClass);
66         final Set JavaDoc<Class JavaDoc> candidates = newSet();
67         for (Class JavaDoc intf : intfs) {
68             if (JMX.isMXBeanInterface(intf))
69                 candidates.add(intf);
70         }
71     reduce:
72         while (candidates.size() > 1) {
73             for (Class JavaDoc intf : candidates) {
74                 for (Iterator JavaDoc<Class JavaDoc> it = candidates.iterator(); it.hasNext();
75                     ) {
76                     final Class JavaDoc intf2 = it.next();
77                     if (intf != intf2 && intf2.isAssignableFrom(intf)) {
78                         it.remove();
79                         continue reduce;
80                     }
81                 }
82             }
83             final String JavaDoc msg =
84                 "Class " + resourceClass.getName() + " implements more than " +
85                 "one MXBean interface: " + candidates;
86             throw new IllegalArgumentException JavaDoc(msg);
87         }
88         if (candidates.iterator().hasNext()) {
89             return candidates.iterator().next();
90         } else {
91             final String JavaDoc msg =
92                 "Class " + resourceClass.getName() +
93                 " is not a JMX compliant MXBean";
94             throw new IllegalArgumentException JavaDoc(msg);
95         }
96     }
97
98     /* Return all interfaces inherited by this class, directly or
99      * indirectly through the parent class and interfaces.
100      */

101     private static Set JavaDoc<Class JavaDoc> transitiveInterfaces(Class JavaDoc c) {
102         Set JavaDoc<Class JavaDoc> set = newSet();
103         transitiveInterfaces(c, set);
104         return set;
105     }
106     private static void transitiveInterfaces(Class JavaDoc c, Set JavaDoc<Class JavaDoc> intfs) {
107         if (c == null)
108             return;
109         if (c.isInterface())
110             intfs.add(c);
111         transitiveInterfaces(c.getSuperclass(), intfs);
112         for (Class JavaDoc sup : c.getInterfaces())
113             transitiveInterfaces(sup, intfs);
114     }
115
116     /*
117      * The sequence of events for tracking inter-MXBean references is
118      * relatively complicated. We use the magical preRegister2 method
119      * which the MBeanServer knows about. The steps during registration
120      * are:
121      * (1) Call user preRegister, if any. If exception, abandon.
122      * (2) Call preRegister2 and hence this register method. If exception,
123      * call postRegister(false) and abandon.
124      * (3) Try to register the MBean. If exception, call registerFailed()
125      * which will call the unregister method. (Also call postRegister(false).)
126      * (4) If we get this far, we can call postRegister(true).
127      *
128      * When we are wrapped in an instance of javax.management.StandardMBean,
129      * things are simpler. That class calls this method from its preRegister,
130      * and propagates any exception. There is no user preRegister in this case.
131      * If this method succeeds but registration subsequently fails,
132      * StandardMBean calls unregister from its postRegister(false) method.
133      */

134     @Override JavaDoc
135     public void register(MBeanServer JavaDoc server, ObjectName JavaDoc name)
136             throws InstanceAlreadyExistsException JavaDoc {
137         if (name == null)
138             throw new IllegalArgumentException JavaDoc("Null object name");
139         // eventually we could have some logic to supply a default name
140

141         synchronized (lock) {
142             this.mxbeanLookup = MXBeanLookup.lookupFor(server);
143             this.mxbeanLookup.addReference(name, getResource());
144             this.objectName = name;
145         }
146     }
147
148     @Override JavaDoc
149     public void unregister() {
150         synchronized (lock) {
151             if (mxbeanLookup.removeReference(objectName, getResource()))
152                 objectName = null;
153         }
154     }
155
156     private Object JavaDoc lock = new Object JavaDoc(); // for mxbeanLookup and objectName
157
private MXBeanLookup mxbeanLookup;
158     private ObjectName JavaDoc objectName;
159 }
160
Popular Tags