Search in sources :

Example 41 with DynamicMBean

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;
}
Also used : ExtendedPlatformComponent(sun.management.ExtendedPlatformComponent) DynamicMBean(javax.management.DynamicMBean) MBeanServerPermission(javax.management.MBeanServerPermission) Permission(java.security.Permission) HashMap(java.util.HashMap) Map(java.util.Map) MBeanServerPermission(javax.management.MBeanServerPermission) ObjectName(javax.management.ObjectName)

Example 42 with DynamicMBean

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);
}
Also used : DynamicMBean(javax.management.DynamicMBean) NotificationBroadcaster(javax.management.NotificationBroadcaster) RuntimeOperationsException(javax.management.RuntimeOperationsException) NotificationListener(javax.management.NotificationListener)

Example 43 with DynamicMBean

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();
    }
}
Also used : DynamicMBean(javax.management.DynamicMBean) AttributeNotFoundException(javax.management.AttributeNotFoundException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 44 with DynamicMBean

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);
}
Also used : DynamicMBean(javax.management.DynamicMBean) ObjectInstance(javax.management.ObjectInstance)

Example 45 with DynamicMBean

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;
    }
}
Also used : DynamicMBean(javax.management.DynamicMBean) NamedObject(com.sun.jmx.mbeanserver.NamedObject) IntrospectionException(javax.management.IntrospectionException) OperationsException(javax.management.OperationsException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) ReflectionException(javax.management.ReflectionException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) RuntimeMBeanException(javax.management.RuntimeMBeanException) RuntimeErrorException(javax.management.RuntimeErrorException) ListenerNotFoundException(javax.management.ListenerNotFoundException) RuntimeOperationsException(javax.management.RuntimeOperationsException) AttributeNotFoundException(javax.management.AttributeNotFoundException) MBeanRegistrationException(javax.management.MBeanRegistrationException) InstanceNotFoundException(javax.management.InstanceNotFoundException) JMRuntimeException(javax.management.JMRuntimeException) InvalidAttributeValueException(javax.management.InvalidAttributeValueException) MBeanException(javax.management.MBeanException)

Aggregations

DynamicMBean (javax.management.DynamicMBean)78 MBeanException (javax.management.MBeanException)29 ObjectName (javax.management.ObjectName)24 MalformedObjectNameException (javax.management.MalformedObjectNameException)20 ManagedBean (org.apache.tomcat.util.modeler.ManagedBean)18 RuntimeOperationsException (javax.management.RuntimeOperationsException)16 Test (org.junit.Test)15 AttributeNotFoundException (javax.management.AttributeNotFoundException)14 InstanceNotFoundException (javax.management.InstanceNotFoundException)13 InvalidAttributeValueException (javax.management.InvalidAttributeValueException)13 ListenerNotFoundException (javax.management.ListenerNotFoundException)13 ReflectionException (javax.management.ReflectionException)12 RuntimeErrorException (javax.management.RuntimeErrorException)12 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 Attribute (javax.management.Attribute)8 JMRuntimeException (javax.management.JMRuntimeException)7 MBeanRegistrationException (javax.management.MBeanRegistrationException)7 NamedObject (com.sun.jmx.mbeanserver.NamedObject)6 Method (java.lang.reflect.Method)6 RuntimeMBeanException (javax.management.RuntimeMBeanException)6