Search in sources :

Example 16 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project jdk8u_jdk by JetBrains.

the class RMIExitTest method test.

private static void test() {
    try {
        JMXServiceURL u = new JMXServiceURL("rmi", null, 0);
        JMXConnectorServer server;
        JMXServiceURL addr;
        JMXConnector client;
        MBeanServerConnection mserver;
        final ObjectName delegateName = new ObjectName("JMImplementation:type=MBeanServerDelegate");
        final NotificationListener dummyListener = new NotificationListener() {

            public void handleNotification(Notification n, Object o) {
                // do nothing
                return;
            }
        };
        server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
        server.start();
        addr = server.getAddress();
        client = JMXConnectorFactory.newJMXConnector(addr, null);
        client.connect(null);
        mserver = client.getMBeanServerConnection();
        String s1 = "1";
        String s2 = "2";
        String s3 = "3";
        mserver.addNotificationListener(delegateName, dummyListener, null, s1);
        mserver.addNotificationListener(delegateName, dummyListener, null, s2);
        mserver.addNotificationListener(delegateName, dummyListener, null, s3);
        mserver.removeNotificationListener(delegateName, dummyListener, null, s3);
        mserver.removeNotificationListener(delegateName, dummyListener, null, s2);
        mserver.removeNotificationListener(delegateName, dummyListener, null, s1);
        client.close();
        server.stop();
    } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) Notification(javax.management.Notification) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) NotificationListener(javax.management.NotificationListener)

Example 17 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project jdk8u_jdk by JetBrains.

the class RMIDownloadTest method testWithException.

private static void testWithException(boolean send) throws Exception {
    ClassLoader zoobyCL = new ZoobyClassLoader();
    Class<?> zoobyClass = Class.forName("Zooby", false, zoobyCL);
    Object zooby = zoobyClass.newInstance();
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///");
    JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, pmbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    MBeanServerConnection mbsc = cc.getMBeanServerConnection();
    Object rzooby;
    if (send) {
        System.out.println("Sending object...");
        mbsc.setAttribute(getSetName, new Attribute("It", zooby));
        rzooby = getSetInstance.getIt();
    } else {
        System.out.println("Receiving object...");
        getSetInstance.setIt(zooby);
        rzooby = mbsc.getAttribute(getSetName, "It");
    }
    if (!rzooby.getClass().getName().equals("Zooby")) {
        throw new Exception("FAILED: remote object is not a Zooby");
    }
    if (rzooby.getClass().getClassLoader() == zooby.getClass().getClassLoader()) {
        throw new Exception("FAILED: same class loader: " + zooby.getClass().getClassLoader());
    }
    cc.close();
    cs.stop();
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) Attribute(javax.management.Attribute) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Example 18 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project jdk8u_jdk by JetBrains.

the class MXBeanExceptionHandlingTest method run.

