Java Code Examples for javax.management.MBeanServerFactory#newMBeanServer()

The following examples show how to use javax.management.MBeanServerFactory#newMBeanServer() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImmutableNotificationInfoTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static boolean test(Object mbean, boolean expectImmutable)
        throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName on = new ObjectName("a:b=c");
    mbs.registerMBean(mbean, on);
    MBeanInfo mbi = mbs.getMBeanInfo(on);
    Descriptor d = mbi.getDescriptor();
    String immutableValue = (String) d.getFieldValue("immutableInfo");
    boolean immutable = ("true".equals(immutableValue));
    if (immutable != expectImmutable) {
        System.out.println("FAILED: " + mbean.getClass().getName() +
                " -> " + immutableValue);
        return false;
    } else {
        System.out.println("OK: " + mbean.getClass().getName());
        return true;
    }
}
 
Example 2
Source File: MBeanFallbackTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void testPrivate(Class<?> iface, Object bean) throws Exception {
    try {
        System.out.println("Registering a private MBean " +
                            iface.getName() + " ...");

        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=Compliant");

        mbs.registerMBean(bean, on);
        success("Registered a private MBean - " + iface.getName());
    } catch (Exception e) {
        Throwable t = e;
        while (t != null && !(t instanceof NotCompliantMBeanException)) {
            t = t.getCause();
        }
        if (t != null) {
            fail("MBean not registered");
        } else {
            throw e;
        }
    }
}
 
Example 3
Source File: ImmutableNotificationInfoTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static boolean test(Object mbean, boolean expectImmutable)
        throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName on = new ObjectName("a:b=c");
    mbs.registerMBean(mbean, on);
    MBeanInfo mbi = mbs.getMBeanInfo(on);
    Descriptor d = mbi.getDescriptor();
    String immutableValue = (String) d.getFieldValue("immutableInfo");
    boolean immutable = ("true".equals(immutableValue));
    if (immutable != expectImmutable) {
        System.out.println("FAILED: " + mbean.getClass().getName() +
                " -> " + immutableValue);
        return false;
    } else {
        System.out.println("OK: " + mbean.getClass().getName());
        return true;
    }
}
 
Example 4
Source File: MBeanTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void testNonCompliant(Class<?> iface, Object bean) throws Exception {
    try {
        System.out.println("Registering a non-compliant MBean " +
                            iface.getName() + " ...");

        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=NonCompliant");

        mbs.registerMBean(bean, on);

        fail("Registered a non-compliant MBean - " + iface.getName());
    } catch (Exception e) {
        Throwable t = e;
        while (t != null && !(t instanceof NotCompliantMBeanException)) {
            t = t.getCause();
        }
        if (t != null) {
            success("MBean not registered");
        } else {
            throw e;
        }
    }
}
 
Example 5
Source File: MBeanTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void testNonCompliant(Class<?> iface, Object bean) throws Exception {
    try {
        System.out.println("Registering a non-compliant MBean " +
                            iface.getName() + " ...");

        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=NonCompliant");

        mbs.registerMBean(bean, on);

        fail("Registered a non-compliant MBean - " + iface.getName());
    } catch (Exception e) {
        Throwable t = e;
        while (t != null && !(t instanceof NotCompliantMBeanException)) {
            t = t.getCause();
        }
        if (t != null) {
            success("MBean not registered");
        } else {
            throw e;
        }
    }
}
 
Example 6
Source File: ExceptionDiagnosisTest.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
private static void testCaseProb() throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName name = new ObjectName("a:b=c");
    mbs.registerMBean(new CaseProbImpl(), name);
    CaseProbMXBean proxy = JMX.newMXBeanProxy(mbs, name, CaseProbMXBean.class);
    try {
        CaseProb prob = proxy.getCaseProb();
        fail("No exception from proxy method getCaseProb");
    } catch (IllegalArgumentException e) {
        String messageChain = messageChain(e);
        if (messageChain.contains("URLPath")) {
            System.out.println("Message chain contains URLPath as required: "
                    + messageChain);
        } else {
            fail("Exception chain for CaseProb does not mention property" +
                    " URLPath differing only in case");
            System.out.println("Full stack trace:");
            e.printStackTrace(System.out);
        }
    }
}
 
Example 7
Source File: ListenerScaleTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    Sender sender = new Sender();
    mbs.registerMBean(sender, testObjectName);
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
    JMXConnectorServer cs =
        JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    cs.start();
    JMXServiceURL addr = cs.getAddress();
    JMXConnector cc = JMXConnectorFactory.connect(addr);
    try {
        test(mbs, cs, cc);
    } finally {
        cc.close();
        cs.stop();
    }
}
 
