KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)BaseMetaDataImpl.java 1.16 05/05/27
3  *
4  * Copyright 2005 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
import java.util.Iterator JavaDoc;
14 import java.io.PrintWriter JavaDoc;
15 import java.io.StringWriter JavaDoc;
16
17 // RI import
18
import javax.management.* ;
19 import com.sun.jmx.trace.Trace;
20
21 /**
22  * The DynamicMetaDataImpl class provides local access to the metadata
23  * service in an agent.
24  * The DynamicMetaDataImpl only handles DynamicMBeans.
25  *
26  * @since 1.5
27  * @since.unbundled JMX RI 1.2
28  */

29 abstract class BaseMetaDataImpl implements MetaData {
30
31     /** The name of this class to be used for tracing */
32     private final static String JavaDoc dbgTag = "BaseMetaDataImpl";
33     
34
35     /**
36      * Creates a Metadata Service.
37      */

38     BaseMetaDataImpl() {
39
40     // ------------------------------
41
// ------------------------------
42
}
43
44     
45     //---------------------------------------------------------------------
46
//
47
// From the MetaData interface
48
//
49
//---------------------------------------------------------------------
50

51     public abstract MBeanInfo getMBeanInfo(Object JavaDoc moi)
52     throws IntrospectionException;
53
54     public abstract Object JavaDoc getAttribute(Object JavaDoc instance, String JavaDoc attribute)
55     throws MBeanException, AttributeNotFoundException,
56            ReflectionException;
57
58     public abstract AttributeList getAttributes(Object JavaDoc instance,
59                         String JavaDoc[] attributes)
60     throws ReflectionException;
61
62     public abstract AttributeList setAttributes(Object JavaDoc instance,
63                         AttributeList attributes)
64     throws ReflectionException;
65     
66     public abstract Object JavaDoc setAttribute(Object JavaDoc instance, Attribute attribute)
67     throws AttributeNotFoundException, InvalidAttributeValueException,
68            MBeanException, ReflectionException;
69
70     public abstract Object JavaDoc invoke(Object JavaDoc instance, String JavaDoc operationName,
71              Object JavaDoc params[], String JavaDoc signature[])
72     throws MBeanException, ReflectionException;
73
74     public ObjectName preRegisterInvoker(Object JavaDoc moi, ObjectName name,
75                      MBeanServer mbs)
76     throws InstanceAlreadyExistsException, MBeanRegistrationException {
77    
78     if (!(moi instanceof MBeanRegistration)) return name;
79
80         final ObjectName newName;
81       
82         try {
83             newName=(ObjectName)
84         ((MBeanRegistration)moi).preRegister(mbs, name);
85         } catch (RuntimeException JavaDoc e) {
86                 throw new RuntimeMBeanException((RuntimeException JavaDoc)e,
87                "RuntimeException thrown in preRegister method");
88     } catch (Error JavaDoc er) {
89                 throw new RuntimeErrorException((Error JavaDoc) er,
90                            "Error thrown in preRegister method");
91     } catch (MBeanRegistrationException r) {
92         throw (MBeanRegistrationException)r;
93     } catch (Exception JavaDoc ex) {
94         throw new MBeanRegistrationException((Exception JavaDoc) ex,
95               "Exception thrown in preRegister method");
96     }
97         
98     if (newName != null) return newName;
99     else return name;
100     }
101    
102     public void postRegisterInvoker(Object JavaDoc moi, boolean registrationDone) {
103     if (!(moi instanceof MBeanRegistration)) return;
104
105         try {
106             ((MBeanRegistration)moi).
107         postRegister(new Boolean JavaDoc(registrationDone));
108         } catch (RuntimeException JavaDoc e) {
109         throw new RuntimeMBeanException((RuntimeException JavaDoc)e,
110               "RuntimeException thrown in postRegister method");
111     } catch (Error JavaDoc er) {
112         throw new RuntimeErrorException((Error JavaDoc) er,
113               "Error thrown in postRegister method");
114     }
115     }
116     
117     public void preDeregisterInvoker(Object JavaDoc moi)
118     throws MBeanRegistrationException {
119     if (!(moi instanceof MBeanRegistration)) return;
120
121     try {
122             ((MBeanRegistration)moi).preDeregister();
123         } catch (RuntimeException JavaDoc e) {
124         throw new RuntimeMBeanException((RuntimeException JavaDoc) e,
125                          "RuntimeException thrown in preDeregister method");
126     } catch (Error JavaDoc er) {
127         throw new RuntimeErrorException((Error JavaDoc) er,
128                          "Error thrown in preDeregister method");
129     } catch (MBeanRegistrationException t) {
130         throw (MBeanRegistrationException)t;
131     } catch (Exception JavaDoc ex) {
132         throw new MBeanRegistrationException((Exception JavaDoc)ex,
133                          "Exception thrown in preDeregister method");
134     }
135     }
136    
137     public void postDeregisterInvoker(Object JavaDoc moi) {
138     if (!(moi instanceof MBeanRegistration)) return;
139
140         try {
141             ((MBeanRegistration)moi).postDeregister();
142         } catch (RuntimeException JavaDoc e) {
143         throw new RuntimeMBeanException((RuntimeException JavaDoc)e,
144                          "RuntimeException thrown in postDeregister method");
145     } catch (Error JavaDoc er) {
146         throw new RuntimeErrorException((Error JavaDoc) er,
147                          "Error thrown in postDeregister method");
148     }
149     }
150
151     public String JavaDoc getMBeanClassName(Object JavaDoc moi)
152     throws IntrospectionException, NotCompliantMBeanException {
153
154     final MBeanInfo mbi;
155     try {
156         // Ask the MBeanInfo for the class name
157
mbi = getMBeanInfo(moi);
158     } catch (SecurityException JavaDoc x) {
159         throw x;
160     } catch (IntrospectionException x) {
161         throw x;
162     } catch (Exception JavaDoc x) {
163         throw new NotCompliantMBeanException("Can't obtain MBeanInfo " +
164                          "from DynamicMBean: " + x);
165     }
166
167     if (mbi == null) {
168         throw new
169         NotCompliantMBeanException("The MBeanInfo returned is null");
170     }
171     
172     final String JavaDoc className = mbi.getClassName();
173         
174     if (className == null) {
175         throw new
176         IntrospectionException("The class Name returned is null");
177     }
178
179     return className;
180     }
181
182     public boolean isInstanceOf(Object JavaDoc instance, String JavaDoc className)
183     throws ReflectionException {
184
185     try {
186         final String JavaDoc cn = getMBeanClassName(instance);
187         if (cn.equals(className))
188         return true;
189         try {
190         final ClassLoader JavaDoc cl = instance.getClass().getClassLoader();
191         
192         final Class JavaDoc classNameClass = findClass(className,cl);
193         if (classNameClass == null) return false;
194         if (classNameClass.isInstance(instance)) return true;
195         
196         final Class JavaDoc instanceClass = findClass(cn,cl);
197         if (instanceClass == null) return false;
198         return classNameClass.isAssignableFrom(instanceClass);
199         } catch (Exception JavaDoc x) {
200         /* Could be SecurityException or ClassNotFoundException */
201         debugX("isInstanceOf",x);
202         return false;
203         }
204     } catch (IntrospectionException x) {
205         debugX("isInstanceOf",x);
206         throw new ReflectionException(x,x.getMessage());
207     } catch (NotCompliantMBeanException x) {
208         debugX("isInstanceOf",x);
209         throw new ReflectionException(x,x.getMessage());
210     }
211     }
212
213     Class JavaDoc findClass(String JavaDoc className, ClassLoader JavaDoc loader)
214     throws ReflectionException {
215     try {
216         if (loader == null)
217         return Class.forName(className);
218         else
219         return loader.loadClass(className);
220     } catch (ClassNotFoundException JavaDoc x) {
221         throw new ReflectionException(x,x.getMessage());
222     }
223     }
224
225     // TRACES & DEBUG
226
//---------------
227

228     private static boolean isTraceOn() {
229         return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER);
230     }
231
232     private static void trace(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
233         Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info);
234     }
235     
236     private static void trace(String JavaDoc func, String JavaDoc info) {
237         trace(dbgTag, func, info);
238     }
239     
240     private static boolean isDebugOn() {
241         return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER);
242     }
243     
244     private static void debug(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
245         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info);
246     }
247     
248     private static void debug(String JavaDoc func, String JavaDoc info) {
249         debug(dbgTag, func, info);
250     }
251     
252     private static void debugX(String JavaDoc func,Throwable JavaDoc e) {
253     if (isDebugOn()) {
254         final StringWriter JavaDoc s = new StringWriter JavaDoc();
255         e.printStackTrace(new PrintWriter JavaDoc(s));
256         final String JavaDoc stack = s.toString();
257         
258         debug(dbgTag,func,"Exception caught in "+ func+"(): "+e);
259         debug(dbgTag,func,stack);
260         // java.lang.System.err.println("**** Exception caught in "+
261
// func+"(): "+e);
262
// java.lang.System.err.println(stack);
263
}
264     }
265  }
266
Popular Tags