1/*
2 * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package javax.management;
27
28
29import com.sun.jmx.mbeanserver.Introspector;
30import java.io.IOException;
31import java.io.ObjectInputStream;
32
33/**
34 * <p>Represents attributes used as arguments to relational constraints.
35 * Instances of this class are usually obtained using {@link Query#attr(String)
36 * Query.attr}.</p>
37 *
38 * <p>An <CODE>AttributeValueExp</CODE> may be used anywhere a
39 * <CODE>ValueExp</CODE> is required.
40 *
41 * @since 1.5
42 */
43public class AttributeValueExp implements ValueExp  {
44
45
46    /* Serial version */
47    private static final long serialVersionUID = -7768025046539163385L;
48
49    /**
50     * @serial The name of the attribute
51     */
52    private String attr;
53
54    /**
55     * An <code>AttributeValueExp</code> with a null attribute.
56     * @deprecated An instance created with this constructor cannot be
57     * used in a query.
58     */
59    @Deprecated
60    public AttributeValueExp() {
61    }
62
63    /**
64     * Creates a new <CODE>AttributeValueExp</CODE> representing the
65     * specified object attribute, named attr.
66     *
67     * @param attr the name of the attribute whose value is the value
68     * of this {@link ValueExp}.
69     */
70    public AttributeValueExp(String attr) {
71        this.attr = attr;
72    }
73
74    /**
75     * Returns a string representation of the name of the attribute.
76     *
77     * @return the attribute name.
78     */
79    public String getAttributeName()  {
80        return attr;
81    }
82
83    /**
84     * <p>Applies the <CODE>AttributeValueExp</CODE> on an MBean.
85     * This method calls {@link #getAttribute getAttribute(name)} and wraps
86     * the result as a {@code ValueExp}.  The value returned by
87     * {@code getAttribute} must be a {@code Number}, {@code String},
88     * or {@code Boolean}; otherwise this method throws a
89     * {@code BadAttributeValueExpException}, which will cause
90     * the containing query to be false for this {@code name}.</p>
91     *
92     * @param name The name of the MBean on which the <CODE>AttributeValueExp</CODE> will be applied.
93     *
94     * @return  The <CODE>ValueExp</CODE>.
95     *
96     * @throws BadStringOperationException {@inheritDoc}
97     * @throws BadBinaryOpValueExpException {@inheritDoc}
98     * @throws BadAttributeValueExpException {@inheritDoc}
99     * @throws InvalidApplicationException  {@inheritDoc}
100     */
101    @Override
102    public ValueExp apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException,
103        BadAttributeValueExpException, InvalidApplicationException {
104        Object result = getAttribute(name);
105
106        if (result instanceof Number) {
107            return new NumericValueExp((Number)result);
108        } else if (result instanceof String) {
109            return new StringValueExp((String)result);
110        } else if (result instanceof Boolean) {
111            return new BooleanValueExp((Boolean)result);
112        } else {
113            throw new BadAttributeValueExpException(result);
114        }
115    }
116
117    /**
118     * Returns the string representing its value.
119     */
120    @Override
121    public String toString()  {
122        return attr;
123    }
124
125
126    /**
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 to
132     * obtain an attribute value is {@link QueryEval#getMBeanServer()}.
133     */
134    /* There is no need for this method, because if a query is being
135       evaluted an AttributeValueExp can only appear inside a QueryExp,
136       and that QueryExp will itself have done setMBeanServer.  */
137    @Deprecated
138    @Override
139    public void setMBeanServer(MBeanServer s)  {
140    }
141
142
143    /**
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 {@link
149     * 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 be
154     * obtained.
155     */
156    protected Object getAttribute(ObjectName name) {
157        try {
158            // Get the value from the MBeanServer
159
160            MBeanServer server = QueryEval.getMBeanServer();
161
162            return server.getAttribute(name, attr);
163        } catch (Exception re) {
164            return null;
165        }
166    }
167}
168