blob: 49238c71ef8ef70585df470b72dccfc1eace5865 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25package com.sun.jmx.mbeanserver;
26
27import java.util.logging.Level;
28
29import javax.management.Attribute;
30import javax.management.AttributeList;
31import javax.management.AttributeNotFoundException;
32import javax.management.DynamicMBean;
33import javax.management.InvalidAttributeValueException;
34import javax.management.JMRuntimeException;
35import javax.management.MBeanAttributeInfo;
36import javax.management.MBeanException;
37import javax.management.MBeanInfo;
38import javax.management.MBeanRegistration;
39import javax.management.MBeanServer;
40import javax.management.MBeanServerDelegate;
41import javax.management.ObjectName;
42import javax.management.ReflectionException;
43import javax.management.RuntimeOperationsException;
44
45import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER;
46
47/**
48 * This class is the MBean implementation of the MBeanServerDelegate.
49 *
50 * @since 1.5
51 */
52final class MBeanServerDelegateImpl
53 extends MBeanServerDelegate
54 implements DynamicMBean, MBeanRegistration {
55
56 final private static String[] attributeNames = new String[] {
57 "MBeanServerId",
58 "SpecificationName",
59 "SpecificationVersion",
60 "SpecificationVendor",
61 "ImplementationName",
62 "ImplementationVersion",
63 "ImplementationVendor"
64 };
65
66 private static final MBeanAttributeInfo[] attributeInfos =
67 new MBeanAttributeInfo[] {
68 new MBeanAttributeInfo("MBeanServerId","java.lang.String",
69 "The MBean server agent identification",
70 true,false,false),
71 new MBeanAttributeInfo("SpecificationName","java.lang.String",
72 "The full name of the JMX specification "+
73 "implemented by this product.",
74 true,false,false),
75 new MBeanAttributeInfo("SpecificationVersion","java.lang.String",
76 "The version of the JMX specification "+
77 "implemented by this product.",
78 true,false,false),
79 new MBeanAttributeInfo("SpecificationVendor","java.lang.String",
80 "The vendor of the JMX specification "+
81 "implemented by this product.",
82 true,false,false),
83 new MBeanAttributeInfo("ImplementationName","java.lang.String",
84 "The JMX implementation name "+
85 "(the name of this product)",
86 true,false,false),
87 new MBeanAttributeInfo("ImplementationVersion","java.lang.String",
88 "The JMX implementation version "+
89 "(the version of this product).",
90 true,false,false),
91 new MBeanAttributeInfo("ImplementationVendor","java.lang.String",
92 "the JMX implementation vendor "+
93 "(the vendor of this product).",
94 true,false,false)
95 };
96
97 private final MBeanInfo delegateInfo;
98
99 public MBeanServerDelegateImpl () {
100 super();
101 delegateInfo =
102 new MBeanInfo("javax.management.MBeanServerDelegate",
103 "Represents the MBean server from the management "+
104 "point of view.",
105 MBeanServerDelegateImpl.attributeInfos, null,
106 null,getNotificationInfo());
107 }
108
109 final public ObjectName preRegister (MBeanServer server, ObjectName name)
110 throws java.lang.Exception {
111 if (name == null) return DELEGATE_NAME;
112 else return name;
113 }
114
115 final public void postRegister (Boolean registrationDone) {
116 }
117
118 final public void preDeregister()
119 throws java.lang.Exception {
120 throw new IllegalArgumentException(
121 "The MBeanServerDelegate MBean cannot be unregistered");
122 }
123
124 final public void postDeregister() {
125 }
126
127 /**
128 * Obtains the value of a specific attribute of the MBeanServerDelegate.
129 *
130 * @param attribute The name of the attribute to be retrieved
131 *
132 * @return The value of the attribute retrieved.
133 *
134 * @exception AttributeNotFoundException
135 * @exception MBeanException
136 * Wraps a <CODE>java.lang.Exception</CODE> thrown by the
137 * MBean's getter.
138 */
139 public Object getAttribute(String attribute)
140 throws AttributeNotFoundException,
141 MBeanException, ReflectionException {
142 try {
143 // attribute must not be null
144 //
145 if (attribute == null)
146 throw new AttributeNotFoundException("null");
147
148 // Extract the requested attribute from file
149 //
150 if (attribute.equals("MBeanServerId"))
151 return getMBeanServerId();
152 else if (attribute.equals("SpecificationName"))
153 return getSpecificationName();
154 else if (attribute.equals("SpecificationVersion"))
155 return getSpecificationVersion();
156 else if (attribute.equals("SpecificationVendor"))
157 return getSpecificationVendor();
158 else if (attribute.equals("ImplementationName"))
159 return getImplementationName();
160 else if (attribute.equals("ImplementationVersion"))
161 return getImplementationVersion();
162 else if (attribute.equals("ImplementationVendor"))
163 return getImplementationVendor();
164
165 // Unknown attribute
166 //
167 else
168 throw new AttributeNotFoundException("null");
169
170 } catch (AttributeNotFoundException x) {
171 throw x;
172 } catch (JMRuntimeException j) {
173 throw j;
174 } catch (SecurityException s) {
175 throw s;
176 } catch (Exception x) {
177 throw new MBeanException(x,"Failed to get " + attribute);
178 }
179 }
180
181 /**
182 * This method always fail since all MBeanServerDelegateMBean attributes
183 * are read-only.
184 *
185 * @param attribute The identification of the attribute to
186 * be set and the value it is to be set to.
187 *
188 * @exception AttributeNotFoundException
189 */
190 public void setAttribute(Attribute attribute)
191 throws AttributeNotFoundException, InvalidAttributeValueException,
192 MBeanException, ReflectionException {
193
194 // Now we will always fail:
195 // Either because the attribute is null or because it is not
196 // accessible (or does not exist).
197 //
198 final String attname = (attribute==null?null:attribute.getName());
199 if (attname == null) {
200 final RuntimeException r =
201 new IllegalArgumentException("Attribute name cannot be null");
202 throw new RuntimeOperationsException(r,
203 "Exception occurred trying to invoke the setter on the MBean");
204 }
205
206 // This is a hack: we call getAttribute in order to generate an
207 // AttributeNotFoundException if the attribute does not exist.
208 //
209 Object val = getAttribute(attname);
210
211 // If we reach this point, we know that the requested attribute
212 // exists. However, since all attributes are read-only, we throw
213 // an AttributeNotFoundException.
214 //
215 throw new AttributeNotFoundException(attname + " not accessible");
216 }
217
218 /**
219 * Makes it possible to get the values of several attributes of
220 * the MBeanServerDelegate.
221 *
222 * @param attributes A list of the attributes to be retrieved.
223 *
224 * @return The list of attributes retrieved.
225 *
226 */
227 public AttributeList getAttributes(String[] attributes) {
228 // If attributes is null, the get all attributes.
229 //
230 final String[] attn = (attributes==null?attributeNames:attributes);
231
232 // Prepare the result list.
233 //
234 final int len = attn.length;
235 final AttributeList list = new AttributeList(len);
236
237 // Get each requested attribute.
238 //
239 for (int i=0;i<len;i++) {
240 try {
241 final Attribute a =
242 new Attribute(attn[i],getAttribute(attn[i]));
243 list.add(a);
244 } catch (Exception x) {
245 // Skip the attribute that couldn't be obtained.
246 //
247 if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) {
248 MBEANSERVER_LOGGER.logp(Level.FINEST,
249 MBeanServerDelegateImpl.class.getName(),
250 "getAttributes",
251 "Attribute " + attn[i] + " not found");
252 }
253 }
254 }
255
256 // Finally return the result.
257 //
258 return list;
259 }
260
261 /**
262 * This method always return an empty list since all
263 * MBeanServerDelegateMBean attributes are read-only.
264 *
265 * @param attributes A list of attributes: The identification of the
266 * attributes to be set and the values they are to be set to.
267 *
268 * @return The list of attributes that were set, with their new values.
269 * In fact, this method always return an empty list since all
270 * MBeanServerDelegateMBean attributes are read-only.
271 */
272 public AttributeList setAttributes(AttributeList attributes) {
273 return new AttributeList(0);
274 }
275
276 /**
277 * Always fails since the MBeanServerDelegate MBean has no operation.
278 *
279 * @param actionName The name of the action to be invoked.
280 * @param params An array containing the parameters to be set when the
281 * action is invoked.
282 * @param signature An array containing the signature of the action.
283 *
284 * @return The object returned by the action, which represents
285 * the result of invoking the action on the MBean specified.
286 *
287 * @exception MBeanException Wraps a <CODE>java.lang.Exception</CODE>
288 * thrown by the MBean's invoked method.
289 * @exception ReflectionException Wraps a
290 * <CODE>java.lang.Exception</CODE> thrown while trying to invoke
291 * the method.
292 */
293 public Object invoke(String actionName, Object params[],
294 String signature[])
295 throws MBeanException, ReflectionException {
296 // Check that operation name is not null.
297 //
298 if (actionName == null) {
299 final RuntimeException r =
300 new IllegalArgumentException("Operation name cannot be null");
301 throw new RuntimeOperationsException(r,
302 "Exception occurred trying to invoke the operation on the MBean");
303 }
304
305 throw new ReflectionException(
306 new NoSuchMethodException(actionName),
307 "The operation with name " + actionName +
308 " could not be found");
309 }
310
311 /**
312 * Provides the MBeanInfo describing the MBeanServerDelegate.
313 *
314 * @return The MBeanInfo describing the MBeanServerDelegate.
315 *
316 */
317 public MBeanInfo getMBeanInfo() {
318 return delegateInfo;
319 }
320
321}