Example 8
Source File: ExceptionDiagnosisTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void testCaseProb() throws Exception {
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName name = new ObjectName("a:b=c");
    mbs.registerMBean(new CaseProbImpl(), name);
    CaseProbMXBean proxy = JMX.newMXBeanProxy(mbs, name, CaseProbMXBean.class);
    try {
        CaseProb prob = proxy.getCaseProb();
        fail("No exception from proxy method getCaseProb");
    } catch (IllegalArgumentException e) {
        String messageChain = messageChain(e);
        if (messageChain.contains("URLPath")) {
            System.out.println("Message chain contains URLPath as required: "
                    + messageChain);
        } else {
            fail("Exception chain for CaseProb does not mention property" +
                    " URLPath differing only in case");
            System.out.println("Full stack trace:");
            e.printStackTrace(System.out);
        }
    }
}
 
Example 9
Source File: MBeanTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void testCompliant(Class<?> iface, Object bean) throws Exception {
    try {
        System.out.println("Registering a compliant MBean " +
                            iface.getName() + " ...");

        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName on = new ObjectName("test:type=Compliant");

        mbs.registerMBean(bean, on);
        success("Registered a compliant MBean - " + iface.getName());
    } catch (Exception e) {
        Throwable t = e;
        while (t != null && !(t instanceof NotCompliantMBeanException)) {
            t = t.getCause();
        }
        if (t != null) {
            fail("MBean not registered");
        } else {
            throw e;
        }
    }
}
 
Example 10
Source File: ReflectionExceptionTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Test the monitor notifications.
 */
public int monitorNotifications() throws Exception {

    server = MBeanServerFactory.newMBeanServer();

    MBeanServerForwarderInvocationHandler mbsfih =
        (MBeanServerForwarderInvocationHandler)
        Proxy.getInvocationHandler(server);

    mbsfih.setGetAttributeException(
        new ReflectionException(new RuntimeException(),
                                "Test ReflectionException"));

    domain = server.getDefaultDomain();

    obsObjName = ObjectName.getInstance(domain + ":type=ObservedObject");
    server.registerMBean(new ObservedObject(), obsObjName);

    echo(">>> ----------------------------------------");
    int error = counterMonitorNotification();
    echo(">>> ----------------------------------------");
    error += gaugeMonitorNotification();
    echo(">>> ----------------------------------------");
    error += stringMonitorNotification();
    echo(">>> ----------------------------------------");
    return error;
}
 
Example 11
Source File: MXBeanTest.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testSubclassMXBean() throws Exception {
    System.out.println("Subclass MXBean test...");
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName on = new ObjectName("test:type=Subclass");
    Subclass subclass = new Subclass();
    mbs.registerMBean(subclass, on);
    testMXBean(mbs, on);
}
 
Example 12
Source File: MXBeanTest.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static void testNonCompliantMXBean(String type, Object bean) throws Exception {
    System.out.println(type + " MXBean test...");
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName on = new ObjectName("test:type=" + type);
    try {
        mbs.registerMBean(bean, on);
        failure(bean.getClass().getInterfaces()[0].getName() + " is not a compliant "
            + "MXBean interface");
    } catch (NotCompliantMBeanException e) {
        success("Non-compliant MXBean not registered");
    }
}
 
Example 13
Source File: MXBeanFallbackTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void testPrivateMXBean(String type, Object bean) throws Exception {
    System.out.println(type + " MXBean test...");
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName on = new ObjectName("test:type=" + type);
    try {
        mbs.registerMBean(bean, on);
        success("Private MXBean registered");
    } catch (NotCompliantMBeanException e) {
        failure("Failed to register the private MXBean - " +
                 bean.getClass().getInterfaces()[0].getName());
    }
}
 
Example 14
Source File: AvoidGetMBeanInfoCallsTest.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Standalone entry point.
 *
 * Run the test and report to stdout.
 */
public static void main(String args[]) throws Exception {

    echo(">>> Create MBeanServer");
    MBeanServer server = MBeanServerFactory.newMBeanServer();

    echo(">>> Default Domain: " + server.getDefaultDomain());

    echo(">>> Create and register Test MBean");
    Test mbean = new Test();
    ObjectName name = ObjectName.getInstance(":type=Test");
    server.registerMBean(mbean, name);

    echo(">>> Set entered flag to false in Test MBean");
    mbean.entered = false;

    echo(">>> Query Names:");
    Set<ObjectName> names = server.queryNames(null, null);
    for (ObjectName on : names) {
        echo("\t" + on.toString());
    }

    echo(">>> Entered flag = " + mbean.entered);

    if (mbean.entered) {
        echo(">>> Test FAILED!");
        throw new IllegalArgumentException("getMBeanInfo got called");
    } else {
        echo(">>> Test PASSED!");
    }
}
 
Example 15
Source File: ExceptionDiagnosisTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void testMXBeans(Object mbean, Type... expectedTypes)
        throws Exception {
    try {
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
        ObjectName name = new ObjectName("a:b=c");
        mbs.registerMBean(mbean, name);
        fail("No exception from registerMBean for " + mbean);
    } catch (NotCompliantMBeanException e) {
        checkExceptionChain("MBean " + mbean, e, expectedTypes);
    }
}
 
Example 16
Source File: AvoidGetMBeanInfoCallsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Standalone entry point.
 *
 * Run the test and report to stdout.
 */
