287
|
1 |
/*
|
715
|
2 |
* Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
|
287
|
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 com.sun.jmx.mbeanserver.NotificationMBeanSupport;
|
|
29 |
import com.sun.jmx.mbeanserver.Util;
|
|
30 |
import java.lang.reflect.InvocationHandler;
|
|
31 |
import java.lang.reflect.InvocationTargetException;
|
|
32 |
import java.lang.reflect.Method;
|
|
33 |
import java.lang.reflect.Proxy;
|
|
34 |
import java.util.Collections;
|
|
35 |
import java.util.Set;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* <p>General-purpose notification filter. This filter can be used to
|
|
39 |
* filter notifications from a possibly-remote MBean. Most filtering
|
|
40 |
* decisions can be coded using this filter, which avoids having to
|
|
41 |
* write a custom implementation of the {@link NotificationFilter}
|
|
42 |
* class. Writing a custom implementation requires you to deploy it
|
|
43 |
* on both the client and the server in the remote case, so using this class
|
|
44 |
* instead is recommended where possible.</p>
|
|
45 |
*
|
|
46 |
* <p>This class uses the {@linkplain Query Query API} to specify the
|
|
47 |
* filtering logic. For example, to select only notifications where the
|
|
48 |
* {@linkplain Notification#getType() type} is {@code "com.example.mytype"},
|
|
49 |
* you could use</p>
|
|
50 |
*
|
|
51 |
* <pre>
|
|
52 |
* NotificationFilter filter =
|
|
53 |
* new QueryNotificationFilter("Type = 'com.example.mytype'");
|
|
54 |
* </pre>
|
|
55 |
*
|
|
56 |
* <p>or equivalently</p>
|
|
57 |
*
|
|
58 |
* <pre>
|
|
59 |
* NotificationFilter filter =
|
|
60 |
* new QueryNotificationFilter(
|
|
61 |
* Query.eq(Query.attr("Type"), Query.value("com.example.mytype")));
|
|
62 |
* </pre>
|
|
63 |
*
|
|
64 |
* <p>(This particular example could also use
|
|
65 |
* {@link NotificationFilterSupport}.)</p>
|
|
66 |
*
|
|
67 |
* <p>Here are some other examples of filters you can specify with this class.</p>
|
|
68 |
*
|
|
69 |
* <dl>
|
|
70 |
*
|
|
71 |
* <dt>{@code QueryNotificationFilter("Type = 'com.example.type1' or
|
|
72 |
* Type = 'com.example.type2'")}
|
|
73 |
* <dd>Notifications where the type is either of the given strings.
|
|
74 |
*
|
|
75 |
* <dt>{@code QueryNotificationFilter("Type in ('com.example.type1',
|
|
76 |
* 'com.example.type2')")}
|
|
77 |
* <dd>Another way to write the previous example.
|
|
78 |
*
|
|
79 |
* <dt>{@code QueryNotificationFilter("SequenceNumber > 1000")}
|
|
80 |
* <dd>Notifications where the {@linkplain Notification#getSequenceNumber()
|
|
81 |
* sequence number} is greater than 1000.
|
|
82 |
*
|
|
83 |
* <dt>{@code QueryNotificationFilter(AttributeChangeNotification.class, null)}
|
|
84 |
* <dd>Notifications where the notification class is
|
|
85 |
* {@link AttributeChangeNotification} or a subclass of it.
|
|
86 |
*
|
|
87 |
* <dt>{@code QueryNotificationFilter(AttributeChangeNotification.class,
|
|
88 |
* "AttributeName = 'Size'")}
|
|
89 |
* <dd>Notifications where the notification class is
|
|
90 |
* {@link AttributeChangeNotification} or a subclass, and where the
|
|
91 |
* {@linkplain AttributeChangeNotification#getAttributeName() name of the
|
|
92 |
* changed attribute} is {@code Size}.
|
|
93 |
*
|
|
94 |
* <dt>{@code QueryNotificationFilter(AttributeChangeNotification.class,
|
|
95 |
* "AttributeName = 'Size' and NewValue - OldValue > 100")}
|
|
96 |
* <dd>As above, but the difference between the
|
|
97 |
* {@linkplain AttributeChangeNotification#getNewValue() new value} and the
|
|
98 |
* {@linkplain AttributeChangeNotification#getOldValue() old value} must be
|
|
99 |
* greater than 100.
|
|
100 |
*
|
|
101 |
* <dt>{@code QueryNotificationFilter("like 'com.example.mydomain:*'")}
|
|
102 |
* <dd>Notifications where the {@linkplain Notification#getSource() source}
|
|
103 |
* is an ObjectName that matches the pattern.
|
|
104 |
*
|
|
105 |
* <dt>{@code QueryNotificationFilter("Source.canonicalName like
|
|
106 |
* 'com.example.mydomain:%'")}
|
|
107 |
* <dd>Another way to write the previous example.
|
|
108 |
*
|
|
109 |
* <dt>{@code QueryNotificationFilter(MBeanServerNotification.class,
|
|
110 |
* "Type = 'JMX.mbean.registered' and MBeanName.canonicalName like
|
|
111 |
* 'com.example.mydomain:%'")}
|
|
112 |
* <dd>Notifications of class {@link MBeanServerNotification} representing
|
|
113 |
* an object registered in the domain {@code com.example.mydomain}.
|
|
114 |
*
|
|
115 |
* </dl>
|
|
116 |
*
|
|
117 |
* <h4>How it works</h4>
|
|
118 |
*
|
|
119 |
* <p>Although the examples above are clear, looking closely at the
|
|
120 |
* Query API reveals a subtlety. A {@link QueryExp} is evaluated on
|
|
121 |
* an {@link ObjectName}, not a {@code Notification}.</p>
|
|
122 |
*
|
|
123 |
* <p>Every time a {@code Notification} is to be filtered by a
|
|
124 |
* {@code QueryNotificationFilter}, a special {@link MBeanServer} is created.
|
|
125 |
* This {@code MBeanServer} contains exactly one MBean, which represents the
|
|
126 |
* {@code Notification}. If the {@linkplain Notification#getSource()
|
|
127 |
* source} of the notification is an {@code ObjectName}, which is
|
|
128 |
* recommended practice, then the name of the MBean representing the
|
|
129 |
* {@code Notification} will be this {@code ObjectName}. Otherwise the
|
|
130 |
* name is unspecified.</p>
|
|
131 |
*
|
|
132 |
* <p>The query specified in the {@code QueryNotificationFilter} constructor
|
|
133 |
* is evaluated against this {@code MBeanServer} and {@code ObjectName},
|
|
134 |
* and the filter returns true if and only if the query does. If the
|
|
135 |
* query throws an exception, then the filter will return false.</p>
|
|
136 |
*
|
|
137 |
* <p>The MBean representing the {@code Notification} has one attribute for
|
|
138 |
* every property of the {@code Notification}. Specifically, for every public
|
|
139 |
* method {@code T getX()} in the {@code NotificationClass}, the MBean will
|
|
140 |
* have an attribute called {@code X} of type {@code T}. For example, if the
|
|
141 |
* {@code Notification} is an {@code AttributeChangeNotification}, then the
|
|
142 |
* MBean will have an attribute called {@code AttributeName} of type
|
|
143 |
* {@code "java.lang.String"}, corresponding to the method {@link
|
|
144 |
* AttributeChangeNotification#getAttributeName}.</p>
|
|
145 |
*
|
|
146 |
* <p>Query evaluation usually involves calls to the methods of {@code
|
|
147 |
* MBeanServer}. The methods have the following behavior:</p>
|
|
148 |
*
|
|
149 |
* <ul>
|
|
150 |
* <li>The {@link MBeanServer#getAttribute getAttribute} method returns the
|
|
151 |
* value of the corresponding property.
|
|
152 |
* <li>The {@link MBeanServer#getObjectInstance getObjectInstance}
|
|
153 |
* method returns an {@link ObjectInstance} where the {@link
|
|
154 |
* ObjectInstance#getObjectName ObjectName} is the name of the MBean and the
|
|
155 |
* {@link ObjectInstance#getClassName ClassName} is the class name of the
|
|
156 |
* {@code Notification}.
|
|
157 |
* <li>The {@link MBeanServer#isInstanceOf isInstanceOf} method returns true
|
|
158 |
* if and only if the {@code Notification}'s {@code ClassLoader} can load the
|
|
159 |
* named class, and the {@code Notification} is an {@linkplain Class#isInstance
|
|
160 |
* instance} of that class.
|
|
161 |
* </ul>
|
|
162 |
*
|
|
163 |
* <p>These are the only {@code MBeanServer} methods that are needed to
|
|
164 |
* evaluate standard queries. The behavior of the other {@code MBeanServer}
|
|
165 |
* methods is unspecified.</p>
|
|
166 |
*
|
|
167 |
* @since 1.7
|
|
168 |
*/
|
|
169 |
public class QueryNotificationFilter implements NotificationFilter {
|
|
170 |
private static final long serialVersionUID = -8408613922660635231L;
|
|
171 |
|
|
172 |
private static final ObjectName DEFAULT_NAME =
|
|
173 |
Util.newObjectName(":type=Notification");
|
|
174 |
private static final QueryExp trueQuery;
|
|
175 |
static {
|
|
176 |
ValueExp zero = Query.value(0);
|
|
177 |
trueQuery = Query.eq(zero, zero);
|
|
178 |
}
|
|
179 |
|
|
180 |
private final QueryExp query;
|
|
181 |
|
|
182 |
/**
|
|
183 |
* Construct a {@code QueryNotificationFilter} that evaluates the given
|
|
184 |
* {@code QueryExp} to determine whether to accept a notification.
|
|
185 |
*
|
|
186 |
* @param query the {@code QueryExp} to evaluate. Can be null,
|
|
187 |
* in which case all notifications are accepted.
|
|
188 |
*/
|
|
189 |
public QueryNotificationFilter(QueryExp query) {
|
|
190 |
if (query == null)
|
|
191 |
this.query = trueQuery;
|
|
192 |
else
|
|
193 |
this.query = query;
|
|
194 |
}
|
|
195 |
|
|
196 |
/**
|
|
197 |
* Construct a {@code QueryNotificationFilter} that evaluates the query
|
|
198 |
* in the given string to determine whether to accept a notification.
|
|
199 |
* The string is converted into a {@code QueryExp} using
|
|
200 |
* {@link Query#fromString Query.fromString}.
|
|
201 |
*
|
|
202 |
* @param query the string specifying the query to evaluate. Can be null,
|
|
203 |
* in which case all notifications are accepted.
|
|
204 |
*
|
|
205 |
* @throws IllegalArgumentException if the string is not a valid
|
|
206 |
* query string.
|
|
207 |
*/
|
|
208 |
public QueryNotificationFilter(String query) {
|
|
209 |
this(Query.fromString(query));
|
|
210 |
}
|
|
211 |
|
|
212 |
/**
|
|
213 |
* <p>Construct a {@code QueryNotificationFilter} that evaluates the query
|
|
214 |
* in the given string to determine whether to accept a notification,
|
|
215 |
* and where the notification must also be an instance of the given class.
|
|
216 |
* The string is converted into a {@code QueryExp} using
|
|
217 |
* {@link Query#fromString Query.fromString}.</p>
|
|
218 |
*
|
|
219 |
* @param notifClass the class that the notification must be an instance of.
|
|
220 |
* Cannot be null.
|
|
221 |
*
|
|
222 |
* @param query the string specifying the query to evaluate. Can be null,
|
|
223 |
* in which case all notifications are accepted.
|
|
224 |
*
|
|
225 |
* @throws IllegalArgumentException if the string is not a valid
|
|
226 |
* query string, or if {@code notifClass} is null.
|
|
227 |
*/
|
|
228 |
public QueryNotificationFilter(
|
|
229 |
Class<? extends Notification> notifClass, String query) {
|
|
230 |
this(Query.and(Query.isInstanceOf(Query.value(notNull(notifClass).getName())),
|
|
231 |
Query.fromString(query)));
|
|
232 |
}
|
|
233 |
|
|
234 |
private static <T> T notNull(T x) {
|
|
235 |
if (x == null)
|
|
236 |
throw new IllegalArgumentException("Null argument");
|
|
237 |
return x;
|
|
238 |
}
|
|
239 |
|
|
240 |
/**
|
|
241 |
* Retrieve the query that this notification filter will evaluate for
|
|
242 |
* each notification.
|
|
243 |
*
|
|
244 |
* @return the query.
|
|
245 |
*/
|
|
246 |
public QueryExp getQuery() {
|
|
247 |
return query;
|
|
248 |
}
|
|
249 |
|
|
250 |
public boolean isNotificationEnabled(Notification notification) {
|
|
251 |
ObjectName name;
|
|
252 |
|
|
253 |
Object source = notification.getSource();
|
|
254 |
if (source instanceof ObjectName)
|
|
255 |
name = (ObjectName) source;
|
|
256 |
else
|
|
257 |
name = DEFAULT_NAME;
|
|
258 |
|
|
259 |
MBS mbsImpl = new MBS(notification, name);
|
|
260 |
MBeanServer mbs = (MBeanServer) Proxy.newProxyInstance(
|
|
261 |
MBeanServer.class.getClassLoader(),
|
|
262 |
new Class<?>[] {MBeanServer.class},
|
|
263 |
new ForwardIH(mbsImpl));
|
|
264 |
return evalQuery(query, mbs, name);
|
|
265 |
}
|
|
266 |
|
|
267 |
private static boolean evalQuery(
|
|
268 |
QueryExp query, MBeanServer mbs, ObjectName name) {
|
|
269 |
MBeanServer oldMBS = QueryEval.getMBeanServer();
|
|
270 |
try {
|
|
271 |
if (mbs != null)
|
|
272 |
query.setMBeanServer(mbs);
|
|
273 |
return query.apply(name);
|
|
274 |
} catch (Exception e) {
|
|
275 |
return false;
|
|
276 |
} finally {
|
|
277 |
query.setMBeanServer(oldMBS);
|
|
278 |
}
|
|
279 |
}
|
|
280 |
|
|
281 |
private static class ForwardIH implements InvocationHandler {
|
|
282 |
private final MBS mbs;
|
|
283 |
|
|
284 |
ForwardIH(MBS mbs) {
|
|
285 |
this.mbs = mbs;
|
|
286 |
}
|
|
287 |
|
|
288 |
public Object invoke(Object proxy, Method method, Object[] args)
|
|
289 |
throws Throwable {
|
|
290 |
Method forward;
|
|
291 |
try {
|
|
292 |
forward = MBS.class.getMethod(
|
|
293 |
method.getName(), method.getParameterTypes());
|
|
294 |
} catch (NoSuchMethodException e) {
|
|
295 |
throw new UnsupportedOperationException(method.getName());
|
|
296 |
}
|
|
297 |
try {
|
|
298 |
return forward.invoke(mbs, args);
|
|
299 |
} catch (InvocationTargetException e) {
|
|
300 |
throw e.getCause();
|
|
301 |
}
|
|
302 |
}
|
|
303 |
}
|
|
304 |
|
|
305 |
private static class MBS {
|
|
306 |
private final Notification notification;
|
|
307 |
private final ObjectName objectName;
|
|
308 |
private final ObjectInstance objectInstance;
|
|
309 |
private volatile DynamicMBean mbean;
|
|
310 |
|
|
311 |
MBS(Notification n, ObjectName name) {
|
|
312 |
this.notification = n;
|
|
313 |
this.objectName = name;
|
|
314 |
this.objectInstance = new ObjectInstance(name, n.getClass().getName());
|
|
315 |
}
|
|
316 |
|
|
317 |
private void checkName(ObjectName name) throws InstanceNotFoundException {
|
|
318 |
if (!objectName.equals(name))
|
|
319 |
throw new InstanceNotFoundException(String.valueOf(name));
|
|
320 |
}
|
|
321 |
|
|
322 |
private DynamicMBean mbean(ObjectName name)
|
|
323 |
throws InstanceNotFoundException, ReflectionException {
|
|
324 |
if (mbean == null) {
|
|
325 |
try {
|
|
326 |
mbean = new NotificationMBeanSupport(notification);
|
|
327 |
} catch (NotCompliantMBeanException e) {
|
|
328 |
throw new ReflectionException(e);
|
|
329 |
}
|
|
330 |
}
|
|
331 |
return mbean;
|
|
332 |
}
|
|
333 |
|
|
334 |
public ObjectInstance getObjectInstance(ObjectName name)
|
|
335 |
throws InstanceNotFoundException {
|
|
336 |
checkName(name);
|
|
337 |
return objectInstance;
|
|
338 |
}
|
|
339 |
|
|
340 |
public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) {
|
|
341 |
Set<ObjectName> names = queryNames(name, query);
|
|
342 |
switch (names.size()) {
|
|
343 |
case 0:
|
|
344 |
return Collections.emptySet();
|
|
345 |
case 1:
|
|
346 |
return Collections.singleton(objectInstance);
|
|
347 |
default:
|
|
348 |
throw new UnsupportedOperationException("Internal error");
|
|
349 |
}
|
|
350 |
}
|
|
351 |
|
|
352 |
public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
|
|
353 |
if ((name != null && !name.apply(objectName)) ||
|
|
354 |
(query != null && !evalQuery(query, null, name)))
|
|
355 |
return Collections.emptySet();
|
|
356 |
return Collections.singleton(objectName);
|
|
357 |
}
|
|
358 |
|
|
359 |
public boolean isRegistered(ObjectName name) {
|
|
360 |
return objectName.equals(name);
|
|
361 |
}
|
|
362 |
|
|
363 |
public Integer getMBeanCount() {
|
|
364 |
return 1;
|
|
365 |
}
|
|
366 |
|
|
367 |
public Object getAttribute(ObjectName name, String attribute)
|
|
368 |
throws MBeanException, AttributeNotFoundException,
|
|
369 |
InstanceNotFoundException, ReflectionException {
|
|
370 |
return mbean(name).getAttribute(attribute);
|
|
371 |
}
|
|
372 |
|
|
373 |
public AttributeList getAttributes(ObjectName name, String[] attributes)
|
|
374 |
throws InstanceNotFoundException, ReflectionException {
|
|
375 |
return mbean(name).getAttributes(attributes);
|
|
376 |
}
|
|
377 |
|
|
378 |
public String getDefaultDomain() {
|
|
379 |
return objectName.getDomain();
|
|
380 |
}
|
|
381 |
|
|
382 |
public String[] getDomains() {
|
|
383 |
return new String[] {objectName.getDomain()};
|
|
384 |
}
|
|
385 |
|
|
386 |
public MBeanInfo getMBeanInfo(ObjectName name)
|
|
387 |
throws InstanceNotFoundException, ReflectionException {
|
|
388 |
return mbean(name).getMBeanInfo();
|
|
389 |
}
|
|
390 |
|
|
391 |
public boolean isInstanceOf(ObjectName name, String className)
|
|
392 |
throws InstanceNotFoundException {
|
|
393 |
try {
|
|
394 |
mbean(name);
|
|
395 |
ClassLoader loader = notification.getClass().getClassLoader();
|
|
396 |
Class<?> c = Class.forName(className, false, loader);
|
|
397 |
return c.isInstance(notification);
|
|
398 |
} catch (ReflectionException e) {
|
|
399 |
return false;
|
|
400 |
} catch (ClassNotFoundException e) {
|
|
401 |
return false;
|
|
402 |
}
|
|
403 |
}
|
|
404 |
|
|
405 |
public ClassLoader getClassLoaderFor(ObjectName mbeanName)
|
|
406 |
throws InstanceNotFoundException {
|
|
407 |
checkName(mbeanName);
|
|
408 |
return notification.getClass().getClassLoader();
|
|
409 |
}
|
|
410 |
}
|
|
411 |
}
|