0N/A/*
2362N/A * Copyright (c) 2002, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/Apackage com.sun.jmx.mbeanserver;
0N/A
0N/Aimport java.util.logging.Level;
0N/A
0N/Aimport javax.management.Attribute;
0N/Aimport javax.management.AttributeList;
0N/Aimport javax.management.AttributeNotFoundException;
0N/Aimport javax.management.DynamicMBean;
0N/Aimport javax.management.InvalidAttributeValueException;
0N/Aimport javax.management.JMRuntimeException;
0N/Aimport javax.management.MBeanAttributeInfo;
0N/Aimport javax.management.MBeanException;
0N/Aimport javax.management.MBeanInfo;
0N/Aimport javax.management.MBeanRegistration;
0N/Aimport javax.management.MBeanServer;
0N/Aimport javax.management.MBeanServerDelegate;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.ReflectionException;
0N/Aimport javax.management.RuntimeOperationsException;
0N/A
0N/Aimport static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER;
0N/A
0N/A/**
0N/A * This class is the MBean implementation of the MBeanServerDelegate.
0N/A *
0N/A * @since 1.5
0N/A */
0N/Afinal class MBeanServerDelegateImpl
0N/A extends MBeanServerDelegate
0N/A implements DynamicMBean, MBeanRegistration {
0N/A
0N/A final private static String[] attributeNames = new String[] {
0N/A "MBeanServerId",
0N/A "SpecificationName",
0N/A "SpecificationVersion",
0N/A "SpecificationVendor",
0N/A "ImplementationName",
0N/A "ImplementationVersion",
0N/A "ImplementationVendor"
0N/A };
0N/A
0N/A private static final MBeanAttributeInfo[] attributeInfos =
0N/A new MBeanAttributeInfo[] {
0N/A new MBeanAttributeInfo("MBeanServerId","java.lang.String",
0N/A "The MBean server agent identification",
0N/A true,false,false),
0N/A new MBeanAttributeInfo("SpecificationName","java.lang.String",
0N/A "The full name of the JMX specification "+
0N/A "implemented by this product.",
0N/A true,false,false),
0N/A new MBeanAttributeInfo("SpecificationVersion","java.lang.String",
0N/A "The version of the JMX specification "+
0N/A "implemented by this product.",
0N/A true,false,false),
0N/A new MBeanAttributeInfo("SpecificationVendor","java.lang.String",
0N/A "The vendor of the JMX specification "+
0N/A "implemented by this product.",
0N/A true,false,false),
0N/A new MBeanAttributeInfo("ImplementationName","java.lang.String",
0N/A "The JMX implementation name "+
0N/A "(the name of this product)",
0N/A true,false,false),
0N/A new MBeanAttributeInfo("ImplementationVersion","java.lang.String",
0N/A "The JMX implementation version "+
0N/A "(the version of this product).",
0N/A true,false,false),
0N/A new MBeanAttributeInfo("ImplementationVendor","java.lang.String",
0N/A "the JMX implementation vendor "+
0N/A "(the vendor of this product).",
0N/A true,false,false)
0N/A };
0N/A
0N/A private final MBeanInfo delegateInfo;
0N/A
0N/A public MBeanServerDelegateImpl () {
0N/A super();
0N/A delegateInfo =
0N/A new MBeanInfo("javax.management.MBeanServerDelegate",
0N/A "Represents the MBean server from the management "+
0N/A "point of view.",
0N/A MBeanServerDelegateImpl.attributeInfos, null,
0N/A null,getNotificationInfo());
0N/A }
0N/A
0N/A final public ObjectName preRegister (MBeanServer server, ObjectName name)
0N/A throws java.lang.Exception {
0N/A if (name == null) return DELEGATE_NAME;
0N/A else return name;
0N/A }
0N/A
0N/A final public void postRegister (Boolean registrationDone) {
0N/A }
0N/A
0N/A final public void preDeregister()
0N/A throws java.lang.Exception {
0N/A throw new IllegalArgumentException(
0N/A "The MBeanServerDelegate MBean cannot be unregistered");
0N/A }
0N/A
0N/A final public void postDeregister() {
0N/A }
0N/A
0N/A /**
0N/A * Obtains the value of a specific attribute of the MBeanServerDelegate.
0N/A *
0N/A * @param attribute The name of the attribute to be retrieved
0N/A *
0N/A * @return The value of the attribute retrieved.
0N/A *
0N/A * @exception AttributeNotFoundException
0N/A * @exception MBeanException
0N/A * Wraps a <CODE>java.lang.Exception</CODE> thrown by the
0N/A * MBean's getter.
0N/A */
0N/A public Object getAttribute(String attribute)
0N/A throws AttributeNotFoundException,
0N/A MBeanException, ReflectionException {
0N/A try {
0N/A // attribute must not be null
0N/A //
0N/A if (attribute == null)
0N/A throw new AttributeNotFoundException("null");
0N/A
0N/A // Extract the requested attribute from file
0N/A //
0N/A if (attribute.equals("MBeanServerId"))
0N/A return getMBeanServerId();
0N/A else if (attribute.equals("SpecificationName"))
0N/A return getSpecificationName();
0N/A else if (attribute.equals("SpecificationVersion"))
0N/A return getSpecificationVersion();
0N/A else if (attribute.equals("SpecificationVendor"))
0N/A return getSpecificationVendor();
0N/A else if (attribute.equals("ImplementationName"))
0N/A return getImplementationName();
0N/A else if (attribute.equals("ImplementationVersion"))
0N/A return getImplementationVersion();
0N/A else if (attribute.equals("ImplementationVendor"))
0N/A return getImplementationVendor();
0N/A
0N/A // Unknown attribute
0N/A //
0N/A else
0N/A throw new AttributeNotFoundException("null");
0N/A
0N/A } catch (AttributeNotFoundException x) {
0N/A throw x;
0N/A } catch (JMRuntimeException j) {
0N/A throw j;
0N/A } catch (SecurityException s) {
0N/A throw s;
0N/A } catch (Exception x) {
0N/A throw new MBeanException(x,"Failed to get " + attribute);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This method always fail since all MBeanServerDelegateMBean attributes
0N/A * are read-only.
0N/A *
0N/A * @param attribute The identification of the attribute to
0N/A * be set and the value it is to be set to.
0N/A *
0N/A * @exception AttributeNotFoundException
0N/A */
0N/A public void setAttribute(Attribute attribute)
0N/A throws AttributeNotFoundException, InvalidAttributeValueException,
0N/A MBeanException, ReflectionException {
0N/A
0N/A // Now we will always fail:
0N/A // Either because the attribute is null or because it is not
0N/A // accessible (or does not exist).
0N/A //
0N/A final String attname = (attribute==null?null:attribute.getName());
0N/A if (attname == null) {
0N/A final RuntimeException r =
0N/A new IllegalArgumentException("Attribute name cannot be null");
0N/A throw new RuntimeOperationsException(r,
0N/A "Exception occurred trying to invoke the setter on the MBean");
0N/A }
0N/A
0N/A // This is a hack: we call getAttribute in order to generate an
0N/A // AttributeNotFoundException if the attribute does not exist.
0N/A //
0N/A Object val = getAttribute(attname);
0N/A
0N/A // If we reach this point, we know that the requested attribute
0N/A // exists. However, since all attributes are read-only, we throw
0N/A // an AttributeNotFoundException.
0N/A //
0N/A throw new AttributeNotFoundException(attname + " not accessible");
0N/A }
0N/A
0N/A /**
0N/A * Makes it possible to get the values of several attributes of
0N/A * the MBeanServerDelegate.
0N/A *
0N/A * @param attributes A list of the attributes to be retrieved.
0N/A *
0N/A * @return The list of attributes retrieved.
0N/A *
0N/A */
0N/A public AttributeList getAttributes(String[] attributes) {
0N/A // If attributes is null, the get all attributes.
0N/A //
0N/A final String[] attn = (attributes==null?attributeNames:attributes);
0N/A
0N/A // Prepare the result list.
0N/A //
0N/A final int len = attn.length;
0N/A final AttributeList list = new AttributeList(len);
0N/A
0N/A // Get each requested attribute.
0N/A //
0N/A for (int i=0;i<len;i++) {
0N/A try {
0N/A final Attribute a =
0N/A new Attribute(attn[i],getAttribute(attn[i]));
0N/A list.add(a);
0N/A } catch (Exception x) {
0N/A // Skip the attribute that couldn't be obtained.
0N/A //
0N/A if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) {
0N/A MBEANSERVER_LOGGER.logp(Level.FINEST,
0N/A MBeanServerDelegateImpl.class.getName(),
0N/A "getAttributes",
0N/A "Attribute " + attn[i] + " not found");
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Finally return the result.
0N/A //
0N/A return list;
0N/A }
0N/A
0N/A /**
0N/A * This method always return an empty list since all
0N/A * MBeanServerDelegateMBean attributes are read-only.
0N/A *
0N/A * @param attributes A list of attributes: The identification of the
0N/A * attributes to be set and the values they are to be set to.
0N/A *
0N/A * @return The list of attributes that were set, with their new values.
0N/A * In fact, this method always return an empty list since all
0N/A * MBeanServerDelegateMBean attributes are read-only.
0N/A */
0N/A public AttributeList setAttributes(AttributeList attributes) {
0N/A return new AttributeList(0);
0N/A }
0N/A
0N/A /**
0N/A * Always fails since the MBeanServerDelegate MBean has no operation.
0N/A *
0N/A * @param actionName The name of the action to be invoked.
0N/A * @param params An array containing the parameters to be set when the
0N/A * action is invoked.
0N/A * @param signature An array containing the signature of the action.
0N/A *
0N/A * @return The object returned by the action, which represents
0N/A * the result of invoking the action on the MBean specified.
0N/A *
0N/A * @exception MBeanException Wraps a <CODE>java.lang.Exception</CODE>
0N/A * thrown by the MBean's invoked method.
0N/A * @exception ReflectionException Wraps a
0N/A * <CODE>java.lang.Exception</CODE> thrown while trying to invoke
0N/A * the method.
0N/A */
0N/A public Object invoke(String actionName, Object params[],
0N/A String signature[])
0N/A throws MBeanException, ReflectionException {
0N/A // Check that operation name is not null.
0N/A //
0N/A if (actionName == null) {
0N/A final RuntimeException r =
0N/A new IllegalArgumentException("Operation name cannot be null");
0N/A throw new RuntimeOperationsException(r,
0N/A "Exception occurred trying to invoke the operation on the MBean");
0N/A }
0N/A
0N/A throw new ReflectionException(
0N/A new NoSuchMethodException(actionName),
0N/A "The operation with name " + actionName +
0N/A " could not be found");
0N/A }
0N/A
0N/A /**
0N/A * Provides the MBeanInfo describing the MBeanServerDelegate.
0N/A *
0N/A * @return The MBeanInfo describing the MBeanServerDelegate.
0N/A *
0N/A */
0N/A public MBeanInfo getMBeanInfo() {
0N/A return delegateInfo;
0N/A }
0N/A
0N/A}