Search in sources :

Example 36 with JMXServiceURL

use of javax.management.remote.JMXServiceURL 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 37 with JMXServiceURL

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

the class StartManagementAgent method tryConnect.

private static void tryConnect(int port, boolean shouldSucceed) throws Exception {
    String jmxUrlStr = String.format("service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi", port);
    JMXServiceURL url = new JMXServiceURL(jmxUrlStr);
    HashMap<String, ?> env = new HashMap<>();
    boolean succeeded;
    try {
        JMXConnector c = JMXConnectorFactory.connect(url, env);
        c.getMBeanServerConnection();
        succeeded = true;
    } catch (Exception ex) {
        succeeded = false;
    }
    if (succeeded && !shouldSucceed) {
        throw new Exception("Could connect to agent, but should not have been possible");
    }
    if (!succeeded && shouldSucceed) {
        throw new Exception("Could not connect to agent");
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) JMXConnector(javax.management.remote.JMXConnector) AttachOperationFailedException(com.sun.tools.attach.AttachOperationFailedException)

Example 38 with JMXServiceURL

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

the class JMXServerErrorTest method test.

public void test(String url) throws Exception {
    final JMXServiceURL jurl = new JMXServiceURL(url);
    final ObjectName kname = new ObjectName(":type=Kaeffer");
    final MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    final String that = "that";
    mbs.registerMBean(new Kaeffer(that), kname);
    final MBeanServer kbs = new MBeanServerKaeffer(mbs);
    final JMXConnectorServer cs;
    try {
        cs = JMXConnectorServerFactory.newJMXConnectorServer(jurl, null, kbs);
    } catch (MalformedURLException m) {
        if ("jmxmp".equals(jurl.getProtocol()) || "iiop".equals(jurl.getProtocol())) {
            // OK, we may not have this in the classpath...
            System.out.println("WARNING: Skipping protocol: " + jurl);
            return;
        }
        throw m;
    }
    final ObjectName cname = new ObjectName(":type=JMXConnectorServer");
    mbs.registerMBean(cs, cname);
    cs.start();
    JMXConnector c = null;
    try {
        c = JMXConnectorFactory.connect(cs.getAddress(), null);
        final MBeanServerConnection mbsc = c.getMBeanServerConnection();
        final KaefferMBean kaeffer = (KaefferMBean) MBeanServerInvocationHandler.newProxyInstance(mbsc, kname, KaefferMBean.class, false);
        final String that1 = kaeffer.getThis();
        if (!that.equals(that1))
            throw new Exception("Unexpected string returned by " + kname + ": " + that1);
        try {
            kaeffer.setThis("but not that");
            throw new Exception("Expected JMXServerErrorException" + " not thrown" + " for setAttribute \"This\" ");
        } catch (JMXServerErrorException jsee) {
            if (!(jsee.getCause() instanceof KaefferError)) {
                final Exception e = new Exception("Expected JMXServerErrorException" + " is not an instance of " + KaefferError.class.getName() + ": " + jsee.getCause());
                e.initCause(jsee);
                throw e;
            }
            System.out.println("Got expected error: " + jsee);
        }
        try {
            kaeffer.doThis("but not that");
            throw new Exception("Expected JMXServerErrorException" + " not thrown" + " for invoke \"doThis\" ");
        } catch (JMXServerErrorException jsee) {
            if (!(jsee.getCause() instanceof KaefferError)) {
                final Exception e = new Exception("Expected JMXServerErrorException" + " is not an instance of " + KaefferError.class.getName() + ": " + jsee.getCause());
                e.initCause(jsee);
                throw e;
            }
            System.out.println("Got expected error: " + jsee);
        }
    } finally {
        if (c != null)
            try {
                c.close();
            } catch (Exception x) {
                System.err.println("Failed to close client: " + x);
                throw x;
            }
        try {
            cs.stop();
        } catch (Exception x) {
            System.err.println("Failed to stop server: " + x);
            throw x;
        }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) JMXServerErrorException(javax.management.remote.JMXServerErrorException) MalformedURLException(java.net.MalformedURLException) JMXServerErrorException(javax.management.remote.JMXServerErrorException) IOException(java.io.IOException) ObjectName(javax.management.ObjectName) JMXConnectorServer(javax.management.remote.JMXConnectorServer) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer)

Example 39 with JMXServiceURL

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

the class RMISocketFactoriesTest method main.

public static void main(String[] args) {
    System.out.println("Test RMI factories : " + args[0]);
    try {
        // Create an RMI registry
        //
        System.out.println("Start RMI registry...");
        Registry reg = null;
        int port = 5800;
        while (port++ < 6000) {
            try {
                reg = LocateRegistry.createRegistry(port);
                System.out.println("RMI registry running on port " + port);
                break;
            } catch (RemoteException e) {
                // Failed to create RMI registry...
                System.out.println("Failed to create RMI registry " + "on port " + port);
            }
        }
        if (reg == null) {
            System.exit(1);
        }
        // Instantiate the MBean server
        //
        System.out.println("Create the MBean server");
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();
        // Initialize environment map to be passed to the connector server
        //
        System.out.println("Initialize environment map");
        HashMap env = new HashMap();
        env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, new RMIServerFactory(args[0]));
        env.put(RMIConnectorServer.RMI_CLIENT_SOCKET_FACTORY_ATTRIBUTE, new RMIClientFactory(args[0]));
        // Create an RMI connector server
        //
        System.out.println("Create an RMI connector server");
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0, "/jndi/rmi://:" + port + "/server" + port);
        JMXConnectorServer rcs = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
        rcs.start();
        // Create an RMI connector client
        //
        System.out.println("Create an RMI connector client");
        JMXConnector jmxc = JMXConnectorFactory.connect(url, new HashMap());
        // If this line is executed then the test failed
        //
        System.exit(1);
    } catch (Exception e) {
        if (e.getMessage().equals(args[0])) {
            System.out.println("Expected exception caught = " + e);
            System.out.println("Bye! Bye!");
        } else {
            System.out.println("Unexpected exception caught = " + e);
            e.printStackTrace();
            System.exit(1);
        }
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) HashMap(java.util.HashMap) JMXConnector(javax.management.remote.JMXConnector) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) RemoteException(java.rmi.RemoteException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) MBeanServer(javax.management.MBeanServer) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Example 40 with JMXServiceURL

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

