KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > management > InstanceOfQueryExp


1 /*
2  * @(#)InstanceOfQueryExp.java 1.4 05/11/30
3  *
4  * Copyright 2006 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 /**
12  * This class is used by the query building mechanism for isInstanceOf expressions.
13  * @serial include
14  *
15  * @since 1.6
16  */

17 class InstanceOfQueryExp extends QueryEval JavaDoc implements QueryExp JavaDoc {
18     
19     /* Serial version */
20     private static final long serialVersionUID = -1081892073854801359L;
21
22     /**
23      * @serial The {@link StringValueExp} returning the name of the class
24      * of which selected MBeans should be instances.
25      */

26     private StringValueExp JavaDoc classNameValue;
27     
28     /**
29      * Creates a new InstanceOfExp with a specific class name.
30      * @param classNameValue The {@link StringValueExp} returning the name of
31      * the class of which selected MBeans should be instances.
32      */

33     // We are using StringValueExp here to be consistent with other queries,
34
// although we should actually either use a simple string (the classname)
35
// or a ValueExp - which would allow more complex queries - like for
36
// instance evaluating the class name from an AttributeValueExp.
37
// As it stands - using StringValueExp instead of a simple constant string
38
// doesn't serve any useful purpose besides offering a consistent
39
// look & feel.
40
public InstanceOfQueryExp(StringValueExp JavaDoc classNameValue) {
41     if (classNameValue == null) {
42         throw new IllegalArgumentException JavaDoc("Null class name.");
43     }
44     
45     this.classNameValue = classNameValue;
46     }
47     
48     /**
49      * Returns the class name.
50      * @returns The {@link StringValueExp} returning the name of
51      * the class of which selected MBeans should be instances.
52      */

53     public StringValueExp JavaDoc getClassNameValue() {
54     return classNameValue;
55     }
56     
57     /**
58      * Applies the InstanceOf on a MBean.
59      *
60      * @param name The name of the MBean on which the InstanceOf will be applied.
61      *
62      * @return True if the MBean specified by the name is instance of the class.
63      * @exception BadAttributeValueExpException
64      * @exception InvalidApplicationException
65      * @exception BadStringOperationException
66      * @exception BadBinaryOpValueExpException
67      */

68     public boolean apply(ObjectName JavaDoc name)
69     throws BadStringOperationException JavaDoc,
70     BadBinaryOpValueExpException JavaDoc,
71     BadAttributeValueExpException JavaDoc,
72     InvalidApplicationException JavaDoc {
73         
74         // Get the class name value
75
final StringValueExp JavaDoc val;
76     try {
77             val = (StringValueExp JavaDoc) classNameValue.apply(name);
78     } catch (ClassCastException JavaDoc x) {
79             // Should not happen - unless someone wrongly implemented
80
// StringValueExp.apply().
81
final BadStringOperationException JavaDoc y =
82                     new BadStringOperationException JavaDoc(x.toString());
83             y.initCause(x);
84             throw y;
85     }
86         
87         // Test whether the MBean is an instance of that class.
88
try {
89         return getMBeanServer().isInstanceOf(name, val.getValue());
90     } catch (InstanceNotFoundException JavaDoc infe) {
91         return false;
92         }
93     }
94     
95     /**
96      * Returns a string representation of this InstanceOfQueryExp.
97      * @return a string representation of this InstanceOfQueryExp.
98      */

99     public String JavaDoc toString() {
100        return "InstanceOf " + classNameValue.toString();
101    }
102 }
103
104
Popular Tags