Path: blob/master/src/java.management/share/classes/com/sun/jmx/mbeanserver/MBeanServerDelegateImpl.java
41161 views
/*1* Copyright (c) 2002, 2017, 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.lang.System.Logger.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.TRACE)) {247MBEANSERVER_LOGGER.log(Level.TRACE,248"Attribute " + attn[i] + " not found");249}250}251}252253// Finally return the result.254//255return list;256}257258/**259* This method always return an empty list since all260* MBeanServerDelegateMBean attributes are read-only.261*262* @param attributes A list of attributes: The identification of the263* attributes to be set and the values they are to be set to.264*265* @return The list of attributes that were set, with their new values.266* In fact, this method always return an empty list since all267* MBeanServerDelegateMBean attributes are read-only.268*/269public AttributeList setAttributes(AttributeList attributes) {270return new AttributeList(0);271}272273/**274* Always fails since the MBeanServerDelegate MBean has no operation.275*276* @param actionName The name of the action to be invoked.277* @param params An array containing the parameters to be set when the278* action is invoked.279* @param signature An array containing the signature of the action.280*281* @return The object returned by the action, which represents282* the result of invoking the action on the MBean specified.283*284* @exception MBeanException Wraps a <CODE>java.lang.Exception</CODE>285* thrown by the MBean's invoked method.286* @exception ReflectionException Wraps a287* <CODE>java.lang.Exception</CODE> thrown while trying to invoke288* the method.289*/290public Object invoke(String actionName, Object params[],291String signature[])292throws MBeanException, ReflectionException {293// Check that operation name is not null.294//295if (actionName == null) {296final RuntimeException r =297new IllegalArgumentException("Operation name cannot be null");298throw new RuntimeOperationsException(r,299"Exception occurred trying to invoke the operation on the MBean");300}301302throw new ReflectionException(303new NoSuchMethodException(actionName),304"The operation with name " + actionName +305" could not be found");306}307308/**309* Provides the MBeanInfo describing the MBeanServerDelegate.310*311* @return The MBeanInfo describing the MBeanServerDelegate.312*313*/314public MBeanInfo getMBeanInfo() {315return delegateInfo;316}317318}319320321