Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/management/AttributeValueExp.java
38829 views
/*1* Copyright (c) 1999, 2008, 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;262728import com.sun.jmx.mbeanserver.Introspector;29import java.io.IOException;30import java.io.ObjectInputStream;3132/**33* <p>Represents attributes used as arguments to relational constraints.34* Instances of this class are usually obtained using {@link Query#attr(String)35* Query.attr}.</p>36*37* <p>An <CODE>AttributeValueExp</CODE> may be used anywhere a38* <CODE>ValueExp</CODE> is required.39*40* @since 1.541*/42public class AttributeValueExp implements ValueExp {434445/* Serial version */46private static final long serialVersionUID = -7768025046539163385L;4748/**49* @serial The name of the attribute50*/51private String attr;5253/**54* An <code>AttributeValueExp</code> with a null attribute.55* @deprecated An instance created with this constructor cannot be56* used in a query.57*/58@Deprecated59public AttributeValueExp() {60}6162/**63* Creates a new <CODE>AttributeValueExp</CODE> representing the64* specified object attribute, named attr.65*66* @param attr the name of the attribute whose value is the value67* of this {@link ValueExp}.68*/69public AttributeValueExp(String attr) {70this.attr = attr;71}7273/**74* Returns a string representation of the name of the attribute.75*76* @return the attribute name.77*/78public String getAttributeName() {79return attr;80}8182/**83* <p>Applies the <CODE>AttributeValueExp</CODE> on an MBean.84* This method calls {@link #getAttribute getAttribute(name)} and wraps85* the result as a {@code ValueExp}. The value returned by86* {@code getAttribute} must be a {@code Number}, {@code String},87* or {@code Boolean}; otherwise this method throws a88* {@code BadAttributeValueExpException}, which will cause89* the containing query to be false for this {@code name}.</p>90*91* @param name The name of the MBean on which the <CODE>AttributeValueExp</CODE> will be applied.92*93* @return The <CODE>ValueExp</CODE>.94*95* @exception BadAttributeValueExpException96* @exception InvalidApplicationException97* @exception BadStringOperationException98* @exception BadBinaryOpValueExpException99*100*/101@Override102public ValueExp apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException,103BadAttributeValueExpException, InvalidApplicationException {104Object result = getAttribute(name);105106if (result instanceof Number) {107return new NumericValueExp((Number)result);108} else if (result instanceof String) {109return new StringValueExp((String)result);110} else if (result instanceof Boolean) {111return new BooleanValueExp((Boolean)result);112} else {113throw new BadAttributeValueExpException(result);114}115}116117/**118* Returns the string representing its value.119*/120@Override121public String toString() {122return attr;123}124125126/**127* Sets the MBean server on which the query is to be performed.128*129* @param s The MBean server on which the query is to be performed.130*131* @deprecated This method has no effect. The MBean Server used to132* obtain an attribute value is {@link QueryEval#getMBeanServer()}.133*/134/* There is no need for this method, because if a query is being135evaluted an AttributeValueExp can only appear inside a QueryExp,136and that QueryExp will itself have done setMBeanServer. */137@Deprecated138@Override139public void setMBeanServer(MBeanServer s) {140}141142143/**144* <p>Return the value of the given attribute in the named MBean.145* If the attempt to access the attribute generates an exception,146* return null.</p>147*148* <p>The MBean Server used is the one returned by {@link149* QueryEval#getMBeanServer()}.</p>150*151* @param name the name of the MBean whose attribute is to be returned.152*153* @return the value of the attribute, or null if it could not be154* obtained.155*/156protected Object getAttribute(ObjectName name) {157try {158// Get the value from the MBeanServer159160MBeanServer server = QueryEval.getMBeanServer();161162return server.getAttribute(name, attr);163} catch (Exception re) {164return null;165}166}167}168169170