public void run(Map<String, Object> args) {
    System.out.println("MXBeanExceptionHandlingTest::run: Start");
    int errorCount = 0;
    try {
        parseArgs(args);
        notifList = new ArrayBlockingQueue<Notification>(numOfNotifications);
        // JMX MbeanServer used inside single VM as if remote.
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
        JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        cs.start();
        JMXServiceURL addr = cs.getAddress();
        JMXConnector cc = JMXConnectorFactory.connect(addr);
        MBeanServerConnection mbsc = cc.getMBeanServerConnection();
        // ----
        System.out.println("Add me as notification listener");
        mbsc.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, this, null, null);
        System.out.println("---- OK\n");
        // ----
        System.out.println("Create and register the MBean");
        ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi");
        mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
        System.out.println("---- OK\n");
        // ----
        System.out.println("Call method throwException on our MXBean");
        try {
            mbsc.invoke(objName, "throwException", null, null);
            errorCount++;
            System.out.println("(ERROR) Did not get awaited MBeanException");
        } catch (MBeanException mbe) {
            System.out.println("(OK) Got awaited MBeanException");
            Throwable cause = mbe.getCause();
            if (cause instanceof java.lang.Exception) {
                System.out.println("(OK) Cause is of the right class");
                String mess = cause.getMessage();
                if (mess.equals(Basic.EXCEPTION_MESSAGE)) {
                    System.out.println("(OK) Cause message is fine");
                } else {
                    errorCount++;
                    System.out.println("(ERROR) Cause has message " + cause.getMessage() + " as we expect " + Basic.EXCEPTION_MESSAGE);
                }
            } else {
                errorCount++;
                System.out.println("(ERROR) Cause is of  class " + cause.getClass().getName() + " as we expect java.lang.Exception");
            }
        } catch (Exception e) {
            errorCount++;
            System.out.println("(ERROR) Did not get awaited MBeanException but " + e);
            Utils.printThrowable(e, true);
        }
        System.out.println("---- DONE\n");
        // ----
        System.out.println("Call method throwError on our MXBean");
        try {
            mbsc.invoke(objName, "throwError", null, null);
            errorCount++;
            System.out.println("(ERROR) Did not get awaited RuntimeErrorException");
        } catch (RuntimeErrorException ree) {
            System.out.println("(OK) Got awaited RuntimeErrorException");
            Throwable cause = ree.getCause();
            if (cause instanceof java.lang.InternalError) {
                System.out.println("(OK) Cause is of the right class");
                String mess = cause.getMessage();
                if (mess.equals(Basic.EXCEPTION_MESSAGE)) {
                    System.out.println("(OK) Cause message is fine");
                } else {
                    errorCount++;
                    System.out.println("(ERROR) Cause has message " + cause.getMessage() + " as we expect " + Basic.EXCEPTION_MESSAGE);
                }
            } else {
                errorCount++;
                System.out.println("(ERROR) Cause is of  class " + cause.getClass().getName() + " as we expect java.lang.InternalError");
            }
        } catch (Exception e) {
            errorCount++;
            System.out.println("(ERROR) Did not get awaited RuntimeErrorException but " + e);
            Utils.printThrowable(e, true);
        }
        System.out.println("---- DONE\n");
        // ----
        System.out.println("Unregister the MBean");
        mbsc.unregisterMBean(objName);
        System.out.println("---- OK\n");
        Thread.sleep(timeForNotificationInSeconds * 1000);
        int numOfReceivedNotif = notifList.size();
        if (numOfReceivedNotif == numOfNotifications) {
            System.out.println("(OK) We received " + numOfNotifications + " Notifications");
        } else {
            errorCount++;
            System.out.println("(ERROR) We received " + numOfReceivedNotif + " Notifications in place of " + numOfNotifications);
        }
    } catch (Exception e) {
        Utils.printThrowable(e, true);
        throw new RuntimeException(e);
    }
    if (errorCount == 0) {
        System.out.println("MXBeanExceptionHandlingTest::run: Done without any error");
    } else {
        System.out.println("MXBeanExceptionHandlingTest::run: Done with " + errorCount + " error(s)");
        throw new RuntimeException("errorCount = " + errorCount);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) RuntimeErrorException(javax.management.RuntimeErrorException) Notification(javax.management.Notification) RuntimeErrorException(javax.management.RuntimeErrorException) MBeanException(javax.management.MBeanException) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) MBeanException(javax.management.MBeanException) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer)

Example 19 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project jdk8u_jdk by JetBrains.

the class UnserializableTargetObjectTest method main.

