use of javax.management.remote.JMXConnector in project bnd by bndtools.
the class JMXBundleDeployer method getLocalConnectorAddress.
/**
* Uses Oracle JDK's Attach API to try to search VMs on this machine looking
* for the osgi.core MBeans. This will stop searching for VMs once the
* MBeans are found. Beware if you have multiple JVMs with osgi.core MBeans
* published.
*
*/
@SuppressWarnings("unchecked")
static String getLocalConnectorAddress() {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
ClassLoader toolsClassloader = null;
try {
toolsClassloader = getToolsClassLoader(cl);
if (toolsClassloader != null) {
Thread.currentThread().setContextClassLoader(toolsClassloader);
Class<?> vmClass = toolsClassloader.loadClass("com.sun.tools.attach.VirtualMachine");
Method listMethod = vmClass.getMethod("list");
List<Object> vmds = (List<Object>) listMethod.invoke(null);
for (Object vmd : vmds) {
try {
Class<?> vmdClass = toolsClassloader.loadClass("com.sun.tools.attach.VirtualMachineDescriptor");
Method idMethod = vmdClass.getMethod("id");
String id = (String) idMethod.invoke(vmd);
Method attachMethod = vmClass.getMethod("attach", String.class);
Object vm = attachMethod.invoke(null, id);
try {
Method getAgentPropertiesMethod = vmClass.getMethod("getAgentProperties");
Properties agentProperties = (Properties) getAgentPropertiesMethod.invoke(vm);
String localConnectorAddress = agentProperties.getProperty("com.sun.management.jmxremote.localConnectorAddress");
if (localConnectorAddress == null) {
File agentJar = findJdkJar("management-agent.jar");
if (agentJar != null) {
Method loadAgent = vmClass.getMethod("loadAgent", String.class);
loadAgent.invoke(vm, agentJar.getCanonicalPath());
agentProperties = (Properties) getAgentPropertiesMethod.invoke(vm);
localConnectorAddress = agentProperties.getProperty("com.sun.management.jmxremote.localConnectorAddress");
}
}
if (localConnectorAddress != null) {
final JMXServiceURL jmxServiceUrl = new JMXServiceURL(localConnectorAddress);
final JMXConnector jmxConnector = JMXConnectorFactory.connect(jmxServiceUrl, null);
final MBeanServerConnection mBeanServerConnection = jmxConnector.getMBeanServerConnection();
if (mBeanServerConnection != null) {
final ObjectName framework = getFramework(mBeanServerConnection);
if (framework != null) {
return localConnectorAddress;
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Method detachMethod = vmClass.getMethod("detach");
detachMethod.invoke(vm);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
Thread.currentThread().setContextClassLoader(cl);
// try to get custom classloader to unload native libs
try {
if (toolsClassloader != null) {
Field nl = ClassLoader.class.getDeclaredField("nativeLibraries");
nl.setAccessible(true);
Vector<?> nativeLibs = (Vector<?>) nl.get(toolsClassloader);
for (Object nativeLib : nativeLibs) {
Field nameField = nativeLib.getClass().getDeclaredField("name");
nameField.setAccessible(true);
String name = (String) nameField.get(nativeLib);
if (new File(name).getName().contains("attach")) {
Method f = nativeLib.getClass().getDeclaredMethod("finalize");
f.setAccessible(true);
f.invoke(nativeLib);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
use of javax.management.remote.JMXConnector in project hbase by apache.
the class TestJMXListener method testStart.
@Test
public void testStart() throws Exception {
JMXConnector connector = JMXConnectorFactory.connect(JMXListener.buildJMXServiceURL(connectorPort, connectorPort));
MBeanServerConnection mb = connector.getMBeanServerConnection();
String domain = mb.getDefaultDomain();
Assert.assertTrue("default domain is not correct", !domain.isEmpty());
connector.close();
}
use of javax.management.remote.JMXConnector in project hbase by apache.
the class TestJMXConnectorServer method testHMConnectorServerWhenStopMaster.
/**
* This tests to validate the HMaster's ConnectorServer after unauthorised stopMaster call.
*/
@Test(timeout = 180000)
public void testHMConnectorServerWhenStopMaster() throws Exception {
conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY, JMXListener.class.getName() + "," + MyAccessController.class.getName());
conf.setInt("master.rmi.registry.port", rmiRegistryPort);
UTIL.startMiniCluster();
admin = UTIL.getConnection().getAdmin();
// try to stop master
boolean accessDenied = false;
try {
hasAccess = false;
LOG.info("Stopping HMaster...");
admin.stopMaster();
} catch (AccessDeniedException e) {
LOG.info("Exception occured while stopping HMaster. ", e);
accessDenied = true;
}
Assert.assertTrue(accessDenied);
// Check whether HMaster JMX Connector server can be connected
JMXConnector connector = null;
try {
connector = JMXConnectorFactory.connect(JMXListener.buildJMXServiceURL(rmiRegistryPort, rmiRegistryPort));
} catch (IOException e) {
if (e.getCause() instanceof ServiceUnavailableException) {
Assert.fail("Can't connect to HMaster ConnectorServer.");
}
}
Assert.assertNotNull("JMXConnector should not be null.", connector);
connector.close();
}
use of javax.management.remote.JMXConnector in project hbase by apache.
the class TestStochasticBalancerJmxMetrics method readJmxMetrics.
/**
* Read the attributes from Hadoop->HBase->Master->Balancer in JMX
* @throws IOException
*/
private Set<String> readJmxMetrics() throws IOException {
JMXConnector connector = null;
ObjectName target = null;
MBeanServerConnection mb = null;
try {
connector = JMXConnectorFactory.connect(JMXListener.buildJMXServiceURL(connectorPort, connectorPort));
mb = connector.getMBeanServerConnection();
Hashtable<String, String> pairs = new Hashtable<>();
pairs.put("service", "HBase");
pairs.put("name", "Master");
pairs.put("sub", "Balancer");
target = new ObjectName("Hadoop", pairs);
MBeanInfo beanInfo = mb.getMBeanInfo(target);
Set<String> existingAttrs = new HashSet<>();
for (MBeanAttributeInfo attrInfo : beanInfo.getAttributes()) {
existingAttrs.add(attrInfo.getName());
}
return existingAttrs;
} catch (Exception e) {
LOG.warn("Failed to get bean!!! " + target, e);
if (mb != null) {
Set<ObjectInstance> instances = mb.queryMBeans(null, null);
Iterator<ObjectInstance> iterator = instances.iterator();
System.out.println("MBean Found:");
while (iterator.hasNext()) {
ObjectInstance instance = iterator.next();
System.out.println("Class Name: " + instance.getClassName());
System.out.println("Object Name: " + instance.getObjectName());
}
}
} finally {
if (connector != null) {
try {
connector.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
use of javax.management.remote.JMXConnector in project camel by apache.
the class ManagedSEDeployableContainer method isJMXTestRunnerMBeanRegistered.
private boolean isJMXTestRunnerMBeanRegistered(final String host, final int port, int waitTime) throws DeploymentException {
// Taken from org.jboss.arquillian.container.spi.client.protocol.metadata.JMXContext
final String jmxServiceUrl = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi";
try (JMXConnector jmxc = JMXConnectorFactory.connect(new JMXServiceURL(jmxServiceUrl), null)) {
final MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
return new Await(waitTime, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
mbsc.getObjectInstance(new ObjectName(JMXTestRunnerMBean.OBJECT_NAME));
LOGGER.fine("JMXTestRunnerMBean registered with the remote MBean server at: " + jmxServiceUrl);
return true;
}
}).start();
} catch (IOException e) {
throw new DeploymentException("Could not verify JMXTestRunnerMBean registration", e);
}
}
Aggregations