1 7 8 package javax.management.relation; 9 10 import java.io.IOException ; 11 import java.io.ObjectInputStream ; 12 import java.io.ObjectOutputStream ; 13 import java.io.ObjectStreamField ; 14 import java.security.AccessController ; 15 import java.security.PrivilegedAction ; 16 17 import javax.management.Notification ; 18 import javax.management.NotificationFilterSupport ; 19 import javax.management.MBeanServerNotification ; 20 import javax.management.ObjectName ; 21 22 import java.util.List ; 23 import java.util.Vector ; 24 25 import com.sun.jmx.mbeanserver.GetPropertyAction; 26 import com.sun.jmx.trace.Trace; 27 28 37 public class MBeanServerNotificationFilter extends NotificationFilterSupport { 38 39 private static final long oldSerialVersionUID = 6001782699077323605L; 47 private static final long newSerialVersionUID = 2605900539589789736L; 50 private static final ObjectStreamField [] oldSerialPersistentFields = 53 { 54 new ObjectStreamField ("mySelectObjNameList", Vector .class), 55 new ObjectStreamField ("myDeselectObjNameList", Vector .class) 56 }; 57 private static final ObjectStreamField [] newSerialPersistentFields = 60 { 61 new ObjectStreamField ("selectedNames", List .class), 62 new ObjectStreamField ("deselectedNames", List .class) 63 }; 64 private static final long serialVersionUID; 67 81 private static final ObjectStreamField [] serialPersistentFields; 82 private static boolean compat = false; 83 static { 84 try { 85 PrivilegedAction act = new GetPropertyAction("jmx.serial.form"); 86 String form = (String ) AccessController.doPrivileged(act); 87 compat = (form != null && form.equals("1.0")); 88 } catch (Exception e) { 89 } 91 if (compat) { 92 serialPersistentFields = oldSerialPersistentFields; 93 serialVersionUID = oldSerialVersionUID; 94 } else { 95 serialPersistentFields = newSerialPersistentFields; 96 serialVersionUID = newSerialVersionUID; 97 } 98 } 99 102 106 114 private List selectedNames = new Vector (); 115 116 124 private List deselectedNames = null; 125 126 130 134 public MBeanServerNotificationFilter() { 135 136 super(); 137 138 if (isTraceOn()) 139 trace("Constructor: entering", null); 140 141 enableType(MBeanServerNotification.REGISTRATION_NOTIFICATION); 142 enableType(MBeanServerNotification.UNREGISTRATION_NOTIFICATION); 143 144 if (isTraceOn()) 145 trace("Constructor: exiting", null); 146 return; 147 } 148 149 153 157 public synchronized void disableAllObjectNames() { 158 159 if (isTraceOn()) 160 trace("disableAllObjectNames: entering", null); 161 162 selectedNames = new Vector (); 163 deselectedNames = null; 164 165 if (isTraceOn()) 166 trace("disableAllObjectNames: exiting", null); 167 return; 168 } 169 170 177 public synchronized void disableObjectName(ObjectName theObjName) 178 throws IllegalArgumentException { 179 180 if (theObjName == null) { 181 String excMsg = "Invalid parameter."; 183 throw new IllegalArgumentException (excMsg); 184 } 185 186 if (isTraceOn()) 187 trace("disableObjectName: entering", theObjName.toString()); 188 189 if (selectedNames != null) { 191 if (selectedNames.size() != 0) { 192 selectedNames.remove(theObjName); 193 } 194 } 195 196 if (deselectedNames != null) { 198 if (!(deselectedNames.contains(theObjName))) { 200 deselectedNames.add(theObjName); 202 } 203 } 204 205 if (isTraceOn()) 206 trace("disableObjectName: exiting", null); 207 return; 208 } 209 210 213 public synchronized void enableAllObjectNames() { 214 215 if (isTraceOn()) 216 trace("enableAllObjectNames: entering", null); 217 218 selectedNames = null; 219 deselectedNames = new Vector (); 220 221 if (isTraceOn()) 222 trace("enableAllObjectNames: exiting", null); 223 return; 224 } 225 226 233 public synchronized void enableObjectName(ObjectName theObjName) 234 throws IllegalArgumentException { 235 236 if (theObjName == null) { 237 String excMsg = "Invalid parameter."; 239 throw new IllegalArgumentException (excMsg); 240 } 241 242 if (isTraceOn()) 243 trace("enableObjectName: entering", theObjName.toString()); 244 245 if (deselectedNames != null) { 247 if (deselectedNames.size() != 0) { 248 deselectedNames.remove(theObjName); 249 } 250 } 251 252 if (selectedNames != null) { 254 if (!(selectedNames.contains(theObjName))) { 256 selectedNames.add(theObjName); 258 } 259 } 260 261 if (isTraceOn()) 262 trace("enableObjectName: exiting", null); 263 return; 264 } 265 266 275 public synchronized Vector getEnabledObjectNames() { 276 if (selectedNames != null) { 277 return (Vector )((Vector )selectedNames).clone(); 278 } else { 279 return null; 280 } 281 } 282 283 292 public synchronized Vector getDisabledObjectNames() { 293 if (deselectedNames != null) { 294 return (Vector )((Vector )deselectedNames).clone(); 295 } else { 296 return null; 297 } 298 } 299 300 304 321 public synchronized boolean isNotificationEnabled(Notification theNtf) 322 throws IllegalArgumentException { 323 324 if (theNtf == null) { 325 String excMsg = "Invalid parameter."; 327 throw new IllegalArgumentException (excMsg); 328 } 329 330 if (isTraceOn()) 331 trace("isNotificationEnabled: entering", theNtf.toString()); 332 333 String ntfType = theNtf.getType(); 335 Vector enabledTypes = getEnabledTypes(); 336 if (!(enabledTypes.contains(ntfType))) { 337 338 if (isTraceOn()) 339 trace("isNotificationEnabled: type not selected, exiting", null); 340 return false; 341 } 342 343 MBeanServerNotification mbsNtf = (MBeanServerNotification )theNtf; 345 346 ObjectName objName = mbsNtf.getMBeanName(); 348 boolean isSelectedFlg = false; 350 if (selectedNames != null) { 351 if (selectedNames.size() == 0) { 354 if (isTraceOn()) 356 trace("isNotificationEnabled: no ObjectNames selected, exiting", null); 357 return false; 358 } 359 360 isSelectedFlg = selectedNames.contains(objName); 361 if (!isSelectedFlg) { 362 if (isTraceOn()) 364 trace("isNotificationEnabled: ObjectName not in selected list, exiting", null); 365 return false; 366 } 367 } 368 369 if (!isSelectedFlg) { 370 372 if (deselectedNames == null) { 373 if (isTraceOn()) 376 trace("isNotificationEnabled: ObjectName not selected and all deselectedm, exiting", null); 377 return false; 378 379 } else if (deselectedNames.contains(objName)) { 380 if (isTraceOn()) 382 trace("isNotificationEnabled: ObjectName explicitly not selected, exiting", null); 383 return false; 384 } 385 } 386 387 if (isTraceOn()) 388 trace("isNotificationEnabled: ObjectName selected, exiting", null); 389 return true; 390 } 391 392 394 private static String localClassName = "MBeanServerNotificationFilter"; 395 396 private boolean isTraceOn() { 398 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_RELATION); 399 } 400 401 405 private void trace(String methodName, String info) { 406 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_RELATION, localClassName, methodName, info); 407 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_RELATION, "", "", "\n"); 408 } 409 410 414 418 private boolean isDebugOn() { 420 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_RELATION); 421 } 422 423 427 private void debug(String methodName, String info) { 428 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_RELATION, localClassName, methodName, info); 429 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_RELATION, "", "", "\n"); 430 } 431 432 436 440 443 private void readObject(ObjectInputStream in) 444 throws IOException , ClassNotFoundException { 445 if (compat) 446 { 447 ObjectInputStream.GetField fields = in.readFields(); 450 selectedNames = (List ) fields.get("mySelectObjNameList", null); 451 if (fields.defaulted("mySelectObjNameList")) 452 { 453 throw new NullPointerException ("mySelectObjNameList"); 454 } 455 deselectedNames = (List ) fields.get("myDeselectObjNameList", null); 456 if (fields.defaulted("myDeselectObjNameList")) 457 { 458 throw new NullPointerException ("myDeselectObjNameList"); 459 } 460 } 461 else 462 { 463 in.defaultReadObject(); 466 } 467 } 468 469 470 473 private void writeObject(ObjectOutputStream out) 474 throws IOException { 475 if (compat) 476 { 477 ObjectOutputStream.PutField fields = out.putFields(); 480 fields.put("mySelectObjNameList", (Vector )selectedNames); 481 fields.put("myDeselectObjNameList", (Vector )deselectedNames); 482 out.writeFields(); 483 } 484 else 485 { 486 out.defaultWriteObject(); 489 } 490 } 491 } 492 | Popular Tags |