KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)MBeanServerDelegateImpl.java 1.14 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 package com.sun.jmx.mbeanserver;
8
9 import javax.management.ObjectName JavaDoc;
10 import javax.management.MBeanServer JavaDoc;
11 import javax.management.MBeanRegistration JavaDoc;
12 import javax.management.DynamicMBean JavaDoc;
13 import javax.management.AttributeNotFoundException JavaDoc;
14 import javax.management.MBeanException JavaDoc;
15 import javax.management.ReflectionException JavaDoc;
16 import javax.management.MBeanAttributeInfo JavaDoc;
17 import javax.management.MBeanInfo JavaDoc;
18 import javax.management.MBeanNotificationInfo JavaDoc;
19 import javax.management.JMRuntimeException JavaDoc;
20 import javax.management.InvalidAttributeValueException JavaDoc;
21 import javax.management.Attribute JavaDoc;
22 import javax.management.AttributeList JavaDoc;
23 import javax.management.RuntimeOperationsException JavaDoc;
24
25
26 import com.sun.jmx.defaults.ServiceName;
27 import com.sun.jmx.trace.Trace;
28
29 /**
30  * This class is the MBean implementation of the MBeanServerDelegate.
31  *
32  * @since 1.5
33  */

34 final class MBeanServerDelegateImpl
35     extends javax.management.MBeanServerDelegate JavaDoc
36     implements DynamicMBean JavaDoc, MBeanRegistration JavaDoc {
37
38     /** The name of this class to be used for tracing */
39     private final static String JavaDoc dbgTag = "MBeanServerDelegateImpl";
40      
41     final private static String JavaDoc[] attributeNames = new String JavaDoc[] {
42     "MBeanServerId",
43     "SpecificationName",
44     "SpecificationVersion",
45     "SpecificationVendor",
46     "ImplementationName",
47     "ImplementationVersion",
48     "ImplementationVendor"
49     };
50
51     private static final MBeanAttributeInfo JavaDoc[] attributeInfos =
52     new MBeanAttributeInfo JavaDoc[] {
53         new MBeanAttributeInfo JavaDoc("MBeanServerId","java.lang.String",
54                    "The MBean server agent identification",
55                    true,false,false),
56         new MBeanAttributeInfo JavaDoc("SpecificationName","java.lang.String",
57                    "The full name of the JMX specification "+
58                    "implemented by this product.",
59                    true,false,false),
60         new MBeanAttributeInfo JavaDoc("SpecificationVersion","java.lang.String",
61                    "The version of the JMX specification "+
62                    "implemented by this product.",
63                    true,false,false),
64         new MBeanAttributeInfo JavaDoc("SpecificationVendor","java.lang.String",
65                    "The vendor of the JMX specification "+
66                    "implemented by this product.",
67                    true,false,false),
68         new MBeanAttributeInfo JavaDoc("ImplementationName","java.lang.String",
69                    "The JMX implementation name "+
70                    "(the name of this product)",
71                    true,false,false),
72         new MBeanAttributeInfo JavaDoc("ImplementationVersion","java.lang.String",
73                    "The JMX implementation version "+
74                    "(the version of this product).",
75                    true,false,false),
76         new MBeanAttributeInfo JavaDoc("ImplementationVendor","java.lang.String",
77                    "the JMX implementation vendor "+
78                    "(the vendor of this product).",
79                    true,false,false)
80         };
81
82     private final MBeanInfo JavaDoc delegateInfo;
83     
84     public MBeanServerDelegateImpl () {
85     super();
86     delegateInfo =
87         new MBeanInfo JavaDoc("javax.management.MBeanServerDelegate",
88               "Represents the MBean server from the management "+
89               "point of view.",
90               MBeanServerDelegateImpl.attributeInfos, null,
91               null,getNotificationInfo());
92     }
93
94     final public ObjectName JavaDoc preRegister (MBeanServer JavaDoc server, ObjectName JavaDoc name)
95     throws java.lang.Exception JavaDoc {
96     if (name == null) return new ObjectName JavaDoc(ServiceName.DELEGATE);
97     else return name;
98     }
99     
100     final public void postRegister (Boolean JavaDoc registrationDone) {
101     }
102        
103     final public void preDeregister()
104     throws java.lang.Exception JavaDoc {
105     throw new IllegalArgumentException JavaDoc(
106              "The MBeanServerDelegate MBean cannot be unregistered");
107     }
108
109     final public void postDeregister() {
110     }
111
112     /**
113      * Obtains the value of a specific attribute of the MBeanServerDelegate.
114      *
115      * @param attribute The name of the attribute to be retrieved
116      *
117      * @return The value of the attribute retrieved.
118      *
119      * @exception AttributeNotFoundException
120      * @exception MBeanException
121      * Wraps a <CODE>java.lang.Exception</CODE> thrown by the
122      * MBean's getter.
123      */

124     public Object JavaDoc getAttribute(String JavaDoc attribute)
125     throws AttributeNotFoundException JavaDoc,
126            MBeanException JavaDoc, ReflectionException JavaDoc {
127     try {
128         // attribute must not be null
129
//
130
if (attribute == null)
131         throw new AttributeNotFoundException JavaDoc("null");
132
133         // Extract the requested attribute from file
134
//
135
if (attribute.equals("MBeanServerId"))
136         return getMBeanServerId();
137         else if (attribute.equals("SpecificationName"))
138         return getSpecificationName();
139         else if (attribute.equals("SpecificationVersion"))
140         return getSpecificationVersion();
141         else if (attribute.equals("SpecificationVendor"))
142         return getSpecificationVendor();
143         else if (attribute.equals("ImplementationName"))
144         return getImplementationName();
145         else if (attribute.equals("ImplementationVersion"))
146         return getImplementationVersion();
147         else if (attribute.equals("ImplementationVendor"))
148         return getImplementationVendor();
149
150         // Unknown attribute
151
//
152
else
153         throw new AttributeNotFoundException JavaDoc("null");
154
155     } catch (AttributeNotFoundException JavaDoc x) {
156         throw x;
157     } catch (JMRuntimeException JavaDoc j) {
158         throw j;
159     } catch (SecurityException JavaDoc s) {
160         throw s;
161     } catch (Exception JavaDoc x) {
162         throw new MBeanException JavaDoc(x,"Failed to get " + attribute);
163     }
164     }
165     
166     /**
167      * This method always fail since all MBeanServerDelegateMBean attributes
168      * are read-only.
169      *
170      * @param attribute The identification of the attribute to
171      * be set and the value it is to be set to.
172      *
173      * @exception AttributeNotFoundException
174      */

175     public void setAttribute(Attribute JavaDoc attribute)
176     throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc,
177            MBeanException JavaDoc, ReflectionException JavaDoc {
178     
179     // Now we will always fail:
180
// Either because the attribute is null or because it is not
181
// accessible (or does not exist).
182
//
183
final String JavaDoc attname = (attribute==null?null:attribute.getName());
184         if (attname == null) {
185         final RuntimeException JavaDoc r =
186         new IllegalArgumentException JavaDoc("Attribute name cannot be null");
187             throw new RuntimeOperationsException JavaDoc(r,
188                 "Exception occured trying to invoke the setter on the MBean");
189         }
190     
191     // This is a hack: we call getAttribute in order to generate an
192
// AttributeNotFoundException if the attribute does not exist.
193
//
194
Object JavaDoc val = getAttribute(attname);
195
196     // If we reach this point, we know that the requested attribute
197
// exists. However, since all attributes are read-only, we throw
198
// an AttributeNotFoundException.
199
//
200
throw new AttributeNotFoundException JavaDoc(attname + " not accessible");
201     }
202     
203     /**
204      * Makes it possible to get the values of several attributes of
205      * the MBeanServerDelegate.
206      *
207      * @param attributes A list of the attributes to be retrieved.
208      *
209      * @return The list of attributes retrieved.
210      *
211      */

212     public AttributeList JavaDoc getAttributes(String JavaDoc[] attributes) {
213     // If attributes is null, the get all attributes.
214
//
215
final String JavaDoc[] attn = (attributes==null?attributeNames:attributes);
216
217     // Prepare the result list.
218
//
219
final int len = attn.length;
220     final AttributeList JavaDoc list = new AttributeList JavaDoc(len);
221
222     // Get each requested attribute.
223
//
224
for (int i=0;i<len;i++) {
225         try {
226         final Attribute JavaDoc a =
227             new Attribute JavaDoc(attn[i],getAttribute(attn[i]));
228         list.add(a);
229         } catch (Exception JavaDoc x) {
230         // Skip the attribute that couldn't be obtained.
231
//
232
debug("getAttributes","Attribute " + attn[i] +
233               " not found.");
234         }
235     }
236
237     // Finally return the result.
238
//
239
return list;
240     }
241
242     /**
243      * This method always return an empty list since all
244      * MBeanServerDelegateMBean attributes are read-only.
245      *
246      * @param attributes A list of attributes: The identification of the
247      * attributes to be set and the values they are to be set to.
248      *
249      * @return The list of attributes that were set, with their new values.
250      * In fact, this method always return an empty list since all
251      * MBeanServerDelegateMBean attributes are read-only.
252      */

253     public AttributeList JavaDoc setAttributes(AttributeList JavaDoc attributes) {
254     return new AttributeList JavaDoc(0);
255     }
256     
257     /**
258      * Always fails since the MBeanServerDelegate MBean has no operation.
259      *
260      * @param actionName The name of the action to be invoked.
261      * @param params An array containing the parameters to be set when the
262      * action is invoked.
263      * @param signature An array containing the signature of the action.
264      *
265      * @return The object returned by the action, which represents
266      * the result of invoking the action on the MBean specified.
267      *
268      * @exception MBeanException Wraps a <CODE>java.lang.Exception</CODE>
269      * thrown by the MBean's invoked method.
270      * @exception ReflectionException Wraps a
271      * <CODE>java.lang.Exception</CODE> thrown while trying to invoke
272      * the method.
273      */

274     public Object JavaDoc invoke(String JavaDoc actionName, Object JavaDoc params[],
275              String JavaDoc signature[])
276     throws MBeanException JavaDoc, ReflectionException JavaDoc {
277     // Check that operation name is not null.
278
//
279
if (actionName == null) {
280         final RuntimeException JavaDoc r =
281           new IllegalArgumentException JavaDoc("Operation name cannot be null");
282             throw new RuntimeOperationsException JavaDoc(r,
283             "Exception occured trying to invoke the operation on the MBean");
284         }
285
286     throw new ReflectionException JavaDoc(
287               new NoSuchMethodException JavaDoc(actionName),
288                           "The operation with name " + actionName +
289               " could not be found");
290     }
291     
292     /**
293      * Provides the MBeanInfo describing the MBeanServerDelegate.
294      *
295      * @return The MBeanInfo describing the MBeanServerDelegate.
296      *
297      */

298     public MBeanInfo JavaDoc getMBeanInfo() {
299     return delegateInfo;
300     }
301
302     // TRACES & DEBUG
303
//---------------
304

305     private final static boolean isTraceOn() {
306         return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER);
307     }
308
309     private final static void trace(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
310         Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info);
311     }
312
313     private final static void trace(String JavaDoc func, String JavaDoc info) {
314         trace(dbgTag, func, info);
315     }
316
317     private final static boolean isDebugOn() {
318         return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER);
319     }
320
321     private final static void debug(String JavaDoc clz, String JavaDoc func, String JavaDoc info) {
322         Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info);
323     }
324
325     private final static void debug(String JavaDoc func, String JavaDoc info) {
326         debug(dbgTag, func, info);
327     }
328   
329
330 }
331
Popular Tags