1 /*
2  * Copyright (c) 1999, 2008, 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 
26 package javax.management;
27 
28 
29 /**
30  * <p>Represents attributes used as arguments to relational constraints,
31  * where the attribute must be in an MBean of a specified {@linkplain
32  * MBeanInfo#getClassName() class}. A QualifiedAttributeValueExp may be used
33  * anywhere a ValueExp is required.
34  *
35  * @serial include
36  *
37  * @since 1.5
38  */
39 class QualifiedAttributeValueExp extends AttributeValueExp   {
40 
41 
42     /* Serial version */
43     private static final long serialVersionUID = 8832517277410933254L;
44 
45     /**
46      * @serial The attribute class name
47      */
48     private String className;
49 
50 
51     /**
52      * Basic Constructor.
53      * @deprecated see {@link AttributeValueExp#AttributeValueExp()}
54      */
55     @Deprecated
QualifiedAttributeValueExp()56     public QualifiedAttributeValueExp() {
57     }
58 
59     /**
60      * Creates a new QualifiedAttributeValueExp representing the specified object
61      * attribute, named attr with class name className.
62      */
QualifiedAttributeValueExp(String className, String attr)63     public QualifiedAttributeValueExp(String className, String attr) {
64         super(attr);
65         this.className = className;
66     }
67 
68 
69     /**
70      * Returns a string representation of the class name of the attribute.
71      */
getAttrClassName()72     public String getAttrClassName()  {
73         return className;
74     }
75 
76     /**
77      * Applies the QualifiedAttributeValueExp to an MBean.
78      *
79      * @param name The name of the MBean on which the QualifiedAttributeValueExp will be applied.
80      *
81      * @return  The ValueExp.
82      *
83      * @exception BadStringOperationException
84      * @exception BadBinaryOpValueExpException
85      * @exception BadAttributeValueExpException
86      * @exception InvalidApplicationException
87      */
88     @Override
apply(ObjectName name)89     public ValueExp apply(ObjectName name) throws BadStringOperationException, BadBinaryOpValueExpException,
90         BadAttributeValueExpException, InvalidApplicationException  {
91         try {
92             MBeanServer server = QueryEval.getMBeanServer();
93             String v = server.getObjectInstance(name).getClassName();
94 
95             if (v.equals(className)) {
96                 return super.apply(name);
97             }
98             throw new InvalidApplicationException("Class name is " + v +
99                                                   ", should be " + className);
100 
101         } catch (Exception e) {
102             throw new InvalidApplicationException("Qualified attribute: " + e);
103             /* Can happen if MBean disappears between the time we
104                construct the list of MBeans to query and the time we
105                evaluate the query on this MBean, or if
106                getObjectInstance throws SecurityException.  */
107         }
108     }
109 
110     /**
111      * Returns the string representing its value
112      */
113     @Override
toString()114     public String toString()  {
115         if (className != null) {
116             return className + "." + super.toString();
117         } else {
118             return super.toString();
119         }
120     }
121 
122 }
123