Search in sources :

Example 21 with JMXServiceURL

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

the class SubjectDelegation3Test method main.

public static void main(String[] args) throws Exception {
    // Check for supported operating systems: Solaris
    //
    // This test runs only on Solaris due to CR 6285916
    //
    String osName = System.getProperty("os.name");
    System.out.println("os.name = " + osName);
    if (!osName.equals("SunOS")) {
        System.out.println("This test runs on Solaris only.");
        System.out.println("Bye! Bye!");
        return;
    }
    String policyFile = args[0];
    String testResult = args[1];
    System.out.println("Policy file = " + policyFile);
    System.out.println("Expected test result = " + testResult);
    JMXConnectorServer jmxcs = null;
    JMXConnector jmxc = null;
    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);
        }
        // Set the default password file
        //
        final String passwordFile = System.getProperty("test.src") + File.separator + "jmxremote.password";
        System.out.println("Password file = " + passwordFile);
        // Set policy file
        //
        final String policy = System.getProperty("test.src") + File.separator + policyFile;
        System.out.println("PolicyFile = " + policy);
        System.setProperty("java.security.policy", policy);
        // Instantiate the MBean server
        //
        System.out.println("Create the MBean server");
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        // Register the SimpleStandardMBean
        //
        System.out.println("Create SimpleStandard MBean");
        SimpleStandard s = new SimpleStandard("delegate");
        mbs.registerMBean(s, new ObjectName("MBeans:type=SimpleStandard"));
        // Create Properties containing the username/password entries
        //
        Properties props = new Properties();
        props.setProperty("jmx.remote.x.password.file", passwordFile);
        // Initialize environment map to be passed to the connector server
        //
        System.out.println("Initialize environment map");
        HashMap env = new HashMap();
        env.put("jmx.remote.authenticator", new JMXPluggableAuthenticator(props));
        // Set Security Manager
        //
        System.setSecurityManager(new SecurityManager());
        // 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);
        jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
        jmxcs.start();
        // Create an RMI connector client
        //
        System.out.println("Create an RMI connector client");
        HashMap cli_env = new HashMap();
        // These credentials must match those in the default password file
        //
        String[] credentials = new String[] { "monitorRole", "QED" };
        cli_env.put("jmx.remote.credentials", credentials);
        jmxc = JMXConnectorFactory.connect(url, cli_env);
        Subject delegationSubject = new Subject(true, Collections.singleton(new JMXPrincipal("delegate")), Collections.EMPTY_SET, Collections.EMPTY_SET);
        MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(delegationSubject);
        // Get domains from MBeanServer
        //
        System.out.println("Domains:");
        String[] domains = mbsc.getDomains();
        for (int i = 0; i < domains.length; i++) {
            System.out.println("\tDomain[" + i + "] = " + domains[i]);
        }
        // Get MBean count
        //
        System.out.println("MBean count = " + mbsc.getMBeanCount());
        // Get State attribute
        //
        String oldState = (String) mbsc.getAttribute(new ObjectName("MBeans:type=SimpleStandard"), "State");
        System.out.println("Old State = \"" + oldState + "\"");
        // Set State attribute
        //
        System.out.println("Set State to \"changed state\"");
        mbsc.setAttribute(new ObjectName("MBeans:type=SimpleStandard"), new Attribute("State", "changed state"));
        // Get State attribute
        //
        String newState = (String) mbsc.getAttribute(new ObjectName("MBeans:type=SimpleStandard"), "State");
        System.out.println("New State = \"" + newState + "\"");
        if (!newState.equals("changed state")) {
            System.out.println("Invalid State = \"" + newState + "\"");
            System.exit(1);
        }
        // Add notification listener on SimpleStandard MBean
        //
        System.out.println("Add notification listener...");
        mbsc.addNotificationListener(new ObjectName("MBeans:type=SimpleStandard"), new NotificationListener() {

            public void handleNotification(Notification notification, Object handback) {
                System.out.println("Received notification: " + notification);
            }
        }, null, null);
        // Unregister SimpleStandard MBean
        //
        System.out.println("Unregister SimpleStandard MBean...");
        mbsc.unregisterMBean(new ObjectName("MBeans:type=SimpleStandard"));
    } catch (SecurityException e) {
        if (testResult.equals("ko")) {
            System.out.println("Got expected security exception = " + e);
        } else {
            System.out.println("Got unexpected security exception = " + e);
            e.printStackTrace();
            throw e;
        }
    } catch (Exception e) {
        System.out.println("Unexpected exception caught = " + e);
        e.printStackTrace();
        throw e;
    } finally {
        //
        if (jmxc != null)
            jmxc.close();
        //
        if (jmxcs != null)
            jmxcs.stop();
        // Say goodbye
        //
        System.out.println("Bye! Bye!");
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXPluggableAuthenticator(com.sun.jmx.remote.security.JMXPluggableAuthenticator) HashMap(java.util.HashMap) Attribute(javax.management.Attribute) JMXPrincipal(javax.management.remote.JMXPrincipal) Registry(java.rmi.registry.Registry) LocateRegistry(java.rmi.registry.LocateRegistry) Properties(java.util.Properties) Subject(javax.security.auth.Subject) Notification(javax.management.Notification) RemoteException(java.rmi.RemoteException) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) RemoteException(java.rmi.RemoteException) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer) NotificationListener(javax.management.NotificationListener)

