1 /*
2 * Copyright 1999-2006 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
26 package javax.management;
27
28 import java.security.AccessController;
29
30 import com.sun.jmx.mbeanserver.GetPropertyAction;
31
32 /**
33 * This class represents the name of the Java implementation class of
34 * the MBean. It is used for performing queries based on the class of
35 * the MBean.
36 * @serial include
37 *
38 * <p>The <b>serialVersionUID</b> of this class is <code>-1081892073854801359L</code>.
39 *
40 * @since 1.5
41 */
42 @SuppressWarnings("serial") // serialVersionUID is not constant
43 class ClassAttributeValueExp extends AttributeValueExp {
44
45 // Serialization compatibility stuff:
46 // Two serial forms are supported in this class. The selected form depends
47 // on system property "jmx.serial.form":
48 // - "1.0" for JMX 1.0
49 // - any other value for JMX 1.1 and higher
50 //
51 // Serial version for old serial form
52 private static final long oldSerialVersionUID = -2212731951078526753L;
53 //
54 // Serial version for new serial form
55 private static final long newSerialVersionUID = -1081892073854801359L;
56
57 private static final long serialVersionUID;
58 static {
59 boolean compat = false;
60 try {
61 GetPropertyAction act = new GetPropertyAction("jmx.serial.form");
62 String form = AccessController.doPrivileged(act);
63 compat = (form != null && form.equals("1.0"));
64 } catch (Exception e) {
65 // OK: exception means no compat with 1.0, too bad
66 }
67 if (compat)
68 serialVersionUID = oldSerialVersionUID;
69 else
70 serialVersionUID = newSerialVersionUID;
71 }
72
73 /**
74 * @serial The name of the attribute
75 *
76 * <p>The <b>serialVersionUID</b> of this class is <code>-1081892073854801359L</code>.
77 */
78 private String attr;
79
80 /**
81 * Basic Constructor.
82 */
83 public ClassAttributeValueExp() {
84 /* Compatibility: we have an attr field that we must hold on to
85 for serial compatibility, even though our parent has one too. */
86 super("Class");
87 attr = "Class";
88 }
89
90
91 /**
92 * Applies the ClassAttributeValueExp on an MBean. Returns the name of
93 * the Java implementation class of the MBean.
94 *
95 * @param name The name of the MBean on which the ClassAttributeValueExp will be applied.
96 *
97 * @return The ValueExp.
98 *
99 * @exception BadAttributeValueExpException
100 * @exception InvalidApplicationException
101 */
102 public ValueExp apply(ObjectName name)
103 throws BadStringOperationException, BadBinaryOpValueExpException,
104 BadAttributeValueExpException, InvalidApplicationException {
105 // getAttribute(name);
106 Object result = getValue(name);
107 if (result instanceof String) {
108 return new StringValueExp((String)result);
109 } else {
110 throw new BadAttributeValueExpException(result);
111 }
112 }
113
114 /**
115 * Returns the string "Class" representing its value
116 */
117 public String toString() {
118 return attr;
119 }
120
121
122 protected Object getValue(ObjectName name) {
123 try {
124 // Get the class of the object
125 MBeanServer server = QueryEval.getMBeanServer();
126 return server.getObjectInstance(name).getClassName();
127 } catch (Exception re) {
128 return null;
129 /* In principle the MBean does exist because otherwise we
130 wouldn't be evaluating the query on it. But it could
131 potentially have disappeared in between the time we
132 discovered it and the time the query is evaluated.
133
134 Also, the exception could be a SecurityException.
135
136 Returning null from here will cause
137 BadAttributeValueExpException, which will in turn cause
138 this MBean to be omitted from the query result. */
139 }
140 }
141
142 }