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());
}
}
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();
}
}
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 !!!");
}
}
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());
}
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;
}
}
Aggregations