the class ConcurrentModificationTest method test.

private static void test(String proto) throws Exception {
    JMXServiceURL u = new JMXServiceURL(proto, null, 0);
    JMXConnectorServer server;
    JMXConnector client;
    try {
        server = JMXConnectorServerFactory.newJMXConnectorServer(u, null, mbs);
        server.start();
        JMXServiceURL addr = server.getAddress();
        client = JMXConnectorFactory.connect(addr, null);
    } catch (MalformedURLException e) {
        System.out.println(">>> not support: " + proto);
        return;
    }
    final MBeanServerConnection mserver = client.getMBeanServerConnection();
    int count = 0;
    boolean adding = true;
    while (uncaughtException == null && count++ < 10) {
        for (int i = 0; i < number; i++) {
            listenerOp(mserver, listeners[i], adding);
            mbeanOp(mserver, timerNames[i], adding);
        }
        adding = !adding;
    }
    if (uncaughtException != null) {
        // clean
        for (int i = 0; i < number; i++) {
            try {
                mbeanOp(mserver, timerNames[i], false);
            } catch (Exception e) {
            }
        }
    }
    client.close();
    server.stop();
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedURLException(java.net.MalformedURLException) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) MalformedURLException(java.net.MalformedURLException) ConcurrentModificationException(java.util.ConcurrentModificationException) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Aggregations

JMXServiceURL (javax.management.remote.JMXServiceURL)273 JMXConnector (javax.management.remote.JMXConnector)146 MBeanServerConnection (javax.management.MBeanServerConnection)107 ObjectName (javax.management.ObjectName)90 IOException (java.io.IOException)75 HashMap (java.util.HashMap)75 MBeanServer (javax.management.MBeanServer)70 JMXConnectorServer (javax.management.remote.JMXConnectorServer)64 MalformedURLException (java.net.MalformedURLException)45 RemoteException (java.rmi.RemoteException)22 Test (org.junit.Test)20 Map (java.util.Map)16 UnknownHostException (java.net.UnknownHostException)14 Notification (javax.management.Notification)14 NotificationListener (javax.management.NotificationListener)14 Properties (java.util.Properties)13 MalformedObjectNameException (javax.management.MalformedObjectNameException)13 LocateRegistry (java.rmi.registry.LocateRegistry)12 Registry (java.rmi.registry.Registry)12 ArrayList (java.util.ArrayList)11