Search in sources :

Example 86 with MalformedObjectNameException

use of javax.management.MalformedObjectNameException in project openj9 by eclipse.

the class TestManagementFactory method testExtMemoryMXBeanProxyNotificationEmitter.

/**
 * For IBM extensions on the MemoryMXBean
 */
@Test
public void testExtMemoryMXBeanProxyNotificationEmitter() {
    try {
        com.ibm.lang.management.MemoryMXBean proxy = ManagementFactory.newPlatformMXBeanProxy(ManagementFactory.getPlatformMBeanServer(), "java.lang:type=Memory", com.ibm.lang.management.MemoryMXBean.class);
        MemoryMXBean stdProxy = proxy;
        AssertJUnit.assertNotNull(stdProxy);
        // Verify that the proxy is acting as a NotificationEmitter
        AssertJUnit.assertTrue(proxy instanceof NotificationEmitter);
        NotificationEmitter proxyEmitter = (NotificationEmitter) proxy;
        // Add a listener with a handback object.
        MyTestListener listener = new MyTestListener();
        ArrayList<String> arr = new ArrayList<>();
        arr.add("Save your money for the children.");
        proxyEmitter.addNotificationListener(listener, null, arr);
        // Fire off a notification and ensure that the listener receives it.
        try {
            MemoryUsage mu = new MemoryUsage(1, 2, 3, 4);
            MemoryNotificationInfo info = new MemoryNotificationInfo("Bob", mu, 42);
            CompositeData cd = TestUtil.toCompositeData(info);
            Notification notification = new Notification(MemoryNotificationInfo.MEMORY_THRESHOLD_EXCEEDED, new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), 42);
            notification.setUserData(cd);
            ((MemoryMXBeanImpl) ManagementFactory.getMemoryMXBean()).sendNotification(notification);
            AssertJUnit.assertEquals(1, listener.getNotificationsReceivedCount());
            // Verify that the handback is as expected.
            AssertJUnit.assertNotNull(listener.getHandback());
            AssertJUnit.assertSame(arr, listener.getHandback());
            ArrayList<?> arr2 = (ArrayList<?>) listener.getHandback();
            AssertJUnit.assertEquals(1, arr2.size());
            AssertJUnit.assertEquals("Save your money for the children.", arr2.get(0));
        } catch (MalformedObjectNameException e) {
            Assert.fail("Unexpected MalformedObjectNameException : " + e.getMessage());
            e.printStackTrace();
        }
    } catch (IllegalArgumentException e) {
        Assert.fail("Unexpected IllegalArgumentException : " + e.getMessage());
    } catch (NullPointerException e) {
        Assert.fail("Unexpected NullPointerException : " + e.getMessage());
    } catch (IOException e) {
        Assert.fail("Unexpected IOException : " + e.getMessage());
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) MemoryMXBeanImpl(com.ibm.java.lang.management.internal.MemoryMXBeanImpl) CompositeData(javax.management.openmbean.CompositeData) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MemoryUsage(java.lang.management.MemoryUsage) Notification(javax.management.Notification) ObjectName(javax.management.ObjectName) MemoryNotificationInfo(java.lang.management.MemoryNotificationInfo) MemoryMXBean(java.lang.management.MemoryMXBean) NotificationEmitter(javax.management.NotificationEmitter) Test(org.testng.annotations.Test)

Example 87 with MalformedObjectNameException

use of javax.management.MalformedObjectNameException in project openj9 by eclipse.

the class TestManagementFactory method testThreadBeanRegisteredWithServer.

@Test
public final void testThreadBeanRegisteredWithServer() {
    MBeanServer pServer = ManagementFactory.getPlatformMBeanServer();
    AssertJUnit.assertNotNull(pServer);
    try {
        AssertJUnit.assertTrue(pServer.isRegistered(new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME)));
    } catch (MalformedObjectNameException e) {
        Assert.fail("Unexpected MalformedObjectNameException !");
        e.printStackTrace();
    } catch (NullPointerException e) {
        Assert.fail("Unexpected NullPointerException !");
        e.printStackTrace();
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName) Test(org.testng.annotations.Test)

Example 88 with MalformedObjectNameException

use of javax.management.MalformedObjectNameException in project openj9 by eclipse.

the class GuestOSMXBeanTest method runGuestOSMXBeanTest.

/**
 * Starting point for the test program. Invokes the memory and processor test routines and
 * indicates success or failure accordingly.
 *
 * @param args
 */
