Path: blob/master/src/java.management/share/classes/javax/management/AttributeValueExp.java
41155 views
/*1* Copyright (c) 1999, 2015, 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* @throws BadStringOperationException {@inheritDoc}96* @throws BadBinaryOpValueExpException {@inheritDoc}97* @throws BadAttributeValueExpException {@inheritDoc}98* @throws InvalidApplicationException {@inheritDoc}99*/100@Override101public ValueExp apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException,102BadAttributeValueExpException, InvalidApplicationException {103Object result = getAttribute(name);104105if (result instanceof Number) {106return new NumericValueExp((Number)result);107} else if (result instanceof String) {108return new StringValueExp((String)result);109} else if (result instanceof Boolean) {110return new BooleanValueExp((Boolean)result);111} else {112throw new BadAttributeValueExpException(result);113}114}115116/**117* Returns the string representing its value.118*/119@Override120public String toString() {121return attr;122}123124125/**126* Sets the MBean server on which the query is to be performed.127*128* @param s The MBean server on which the query is to be performed.129*130* @deprecated This method has no effect. The MBean Server used to131* obtain an attribute value is {@link QueryEval#getMBeanServer()}.132*/133/* There is no need for this method, because if a query is being134evaluted an AttributeValueExp can only appear inside a QueryExp,135and that QueryExp will itself have done setMBeanServer. */136@Deprecated137@Override138public void setMBeanServer(MBeanServer s) {139}140141142/**143* <p>Return the value of the given attribute in the named MBean.144* If the attempt to access the attribute generates an exception,145* return null.</p>146*147* <p>The MBean Server used is the one returned by {@link148* QueryEval#getMBeanServer()}.</p>149*150* @param name the name of the MBean whose attribute is to be returned.151*152* @return the value of the attribute, or null if it could not be153* obtained.154*/155protected Object getAttribute(ObjectName name) {156try {157// Get the value from the MBeanServer158159MBeanServer server = QueryEval.getMBeanServer();160161return server.getAttribute(name, attr);162} catch (Exception re) {163return null;164}165}166}167168169