2
|
1 |
/*
|
43235
|
2 |
* Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
|
2
|
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
|
5506
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
2
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
5506
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
2
|
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 |
*
|
5506
|
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.
|
2
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.management.relation;
|
|
27 |
|
|
28 |
import static com.sun.jmx.mbeanserver.Util.cast;
|
|
29 |
import static com.sun.jmx.defaults.JmxProperties.RELATION_LOGGER;
|
|
30 |
import com.sun.jmx.mbeanserver.GetPropertyAction;
|
|
31 |
|
|
32 |
import java.io.IOException;
|
|
33 |
import java.io.ObjectInputStream;
|
|
34 |
import java.io.ObjectOutputStream;
|
|
35 |
import java.io.ObjectStreamField;
|
|
36 |
import java.security.AccessController;
|
|
37 |
|
|
38 |
import java.util.List;
|
|
39 |
import java.util.Vector;
|
|
40 |
|
|
41 |
import javax.management.MBeanServerNotification;
|
|
42 |
|
|
43 |
import javax.management.Notification;
|
|
44 |
import javax.management.NotificationFilterSupport;
|
|
45 |
import javax.management.ObjectName;
|
|
46 |
|
|
47 |
import java.util.List;
|
43235
|
48 |
import java.lang.System.Logger.Level;
|
2
|
49 |
import java.util.Vector;
|
|
50 |
|
|
51 |
/**
|
|
52 |
* Filter for {@link MBeanServerNotification}.
|
|
53 |
* This filter filters MBeanServerNotification notifications by
|
|
54 |
* selecting the ObjectNames of interest and the operations (registration,
|
|
55 |
* unregistration, both) of interest (corresponding to notification
|
|
56 |
* types).
|
|
57 |
*
|
|
58 |
* <p>The <b>serialVersionUID</b> of this class is <code>2605900539589789736L</code>.
|
|
59 |
*
|
|
60 |
* @since 1.5
|
|
61 |
*/
|
|
62 |
@SuppressWarnings("serial") // serialVersionUID must be constant
|
|
63 |
public class MBeanServerNotificationFilter extends NotificationFilterSupport {
|
|
64 |
|
|
65 |
// Serialization compatibility stuff:
|
|
66 |
// Two serial forms are supported in this class. The selected form depends
|
|
67 |
// on system property "jmx.serial.form":
|
|
68 |
// - "1.0" for JMX 1.0
|
|
69 |
// - any other value for JMX 1.1 and higher
|
|
70 |
//
|
|
71 |
// Serial version for old serial form
|
|
72 |
private static final long oldSerialVersionUID = 6001782699077323605L;
|
|
73 |
//
|
|
74 |
// Serial version for new serial form
|
|
75 |
private static final long newSerialVersionUID = 2605900539589789736L;
|
|
76 |
//
|
|
77 |
// Serializable fields in old serial form
|
|
78 |
private static final ObjectStreamField[] oldSerialPersistentFields =
|
|
79 |
{
|
|
80 |
new ObjectStreamField("mySelectObjNameList", Vector.class),
|
|
81 |
new ObjectStreamField("myDeselectObjNameList", Vector.class)
|
|
82 |
};
|
|
83 |
//
|
|
84 |
// Serializable fields in new serial form
|
|
85 |
private static final ObjectStreamField[] newSerialPersistentFields =
|
|
86 |
{
|
|
87 |
new ObjectStreamField("selectedNames", List.class),
|
|
88 |
new ObjectStreamField("deselectedNames", List.class)
|
|
89 |
};
|
|
90 |
//
|
|
91 |
// Actual serial version and serial form
|
|
92 |
private static final long serialVersionUID;
|
|
93 |
/**
|
|
94 |
* @serialField selectedNames List List of {@link ObjectName}s of interest
|
|
95 |
* <ul>
|
|
96 |
* <li><code>null</code> means that all {@link ObjectName}s are implicitly selected
|
|
97 |
* (check for explicit deselections)</li>
|
|
98 |
* <li>Empty vector means that no {@link ObjectName} is explicitly selected</li>
|
|
99 |
* </ul>
|
|
100 |
* @serialField deselectedNames List List of {@link ObjectName}s with no interest
|
|
101 |
* <ul>
|
|
102 |
* <li><code>null</code> means that all {@link ObjectName}s are implicitly deselected
|
|
103 |
* (check for explicit selections))</li>
|
|
104 |
* <li>Empty vector means that no {@link ObjectName} is explicitly deselected</li>
|
|
105 |
* </ul>
|
|
106 |
*/
|
|
107 |
private static final ObjectStreamField[] serialPersistentFields;
|
|
108 |
private static boolean compat = false;
|
|
109 |
static {
|
|
110 |
try {
|
|
111 |
GetPropertyAction act = new GetPropertyAction("jmx.serial.form");
|
|
112 |
String form = AccessController.doPrivileged(act);
|
|
113 |
compat = (form != null && form.equals("1.0"));
|
|
114 |
} catch (Exception e) {
|
|
115 |
// OK : Too bad, no compat with 1.0
|
|
116 |
}
|
|
117 |
if (compat) {
|
|
118 |
serialPersistentFields = oldSerialPersistentFields;
|
|
119 |
serialVersionUID = oldSerialVersionUID;
|
|
120 |
} else {
|
|
121 |
serialPersistentFields = newSerialPersistentFields;
|
|
122 |
serialVersionUID = newSerialVersionUID;
|
|
123 |
}
|
|
124 |
}
|
|
125 |
//
|
|
126 |
// END Serialization compatibility stuff
|
|
127 |
|
|
128 |
//
|
|
129 |
// Private members
|
|
130 |
//
|
|
131 |
|
|
132 |
/**
|
|
133 |
* @serial List of {@link ObjectName}s of interest
|
|
134 |
* <ul>
|
|
135 |
* <li><code>null</code> means that all {@link ObjectName}s are implicitly selected
|
|
136 |
* (check for explicit deselections)</li>
|
|
137 |
* <li>Empty vector means that no {@link ObjectName} is explicitly selected</li>
|
|
138 |
* </ul>
|
|
139 |
*/
|
|
140 |
private List<ObjectName> selectedNames = new Vector<ObjectName>();
|
|
141 |
|
|
142 |
/**
|
|
143 |
* @serial List of {@link ObjectName}s with no interest
|
|
144 |
* <ul>
|
|
145 |
* <li><code>null</code> means that all {@link ObjectName}s are implicitly deselected
|
|
146 |
* (check for explicit selections))</li>
|
|
147 |
* <li>Empty vector means that no {@link ObjectName} is explicitly deselected</li>
|
|
148 |
* </ul>
|
|
149 |
*/
|
|
150 |
private List<ObjectName> deselectedNames = null;
|
|
151 |
|
|
152 |
//
|
|
153 |
// Constructor
|
|
154 |
//
|
|
155 |
|
|
156 |
/**
|
|
157 |
* Creates a filter selecting all MBeanServerNotification notifications for
|
|
158 |
* all ObjectNames.
|
|
159 |
*/
|
|
160 |
public MBeanServerNotificationFilter() {
|
|
161 |
|
|
162 |
super();
|
43235
|
163 |
RELATION_LOGGER.log(Level.TRACE, "ENTRY");
|
2
|
164 |
|
|
165 |
enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION);
|
|
166 |
enableType(MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
|
|
167 |
|
43235
|
168 |
RELATION_LOGGER.log(Level.TRACE, "RETURN");
|
2
|
169 |
return;
|
|
170 |
}
|
|
171 |
|
|
172 |
//
|
|
173 |
// Accessors
|
|
174 |
//
|
|
175 |
|
|
176 |
/**
|
|
177 |
* Disables any MBeanServerNotification (all ObjectNames are
|
|
178 |
* deselected).
|
|
179 |
*/
|
|
180 |
public synchronized void disableAllObjectNames() {
|
|
181 |
|
43235
|
182 |
RELATION_LOGGER.log(Level.TRACE, "ENTRY");
|
2
|
183 |
|
|
184 |
selectedNames = new Vector<ObjectName>();
|
|
185 |
deselectedNames = null;
|
|
186 |
|
43235
|
187 |
RELATION_LOGGER.log(Level.TRACE, "RETURN");
|
2
|
188 |
return;
|
|
189 |
}
|
|
190 |
|
|
191 |
/**
|
|
192 |
* Disables MBeanServerNotifications concerning given ObjectName.
|
|
193 |
*
|
|
194 |
* @param objectName ObjectName no longer of interest
|
|
195 |
*
|
|
196 |
* @exception IllegalArgumentException if the given ObjectName is null
|
|
197 |
*/
|
|
198 |
public synchronized void disableObjectName(ObjectName objectName)
|
|
199 |
throws IllegalArgumentException {
|
|
200 |
|
|
201 |
if (objectName == null) {
|
|
202 |
String excMsg = "Invalid parameter.";
|
|
203 |
throw new IllegalArgumentException(excMsg);
|
|
204 |
}
|
|
205 |
|
43235
|
206 |
RELATION_LOGGER.log(Level.TRACE, "ENTRY {0}" + objectName);
|
2
|
207 |
|
|
208 |
// Removes from selected ObjectNames, if present
|
|
209 |
if (selectedNames != null) {
|
|
210 |
if (selectedNames.size() != 0) {
|
|
211 |
selectedNames.remove(objectName);
|
|
212 |
}
|
|
213 |
}
|
|
214 |
|
|
215 |
// Adds it in deselected ObjectNames
|
|
216 |
if (deselectedNames != null) {
|
|
217 |
// If all are deselected, no need to do anything :)
|
|
218 |
if (!(deselectedNames.contains(objectName))) {
|
|
219 |
// ObjectName was not already deselected
|
|
220 |
deselectedNames.add(objectName);
|
|
221 |
}
|
|
222 |
}
|
|
223 |
|
43235
|
224 |
RELATION_LOGGER.log(Level.TRACE, "RETURN");
|
2
|
225 |
return;
|
|
226 |
}
|
|
227 |
|
|
228 |
/**
|
|
229 |
* Enables all MBeanServerNotifications (all ObjectNames are selected).
|
|
230 |
*/
|
|
231 |
public synchronized void enableAllObjectNames() {
|
|
232 |
|
43235
|
233 |
RELATION_LOGGER.log(Level.TRACE, "ENTRY");
|
2
|
234 |
|
|
235 |
selectedNames = null;
|
|
236 |
deselectedNames = new Vector<ObjectName>();
|
|
237 |
|
43235
|
238 |
RELATION_LOGGER.log(Level.TRACE, "RETURN");
|
2
|
239 |
return;
|
|
240 |
}
|
|
241 |
|
|
242 |
/**
|
|
243 |
* Enables MBeanServerNotifications concerning given ObjectName.
|
|
244 |
*
|
|
245 |
* @param objectName ObjectName of interest
|
|
246 |
*
|
|
247 |
* @exception IllegalArgumentException if the given ObjectName is null
|
|
248 |
*/
|
|
249 |
public synchronized void enableObjectName(ObjectName objectName)
|
|
250 |
throws IllegalArgumentException {
|
|
251 |
|
|
252 |
if (objectName == null) {
|
|
253 |
String excMsg = "Invalid parameter.";
|
|
254 |
throw new IllegalArgumentException(excMsg);
|
|
255 |
}
|
|
256 |
|
43235
|
257 |
RELATION_LOGGER.log(Level.TRACE, "ENTRY {0}", objectName);
|
2
|
258 |
|
|
259 |
// Removes from deselected ObjectNames, if present
|
|
260 |
if (deselectedNames != null) {
|
|
261 |
if (deselectedNames.size() != 0) {
|
|
262 |
deselectedNames.remove(objectName);
|
|
263 |
}
|
|
264 |
}
|
|
265 |
|
|
266 |
// Adds it in selected ObjectNames
|
|
267 |
if (selectedNames != null) {
|
|
268 |
// If all are selected, no need to do anything :)
|
|
269 |
if (!(selectedNames.contains(objectName))) {
|
|
270 |
// ObjectName was not already selected
|
|
271 |
selectedNames.add(objectName);
|
|
272 |
}
|
|
273 |
}
|
|
274 |
|
43235
|
275 |
RELATION_LOGGER.log(Level.TRACE, "RETURN");
|
2
|
276 |
return;
|
|
277 |
}
|
|
278 |
|
|
279 |
/**
|
|
280 |
* Gets all the ObjectNames enabled.
|
|
281 |
*
|
|
282 |
* @return Vector of ObjectNames:
|
|
283 |
* <P>- null means all ObjectNames are implicitly selected, except the
|
|
284 |
* ObjectNames explicitly deselected
|
|
285 |
* <P>- empty means all ObjectNames are deselected, i.e. no ObjectName
|
|
286 |
* selected.
|
|
287 |
*/
|
|
288 |
public synchronized Vector<ObjectName> getEnabledObjectNames() {
|
|
289 |
if (selectedNames != null) {
|
|
290 |
return new Vector<ObjectName>(selectedNames);
|
|
291 |
} else {
|
|
292 |
return null;
|
|
293 |
}
|
|
294 |
}
|
|
295 |
|
|
296 |
/**
|
|
297 |
* Gets all the ObjectNames disabled.
|
|
298 |
*
|
|
299 |
* @return Vector of ObjectNames:
|
|
300 |
* <P>- null means all ObjectNames are implicitly deselected, except the
|
|
301 |
* ObjectNames explicitly selected
|
|
302 |
* <P>- empty means all ObjectNames are selected, i.e. no ObjectName
|
|
303 |
* deselected.
|
|
304 |
*/
|
|
305 |
public synchronized Vector<ObjectName> getDisabledObjectNames() {
|
|
306 |
if (deselectedNames != null) {
|
|
307 |
return new Vector<ObjectName>(deselectedNames);
|
|
308 |
} else {
|
|
309 |
return null;
|
|
310 |
}
|
|
311 |
}
|
|
312 |
|
|
313 |
//
|
|
314 |
// NotificationFilter interface
|
|
315 |
//
|
|
316 |
|
|
317 |
/**
|
|
318 |
* Invoked before sending the specified notification to the listener.
|
|
319 |
* <P>If:
|
|
320 |
* <P>- the ObjectName of the concerned MBean is selected (explicitly OR
|
|
321 |
* (implicitly and not explicitly deselected))
|
|
322 |
* <P>AND
|
|
323 |
* <P>- the type of the operation (registration or unregistration) is
|
|
324 |
* selected
|
|
325 |
* <P>then the notification is sent to the listener.
|
|
326 |
*
|
|
327 |
* @param notif The notification to be sent.
|
|
328 |
*
|
|
329 |
* @return true if the notification has to be sent to the listener, false
|
|
330 |
* otherwise.
|
|
331 |
*
|
|
332 |
* @exception IllegalArgumentException if null parameter
|
|
333 |
*/
|
|
334 |
public synchronized boolean isNotificationEnabled(Notification notif)
|
|
335 |
throws IllegalArgumentException {
|
|
336 |
|
|
337 |
if (notif == null) {
|
|
338 |
String excMsg = "Invalid parameter.";
|
|
339 |
throw new IllegalArgumentException(excMsg);
|
|
340 |
}
|
|
341 |
|
43235
|
342 |
RELATION_LOGGER.log(Level.TRACE, "ENTRY {0}", notif);
|
2
|
343 |
|
|
344 |
// Checks the type first
|
|
345 |
String ntfType = notif.getType();
|
1510
|
346 |
Vector<String> enabledTypes = getEnabledTypes();
|
2
|
347 |
if (!(enabledTypes.contains(ntfType))) {
|
43235
|
348 |
RELATION_LOGGER.log(Level.TRACE,
|
2
|
349 |
"Type not selected, exiting");
|
|
350 |
return false;
|
|
351 |
}
|
|
352 |
|
|
353 |
// We have a MBeanServerNotification: downcasts it
|
|
354 |
MBeanServerNotification mbsNtf = (MBeanServerNotification)notif;
|
|
355 |
|
|
356 |
// Checks the ObjectName
|
|
357 |
ObjectName objName = mbsNtf.getMBeanName();
|
|
358 |
// Is it selected?
|
|
359 |
boolean isSelectedFlg = false;
|
|
360 |
if (selectedNames != null) {
|
|
361 |
// Not all are implicitly selected:
|
|
362 |
// checks for explicit selection
|
|
363 |
if (selectedNames.size() == 0) {
|
|
364 |
// All are explicitly not selected
|
43235
|
365 |
RELATION_LOGGER.log(Level.TRACE,
|
2
|
366 |
"No ObjectNames selected, exiting");
|
|
367 |
return false;
|
|
368 |
}
|
|
369 |
|
|
370 |
isSelectedFlg = selectedNames.contains(objName);
|
|
371 |
if (!isSelectedFlg) {
|
|
372 |
// Not in the explicit selected list
|
43235
|
373 |
RELATION_LOGGER.log(Level.TRACE,
|
2
|
374 |
"ObjectName not in selected list, exiting");
|
|
375 |
return false;
|
|
376 |
}
|
|
377 |
}
|
|
378 |
|
|
379 |
if (!isSelectedFlg) {
|
|
380 |
// Not explicitly selected: is it deselected?
|
|
381 |
|
|
382 |
if (deselectedNames == null) {
|
|
383 |
// All are implicitly deselected and it is not explicitly
|
|
384 |
// selected
|
43235
|
385 |
RELATION_LOGGER.log(Level.TRACE,
|
2
|
386 |
"ObjectName not selected, and all " +
|
|
387 |
"names deselected, exiting");
|
|
388 |
return false;
|
|
389 |
|
|
390 |
} else if (deselectedNames.contains(objName)) {
|
|
391 |
// Explicitly deselected
|
43235
|
392 |
RELATION_LOGGER.log(Level.TRACE,
|
2
|
393 |
"ObjectName explicitly not selected, exiting");
|
|
394 |
return false;
|
|
395 |
}
|
|
396 |
}
|
|
397 |
|
43235
|
398 |
RELATION_LOGGER.log(Level.TRACE,
|
2
|
399 |
"ObjectName selected, exiting");
|
|
400 |
return true;
|
|
401 |
}
|
|
402 |
|
|
403 |
|
|
404 |
/**
|
|
405 |
* Deserializes an {@link MBeanServerNotificationFilter} from an {@link ObjectInputStream}.
|
|
406 |
*/
|
|
407 |
private void readObject(ObjectInputStream in)
|
|
408 |
throws IOException, ClassNotFoundException {
|
|
409 |
if (compat)
|
|
410 |
{
|
|
411 |
// Read an object serialized in the old serial form
|
|
412 |
//
|
|
413 |
ObjectInputStream.GetField fields = in.readFields();
|
|
414 |
selectedNames = cast(fields.get("mySelectObjNameList", null));
|
|
415 |
if (fields.defaulted("mySelectObjNameList"))
|
|
416 |
{
|
|
417 |
throw new NullPointerException("mySelectObjNameList");
|
|
418 |
}
|
|
419 |
deselectedNames = cast(fields.get("myDeselectObjNameList", null));
|
|
420 |
if (fields.defaulted("myDeselectObjNameList"))
|
|
421 |
{
|
|
422 |
throw new NullPointerException("myDeselectObjNameList");
|
|
423 |
}
|
|
424 |
}
|
|
425 |
else
|
|
426 |
{
|
|
427 |
// Read an object serialized in the new serial form
|
|
428 |
//
|
|
429 |
in.defaultReadObject();
|
|
430 |
}
|
|
431 |
}
|
|
432 |
|
|
433 |
|
|
434 |
/**
|
|
435 |
* Serializes an {@link MBeanServerNotificationFilter} to an {@link ObjectOutputStream}.
|
|
436 |
*/
|
|
437 |
private void writeObject(ObjectOutputStream out)
|
|
438 |
throws IOException {
|
|
439 |
if (compat)
|
|
440 |
{
|
|
441 |
// Serializes this instance in the old serial form
|
|
442 |
//
|
|
443 |
ObjectOutputStream.PutField fields = out.putFields();
|
1510
|
444 |
fields.put("mySelectObjNameList", selectedNames);
|
|
445 |
fields.put("myDeselectObjNameList", deselectedNames);
|
2
|
446 |
out.writeFields();
|
|
447 |
}
|
|
448 |
else
|
|
449 |
{
|
|
450 |
// Serializes this instance in the new serial form
|
|
451 |
//
|
|
452 |
out.defaultWriteObject();
|
|
453 |
}
|
|
454 |
}
|
|
455 |
}
|