public static void main(String[] args) throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName name = new ObjectName("a:b=c");
    Resource resource1 = new Resource();
    Resource resource2 = new Resource();
    Resource resource3 = new Resource();
    Method operationMethod = Resource.class.getMethod("operation");
    Method getCountMethod = Resource.class.getMethod("getCount");
    Method setCountMethod = Resource.class.getMethod("setCount", int.class);
    Descriptor operationDescriptor = new DescriptorSupport(new String[] { "descriptorType", "name", "targetObject" }, new Object[] { "operation", "operation", resource1 });
    Descriptor getCountDescriptor = new DescriptorSupport(new String[] { "descriptorType", "name", "targetObject" }, new Object[] { "operation", "getCount", resource2 });
    Descriptor setCountDescriptor = new DescriptorSupport(new String[] { "descriptorType", "name", "targetObject" }, new Object[] { "operation", "setCount", resource2 });
    Descriptor countDescriptor = new DescriptorSupport(new String[] { "descriptorType", "name", "getMethod", "setMethod" }, new Object[] { "attribute", "Count", "getCount", "setCount" });
    ModelMBeanOperationInfo operationInfo = new ModelMBeanOperationInfo("operation description", operationMethod, operationDescriptor);
    ModelMBeanOperationInfo getCountInfo = new ModelMBeanOperationInfo("getCount description", getCountMethod, getCountDescriptor);
    ModelMBeanOperationInfo setCountInfo = new ModelMBeanOperationInfo("setCount description", setCountMethod, setCountDescriptor);
    ModelMBeanAttributeInfo countInfo = new ModelMBeanAttributeInfo("Count", "Count description", getCountMethod, setCountMethod, countDescriptor);
    ModelMBeanInfo mmbi = new ModelMBeanInfoSupport(Resource.class.getName(), "ModelMBean to test targetObject", new ModelMBeanAttributeInfo[] { countInfo }, // no constructors
    null, new ModelMBeanOperationInfo[] { operationInfo, getCountInfo, setCountInfo }, // no notifications
    null);
    ModelMBean mmb = new RequiredModelMBean(mmbi);
    mmb.setManagedResource(resource3, "ObjectReference");
    mbs.registerMBean(mmb, name);
    mbs.invoke(name, "operation", null, null);
    mbs.setAttribute(name, new Attribute("Count", 53));
    if (resource1.operationCount != 1)
        throw new Exception("operationCount: " + resource1.operationCount);
    if (resource2.count != 53)
        throw new Exception("count: " + resource2.count);
    int got = (Integer) mbs.getAttribute(name, "Count");
    if (got != 53)
        throw new Exception("got count: " + got);
    JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
    JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    MBeanServerConnection mbsc = cc.getMBeanServerConnection();
    ModelMBeanInfo rmmbi = (ModelMBeanInfo) mbsc.getMBeanInfo(name);
    // Above gets NotSerializableException if resource included in
    // serialized form
    cc.close();
    cs.stop();
    System.out.println("TEST PASSED");
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) ModelMBean(javax.management.modelmbean.ModelMBean) Attribute(javax.management.Attribute) DescriptorSupport(javax.management.modelmbean.DescriptorSupport) Method(java.lang.reflect.Method) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) ObjectName(javax.management.ObjectName) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) JMXConnector(javax.management.remote.JMXConnector) Descriptor(javax.management.Descriptor) ModelMBeanInfoSupport(javax.management.modelmbean.ModelMBeanInfoSupport) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer)

Example 20 with JMXConnectorServer

use of javax.management.remote.JMXConnectorServer in project jdk8u_jdk by JetBrains.

the class SupportedQueryTypesTest method run.