@Test
public static void runGuestOSMXBeanTest() {
    int retryCounter = 0;
    boolean localTest = true;
    boolean isConnected = false;
    boolean error = false;
    JMXServiceURL urlForRemoteMachine = null;
    JMXConnector connector = null;
    MBeanServerConnection mbeanConnection = null;
    MBeanServer mbeanServer = null;
    ObjectName mxbeanName = null;
    GuestOSMXBean mxbeanProxy = null;
    logger.info(" ---------------------------------------");
    logger.info(" Starting the GuestOSMXBean API tests....");
    logger.info(" ---------------------------------------");
    /* Check and set flag 'localTest'. If the user specified 'local' or nothing, select local testing,
		 * while under explicit specification of the option 'remote' do we platform remote testing.
		 */
    if (System.getProperty("RUNLOCAL").equalsIgnoreCase("false")) {
        localTest = false;
    } else if (!System.getProperty("RUNLOCAL").equalsIgnoreCase("true")) {
        /* Failed parsing a sensible option specified by the user. */
        logger.error("GuestOSMXBeanTest usage:");
        logger.error(" -DRUNLOCAL=[false, true]  $GuestOSMXBeanTest");
        logger.error("Valid options are 'true' and 'false'.");
        logger.error("In absence of a specified option, test defaults to local.");
        Assert.fail("Invalid property");
    }
    /* We need the object name in any case - remote as well as local. */
    try {
        mxbeanName = new ObjectName("com.ibm.virtualization.management:type=GuestOS");
    } catch (MalformedObjectNameException e) {
        Assert.fail("MalformedObjectNameException!", e);
    }
    /* Perform the appropriate test as instructed through the command line. */
    if (false == localTest) {
        /* A remote test needs to be performed. Start the remote server if it is not running yet.
			 * Also, attach a watch dog to this, to bail out, in case it hangs.
			 */
        if (null == remoteServer) {
            startRemoteServer();
            watchdog = new ChildWatchdog(remoteServer, 30000);
        }
        /* Try connecting to the server; retry until limit reached. */
        while (retryCounter < 10 && !isConnected) {
            retryCounter++;
            try {
                urlForRemoteMachine = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi");
                /* Connect to remote host and create a proxy to be used to get an GuestOSMXBean
					 * instance.
					 */
                connector = JMXConnectorFactory.connect(urlForRemoteMachine, null);
                mbeanConnection = connector.getMBeanServerConnection();
                /* Obtain an MBean handle using the connection and the
					 *  object name for the class GuestOSMXBean.
					 */
                mxbeanProxy = JMX.newMXBeanProxy(mbeanConnection, mxbeanName, GuestOSMXBean.class);
                if (true != mbeanConnection.isRegistered(mxbeanName)) {
                    Assert.fail("GuestOSMXBean is not registered. Cannot Proceed with the test. !!!Test Failed !!!");
                }
                /* If we reached here, it means we are connected (no connection failure exception thrown). */
                isConnected = true;
            } catch (MalformedURLException e) {
                Assert.fail("Please check the url supplied to JMXServiceURL!", e);
            } catch (IOException e) {
                /* Waiting 1000 ms before retrying to connect to remote server. */
                logger.warn("Failed connecting. Retry " + retryCounter + " after 1000 ms.");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ie) {
                    logger.warn("Exception occurred while sleeping thread: " + ie.getMessage(), e);
                }
            } catch (Exception e) {
                Assert.fail("Exception occurred in setting up remote test environment.");
            }
            if (!isConnected) {
                Assert.fail("Unable to connect to Remote Server. !!!Test Failed !!!");
            }
        }
    } else {
        /*
			 * A local test has been requested.
			 */
        try {
            mbeanServer = ManagementFactory.getPlatformMBeanServer();
            if (true != mbeanServer.isRegistered(mxbeanName)) {
                Assert.fail("GuestOSMXBean is not registered. Cannot Proceed with the test.");
            }
            mxbeanProxy = JMX.newMXBeanProxy(mbeanServer, mxbeanName, GuestOSMXBean.class);
        } catch (Exception e) {
            Assert.fail("Exception occurred in setting up local test environment." + e.getMessage(), e);
        }
    }
    try {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        HypervisorMXBean bean = ManagementFactory.getPlatformMXBean(mbs, HypervisorMXBean.class);
        HypervisorName = bean.getVendor();
        if (null == HypervisorName) {
            HypervisorName = "none";
        }
    } catch (Exception e) {
        Assert.fail("Unexpected exception occured" + e.getMessage(), e);
    }
    try {
        error |= test_memoryInfo(mxbeanProxy);
        error |= test_processorInfo(mxbeanProxy);
    } finally {
        /* Do all clean up here. */
        if (false == localTest) {
            /* Close the connection and Stop the remote Server */
            try {
                connector.close();
                stopRemoteServer();
            } catch (Exception e) {
                Assert.fail("Unexpected exception occured when close connector" + e.getMessage(), e);
            }
        }
    }
    if (false != error) {
        Assert.fail(" !!!Test Failed !!!");
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) MalformedObjectNameException(javax.management.MalformedObjectNameException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) MalformedObjectNameException(javax.management.MalformedObjectNameException) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) ChildWatchdog(org.openj9.test.management.util.ChildWatchdog) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer) Test(org.testng.annotations.Test)

