Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/jmx/mbeanserver/MBeanServerDelegateImpl.java
38924 views
/*1* Copyright (c) 2002, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package com.sun.jmx.mbeanserver;2526import java.util.logging.Level;2728import javax.management.Attribute;29import javax.management.AttributeList;30import javax.management.AttributeNotFoundException;31import javax.management.DynamicMBean;32import javax.management.InvalidAttributeValueException;33import javax.management.JMRuntimeException;34import javax.management.MBeanAttributeInfo;35import javax.management.MBeanException;36import javax.management.MBeanInfo;37import javax.management.MBeanRegistration;38import javax.management.MBeanServer;39import javax.management.MBeanServerDelegate;40import javax.management.ObjectName;41import javax.management.ReflectionException;42import javax.management.RuntimeOperationsException;4344import static com.sun.jmx.defaults.JmxProperties.MBEANSERVER_LOGGER;4546/**47* This class is the MBean implementation of the MBeanServerDelegate.48*49* @since 1.550*/51final class MBeanServerDelegateImpl52extends MBeanServerDelegate53implements DynamicMBean, MBeanRegistration {5455final private static String[] attributeNames = new String[] {56"MBeanServerId",57"SpecificationName",58"SpecificationVersion",59"SpecificationVendor",60"ImplementationName",61"ImplementationVersion",62"ImplementationVendor"63};6465private static final MBeanAttributeInfo[] attributeInfos =66new MBeanAttributeInfo[] {67new MBeanAttributeInfo("MBeanServerId","java.lang.String",68"The MBean server agent identification",69true,false,false),70new MBeanAttributeInfo("SpecificationName","java.lang.String",71"The full name of the JMX specification "+72"implemented by this product.",73true,false,false),74new MBeanAttributeInfo("SpecificationVersion","java.lang.String",75"The version of the JMX specification "+76"implemented by this product.",77true,false,false),78new MBeanAttributeInfo("SpecificationVendor","java.lang.String",79"The vendor of the JMX specification "+80"implemented by this product.",81true,false,false),82new MBeanAttributeInfo("ImplementationName","java.lang.String",83"The JMX implementation name "+84"(the name of this product)",85true,false,false),86new MBeanAttributeInfo("ImplementationVersion","java.lang.String",87"The JMX implementation version "+88"(the version of this product).",89true,false,false),90new MBeanAttributeInfo("ImplementationVendor","java.lang.String",91"the JMX implementation vendor "+92"(the vendor of this product).",93true,false,false)94};9596private final MBeanInfo delegateInfo;9798public MBeanServerDelegateImpl () {99super();100delegateInfo =101new MBeanInfo("javax.management.MBeanServerDelegate",102"Represents the MBean server from the management "+103"point of view.",104MBeanServerDelegateImpl.attributeInfos, null,105null,getNotificationInfo());106}107108final public ObjectName preRegister (MBeanServer server, ObjectName name)109throws java.lang.Exception {110if (name == null) return DELEGATE_NAME;111else return name;112}113114final public void postRegister (Boolean registrationDone) {115}116117final public void preDeregister()118throws java.lang.Exception {119throw new IllegalArgumentException(120"The MBeanServerDelegate MBean cannot be unregistered");121}122123final public void postDeregister() {124}125126/**127* Obtains the value of a specific attribute of the MBeanServerDelegate.128*129* @param attribute The name of the attribute to be retrieved130*131* @return The value of the attribute retrieved.132*133* @exception AttributeNotFoundException134* @exception MBeanException135* Wraps a <CODE>java.lang.Exception</CODE> thrown by the136* MBean's getter.137*/138public Object getAttribute(String attribute)139throws AttributeNotFoundException,140MBeanException, ReflectionException {141try {142// attribute must not be null143//144if (attribute == null)145throw new AttributeNotFoundException("null");146147// Extract the requested attribute from file148//149if (attribute.equals("MBeanServerId"))150return getMBeanServerId();151else if (attribute.equals("SpecificationName"))152return getSpecificationName();153else if (attribute.equals("SpecificationVersion"))154return getSpecificationVersion();155else if (attribute.equals("SpecificationVendor"))156return getSpecificationVendor();157else if (attribute.equals("ImplementationName"))158return getImplementationName();159else if (attribute.equals("ImplementationVersion"))160return getImplementationVersion();161else if (attribute.equals("ImplementationVendor"))162return getImplementationVendor();163164// Unknown attribute165//166else167throw new AttributeNotFoundException("null");168169} catch (AttributeNotFoundException x) {170throw x;171} catch (JMRuntimeException j) {172throw j;173} catch (SecurityException s) {174throw s;175} catch (Exception x) {176throw new MBeanException(x,"Failed to get " + attribute);177}178}179180/**181* This method always fail since all MBeanServerDelegateMBean attributes182* are read-only.183*184* @param attribute The identification of the attribute to185* be set and the value it is to be set to.186*187* @exception AttributeNotFoundException188*/189public void setAttribute(Attribute attribute)190throws AttributeNotFoundException, InvalidAttributeValueException,191MBeanException, ReflectionException {192193// Now we will always fail:194// Either because the attribute is null or because it is not195// accessible (or does not exist).196//197final String attname = (attribute==null?null:attribute.getName());198if (attname == null) {199final RuntimeException r =200new IllegalArgumentException("Attribute name cannot be null");201throw new RuntimeOperationsException(r,202"Exception occurred trying to invoke the setter on the MBean");203}204205// This is a hack: we call getAttribute in order to generate an206// AttributeNotFoundException if the attribute does not exist.207//208Object val = getAttribute(attname);209210// If we reach this point, we know that the requested attribute211// exists. However, since all attributes are read-only, we throw212// an AttributeNotFoundException.213//214throw new AttributeNotFoundException(attname + " not accessible");215}216217/**218* Makes it possible to get the values of several attributes of219* the MBeanServerDelegate.220*221* @param attributes A list of the attributes to be retrieved.222*223* @return The list of attributes retrieved.224*225*/226public AttributeList getAttributes(String[] attributes) {227// If attributes is null, the get all attributes.228//229final String[] attn = (attributes==null?attributeNames:attributes);230231// Prepare the result list.232//233final int len = attn.length;234final AttributeList list = new AttributeList(len);235236// Get each requested attribute.237//238for (int i=0;i<len;i++) {239try {240final Attribute a =241new Attribute(attn[i],getAttribute(attn[i]));242list.add(a);243} catch (Exception x) {244// Skip the attribute that couldn't be obtained.245//246if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) {247MBEANSERVER_LOGGER.logp(Level.FINEST,248MBeanServerDelegateImpl.class.getName(),249"getAttributes",250"Attribute " + attn[i] + " not found");251}252}253}254255// Finally return the result.256//257return list;258}259260/**261* This method always return an empty list since all262* MBeanServerDelegateMBean attributes are read-only.263*264* @param attributes A list of attributes: The identification of the265* attributes to be set and the values they are to be set to.266*267* @return The list of attributes that were set, with their new values.268* In fact, this method always return an empty list since all269* MBeanServerDelegateMBean attributes are read-only.270*/271public AttributeList setAttributes(AttributeList attributes) {272return new AttributeList(0);273}274275/**276* Always fails since the MBeanServerDelegate MBean has no operation.277*278* @param actionName The name of the action to be invoked.279* @param params An array containing the parameters to be set when the280* action is invoked.281* @param signature An array containing the signature of the action.282*283* @return The object returned by the action, which represents284* the result of invoking the action on the MBean specified.285*286* @exception MBeanException Wraps a <CODE>java.lang.Exception</CODE>287* thrown by the MBean's invoked method.288* @exception ReflectionException Wraps a289* <CODE>java.lang.Exception</CODE> thrown while trying to invoke290* the method.291*/292public Object invoke(String actionName, Object params[],293String signature[])294throws MBeanException, ReflectionException {295// Check that operation name is not null.296//297if (actionName == null) {298final RuntimeException r =299new IllegalArgumentException("Operation name cannot be null");300throw new RuntimeOperationsException(r,301"Exception occurred trying to invoke the operation on the MBean");302}303304throw new ReflectionException(305new NoSuchMethodException(actionName),306"The operation with name " + actionName +307" could not be found");308}309310/**311* Provides the MBeanInfo describing the MBeanServerDelegate.312*313* @return The MBeanInfo describing the MBeanServerDelegate.314*315*/316public MBeanInfo getMBeanInfo() {317return delegateInfo;318}319320}321322323