Search in sources :

Example 56 with JMXConnector

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;
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) Method(java.lang.reflect.Method) Properties(java.util.Properties) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MalformedObjectNameException(javax.management.MalformedObjectNameException) ObjectName(javax.management.ObjectName) Field(java.lang.reflect.Field) JMXConnector(javax.management.remote.JMXConnector) URLClassLoader(java.net.URLClassLoader) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) Vector(java.util.Vector) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 57 with JMXConnector

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();
}
Also used : JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection) Test(org.junit.Test)

Example 58 with JMXConnector

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();
}
Also used : AccessDeniedException(org.apache.hadoop.hbase.security.AccessDeniedException) JMXConnector(javax.management.remote.JMXConnector) IOException(java.io.IOException) ServiceUnavailableException(javax.naming.ServiceUnavailableException) Test(org.junit.Test)

Example 59 with JMXConnector

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;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) MBeanInfo(javax.management.MBeanInfo) Hashtable(java.util.Hashtable) ObjectInstance(javax.management.ObjectInstance) MBeanAttributeInfo(javax.management.MBeanAttributeInfo) IOException(java.io.IOException) ObjectName(javax.management.ObjectName) JMXConnector(javax.management.remote.JMXConnector) Iterator(java.util.Iterator) MBeanServerConnection(javax.management.MBeanServerConnection) HashSet(java.util.HashSet)

Example 60 with JMXConnector

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);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) JMXConnector(javax.management.remote.JMXConnector) DeploymentException(org.jboss.arquillian.container.spi.client.container.DeploymentException) IOException(java.io.IOException) Await(org.apache.camel.itest.springboot.arquillian.container.util.Await) MBeanServerConnection(javax.management.MBeanServerConnection) Callable(java.util.concurrent.Callable) ObjectName(javax.management.ObjectName)

Aggregations

JMXConnector (javax.management.remote.JMXConnector)118 MBeanServerConnection (javax.management.MBeanServerConnection)85 JMXServiceURL (javax.management.remote.JMXServiceURL)78 ObjectName (javax.management.ObjectName)54 JMXConnectorServer (javax.management.remote.JMXConnectorServer)47 MBeanServer (javax.management.MBeanServer)37 IOException (java.io.IOException)35 HashMap (java.util.HashMap)27 Test (org.junit.Test)22 Notification (javax.management.Notification)14 NotificationListener (javax.management.NotificationListener)14 Attribute (javax.management.Attribute)13 MalformedURLException (java.net.MalformedURLException)12 RemoteException (java.rmi.RemoteException)11 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 MalformedObjectNameException (javax.management.MalformedObjectNameException)9 LocateRegistry (java.rmi.registry.LocateRegistry)8 Registry (java.rmi.registry.Registry)8 Properties (java.util.Properties)7