Example 89 with MalformedObjectNameException

use of javax.management.MalformedObjectNameException in project openflowplugin by opendaylight.

the class SalBulkFlowServiceImpl method register.

@Override
public Future<RpcResult<Void>> register() {
    RpcResultBuilder<Void> rpcResultBuilder = RpcResultBuilder.success();
    try {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        String pathToMBean = String.format("%s:type=%s", FlowCounter.class.getPackage().getName(), FlowCounter.class.getSimpleName());
        ObjectName name = new ObjectName(pathToMBean);
        mbs.registerMBean(flowCounterBeanImpl, name);
    } catch (MalformedObjectNameException | InstanceAlreadyExistsException | MBeanRegistrationException | NotCompliantMBeanException e) {
        rpcResultBuilder = RpcResultBuilder.failed();
        LOG.warn("Exception occurred: {} ", e.getMessage(), e);
    }
    return Futures.immediateFuture(rpcResultBuilder.build());
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) InstanceAlreadyExistsException(javax.management.InstanceAlreadyExistsException) MBeanRegistrationException(javax.management.MBeanRegistrationException) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 90 with MalformedObjectNameException

use of javax.management.MalformedObjectNameException in project tomcat70 by apache.

the class MBeanUtils method createObjectName.

/**
 * Create an <code>ObjectName</code> for this
 * <code>Connector</code> object.
 *
 * @param domain Domain in which this name is to be created
 * @param connector The Connector to be named
 *
 * @exception MalformedObjectNameException if a name cannot be created
 * @deprecated  Unused. Will be removed in Tomcat 8.0.x
 */
@Deprecated
static ObjectName createObjectName(String domain, Connector connector) throws MalformedObjectNameException {
    ObjectName name = null;
    try {
        Object addressObj = IntrospectionUtils.getProperty(connector, "address");
        Integer port = (Integer) IntrospectionUtils.getProperty(connector, "port");
        StringBuilder sb = new StringBuilder(domain);
        sb.append(":type=Connector");
        sb.append(",port=");
        sb.append(port);
        if (addressObj != null) {
            String address = addressObj.toString();
            if (address.length() > 0) {
                sb.append(",address=");
                sb.append(ObjectName.quote(address));
            }
        }
        name = new ObjectName(sb.toString());
        return (name);
    } catch (Exception e) {
        MalformedObjectNameException mone = new MalformedObjectNameException("Cannot create object name for " + connector);
        mone.initCause(e);
        throw mone;
    }
}
Also used : MalformedObjectNameException(javax.management.MalformedObjectNameException) MalformedObjectNameException(javax.management.MalformedObjectNameException) MBeanException(javax.management.MBeanException) ObjectName(javax.management.ObjectName)

Aggregations

MalformedObjectNameException (javax.management.MalformedObjectNameException)309 ObjectName (javax.management.ObjectName)285 InstanceNotFoundException (javax.management.InstanceNotFoundException)88 MBeanServer (javax.management.MBeanServer)80 MBeanRegistrationException (javax.management.MBeanRegistrationException)75 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)63 InstanceAlreadyExistsException (javax.management.InstanceAlreadyExistsException)59 IOException (java.io.IOException)53 ArrayList (java.util.ArrayList)45 ReflectionException (javax.management.ReflectionException)30 MBeanException (javax.management.MBeanException)23 Test (org.testng.annotations.Test)23 CompositeData (javax.management.openmbean.CompositeData)18 Notification (javax.management.Notification)15 MBeanInfo (javax.management.MBeanInfo)14 AttributeNotFoundException (javax.management.AttributeNotFoundException)13 HashMap (java.util.HashMap)11 Map (java.util.Map)11 IntrospectionException (javax.management.IntrospectionException)11 MalformedURLException (java.net.MalformedURLException)10