KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > AttributeValueExp


1 /*
2  * @(#)AttributeValueExp.java 4.24 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.management;
9
10
11 // RI import
12
import javax.management.MBeanServer JavaDoc;
13
14 /**
15  * Represents attributes used as arguments to relational constraints.
16  * An <CODE>AttributeValueExp</CODE> may be used anywhere a <CODE>ValueExp</CODE> is required.
17  *
18  * @since 1.5
19  */

20 public class AttributeValueExp implements ValueExp JavaDoc {
21
22
23     /* Serial version */
24     private static final long serialVersionUID = -7768025046539163385L;
25
26     /**
27      * @serial The name of the attribute
28      */

29     private String JavaDoc attr;
30
31     /**
32      * An <code>AttributeValueExp</code> with a null attribute.
33      * @deprecated An instance created with this constructor cannot be
34      * used in a query.
35      */

36     @Deprecated JavaDoc
37     public AttributeValueExp() {
38     }
39        
40     /**
41      * Creates a new <CODE>AttributeValueExp</CODE> representing the
42      * specified object attribute, named attr.
43      *
44      * @param attr the name of the attribute whose value is the value
45      * of this {@link ValueExp}.
46      */

47     public AttributeValueExp(String JavaDoc attr) {
48     this.attr = attr;
49     }
50     
51     /**
52      * Returns a string representation of the name of the attribute.
53      *
54      * @return the attribute name.
55      */

56     public String JavaDoc getAttributeName() {
57     return attr;
58     }
59
60     /**
61      * Applies the <CODE>AttributeValueExp</CODE> on an MBean.
62      *
63      * @param name The name of the MBean on which the <CODE>AttributeValueExp</CODE> will be applied.
64      *
65      * @return The <CODE>ValueExp</CODE>.
66      *
67      * @exception BadAttributeValueExpException
68      * @exception InvalidApplicationException
69      * @exception BadStringOperationException
70      * @exception BadBinaryOpValueExpException
71      *
72      */

73     public ValueExp JavaDoc apply(ObjectName JavaDoc name) throws BadStringOperationException JavaDoc, BadBinaryOpValueExpException JavaDoc,
74     BadAttributeValueExpException JavaDoc, InvalidApplicationException JavaDoc {
75     Object JavaDoc result = getAttribute(name);
76     
77     if (result instanceof Number JavaDoc) {
78         return new NumericValueExp JavaDoc((Number JavaDoc)result);
79     } else if (result instanceof String JavaDoc) {
80         return new StringValueExp JavaDoc((String JavaDoc)result);
81     } else if (result instanceof Boolean JavaDoc) {
82         return new BooleanValueExp JavaDoc((Boolean JavaDoc)result);
83     } else {
84         throw new BadAttributeValueExpException JavaDoc(result);
85     }
86     }
87
88     /**
89      * Returns the string representing its value.
90      */

91     public String JavaDoc toString() {
92     return attr;
93     }
94     
95
96     /**
97      * Sets the MBean server on which the query is to be performed.
98      *
99      * @param s The MBean server on which the query is to be performed.
100      */

101     /* There is no need for this method, because if a query is being
102        evaluted an AttributeValueExp can only appear inside a QueryExp,
103        and that QueryExp will itself have done setMBeanServer. */

104     public void setMBeanServer(MBeanServer JavaDoc s) {
105     }
106
107
108     /**
109      * Return the value of the given attribute in the named MBean.
110      * If the attempt to access the attribute generates an exception,
111      * return null.
112      *
113      * @param name the name of the MBean whose attribute is to be returned.
114      *
115      * @return the value of the attribute, or null if it could not be
116      * obtained.
117      */

118     protected Object JavaDoc getAttribute(ObjectName JavaDoc name) {
119     try {
120         // Get the value from the MBeanServer
121

122         MBeanServer JavaDoc server = QueryEval.getMBeanServer();
123
124         return server.getAttribute(name, attr);
125     } catch (Exception JavaDoc re) {
126         return null;
127     }
128     }
129
130 }
131
Popular Tags