Search in sources :

Example 1 with MBeanServerInvocationHandler

use of javax.management.MBeanServerInvocationHandler in project spring-framework by spring-projects.

the class MBeanClientInterceptor method prepare.

/**
	 * Ensures that an {@code MBeanServerConnection} is configured and attempts
	 * to detect a local connection if one is not supplied.
	 */
public void prepare() {
    synchronized (this.preparationMonitor) {
        if (this.server != null) {
            this.serverToUse = this.server;
        } else {
            this.serverToUse = null;
            this.serverToUse = this.connector.connect(this.serviceUrl, this.environment, this.agentId);
        }
        this.invocationHandler = null;
        if (this.useStrictCasing) {
            // Use the JDK's own MBeanServerInvocationHandler, in particular for native MXBean support.
            this.invocationHandler = new MBeanServerInvocationHandler(this.serverToUse, this.objectName, (this.managementInterface != null && JMX.isMXBeanInterface(this.managementInterface)));
        } else {
            // Non-strict casing can only be achieved through custom invocation handling.
            // Only partial MXBean support available!
            retrieveMBeanInfo();
        }
    }
}
Also used : MBeanServerInvocationHandler(javax.management.MBeanServerInvocationHandler)

Example 2 with MBeanServerInvocationHandler

use of javax.management.MBeanServerInvocationHandler in project jdk8u_jdk by JetBrains.

the class TestUtils method getObjectName.

/**
     * Returns the ObjectName of the MBean that a proxy object
     * is proxying.
     **/
public static ObjectName getObjectName(Object proxy) {
    if (!(proxy instanceof Proxy))
        throw new IllegalArgumentException("not a " + Proxy.class.getName());
    final Proxy p = (Proxy) proxy;
    final InvocationHandler handler = Proxy.getInvocationHandler(proxy);
    if (handler instanceof MBeanServerInvocationHandler)
        return ((MBeanServerInvocationHandler) handler).getObjectName();
    throw new IllegalArgumentException("not a JMX Proxy");
}
Also used : Proxy(java.lang.reflect.Proxy) MBeanServerInvocationHandler(javax.management.MBeanServerInvocationHandler) MBeanServerInvocationHandler(javax.management.MBeanServerInvocationHandler) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 3 with MBeanServerInvocationHandler

use of javax.management.MBeanServerInvocationHandler in project masquerade by cuba-platform.

the class JmxCallHandler method invoke.

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
    JMXServiceURL url;
    try {
        url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + hostInfo.getAddress() + "/jmxrmi");
    } catch (MalformedURLException e) {
        throw new RuntimeException("Incorrect service URL", e);
    }
    Map<String, Object> properties = new HashMap<>();
    if (hostInfo.getUser() != null) {
        properties.put(JMXConnector.CREDENTIALS, new String[] { hostInfo.getUser(), hostInfo.getPassword() });
    }
    try (JMXConnector jmxc = JMXConnectorFactory.connect(url, properties)) {
        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
        ObjectName mbeanName;
        try {
            mbeanName = new ObjectName(objectName);
        } catch (MalformedObjectNameException e) {
            throw new RuntimeException("Incorrect JMX object name", e);
        }
        MBeanServerInvocationHandler wrappedHandler = new MBeanServerInvocationHandler(mbsc, mbeanName);
        if (args != null) {
            log.info("Invoke method {} of {} with parameters {}", method.getName(), objectName, args);
        } else {
            log.info("Invoke method {} of {}", method.getName(), objectName);
        }
        return wrappedHandler.invoke(proxy, method, args);
    } catch (IOException e) {
        throw new RuntimeException("Unable to perform JMX call", e);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) MalformedObjectNameException(javax.management.MalformedObjectNameException) HashMap(java.util.HashMap) MBeanServerInvocationHandler(javax.management.MBeanServerInvocationHandler) IOException(java.io.IOException) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 4 with MBeanServerInvocationHandler

use of javax.management.MBeanServerInvocationHandler in project jdk8u_jdk by JetBrains.

the class TestUtils method makeNotificationEmitter.

/**
     * Transfroms a proxy implementing T in a proxy implementing T plus
     * NotificationEmitter
     *
     **/
public static <T> T makeNotificationEmitter(T proxy, Class<T> mbeanInterface) {
    if (proxy instanceof NotificationEmitter)
        return proxy;
    if (proxy == null)
        return null;
    if (!(proxy instanceof Proxy))
        throw new IllegalArgumentException("not a " + Proxy.class.getName());
    final Proxy p = (Proxy) proxy;
    final InvocationHandler handler = Proxy.getInvocationHandler(proxy);
    if (!(handler instanceof MBeanServerInvocationHandler))
        throw new IllegalArgumentException("not a JMX Proxy");
    final MBeanServerInvocationHandler h = (MBeanServerInvocationHandler) handler;
    final ObjectName name = h.getObjectName();
    final MBeanServerConnection mbs = h.getMBeanServerConnection();
    final boolean isMXBean = h.isMXBean();
    final T newProxy;
    if (isMXBean)
        newProxy = JMX.newMXBeanProxy(mbs, name, mbeanInterface, true);
    else
        newProxy = JMX.newMBeanProxy(mbs, name, mbeanInterface, true);
    return newProxy;
}
Also used : Proxy(java.lang.reflect.Proxy) NotificationEmitter(javax.management.NotificationEmitter) MBeanServerInvocationHandler(javax.management.MBeanServerInvocationHandler) MBeanServerInvocationHandler(javax.management.MBeanServerInvocationHandler) InvocationHandler(java.lang.reflect.InvocationHandler) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName)

