use of javax.management.DynamicMBean in project jdk8u_jdk by JetBrains.
the class ManagementFactory method getPlatformMBeanServer.
/**
* Returns the platform {@link javax.management.MBeanServer MBeanServer}.
* On the first call to this method, it first creates the platform
* {@code MBeanServer} by calling the
* {@link javax.management.MBeanServerFactory#createMBeanServer
* MBeanServerFactory.createMBeanServer}
* method and registers each platform MXBean in this platform
* {@code MBeanServer} with its
* {@link PlatformManagedObject#getObjectName ObjectName}.
* This method, in subsequent calls, will simply return the
* initially created platform {@code MBeanServer}.
* <p>
* MXBeans that get created and destroyed dynamically, for example,
* memory {@link MemoryPoolMXBean pools} and
* {@link MemoryManagerMXBean managers},
* will automatically be registered and deregistered into the platform
* {@code MBeanServer}.
* <p>
* If the system property {@code javax.management.builder.initial}
* is set, the platform {@code MBeanServer} creation will be done
* by the specified {@link javax.management.MBeanServerBuilder}.
* <p>
* It is recommended that this platform MBeanServer also be used
* to register other application managed beans
* besides the platform MXBeans.
* This will allow all MBeans to be published through the same
* {@code MBeanServer} and hence allow for easier network publishing
* and discovery.
* Name conflicts with the platform MXBeans should be avoided.
*
* @return the platform {@code MBeanServer}; the platform
* MXBeans are registered into the platform {@code MBeanServer}
* at the first time this method is called.
*
* @exception SecurityException if there is a security manager
* and the caller does not have the permission required by
* {@link javax.management.MBeanServerFactory#createMBeanServer}.
*
* @see javax.management.MBeanServerFactory
* @see javax.management.MBeanServerFactory#createMBeanServer
*/
public static synchronized MBeanServer getPlatformMBeanServer() {
SecurityManager sm = System.getSecurityManager();
if (sm != null) {
Permission perm = new MBeanServerPermission("createMBeanServer");
sm.checkPermission(perm);
}
if (platformMBeanServer == null) {
platformMBeanServer = MBeanServerFactory.createMBeanServer();
for (PlatformComponent pc : PlatformComponent.values()) {
List<? extends PlatformManagedObject> list = pc.getMXBeans(pc.getMXBeanInterface());
for (PlatformManagedObject o : list) {
// before registering into the platform MBeanServer
if (!platformMBeanServer.isRegistered(o.getObjectName())) {
addMXBean(platformMBeanServer, o);
}
}
}
HashMap<ObjectName, DynamicMBean> dynmbeans = ManagementFactoryHelper.getPlatformDynamicMBeans();
for (Map.Entry<ObjectName, DynamicMBean> e : dynmbeans.entrySet()) {
addDynamicMBean(platformMBeanServer, e.getValue(), e.getKey());
}
for (final PlatformManagedObject o : ExtendedPlatformComponent.getMXBeans()) {
if (!platformMBeanServer.isRegistered(o.getObjectName())) {
addMXBean(platformMBeanServer, o);
}
}
}
return platformMBeanServer;
}
use of javax.management.DynamicMBean 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.DynamicMBean in project jdk8u_jdk by JetBrains.
the class DefaultMBeanServerInterceptor method getAttribute.
public Object getAttribute(ObjectName name, String attribute) throws MBeanException, AttributeNotFoundException, InstanceNotFoundException, ReflectionException {
if (name == null) {
throw new RuntimeOperationsException(new IllegalArgumentException("Object name cannot be null"), "Exception occurred trying to invoke the getter on the MBean");
}
if (attribute == null) {
throw new RuntimeOperationsException(new IllegalArgumentException("Attribute cannot be null"), "Exception occurred trying to invoke the getter on the MBean");
}
name = nonDefaultDomain(name);
if (MBEANSERVER_LOGGER.isLoggable(Level.FINER)) {
MBEANSERVER_LOGGER.logp(Level.FINER, DefaultMBeanServerInterceptor.class.getName(), "getAttribute", "Attribute = " + attribute + ", ObjectName = " + name);
}
final DynamicMBean instance = getMBean(name);
checkMBeanPermission(instance, attribute, name, "getAttribute");
try {
return instance.getAttribute(attribute);
} catch (AttributeNotFoundException e) {
throw e;
} catch (Throwable t) {
rethrowMaybeMBeanException(t);
// not reached
throw new AssertionError();
}
}
use of javax.management.DynamicMBean in project jdk8u_jdk by JetBrains.
the class DefaultMBeanServerInterceptor method getObjectInstance.
public ObjectInstance getObjectInstance(ObjectName name) throws InstanceNotFoundException {
name = nonDefaultDomain(name);
DynamicMBean instance = getMBean(name);
checkMBeanPermission(instance, null, name, "getObjectInstance");
final String className = getClassName(instance);
return new ObjectInstance(name, className);
}
use of javax.management.DynamicMBean in project jdk8u_jdk by JetBrains.
the class DefaultMBeanServerInterceptor method isInstanceOf.
public boolean isInstanceOf(ObjectName name, String className) throws InstanceNotFoundException {
final DynamicMBean instance = getMBean(name);
checkMBeanPermission(instance, null, name, "isInstanceOf");
try {
Object resource = getResource(instance);
final String resourceClassName = (resource instanceof DynamicMBean) ? getClassName((DynamicMBean) resource) : resource.getClass().getName();
if (resourceClassName.equals(className))
return true;
final ClassLoader cl = resource.getClass().getClassLoader();
final Class<?> classNameClass = Class.forName(className, false, cl);
if (classNameClass.isInstance(resource))
return true;
final Class<?> resourceClass = Class.forName(resourceClassName, false, cl);
return classNameClass.isAssignableFrom(resourceClass);
} catch (Exception x) {
/* Could be SecurityException or ClassNotFoundException */
if (MBEANSERVER_LOGGER.isLoggable(Level.FINEST)) {
MBEANSERVER_LOGGER.logp(Level.FINEST, DefaultMBeanServerInterceptor.class.getName(), "isInstanceOf", "Exception calling isInstanceOf", x);
}
return false;
}
}
Aggregations