use of javax.management.NotificationBroadcaster in project jdk8u_jdk by JetBrains.
the class DefaultMBeanServerInterceptor method removeNotificationListener.
private void removeNotificationListener(ObjectName name, NotificationListener listener, NotificationFilter filter, Object handback, boolean removeAll) throws InstanceNotFoundException, ListenerNotFoundException {
if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
MBEANSERVER_LOGGER.logp(Level.FINER, DefaultMBeanServerInterceptor.class.getName(), "removeNotificationListener", "ObjectName = " + name);
}
DynamicMBean instance = getMBean(name);
checkMBeanPermission(instance, null, name, "removeNotificationListener");
/* We could simplify the code by assigning broadcaster after
assigning listenerWrapper, but that would change the error
behavior when both the broadcaster and the listener are
erroneous. */
Class<? extends NotificationBroadcaster> reqClass = removeAll ? NotificationBroadcaster.class : NotificationEmitter.class;
NotificationBroadcaster broadcaster = getNotificationBroadcaster(name, instance, reqClass);
NotificationListener listenerWrapper = getListenerWrapper(listener, name, instance, false);
if (listenerWrapper == null)
throw new ListenerNotFoundException("Unknown listener");
if (removeAll)
broadcaster.removeNotificationListener(listenerWrapper);
else {
NotificationEmitter emitter = (NotificationEmitter) broadcaster;
emitter.removeNotificationListener(listenerWrapper, filter, handback);
}
}
use of javax.management.NotificationBroadcaster in project jdk8u_jdk by JetBrains.
the class StandardMBeanIntrospector method isDefinitelyImmutableInfo.
/* Return true if and only if we can be sure that the given MBean implementation
* class has immutable MBeanInfo. A Standard MBean that is a
* NotificationBroadcaster is allowed to return different values at
* different times from its getNotificationInfo() method, which is when
* we might not know if it is immutable. But if it is a subclass of
* NotificationBroadcasterSupport and does not override
* getNotificationInfo(), then we know it won't change.
*/
static boolean isDefinitelyImmutableInfo(Class<?> implClass) {
if (!NotificationBroadcaster.class.isAssignableFrom(implClass))
return true;
synchronized (definitelyImmutable) {
Boolean immutable = definitelyImmutable.get(implClass);
if (immutable == null) {
final Class<NotificationBroadcasterSupport> nbs = NotificationBroadcasterSupport.class;
if (nbs.isAssignableFrom(implClass)) {
try {
Method m = implClass.getMethod("getNotificationInfo");
immutable = (m.getDeclaringClass() == nbs);
} catch (Exception e) {
// Too bad, we'll say no for now.
return false;
}
} else
immutable = false;
definitelyImmutable.put(implClass, immutable);
}
return immutable;
}
}
use of javax.management.NotificationBroadcaster in project jdk8u_jdk by JetBrains.
the class DefaultMBeanServerInterceptor method addNotificationListener.
/*
* Notification handling.
*
* This is not trivial, because the MBeanServer translates the
* source of a received notification from a reference to an MBean
* into the ObjectName of that MBean. While that does make
* notification sending easier for MBean writers, it comes at a
* considerable cost. We need to replace the source of a
* notification, which is basically wrong if there are also
* listeners registered directly with the MBean (without going
* through the MBean server). We also need to wrap the listener
* supplied by the client of the MBeanServer with a listener that
* performs the substitution before forwarding. This is why we
* strongly discourage people from putting MBean references in the
* source of their notifications. Instead they should arrange to
* put the ObjectName there themselves.
*
* However, existing code relies on the substitution, so we are
* stuck with it.
*
* Here's how we handle it. When you add a listener, we make a
* ListenerWrapper around it. We look that up in the
* listenerWrappers map, and if there was already a wrapper for
* that listener with the given ObjectName, we reuse it. This map
* is a WeakHashMap, so a listener that is no longer registered
* with any MBean can be garbage collected.
*
* We cannot use simpler solutions such as always creating a new
* wrapper or always registering the same listener with the MBean
* and using the handback to find the client's original listener.
* The reason is that we need to support the removeListener
* variant that removes all (listener,filter,handback) triples on
* a broadcaster that have a given listener. And we do not have
* any way to inspect a broadcaster's internal list of triples.
* So the same client listener must always map to the same
* listener registered with the broadcaster.
*
* Another possible solution would be to map from ObjectName to
* list of listener wrappers (or IdentityHashMap of listener
* wrappers), making this list the first time a listener is added
* on a given MBean, and removing it when the MBean is removed.
* This is probably more costly in memory, but could be useful if
* some day we don't want to rely on weak references.
*/
public void addNotificationListener(ObjectName name, NotificationListener listener, NotificationFilter filter, Object handback) throws InstanceNotFoundException {
// ------------------------------
if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
MBEANSERVER_LOGGER.logp(Level.FINER, DefaultMBeanServerInterceptor.class.getName(), "addNotificationListener", "ObjectName = " + name);
}
DynamicMBean instance = getMBean(name);
checkMBeanPermission(instance, null, name, "addNotificationListener");
NotificationBroadcaster broadcaster = getNotificationBroadcaster(name, instance, NotificationBroadcaster.class);
// ------------------
if (listener == null) {
throw new RuntimeOperationsException(new IllegalArgumentException("Null listener"), "Null listener");
}
NotificationListener listenerWrapper = getListenerWrapper(listener, name, instance, true);
broadcaster.addNotificationListener(listenerWrapper, filter, handback);
}
use of javax.management.NotificationBroadcaster in project jdk8u_jdk by JetBrains.
the class MBeanIntrospector method findNotifications.
static MBeanNotificationInfo[] findNotifications(Object moi) {
if (!(moi instanceof NotificationBroadcaster))
return null;
MBeanNotificationInfo[] mbn = ((NotificationBroadcaster) moi).getNotificationInfo();
if (mbn == null)
return null;
MBeanNotificationInfo[] result = new MBeanNotificationInfo[mbn.length];
for (int i = 0; i < mbn.length; i++) {
MBeanNotificationInfo ni = mbn[i];
if (ni.getClass() != MBeanNotificationInfo.class)
ni = (MBeanNotificationInfo) ni.clone();
result[i] = ni;
}
return result;
}
Aggregations