Example 22 with JMXServiceURL

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

the class AuthorizationTest method run.

public void run(Map<String, Object> serverArgs, String[] clientArgs) {
    System.out.println("AuthorizationTest::run: Start");
    int errorCount = 0;
    try {
        // Initialise the server side
        JMXServiceURL urlToUse = createServerSide(serverArgs);
        // Run client side
        errorCount = runClientSide(clientArgs, urlToUse.toString());
        if (errorCount == 0) {
            System.out.println("AuthorizationTest::run: Done without any error");
        } else {
            System.out.println("AuthorizationTest::run: Done with " + errorCount + " error(s)");
            throw new RuntimeException("errorCount = " + errorCount);
        }
        cs.stop();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL)

Example 23 with JMXServiceURL

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

the class SecurityTest method createServerSide.

/*
     * Create the MBeansServer side of the test and returns its address
     */
private JMXServiceURL createServerSide(Map<String, Object> serverMap) throws Exception {
    final int NINETY_SECONDS = 90;
    System.out.println("SecurityTest::createServerSide: Start");
    // Prepare server side security env
    HashMap<String, Object> env = setServerSecurityEnv(serverMap);
    // Create and start mbean server and connector server
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
    cs = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
    cs.start();
    // Waits availibility of connector server
    Utils.waitReady(cs, NINETY_SECONDS);
    JMXServiceURL addr = cs.getAddress();
    System.out.println("SecurityTest::createServerSide: Done.");
    return addr;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MBeanServer(javax.management.MBeanServer)

Example 24 with JMXServiceURL

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

the class MapNullValuesTest method jmxConnectorServerFactoryTests.

private int jmxConnectorServerFactoryTests() {
    int errorCount = 0;
    echo("");
    echo(dashedMessage("Run JMXConnectorServerFactory Tests"));
    for (int i = 0; i < maps.length - 1; i++) {
        echo("\n>>> JMXConnectorServerFactory Test [" + i + "]");
        try {
            echo("\tMap = " + maps[i]);
            echo("\tCreate the MBean server");
            MBeanServer mbs = MBeanServerFactory.createMBeanServer();
            echo("\tCreate the RMI connector server");
            JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:" + port + "/JMXConnectorServerFactory" + i);
            JMXConnectorServer jmxcs = JMXConnectorServerFactory.newJMXConnectorServer(url, maps[i], mbs);
            echo("\tStart the RMI connector server");
            jmxcs.start();
            echo("\tCall RMIConnectorServer.toJMXConnector(Map)");
            jmxcs.toJMXConnector(maps[i]);
            echo("\tStop the RMI connector server");
            jmxcs.stop();
            echo("\tTest [" + i + "] PASSED!");
        } catch (Exception e) {
            errorCount++;
            echo("\tTest [" + i + "] FAILED!");
            e.printStackTrace(System.out);
        }
    }
    if (errorCount == 0) {
        echo("");
        echo(dashedMessage("JMXConnectorServerFactory Tests PASSED!"));
    } else {
        echo("");
        echo(dashedMessage("JMXConnectorServerFactory Tests FAILED!"));
    }
    return errorCount;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) RemoteException(java.rmi.RemoteException) MBeanServer(javax.management.MBeanServer) JMXConnectorServer(javax.management.remote.JMXConnectorServer)

Example 25 with JMXServiceURL

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

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