public static void main(String args[]) throws Exception {

    echo(">>> Create MBeanServer");
    MBeanServer server = MBeanServerFactory.newMBeanServer();

    echo(">>> Default Domain: " + server.getDefaultDomain());

    echo(">>> Create and register Test MBean");
    Test mbean = new Test();
    ObjectName name = ObjectName.getInstance(":type=Test");
    server.registerMBean(mbean, name);

    echo(">>> Set entered flag to false in Test MBean");
    mbean.entered = false;

    echo(">>> Query Names:");
    Set<ObjectName> names = server.queryNames(null, null);
    for (ObjectName on : names) {
        echo("\t" + on.toString());
    }

    echo(">>> Entered flag = " + mbean.entered);

    if (mbean.entered) {
        echo(">>> Test FAILED!");
        throw new IllegalArgumentException("getMBeanInfo got called");
    } else {
        echo(">>> Test PASSED!");
    }
}
 
Example 17
Source File: MXBeanTest.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static void testExplicitMXBean() throws Exception {
    System.out.println("Explicit MXBean test...");
    MBeanServer mbs = MBeanServerFactory.newMBeanServer();
    ObjectName on = new ObjectName("test:type=Explicit");
    Explicit explicit = new Explicit();
    mbs.registerMBean(explicit, on);
    testMXBean(mbs, on);
}
 
Example 18
Source File: MXBeanTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static <T> void testInterface(Class<T> c, boolean nullTest)
        throws Exception {

    System.out.println("Testing " + c.getName() +
                       (nullTest ? " for null values" : "") + "...");

    MBeanServer mbs = MBeanServerFactory.newMBeanServer();

    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();

    NamedMXBeans namedMXBeans = new NamedMXBeans(mbsc);
    InvocationHandler ih =
        nullTest ? new MXBeanNullImplInvocationHandler(c, namedMXBeans) :
                   new MXBeanImplInvocationHandler(c, namedMXBeans);
    T impl = c.cast(Proxy.newProxyInstance(c.getClassLoader(),
                                           new Class[] {c},
                                           ih));
    ObjectName on = new ObjectName("test:type=" + c.getName());
    mbs.registerMBean(impl, on);

    System.out.println("Register any MXBeans...");

    Field[] fields = c.getFields();
    for (Field field : fields) {
        String n = field.getName();
        if (n.endsWith("ObjectName")) {
            String objectNameString = (String) field.get(null);
            String base = n.substring(0, n.length() - 10);
            Field f = c.getField(base);
            Object mxbean = f.get(null);
            ObjectName objectName =
                ObjectName.getInstance(objectNameString);
            mbs.registerMBean(mxbean, objectName);
            namedMXBeans.put(objectName, mxbean);
        }
    }

    try {
        testInterface(c, mbsc, on, namedMXBeans, nullTest);
    } finally {
        try {
            cc.close();
        } finally {
            cs.stop();
        }
    }
}
 
Example 19
Source File: NotifReconnectDeadlockTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    System.out.println(
       ">>> Tests reconnection done by a fetching notif thread.");

    ObjectName oname = new ObjectName ("Default:name=NotificationEmitter");
    JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
    Map env = new HashMap(2);
    env.put("jmx.remote.x.server.connection.timeout", new Long(serverTimeout));
    env.put("jmx.remote.x.client.connection.check.period", new Long(Long.MAX_VALUE));

    final MBeanServer mbs = MBeanServerFactory.newMBeanServer();

    mbs.registerMBean(new NotificationEmitter(), oname);
    JMXConnectorServer server = JMXConnectorServerFactory.newJMXConnectorServer(
                                                                           url,
                                                                           env,
                                                                           mbs);
    server.start();

    JMXServiceURL addr = server.getAddress();
    JMXConnector client = JMXConnectorFactory.connect(addr, env);

    Thread.sleep(100); // let pass the first client open notif if there is
    client.getMBeanServerConnection().addNotificationListener(oname,
                                                              listener,
                                                              null,
                                                              null);

    client.addConnectionNotificationListener(listener, null, null);

    // max test time: 2 minutes
    final long end = System.currentTimeMillis()+120000;

    synchronized(lock) {
        while(clientState == null && System.currentTimeMillis() < end) {
            mbs.invoke(oname, "sendNotifications",
                       new Object[] {new Notification("MyType", "", 0)},
                       new String[] {"javax.management.Notification"});

            try {
                lock.wait(10);
            } catch (Exception e) {}
        }
    }

    if (clientState == null) {
        throw new RuntimeException(
              "No reconnection happened, need to reconfigure the test.");
    } else if (JMXConnectionNotification.FAILED.equals(clientState) ||
               JMXConnectionNotification.CLOSED.equals(clientState)) {
        throw new RuntimeException("Failed to reconnect.");
    }

    System.out.println(">>> Passed!");

    client.removeConnectionNotificationListener(listener);
    client.close();
    server.stop();
}
 
Example 20
Source File: UnserializableTargetObjectTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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},
                                  null,  // no constructors
                                  new ModelMBeanOperationInfo[] {
                                      operationInfo, getCountInfo, setCountInfo
                                  },
                                  null); // no notifications
    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");
}