Path: blob/master/src/java.management/share/classes/javax/management/ClassAttributeValueExp.java
41155 views
/*1* Copyright (c) 1999, 2021, 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*/2425package javax.management;2627import java.security.AccessController;2829import com.sun.jmx.mbeanserver.GetPropertyAction;3031/**32* This class represents the name of the Java implementation class of33* the MBean. It is used for performing queries based on the class of34* the MBean.35* @serial include36*37* <p>The <b>serialVersionUID</b> of this class is <code>-1081892073854801359L</code>.38*39* @since 1.540*/41@SuppressWarnings("serial") // serialVersionUID is not constant42class ClassAttributeValueExp extends AttributeValueExp {4344// Serialization compatibility stuff:45// Two serial forms are supported in this class. The selected form depends46// on system property "jmx.serial.form":47// - "1.0" for JMX 1.048// - any other value for JMX 1.1 and higher49//50// Serial version for old serial form51private static final long oldSerialVersionUID = -2212731951078526753L;52//53// Serial version for new serial form54private static final long newSerialVersionUID = -1081892073854801359L;5556private static final long serialVersionUID;57static {58boolean compat = false;59try {60GetPropertyAction act = new GetPropertyAction("jmx.serial.form");61@SuppressWarnings("removal")62String form = AccessController.doPrivileged(act);63compat = (form != null && form.equals("1.0"));64} catch (Exception e) {65// OK: exception means no compat with 1.0, too bad66}67if (compat)68serialVersionUID = oldSerialVersionUID;69else70serialVersionUID = newSerialVersionUID;71}7273/**74* @serial The name of the attribute75*76* <p>The <b>serialVersionUID</b> of this class is <code>-1081892073854801359L</code>.77*/78private String attr;7980/**81* Basic Constructor.82*/83public ClassAttributeValueExp() {84/* Compatibility: we have an attr field that we must hold on to85for serial compatibility, even though our parent has one too. */86super("Class");87attr = "Class";88}899091/**92* Applies the ClassAttributeValueExp on an MBean. Returns the name of93* the Java implementation class of the MBean.94*95* @param name The name of the MBean on which the ClassAttributeValueExp will be applied.96*97* @return The ValueExp.98*99* @exception BadAttributeValueExpException100* @exception InvalidApplicationException101*/102public ValueExp apply(ObjectName name)103throws BadStringOperationException, BadBinaryOpValueExpException,104BadAttributeValueExpException, InvalidApplicationException {105// getAttribute(name);106Object result = getValue(name);107if (result instanceof String) {108return new StringValueExp((String)result);109} else {110throw new BadAttributeValueExpException(result);111}112}113114/**115* Returns the string "Class" representing its value116*/117public String toString() {118return attr;119}120121122protected Object getValue(ObjectName name) {123try {124// Get the class of the object125MBeanServer server = QueryEval.getMBeanServer();126return server.getObjectInstance(name).getClassName();127} catch (Exception re) {128return null;129/* In principle the MBean does exist because otherwise we130wouldn't be evaluating the query on it. But it could131potentially have disappeared in between the time we132discovered it and the time the query is evaluated.133134Also, the exception could be a SecurityException.135136Returning null from here will cause137BadAttributeValueExpException, which will in turn cause138this MBean to be omitted from the query result. */139}140}141142}143144145