2
|
1 |
/*
|
|
2 |
* Copyright 2005-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 com.sun.jmx.mbeanserver;
|
|
27 |
|
|
28 |
import static com.sun.jmx.mbeanserver.Util.*;
|
|
29 |
|
|
30 |
import java.lang.reflect.Method;
|
|
31 |
import java.util.Arrays;
|
|
32 |
import java.util.Collection;
|
|
33 |
import java.util.Comparator;
|
|
34 |
import java.util.HashSet;
|
|
35 |
import java.util.List;
|
|
36 |
import java.util.Map;
|
|
37 |
import java.util.Set;
|
|
38 |
|
|
39 |
import javax.management.NotCompliantMBeanException;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* <p>An analyzer for a given MBean interface. The analyzer can
|
|
43 |
* be for Standard MBeans or MXBeans, depending on the MBeanIntrospector
|
|
44 |
* passed at construction.
|
|
45 |
*
|
|
46 |
* <p>The analyzer can
|
|
47 |
* visit the attributes and operations of the interface, calling
|
|
48 |
* a caller-supplied visitor method for each one.</p>
|
|
49 |
*
|
|
50 |
* @param <M> Method or ConvertingMethod according as this is a
|
|
51 |
* Standard MBean or an MXBean.
|
|
52 |
*
|
|
53 |
* @since 1.6
|
|
54 |
*/
|
|
55 |
class MBeanAnalyzer<M> {
|
|
56 |
|
|
57 |
static interface MBeanVisitor<M> {
|
|
58 |
public void visitAttribute(String attributeName,
|
|
59 |
M getter,
|
|
60 |
M setter);
|
|
61 |
public void visitOperation(String operationName,
|
|
62 |
M operation);
|
|
63 |
}
|
|
64 |
|
|
65 |
void visit(MBeanVisitor<M> visitor) {
|
|
66 |
// visit attributes
|
|
67 |
for (Map.Entry<String, AttrMethods<M>> entry : attrMap.entrySet()) {
|
|
68 |
String name = entry.getKey();
|
|
69 |
AttrMethods<M> am = entry.getValue();
|
|
70 |
visitor.visitAttribute(name, am.getter, am.setter);
|
|
71 |
}
|
|
72 |
|
|
73 |
// visit operations
|
|
74 |
for (Map.Entry<String, List<M>> entry : opMap.entrySet()) {
|
|
75 |
for (M m : entry.getValue())
|
|
76 |
visitor.visitOperation(entry.getKey(), m);
|
|
77 |
}
|
|
78 |
}
|
|
79 |
|
|
80 |
/* Map op name to method */
|
|
81 |
private Map<String, List<M>> opMap = newInsertionOrderMap();
|
|
82 |
/* Map attr name to getter and/or setter */
|
|
83 |
private Map<String, AttrMethods<M>> attrMap = newInsertionOrderMap();
|
|
84 |
|
|
85 |
private static class AttrMethods<M> {
|
|
86 |
M getter;
|
|
87 |
M setter;
|
|
88 |
}
|
|
89 |
|
|
90 |
/**
|
|
91 |
* <p>Return an MBeanAnalyzer for the given MBean interface and
|
|
92 |
* MBeanIntrospector. Calling this method twice with the same
|
|
93 |
* parameters may return the same object or two different but
|
|
94 |
* equivalent objects.
|
|
95 |
*/
|
|
96 |
// Currently it's two different but equivalent objects. This only
|
|
97 |
// really impacts proxy generation. For MBean creation, the
|
|
98 |
// cached PerInterface object for an MBean interface means that
|
|
99 |
// an analyzer will not be recreated for a second MBean using the
|
|
100 |
// same interface.
|
|
101 |
static <M> MBeanAnalyzer<M> analyzer(Class<?> mbeanInterface,
|
|
102 |
MBeanIntrospector<M> introspector)
|
|
103 |
throws NotCompliantMBeanException {
|
|
104 |
return new MBeanAnalyzer<M>(mbeanInterface, introspector);
|
|
105 |
}
|
|
106 |
|
|
107 |
private MBeanAnalyzer(Class<?> mbeanInterface,
|
|
108 |
MBeanIntrospector<M> introspector)
|
|
109 |
throws NotCompliantMBeanException {
|
287
|
110 |
introspector.checkCompliance(mbeanInterface);
|
2
|
111 |
|
|
112 |
try {
|
|
113 |
initMaps(mbeanInterface, introspector);
|
|
114 |
} catch (Exception x) {
|
|
115 |
throw Introspector.throwException(mbeanInterface,x);
|
|
116 |
}
|
|
117 |
}
|
|
118 |
|
|
119 |
// Introspect the mbeanInterface and initialize this object's maps.
|
|
120 |
//
|
287
|
121 |
private void initMaps(Class<?> mbeanType,
|
2
|
122 |
MBeanIntrospector<M> introspector) throws Exception {
|
287
|
123 |
final List<Method> methods1 = introspector.getMethods(mbeanType);
|
|
124 |
final List<Method> methods = eliminateCovariantMethods(methods1);
|
2
|
125 |
|
|
126 |
/* Run through the methods to detect inconsistencies and to enable
|
|
127 |
us to give getter and setter together to visitAttribute. */
|
|
128 |
for (Method m : methods) {
|
|
129 |
String name = m.getName();
|
|
130 |
|
|
131 |
final M cm = introspector.mFrom(m);
|
|
132 |
|
|
133 |
String attrName = "";
|
|
134 |
if (name.startsWith("get"))
|
|
135 |
attrName = name.substring(3);
|
|
136 |
else if (name.startsWith("is")
|
|
137 |
&& m.getReturnType() == boolean.class)
|
|
138 |
attrName = name.substring(2);
|
|
139 |
|
|
140 |
if (attrName.length() != 0 && m.getParameterTypes().length == 0
|
|
141 |
&& m.getReturnType() != void.class) {
|
|
142 |
// It's a getter
|
|
143 |
// Check we don't have both isX and getX
|
|
144 |
AttrMethods<M> am = attrMap.get(attrName);
|
|
145 |
if (am == null)
|
|
146 |
am = new AttrMethods<M>();
|
|
147 |
else {
|
|
148 |
if (am.getter != null) {
|
|
149 |
final String msg = "Attribute " + attrName +
|
|
150 |
" has more than one getter";
|
|
151 |
throw new NotCompliantMBeanException(msg);
|
|
152 |
}
|
|
153 |
}
|
|
154 |
am.getter = cm;
|
|
155 |
attrMap.put(attrName, am);
|
|
156 |
} else if (name.startsWith("set") && name.length() > 3
|
|
157 |
&& m.getParameterTypes().length == 1 &&
|
|
158 |
m.getReturnType() == void.class) {
|
|
159 |
// It's a setter
|
|
160 |
attrName = name.substring(3);
|
|
161 |
AttrMethods<M> am = attrMap.get(attrName);
|
|
162 |
if (am == null)
|
|
163 |
am = new AttrMethods<M>();
|
|
164 |
else if (am.setter != null) {
|
|
165 |
final String msg = "Attribute " + attrName +
|
|
166 |
" has more than one setter";
|
|
167 |
throw new NotCompliantMBeanException(msg);
|
|
168 |
}
|
|
169 |
am.setter = cm;
|
|
170 |
attrMap.put(attrName, am);
|
|
171 |
} else {
|
|
172 |
// It's an operation
|
|
173 |
List<M> cms = opMap.get(name);
|
|
174 |
if (cms == null)
|
|
175 |
cms = newList();
|
|
176 |
cms.add(cm);
|
|
177 |
opMap.put(name, cms);
|
|
178 |
}
|
|
179 |
}
|
|
180 |
/* Check that getters and setters are consistent. */
|
|
181 |
for (Map.Entry<String, AttrMethods<M>> entry : attrMap.entrySet()) {
|
|
182 |
AttrMethods<M> am = entry.getValue();
|
|
183 |
if (!introspector.consistent(am.getter, am.setter)) {
|
|
184 |
final String msg = "Getter and setter for " + entry.getKey() +
|
|
185 |
" have inconsistent types";
|
|
186 |
throw new NotCompliantMBeanException(msg);
|
|
187 |
}
|
|
188 |
}
|
|
189 |
}
|
|
190 |
|
|
191 |
/**
|
|
192 |
* A comparator that defines a total order so that methods have the
|
|
193 |
* same name and identical signatures appear next to each others.
|
|
194 |
* The methods are sorted in such a way that methods which
|
|
195 |
* override each other will sit next to each other, with the
|
|
196 |
* overridden method first - e.g. Object getFoo() is placed before
|
|
197 |
* Integer getFoo(). This makes it possible to determine whether
|
|
198 |
* a method overrides another one simply by looking at the method(s)
|
|
199 |
* that precedes it in the list. (see eliminateCovariantMethods).
|
|
200 |
**/
|
|
201 |
private static class MethodOrder implements Comparator<Method> {
|
|
202 |
public int compare(Method a, Method b) {
|
|
203 |
final int cmp = a.getName().compareTo(b.getName());
|
|
204 |
if (cmp != 0) return cmp;
|
|
205 |
final Class<?>[] aparams = a.getParameterTypes();
|
|
206 |
final Class<?>[] bparams = b.getParameterTypes();
|
|
207 |
if (aparams.length != bparams.length)
|
|
208 |
return aparams.length - bparams.length;
|
|
209 |
if (!Arrays.equals(aparams, bparams)) {
|
|
210 |
return Arrays.toString(aparams).
|
|
211 |
compareTo(Arrays.toString(bparams));
|
|
212 |
}
|
|
213 |
final Class<?> aret = a.getReturnType();
|
|
214 |
final Class<?> bret = b.getReturnType();
|
|
215 |
if (aret == bret) return 0;
|
|
216 |
|
|
217 |
// Super type comes first: Object, Number, Integer
|
|
218 |
if (aret.isAssignableFrom(bret))
|
|
219 |
return -1;
|
|
220 |
return +1; // could assert bret.isAssignableFrom(aret)
|
|
221 |
}
|
|
222 |
public final static MethodOrder instance = new MethodOrder();
|
|
223 |
}
|
|
224 |
|
|
225 |
|
|
226 |
/* Eliminate methods that are overridden with a covariant return type.
|
|
227 |
Reflection will return both the original and the overriding method
|
|
228 |
but only the overriding one is of interest. We return the methods
|
|
229 |
in the same order they arrived in. This isn't required by the spec
|
|
230 |
but existing code may depend on it and users may be used to seeing
|
|
231 |
operations or attributes appear in a particular order. */
|
|
232 |
static List<Method>
|
287
|
233 |
eliminateCovariantMethods(List<Method> startMethods) {
|
2
|
234 |
// We are assuming that you never have very many methods with the
|
|
235 |
// same name, so it is OK to use algorithms that are quadratic
|
|
236 |
// in the number of methods with the same name.
|
|
237 |
|
287
|
238 |
final int len = startMethods.size();
|
|
239 |
final Method[] sorted = startMethods.toArray(new Method[len]);
|
2
|
240 |
Arrays.sort(sorted,MethodOrder.instance);
|
|
241 |
final Set<Method> overridden = newSet();
|
|
242 |
for (int i=1;i<len;i++) {
|
|
243 |
final Method m0 = sorted[i-1];
|
|
244 |
final Method m1 = sorted[i];
|
|
245 |
|
|
246 |
// Methods that don't have the same name can't override each others
|
|
247 |
if (!m0.getName().equals(m1.getName())) continue;
|
|
248 |
|
|
249 |
// Methods that have the same name and same signature override
|
|
250 |
// each other. In that case, the second method overrides the first,
|
|
251 |
// due to the way we have sorted them in MethodOrder.
|
|
252 |
if (Arrays.equals(m0.getParameterTypes(),
|
|
253 |
m1.getParameterTypes())) {
|
|
254 |
overridden.add(m0);
|
|
255 |
}
|
|
256 |
}
|
|
257 |
|
287
|
258 |
final List<Method> methods = newList(startMethods);
|
2
|
259 |
methods.removeAll(overridden);
|
|
260 |
return methods;
|
|
261 |
}
|
|
262 |
|
|
263 |
|
|
264 |
}
|