KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)DynamicMetaDataImpl.java 1.29 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 class DynamicMetaDataImpl extends BaseMetaDataImpl {
30
31     /** The name of this class to be used for tracing */
32     private final static String JavaDoc dbgTag = "DynamicMetaDataImpl";
33     
34
35     /**
36      * Creates a Metadata Service.
37      */

38     public DynamicMetaDataImpl() {
39     // ------------------------------
40
// ------------------------------
41
}
42
43     
44     /**
45      * This methods tests if the MBean is JMX compliant
46      */

47     public void testCompliance(Class JavaDoc c)
48     throws NotCompliantMBeanException {
49     // ------------------------------
50
// ------------------------------
51
if (DynamicMBean.class.isAssignableFrom(c)) return;
52     throw new NotCompliantMBeanException(
53            "Only DynamicMBeans are supported by this implementation");
54     }
55
56   
57     //---------------------------------------------------------------------
58
//
59
// From the MetaData interface
60
//
61
//---------------------------------------------------------------------
62

63     public MBeanInfo getMBeanInfo(Object JavaDoc moi)
64     throws IntrospectionException {
65
66     try {
67         return (MBeanInfo)
68         ((javax.management.DynamicMBean JavaDoc)moi).getMBeanInfo();
69     } catch (RuntimeMBeanException r) {
70         throw r;
71     } catch (RuntimeErrorException r) {
72         throw r;
73     } catch (RuntimeException JavaDoc r) {
74         debugX("getMBeanInfo",r);
75         throw new RuntimeMBeanException((RuntimeException JavaDoc)r,
76            "Runtime Exception thrown by getMBeanInfo method of Dynamic MBean");
77     } catch (Error JavaDoc e ) {
78         debugX("getMBeanInfo",e);
79         throw new RuntimeErrorException((Error JavaDoc)e,
80                       "Error thrown by getMBeanInfo method of Dynamic MBean");
81     }
82
83     }
84
85     public Object JavaDoc getAttribute(Object JavaDoc instance, String JavaDoc attribute)
86     throws MBeanException, AttributeNotFoundException,
87            ReflectionException {
88         if (attribute == null) {
89         final RuntimeException JavaDoc r =
90         new IllegalArgumentException JavaDoc("Attribute name cannot be null");
91             throw new RuntimeOperationsException(r,
92                 "Exception occured trying to invoke the getter on the MBean");
93         }
94  
95     try {
96         return ((javax.management.DynamicMBean JavaDoc)instance).
97         getAttribute(attribute);
98     } catch (RuntimeOperationsException r) {
99         throw r;
100     } catch (RuntimeErrorException r) {
101         throw r;
102     } catch (RuntimeException JavaDoc e) {
103         debugX("getAttribute",e);
104         throw new RuntimeMBeanException(e, "RuntimeException" +
105             " thrown by the getAttribute method of the DynamicMBean" +
106             " for the attribute " + attribute);
107     } catch (Error JavaDoc e) {
108         debugX("getAttribute",e);
109         throw new RuntimeErrorException((Error JavaDoc)e, "Error" +
110                     " thrown by the getAttribute method of the DynamicMBean "+
111                     " for the attribute " + attribute);
112     }
113     }
114
115     public AttributeList getAttributes(Object JavaDoc instance, String JavaDoc[] attributes)
116     throws ReflectionException {
117
118         if (attributes == null) {
119             throw new RuntimeOperationsException(new
120         IllegalArgumentException JavaDoc("Attributes cannot be null"),
121                 "Exception occured trying to invoke the getter on the MBean");
122         }
123
124     try {
125         return ((javax.management.DynamicMBean JavaDoc)instance).
126         getAttributes(attributes);
127     } catch (RuntimeOperationsException r) {
128         throw r;
129     } catch (RuntimeErrorException r) {
130         throw r;
131     } catch (RuntimeException JavaDoc e) {
132         debugX("getAttributes",e);
133         throw new RuntimeOperationsException(e, "RuntimeException" +
134                    " thrown by the getAttributes method of the DynamicMBean");
135     } catch (Error JavaDoc e) {
136         debugX("getAttributes",e);
137         throw new RuntimeErrorException((Error JavaDoc)e, "Error" +
138                    " thrown by the getAttributes method of the DynamicMBean");
139     }
140     }
141
142
143     public AttributeList setAttributes(Object JavaDoc instance,
144                        AttributeList attributes)
145     throws ReflectionException {
146
147     try {
148         return ((javax.management.DynamicMBean JavaDoc)instance).
149         setAttributes(attributes);
150     } catch (RuntimeOperationsException r) {
151         throw r;
152     } catch (RuntimeErrorException r) {
153         throw r;
154     } catch (RuntimeException JavaDoc e) {
155         debugX("setAttributes",e);
156         throw new RuntimeOperationsException(e,
157              "RuntimeException thrown by the setAttributes " +
158              "method of the Dynamic MBean");
159     } catch (Error JavaDoc e) {
160         debugX("setAttributes",e);
161         throw new RuntimeErrorException((Error JavaDoc)e,
162                       "Error thrown by the setAttributes " +
163               "method of the Dynamic MBean");
164     }
165     }
166     
167
168     public Object JavaDoc setAttribute(Object JavaDoc instance, Attribute attribute)
169     throws AttributeNotFoundException, InvalidAttributeValueException,
170            MBeanException, ReflectionException {
171
172         if (attribute == null) {
173         final RuntimeException JavaDoc r =
174         new IllegalArgumentException JavaDoc("Attribute name cannot be null");
175             throw new RuntimeOperationsException(r,
176                 "Exception occured trying to invoke the setter on the MBean");
177         }
178  
179     try {
180         ((javax.management.DynamicMBean JavaDoc)instance).
181         setAttribute(attribute);
182         return attribute.getValue();
183     } catch (RuntimeOperationsException r) {
184         throw r;
185     } catch (RuntimeErrorException r) {
186         throw r;
187     } catch (RuntimeException JavaDoc e) {
188         debugX("setAttribute",e);
189         throw new RuntimeMBeanException(e,
190               "RuntimeException thrown by the setAttribute " +
191               attribute + "method of the Dynamic MBean");
192     } catch (Error JavaDoc e) {
193         debugX("setAttribute",e);
194         throw new RuntimeErrorException((Error JavaDoc)e,
195                       "Error thrown by the setAttribute " + attribute +
196               "method of the Dynamic MBean");
197     }
198     }
199
200
201     public Object JavaDoc invoke(Object JavaDoc instance, String JavaDoc operationName,
202              Object JavaDoc params[], String JavaDoc signature[])
203     throws MBeanException, ReflectionException {
204
205         if (operationName == null) {
206         final RuntimeException JavaDoc r =
207           new IllegalArgumentException JavaDoc("Operation name cannot be null");
208             throw new RuntimeOperationsException(r,
209               "Exception occured trying to invoke the operation on the MBean");
210         }
211
212     try {
213         return (((javax.management.DynamicMBean JavaDoc)instance).
214             invoke(operationName, params, signature));
215     } catch (ReflectionException e) {
216         debugX("invoke",e);
217         throw e;
218     } catch (MBeanException e) {
219         debugX("invoke",e);
220         throw e;
221     } catch (RuntimeOperationsException r) {
222         throw r;
223     } catch (RuntimeErrorException r) {
224         throw r;
225     } catch (RuntimeException JavaDoc e) {
226         debugX("invoke",e);
227         throw new RuntimeMBeanException(e, "RuntimeException" +
228               " thrown by the invoke method of the Dynamic MBean");
229     } catch (Error JavaDoc e) {
230         debugX("invoke",e);
231         throw new RuntimeErrorException((Error JavaDoc)e, "Error" +
232                      " thrown by the invoke method of the Dynamic MBean");
233     }
234     }
235
236
237     // TRACES & DEBUG
238
//---------------
239

240     private static boolean isTraceOn() {
241         return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER);
242     }
243
244     private static void trace(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
245         Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info);
246     }
247     
248     private static void trace(String JavaDoc func, String JavaDoc info) {
249         trace(dbgTag, func, info);
250     }
251     
252     private static boolean isDebugOn() {
253         return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER);
254     }
255     
256     private static void debug(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
257         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info);
258     }
259     
260     private static void debug(String JavaDoc func, String JavaDoc info) {
261         debug(dbgTag, func, info);
262     }
263     
264     private static void debugX(String JavaDoc func,Throwable JavaDoc e) {
265     if (isDebugOn()) {
266         final StringWriter JavaDoc s = new StringWriter JavaDoc();
267         e.printStackTrace(new PrintWriter JavaDoc(s));
268         final String JavaDoc stack = s.toString();
269         
270         debug(dbgTag,func,"Exception caught in "+ func+"(): "+e);
271         debug(dbgTag,func,stack);
272         // java.lang.System.err.println("**** Exception caught in "+
273
// func+"(): "+e);
274
// java.lang.System.err.println(stack);
275
}
276     }
277  }
278
Popular Tags