public void run(Map<String, Object> args) {
    int errorCount = 0;
    ObjectName on = null;
    ObjectName serverDelegateObjectName = null;
    JMXConnectorServer cs = null;
    JMXConnector cc = null;
    System.out.println("SupportedQueryTypesTest::run: Start");
    try {
        // JMX MbeanServer used inside single VM as if remote.
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
        cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        cs.start();
        JMXServiceURL addr = cs.getAddress();
        cc = JMXConnectorFactory.connect(addr);
        mbsc = cc.getMBeanServerConnection();
        // Create and register the ServerDelegate MBean on the remote MBeanServer
        String serverDelegateClassName = ServerDelegate.class.getName();
        serverDelegateObjectName = new ObjectName("defaultDomain:class=" + serverDelegateClassName);
        mbsc.createMBean(serverDelegateClassName, serverDelegateObjectName);
        // Retrieve the MBean class name
        mbeanClassName = (String) args.get("-mbeanClassName");
        on = new ObjectName("defaultDomain:class=" + mbeanClassName);
        // Create and register the MBean on the remote MBeanServer
        System.out.println("SupportedQueryTypesTest::run: CREATE " + mbeanClassName + " on the remote MBeanServer with name " + on);
        mbsc.createMBean(mbeanClassName, on);
        // Create a QueryFactory and setup which query we'll use.
        QueryFactory queries = new QueryFactory(mbeanClassName);
        queries.buildQueries();
        int maxIndex = queries.getSize();
        int minIndex = 1;
        // Create a reference Set<ObjectName> to check later on
        // the queryNames() results
        Set<ObjectName> referenceNameSet = new HashSet<ObjectName>();
        referenceNameSet.add(on);
        // Create a reference Set<ObjectInstance> to check later on
        // the queryMBeans() results
        ObjectInstance oi = new ObjectInstance(on, mbeanClassName);
        Set<ObjectInstance> referenceInstanceSet = new HashSet<ObjectInstance>();
        referenceInstanceSet.add(oi);
        // Perform the queryNames and queryMBeans requests
        for (int i = minIndex; i <= maxIndex; i++) {
            QueryExp query = queries.getQuery(i);
            System.out.println("----");
            System.out.println("SupportedQueryTypesTest::run: Query # " + i);
            System.out.println("query " + query);
            errorCount += doQueryNames(query, referenceNameSet);
            errorCount += doQueryMBeans(query, referenceInstanceSet);
        }
    } catch (Exception e) {
        Utils.printThrowable(e, true);
        errorCount++;
    } finally {
        // Do unregister the MBean
        try {
            if (mbsc.isRegistered(on)) {
                mbsc.unregisterMBean(on);
            }
            if (mbsc.isRegistered(serverDelegateObjectName)) {
                mbsc.unregisterMBean(serverDelegateObjectName);
            }
        } catch (Exception e) {
            Utils.printThrowable(e, true);
            errorCount++;
        }
        try {
            // Close JMX Connector Client
            cc.close();
            // Stop connertor server
            cs.stop();
        } catch (Exception e) {
            Utils.printThrowable(e, true);
            errorCount++;
        }
    }
    System.out.println("");
    System.out.println("SupportedQueryTypesTest::run: Done");
    // Handle result
    if (errorCount == 0) {
        System.out.println("SupportedQueryTypesTest::run: (OK)");
    } else {
        String message = "SupportedQueryTypesTest::run: (ERROR) Got " + +errorCount + " error(s)";
        System.out.println(message);
        throw new RuntimeException(message);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) QueryExp(javax.management.QueryExp) ObjectInstance(javax.management.ObjectInstance) ObjectName(javax.management.ObjectName) JMXConnectorServer(javax.management.remote.JMXConnectorServer) JMXConnector(javax.management.remote.JMXConnector) MBeanServer(javax.management.MBeanServer) HashSet(java.util.HashSet)

Aggregations

JMXConnectorServer (javax.management.remote.JMXConnectorServer)63 JMXServiceURL (javax.management.remote.JMXServiceURL)57 JMXConnector (javax.management.remote.JMXConnector)47 MBeanServer (javax.management.MBeanServer)44 MBeanServerConnection (javax.management.MBeanServerConnection)38 ObjectName (javax.management.ObjectName)31 HashMap (java.util.HashMap)24 IOException (java.io.IOException)13 MalformedURLException (java.net.MalformedURLException)13 RemoteException (java.rmi.RemoteException)13 NotificationListener (javax.management.NotificationListener)12 Notification (javax.management.Notification)11 Attribute (javax.management.Attribute)9 LocateRegistry (java.rmi.registry.LocateRegistry)7 Registry (java.rmi.registry.Registry)7 Map (java.util.Map)7 Properties (java.util.Properties)7 JMXPluggableAuthenticator (com.sun.jmx.remote.security.JMXPluggableAuthenticator)5 UnknownHostException (java.net.UnknownHostException)5 Test (org.junit.Test)5