KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)MetaDataImpl.java 1.28 03/12/19
3  *
4  * Copyright 2004 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
11
12 // java import
13

14 import java.lang.reflect.Method JavaDoc;
15 import java.lang.reflect.Constructor JavaDoc;
16 import java.lang.reflect.InvocationTargetException JavaDoc;
17 import java.util.Hashtable JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.io.PrintWriter JavaDoc;
20 import java.io.StringWriter JavaDoc;
21
22 // RI import
23
import javax.management.* ;
24 import com.sun.jmx.trace.Trace;
25
26 /**
27  * The MetaData class provides local access to the metadata service in
28  * an agent.
29  *
30  * @since 1.5
31  * @since.unbundled JMX RI 1.2
32  */

33 public class MetaDataImpl implements MetaData {
34
35     /** The name of this class to be used for tracing */
36     private final static String JavaDoc dbgTag = "MetaDataImpl";
37
38     /** MetaData for DynamicMBeans **/
39     private final DynamicMetaDataImpl dynamic;
40     private final StandardMetaDataImpl standard;
41
42     /**
43      * The MBeanInstantiator associated to the MetaData
44      */

45     protected final MBeanInstantiator instantiator;
46
47     // Not sure we need this...
48
private final class PrivateStandardMeta extends StandardMetaDataImpl {
49     PrivateStandardMeta() {
50         super();
51     }
52     // public synchronized void testCompliance(Class c) {
53
// MetaDataImpl.this.testStrictCompliance(c);
54
// }
55
protected Class JavaDoc findClass(String JavaDoc className, ClassLoader JavaDoc loader)
56         throws ReflectionException {
57         return MetaDataImpl.this.findClass(className,loader);
58     }
59     protected Class JavaDoc[] findSignatureClasses(String JavaDoc[] signature,
60                            ClassLoader JavaDoc loader)
61         throws ReflectionException {
62         return MetaDataImpl.this.findSignatureClasses(signature,loader);
63     }
64     
65     }
66
67     // Not sure we need this...
68
private final class PrivateDynamicMeta extends DynamicMetaDataImpl {
69     PrivateDynamicMeta() {
70         super();
71     }
72     // public synchronized void testCompliance(Class c) {
73
// MetaDataImpl.this.testStrictCompliance(c);
74
// }
75
protected Class JavaDoc findClass(String JavaDoc className, ClassLoader JavaDoc loader)
76         throws ReflectionException {
77         return MetaDataImpl.this.findClass(className,loader);
78     }
79     protected Class JavaDoc[] findSignatureClasses(String JavaDoc[] signature,
80                            ClassLoader JavaDoc loader)
81         throws ReflectionException {
82         return MetaDataImpl.this.findSignatureClasses(signature,loader);
83     }
84     
85     }
86
87     /**
88      * Creates a Metadata Service.
89      * @param instantiator The MBeanInstantiator that will be used to
90      * take care of class loading issues.
91      * This parameter may not be null.
92      * @exception IllegalArgumentException if the instantiator is null.
93      */

94     public MetaDataImpl(MBeanInstantiator instantiator) {
95     if (instantiator == null) throw new
96         IllegalArgumentException JavaDoc("instantiator must not be null.");
97     this.instantiator = instantiator;
98     this.dynamic = new PrivateDynamicMeta();
99     this.standard = new PrivateStandardMeta();
100     // ------------------------------
101
// ------------------------------
102
}
103
104
105     protected MetaData getMetaData(Class JavaDoc c) {
106     if (DynamicMBean.class.isAssignableFrom(c))
107         return dynamic;
108     else
109         return standard;
110     }
111
112     protected MetaData getMetaData(Object JavaDoc moi) {
113     if (moi instanceof DynamicMBean)
114         return dynamic;
115     else
116         return standard;
117     }
118
119     /**
120      * This methods tests if the MBean is JMX compliant
121      */

122     public synchronized void testCompliance(Class JavaDoc c)
123     throws NotCompliantMBeanException {
124     final MetaData meta = getMetaData(c);
125     meta.testCompliance(c);
126     }
127
128   
129     /**
130      * This methods returns the MBean interface of an MBean
131      */

132     public Class JavaDoc getMBeanInterfaceFromClass(Class JavaDoc c) {
133     return standard.getMBeanInterfaceFromClass(c);
134     }
135
136     
137     /**
138      * This method discovers the attributes and operations that an MBean
139      * exposes for management.
140      *
141      * @param beanClass The class to be analyzed.
142      *
143      * @return An instance of MBeanInfo allowing to retrieve all methods
144      * and operations of this class.
145      *
146      * @exception IntrospectionException if an exception occurs during
147      * introspection.
148      * @exception NotCompliantMBeanException if the MBean class is not
149      * MBean compliant.
150      *
151      */

152     public MBeanInfo getMBeanInfoFromClass(Class JavaDoc beanClass)
153     throws IntrospectionException, NotCompliantMBeanException {
154     return standard.getMBeanInfoFromClass(beanClass);
155     }
156        
157
158     //---------------------------------------------------------------------
159
//
160
// From the MetaData interface
161
//
162
//---------------------------------------------------------------------
163

164     public final String JavaDoc getMBeanClassName(Object JavaDoc moi)
165     throws IntrospectionException, NotCompliantMBeanException {
166     final MetaData meta = getMetaData(moi);
167     return meta.getMBeanClassName(moi);
168     }
169
170     public final MBeanInfo getMBeanInfo(Object JavaDoc moi)
171     throws IntrospectionException {
172     final MetaData meta = getMetaData(moi);
173     return meta.getMBeanInfo(moi);
174     }
175
176     public final Object JavaDoc getAttribute(Object JavaDoc instance, String JavaDoc attribute)
177     throws MBeanException, AttributeNotFoundException,
178            ReflectionException {
179     
180     final MetaData meta = getMetaData(instance);
181     return meta.getAttribute(instance,attribute);
182     }
183
184     public final AttributeList getAttributes(Object JavaDoc instance,
185                          String JavaDoc[] attributes)
186     throws ReflectionException {
187
188     final MetaData meta = getMetaData(instance);
189     return meta.getAttributes(instance, attributes);
190     }
191
192     public final AttributeList setAttributes(Object JavaDoc instance,
193                        AttributeList attributes)
194     throws ReflectionException {
195     
196     final MetaData meta = getMetaData(instance);
197     return meta.setAttributes(instance,attributes);
198     }
199     
200
201     public final Object JavaDoc setAttribute(Object JavaDoc instance, Attribute attribute)
202     throws AttributeNotFoundException, InvalidAttributeValueException,
203            MBeanException, ReflectionException {
204
205     final MetaData meta = getMetaData(instance);
206     return meta.setAttribute(instance,attribute);
207     }
208
209     public final Object JavaDoc invoke(Object JavaDoc instance, String JavaDoc operationName,
210              Object JavaDoc params[], String JavaDoc signature[])
211     throws MBeanException, ReflectionException {
212
213         if (operationName == null) {
214         final RuntimeException JavaDoc r =
215           new IllegalArgumentException JavaDoc("Operation name cannot be null");
216             throw new RuntimeOperationsException(r,
217               "Exception occured trying to invoke the operation on the MBean");
218         }
219     final MetaData meta = getMetaData(instance);
220     return meta.invoke(instance,operationName,params,signature);
221     }
222
223     public boolean isInstanceOf(Object JavaDoc instance, String JavaDoc className)
224     throws ReflectionException {
225
226     // XXX revisit here: ModelMBean ???
227
final MetaData meta = getMetaData(instance);
228     return meta.isInstanceOf(instance,className);
229     }
230
231     public ObjectName preRegisterInvoker(Object JavaDoc moi, ObjectName name,
232                      MBeanServer mbs)
233     throws InstanceAlreadyExistsException, MBeanRegistrationException {
234    
235     if (!(moi instanceof MBeanRegistration)) return name;
236     final MetaData meta = getMetaData(moi);
237     return meta.preRegisterInvoker(moi,name,mbs);
238     }
239    
240     public void postRegisterInvoker(Object JavaDoc moi, boolean registrationDone) {
241     if (!(moi instanceof MBeanRegistration)) return;
242
243     final MetaData meta = getMetaData(moi);
244     meta.postRegisterInvoker(moi,registrationDone);
245     }
246     
247     public void preDeregisterInvoker(Object JavaDoc moi)
248     throws MBeanRegistrationException {
249     if (!(moi instanceof MBeanRegistration)) return;
250     final MetaData meta = getMetaData(moi);
251     meta.preDeregisterInvoker(moi);
252     }
253
254   
255     public void postDeregisterInvoker(Object JavaDoc moi) {
256     if (!(moi instanceof MBeanRegistration)) return;
257     final MetaData meta = getMetaData(moi);
258     meta.postDeregisterInvoker(moi);
259     }
260
261     /**
262      * Find a class using the specified ClassLoader.
263      **/

264     protected Class JavaDoc findClass(String JavaDoc className, ClassLoader JavaDoc loader)
265     throws ReflectionException {
266     return instantiator.findClass(className, loader);
267     }
268
269     /**
270      * Find the classes from a signature using the specified ClassLoader.
271      **/

272     protected Class JavaDoc[] findSignatureClasses(String JavaDoc[] signature,
273                        ClassLoader JavaDoc loader)
274     throws ReflectionException {
275     return ((signature == null)?null:
276         instantiator.findSignatureClasses(signature,loader));
277     }
278
279     // TRACES & DEBUG
280
//---------------
281

282     private static boolean isTraceOn() {
283         return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER);
284     }
285
286     private static void trace(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
287         Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info);
288     }
289     
290     private static void trace(String JavaDoc func, String JavaDoc info) {
291         trace(dbgTag, func, info);
292     }
293     
294     private static boolean isDebugOn() {
295         return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER);
296     }
297     
298     private static void debug(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
299         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info);
300     }
301     
302     private static void debug(String JavaDoc func, String JavaDoc info) {
303         debug(dbgTag, func, info);
304     }
305     
306     private static void debugX(String JavaDoc func,Throwable JavaDoc e) {
307     if (isDebugOn()) {
308         final StringWriter JavaDoc s = new StringWriter JavaDoc();
309         e.printStackTrace(new PrintWriter JavaDoc(s));
310         final String JavaDoc stack = s.toString();
311         
312         debug(dbgTag,func,"Exception caught in "+ func+"(): "+e);
313         debug(dbgTag,func,stack);
314     
315         // java.lang.System.err.println("**** Exception caught in "+
316
// func+"(): "+e);
317
// java.lang.System.err.println(stack);
318
}
319     }
320     
321  }
322
Popular Tags