Example 5 with MBeanServerInvocationHandler

use of javax.management.MBeanServerInvocationHandler in project jdk8u_jdk by JetBrains.

the class MXBeanRefTest method main.

public static void main(String[] args) throws Exception {
    MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    ObjectName productName = new ObjectName("d:type=Product,n=1");
    ObjectName product2Name = new ObjectName("d:type=Product,n=2");
    ObjectName moduleName = new ObjectName("d:type=Module");
    mbs.registerMBean(product, productName);
    mbs.registerMBean(product2, product2Name);
    mbs.registerMBean(module, moduleName);
    ModuleMXBean moduleProxy = JMX.newMXBeanProxy(mbs, moduleName, ModuleMXBean.class);
    ObjectName on;
    on = (ObjectName) mbs.getAttribute(moduleName, "Product");
    check("ObjectName attribute value", on.equals(productName));
    ProductMXBean productProxy = moduleProxy.getProduct();
    MBeanServerInvocationHandler mbsih = (MBeanServerInvocationHandler) Proxy.getInvocationHandler(productProxy);
    check("ObjectName in proxy", mbsih.getObjectName().equals(productName));
    mbs.setAttribute(moduleName, new Attribute("Product", product2Name));
    ProductMXBean product2Proxy = module.getProduct();
    mbsih = (MBeanServerInvocationHandler) Proxy.getInvocationHandler(product2Proxy);
    check("Proxy after setAttribute", mbsih.getObjectName().equals(product2Name));
    moduleProxy.setProduct(productProxy);
    ProductMXBean productProxyAgain = module.getProduct();
    mbsih = (MBeanServerInvocationHandler) Proxy.getInvocationHandler(productProxyAgain);
    check("Proxy after proxied set", mbsih.getObjectName().equals(productName));
    MBeanServer mbs2 = MBeanServerFactory.createMBeanServer();
    ProductMXBean productProxy2 = JMX.newMXBeanProxy(mbs2, productName, ProductMXBean.class);
    try {
        moduleProxy.setProduct(productProxy2);
        check("Proxy for wrong MBeanServer worked but shouldn't", false);
    } catch (Exception e) {
        if (e instanceof UndeclaredThrowableException && e.getCause() instanceof OpenDataException)
            check("Proxy for wrong MBeanServer correctly rejected", true);
        else {
            e.printStackTrace(System.out);
            check("Proxy for wrong MBeanServer got wrong exception", false);
        }
    }
    // Test 6283873
    ObjectName dup = new ObjectName("a:b=c");
    mbs.registerMBean(new MBeanServerDelegate(), dup);
    try {
        mbs.registerMBean(new ProductImpl(), dup);
        check("Duplicate register succeeded but should fail", false);
    } catch (InstanceAlreadyExistsException e) {
        check("Got correct exception from duplicate name", true);
    } catch (Exception e) {
        e.printStackTrace(System.out);
        check("Got wrong exception from duplicate name", false);
    }
    if (failure != null)
        throw new Exception("TEST FAILED: " + failure);
    System.out.println("TEST PASSED");
}
Also used : Attribute(javax.management.Attribute) OpenDataException(javax.management.openmbean.OpenDataException) MBeanServerDelegate(javax.management.MBeanServerDelegate) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) MBeanServerInvocationHandler(javax.management.MBeanServerInvocationHandler) OpenDataException(javax.management.openmbean.OpenDataException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Aggregations

MBeanServerInvocationHandler (javax.management.MBeanServerInvocationHandler)6 ObjectName (javax.management.ObjectName)4 InvocationHandler (java.lang.reflect.InvocationHandler)3 Proxy (java.lang.reflect.Proxy)3 MBeanServerConnection (javax.management.MBeanServerConnection)2 OpenDataException (javax.management.openmbean.OpenDataException)2 IOException (java.io.IOException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1 Attribute (javax.management.Attribute)1 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)1 MBeanServer (javax.management.MBeanServer)1 MBeanServerDelegate (javax.management.MBeanServerDelegate)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 NotificationEmitter (javax.management.NotificationEmitter)1 JMXConnector (javax.management.remote.JMXConnector)1 JMXServiceURL (javax.management.remote.JMXServiceURL)1