blob: f520bbaf06ccaf39c66a01309c409df27bc0a1d5 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 1999-2004 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package javax.management;
27
28
29// RI import
30import javax.management.MBeanServer;
31
32/**
33 * Represents attributes used as arguments to relational constraints.
34 * An <CODE>AttributeValueExp</CODE> may be used anywhere a <CODE>ValueExp</CODE> is required.
35 *
36 * @since 1.5
37 */
38public class AttributeValueExp implements ValueExp {
39
40
41 /* Serial version */
42 private static final long serialVersionUID = -7768025046539163385L;
43
44 /**
45 * @serial The name of the attribute
46 */
47 private String attr;
48
49 /**
50 * An <code>AttributeValueExp</code> with a null attribute.
51 * @deprecated An instance created with this constructor cannot be
52 * used in a query.
53 */
54 @Deprecated
55 public AttributeValueExp() {
56 }
57
58 /**
59 * Creates a new <CODE>AttributeValueExp</CODE> representing the
60 * specified object attribute, named attr.
61 *
62 * @param attr the name of the attribute whose value is the value
63 * of this {@link ValueExp}.
64 */
65 public AttributeValueExp(String attr) {
66 this.attr = attr;
67 }
68
69 /**
70 * Returns a string representation of the name of the attribute.
71 *
72 * @return the attribute name.
73 */
74 public String getAttributeName() {
75 return attr;
76 }
77
78 /**
79 * Applies the <CODE>AttributeValueExp</CODE> on an MBean.
80 *
81 * @param name The name of the MBean on which the <CODE>AttributeValueExp</CODE> will be applied.
82 *
83 * @return The <CODE>ValueExp</CODE>.
84 *
85 * @exception BadAttributeValueExpException
86 * @exception InvalidApplicationException
87 * @exception BadStringOperationException
88 * @exception BadBinaryOpValueExpException
89 *
90 */
91 public ValueExp apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException,
92 BadAttributeValueExpException, InvalidApplicationException {
93 Object result = getAttribute(name);
94
95 if (result instanceof Number) {
96 return new NumericValueExp((Number)result);
97 } else if (result instanceof String) {
98 return new StringValueExp((String)result);
99 } else if (result instanceof Boolean) {
100 return new BooleanValueExp((Boolean)result);
101 } else {
102 throw new BadAttributeValueExpException(result);
103 }
104 }
105
106 /**
107 * Returns the string representing its value.
108 */
109 public String toString() {
110 return attr;
111 }
112
113
114 /**
115 * Sets the MBean server on which the query is to be performed.
116 *
117 * @param s The MBean server on which the query is to be performed.
118 */
119 /* There is no need for this method, because if a query is being
120 evaluted an AttributeValueExp can only appear inside a QueryExp,
121 and that QueryExp will itself have done setMBeanServer. */
122 public void setMBeanServer(MBeanServer s) {
123 }
124
125
126 /**
127 * Return the value of the given attribute in the named MBean.
128 * If the attempt to access the attribute generates an exception,
129 * return null.
130 *
131 * @param name the name of the MBean whose attribute is to be returned.
132 *
133 * @return the value of the attribute, or null if it could not be
134 * obtained.
135 */
136 protected Object getAttribute(ObjectName name) {
137 try {
138 // Get the value from the MBeanServer
139
140 MBeanServer server = QueryEval.getMBeanServer();
141
142 return server.getAttribute(name, attr);
143 } catch (Exception re) {
144 return null;
145 }
146 }
147
148}