author | emcmanus |
Tue, 09 Dec 2008 12:01:07 +0100 | |
changeset 1697 | 98a530cd0594 |
parent 1247 | b4c26443dee5 |
permissions | -rw-r--r-- |
833 | 1 |
/* |
1247 | 2 |
* Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved. |
833 | 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 java.lang.ref.WeakReference; |
|
29 |
import java.security.PrivilegedAction; |
|
30 |
import java.util.Map; |
|
31 |
import java.util.WeakHashMap; |
|
32 |
import javax.annotation.Resource; |
|
33 |
import javax.management.MBeanServer; |
|
34 |
import javax.management.NotCompliantMBeanException; |
|
35 |
import javax.management.ObjectName; |
|
36 |
||
37 |
import static com.sun.jmx.mbeanserver.Util.newMap; |
|
38 |
import java.lang.reflect.AccessibleObject; |
|
39 |
import java.lang.reflect.Field; |
|
40 |
import java.lang.reflect.InvocationTargetException; |
|
41 |
import java.lang.reflect.Method; |
|
42 |
import java.lang.reflect.Modifier; |
|
43 |
import java.security.AccessController; |
|
44 |
import java.util.ArrayList; |
|
45 |
import java.util.Collections; |
|
46 |
import java.util.List; |
|
47 |
import javax.management.SendNotification; |
|
48 |
||
49 |
public class MBeanInjector { |
|
1697
98a530cd0594
6774918: @NotificationInfo is ineffective on MBeans that cannot send notifications
emcmanus
parents:
1247
diff
changeset
|
50 |
// There are no instances of this class |
98a530cd0594
6774918: @NotificationInfo is ineffective on MBeans that cannot send notifications
emcmanus
parents:
1247
diff
changeset
|
51 |
private MBeanInjector() { |
98a530cd0594
6774918: @NotificationInfo is ineffective on MBeans that cannot send notifications
emcmanus
parents:
1247
diff
changeset
|
52 |
} |
98a530cd0594
6774918: @NotificationInfo is ineffective on MBeans that cannot send notifications
emcmanus
parents:
1247
diff
changeset
|
53 |
|
833 | 54 |
private static Class<?>[] injectedClasses = { |
55 |
MBeanServer.class, ObjectName.class, SendNotification.class, |
|
56 |
}; |
|
57 |
||
58 |
public static void inject(Object mbean, MBeanServer mbs, ObjectName name) |
|
59 |
throws Exception { |
|
60 |
ClassInjector injector = injectorForClass(mbean.getClass()); |
|
61 |
injector.inject(mbean, MBeanServer.class, mbs); |
|
62 |
injector.inject(mbean, ObjectName.class, name); |
|
63 |
} |
|
64 |
||
65 |
public static boolean injectsSendNotification(Object mbean) |
|
66 |
throws NotCompliantMBeanException { |
|
67 |
ClassInjector injector = injectorForClass(mbean.getClass()); |
|
68 |
return injector.injects(SendNotification.class); |
|
69 |
} |
|
70 |
||
71 |
public static void injectSendNotification(Object mbean, SendNotification sn) |
|
72 |
throws Exception { |
|
73 |
ClassInjector injector = injectorForClass(mbean.getClass()); |
|
74 |
injector.inject(mbean, SendNotification.class, sn); |
|
75 |
} |
|
76 |
||
77 |
public static void validate(Class<?> c) throws NotCompliantMBeanException { |
|
78 |
injectorForClass(c); |
|
79 |
} |
|
80 |
||
81 |
private static class ClassInjector { |
|
82 |
private Map<Class<?>, List<Field>> fields; |
|
83 |
private Map<Class<?>, List<Method>> methods; |
|
84 |
||
85 |
ClassInjector(Class<?> c) throws NotCompliantMBeanException { |
|
86 |
fields = newMap(); |
|
87 |
methods = newMap(); |
|
88 |
||
89 |
Class<?> sup = c.getSuperclass(); |
|
90 |
ClassInjector supInjector; |
|
91 |
if (sup == null) { |
|
92 |
supInjector = null; |
|
93 |
} else { |
|
94 |
supInjector = injectorForClass(sup); |
|
95 |
fields.putAll(supInjector.fields); |
|
96 |
methods.putAll(supInjector.methods); |
|
97 |
} |
|
98 |
||
99 |
addMembers(c); |
|
100 |
eliminateOverriddenMethods(); |
|
101 |
||
102 |
// If we haven't added any new fields or methods to what we |
|
103 |
// inherited, then we can share the parent's maps. |
|
104 |
if (supInjector != null) { |
|
105 |
if (fields.equals(supInjector.fields)) |
|
106 |
fields = supInjector.fields; |
|
107 |
if (methods.equals(supInjector.methods)) |
|
108 |
methods = supInjector.methods; |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
boolean injects(Class<?> c) { |
|
113 |
return (fields.get(c) != null || methods.get(c) != null); |
|
114 |
} |
|
115 |
||
116 |
<T> void inject(Object instance, Class<T> type, T resource) |
|
117 |
throws Exception { |
|
118 |
List<Field> fs = fields.get(type); |
|
119 |
if (fs != null) { |
|
120 |
for (Field f : fs) |
|
121 |
f.set(instance, resource); |
|
122 |
} |
|
123 |
List<Method> ms = methods.get(type); |
|
124 |
if (ms != null) { |
|
125 |
for (Method m : ms) { |
|
126 |
try { |
|
127 |
m.invoke(instance, resource); |
|
128 |
} catch (InvocationTargetException e) { |
|
129 |
Throwable cause = e.getCause(); |
|
130 |
if (cause instanceof Error) |
|
131 |
throw (Error) cause; |
|
132 |
else |
|
133 |
throw (Exception) cause; |
|
134 |
} |
|
135 |
} |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
private void eliminateOverriddenMethods() { |
|
140 |
/* Covariant overriding is unlikely, but it is possible that the |
|
141 |
* parent has a @Resource method that we override with another |
|
142 |
* @Resource method. We don't want to invoke both methods, |
|
143 |
* because polymorphism means we would actually invoke the same |
|
144 |
* method twice. |
|
145 |
*/ |
|
146 |
for (Map.Entry<Class<?>, List<Method>> entry : methods.entrySet()) { |
|
147 |
List<Method> list = entry.getValue(); |
|
148 |
list = MBeanAnalyzer.eliminateCovariantMethods(list); |
|
149 |
entry.setValue(list); |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
/* |
|
154 |
* Find Fields or Methods within the given Class that we can inject |
|
155 |
* resource references into. Suppose we want to know if a Field can get |
|
156 |
* a reference to an ObjectName. We'll accept fields like this: |
|
157 |
* |
|
158 |
* @Resource |
|
159 |
* private transient ObjectName name; |
|
160 |
* |
|
161 |
* or like this: |
|
162 |
* |
|
163 |
* @Resource(type = ObjectName.class) |
|
164 |
* private transient Object name; |
|
165 |
* |
|
166 |
* but not like this: |
|
167 |
* |
|
168 |
* @Resource |
|
169 |
* private transient Object name; |
|
170 |
* |
|
171 |
* (Plain @Resource is equivalent to @Resource(type = Object.class).) |
|
172 |
* |
|
173 |
* We don't want to inject into everything that might possibly accept |
|
174 |
* an ObjectName reference, because examples like the last one above |
|
175 |
* could also accept an MBeanServer reference or any other sort of |
|
176 |
* reference. |
|
177 |
* |
|
178 |
* So we accept a Field if it has a @Resource annotation and either |
|
1004 | 179 |
* (a) its type is exactly ObjectName and its @Resource type is |
833 | 180 |
* compatible with ObjectName (e.g. it is Object); or |
181 |
* (b) its type is compatible with ObjectName and its @Resource type |
|
182 |
* is exactly ObjectName. Fields that meet these criteria will not |
|
183 |
* meet the same criteria with respect to other types such as MBeanServer. |
|
184 |
* |
|
185 |
* The same logic applies mutatis mutandis to Methods such as this: |
|
186 |
* |
|
187 |
* @Resource |
|
188 |
* private void setObjectName1(ObjectName name) |
|
189 |
* @Resource(type = Object.class) |
|
190 |
* private void setObjectName2(Object name) |
|
191 |
*/ |
|
192 |
private void addMembers(final Class<?> c) |
|
193 |
throws NotCompliantMBeanException { |
|
194 |
AccessibleObject[][] memberArrays = |
|
195 |
AccessController.doPrivileged( |
|
196 |
new PrivilegedAction<AccessibleObject[][]>() { |
|
197 |
public AccessibleObject[][] run() { |
|
198 |
return new AccessibleObject[][] { |
|
199 |
c.getDeclaredFields(), c.getDeclaredMethods() |
|
200 |
}; |
|
201 |
} |
|
202 |
}); |
|
203 |
for (AccessibleObject[] members : memberArrays) { |
|
204 |
for (final AccessibleObject member : members) { |
|
205 |
Resource res = member.getAnnotation(Resource.class); |
|
206 |
if (res == null) |
|
207 |
continue; |
|
208 |
||
209 |
final Field field; |
|
210 |
final Method method; |
|
211 |
final Class<?> memberType; |
|
212 |
final int modifiers; |
|
213 |
if (member instanceof Field) { |
|
214 |
field = (Field) member; |
|
215 |
memberType = field.getType(); |
|
216 |
modifiers = field.getModifiers(); |
|
217 |
method = null; |
|
218 |
} else { |
|
219 |
field = null; |
|
220 |
method = (Method) member; |
|
221 |
Class<?>[] paramTypes = method.getParameterTypes(); |
|
222 |
if (paramTypes.length != 1) { |
|
223 |
throw new NotCompliantMBeanException( |
|
224 |
"@Resource method must have exactly 1 " + |
|
225 |
"parameter: " + method); |
|
226 |
} |
|
227 |
if (method.getReturnType() != void.class) { |
|
228 |
throw new NotCompliantMBeanException( |
|
229 |
"@Resource method must return void: " + |
|
230 |
method); |
|
231 |
} |
|
232 |
memberType = paramTypes[0]; |
|
233 |
modifiers = method.getModifiers(); |
|
234 |
} |
|
235 |
||
236 |
if (Modifier.isStatic(modifiers)) { |
|
237 |
throw new NotCompliantMBeanException( |
|
238 |
"@Resource method or field cannot be static: " + |
|
239 |
member); |
|
240 |
} |
|
241 |
||
242 |
for (Class<?> injectedClass : injectedClasses) { |
|
243 |
Class<?>[] types = {memberType, res.type()}; |
|
244 |
boolean accept = false; |
|
245 |
for (int i = 0; i < 2; i++) { |
|
246 |
if (types[i] == injectedClass && |
|
247 |
types[1 - i].isAssignableFrom(injectedClass)) { |
|
248 |
accept = true; |
|
249 |
break; |
|
250 |
} |
|
251 |
} |
|
252 |
if (accept) { |
|
253 |
AccessController.doPrivileged(new PrivilegedAction<Void>() { |
|
254 |
public Void run() { |
|
255 |
member.setAccessible(true); |
|
256 |
return null; |
|
257 |
} |
|
258 |
}); |
|
259 |
addToMap(fields, injectedClass, field); |
|
260 |
addToMap(methods, injectedClass, method); |
|
261 |
} |
|
262 |
} |
|
263 |
} |
|
264 |
} |
|
265 |
} |
|
266 |
||
267 |
private static <K, V> void addToMap(Map<K, List<V>> map, K key, V value) { |
|
268 |
if (value == null) |
|
269 |
return; |
|
270 |
List<V> list = map.get(key); |
|
271 |
if (list == null) |
|
272 |
list = Collections.singletonList(value); |
|
273 |
else { |
|
274 |
if (list.size() == 1) |
|
275 |
list = new ArrayList<V>(list); |
|
276 |
list.add(value); |
|
277 |
} |
|
278 |
map.put(key, list); |
|
279 |
} |
|
280 |
} |
|
281 |
||
282 |
private static synchronized ClassInjector injectorForClass(Class<?> c) |
|
283 |
throws NotCompliantMBeanException { |
|
284 |
WeakReference<ClassInjector> wr = injectorMap.get(c); |
|
285 |
ClassInjector ci = (wr == null) ? null : wr.get(); |
|
286 |
if (ci == null) { |
|
287 |
ci = new ClassInjector(c); |
|
288 |
injectorMap.put(c, new WeakReference<ClassInjector>(ci)); |
|
289 |
} |
|
290 |
return ci; |
|
291 |
} |
|
292 |
||
293 |
private static Map<Class<?>, WeakReference<ClassInjector>> injectorMap = |
|
294 |
new WeakHashMap<Class<?>, WeakReference<ClassInjector>>(); |
|
295 |
} |