Search in sources :

Example 56 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project jdk8u_jdk by JetBrains.

the class UnserializableTargetObjectTest method main.

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 }, // no constructors
    null, new ModelMBeanOperationInfo[] { operationInfo, getCountInfo, setCountInfo }, // no notifications
    null);
    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");
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) ModelMBeanOperationInfo(javax.management.modelmbean.ModelMBeanOperationInfo) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) ModelMBean(javax.management.modelmbean.ModelMBean) Attribute(javax.management.Attribute) DescriptorSupport(javax.management.modelmbean.DescriptorSupport) Method(java.lang.reflect.Method) ModelMBeanInfo(javax.management.modelmbean.ModelMBeanInfo) ObjectName(javax.management.ObjectName) RequiredModelMBean(javax.management.modelmbean.RequiredModelMBean) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ModelMBeanAttributeInfo(javax.management.modelmbean.ModelMBeanAttributeInfo) JMXConnector(javax.management.remote.JMXConnector) Descriptor(javax.management.Descriptor) ModelMBeanInfoSupport(javax.management.modelmbean.ModelMBeanInfoSupport) MBeanServerConnection(javax.management.MBeanServerConnection) MBeanServer(javax.management.MBeanServer)

Example 57 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project geode by apache.

the class MBeanProcessController method connect.

/**
   * Connects to the JMX agent in the local process.
   * 
   * @throws ConnectionFailedException if there was a failure to connect to the local JMX connector
   *         in the process
   * @throws IOException if the JDK management agent cannot be found and loaded
   */
private void connect() throws ConnectionFailedException, IOException {
    try {
        final JMXServiceURL jmxUrl = getJMXServiceURL();
        this.jmxc = JMXConnectorFactory.connect(jmxUrl);
        this.server = this.jmxc.getMBeanServerConnection();
    } catch (AttachNotSupportedException e) {
        throw new ConnectionFailedException("Failed to connect to process '" + this.pid + "'", e);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException)

Example 58 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project geode by apache.

the class MBeanProcessController method getJMXServiceURL.

/**
   * Uses the Attach API to connect to the local process and ensures that it has loaded the JMX
   * management agent. The JMXServiceURL identifying the local connector address for the JMX agent
   * in the process is returned.
   * 
   * @return the address of the JMX API connector server for connecting to the local process
   * 
   * @throws AttachNotSupportedException if unable to use the Attach API to connect to the process
   * @throws IOException if the JDK management agent cannot be found and loaded
   */
private JMXServiceURL getJMXServiceURL() throws AttachNotSupportedException, IOException {
    String connectorAddress = null;
    final VirtualMachine vm = VirtualMachine.attach(String.valueOf(this.pid));
    try {
        Properties agentProps = vm.getAgentProperties();
        connectorAddress = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
        if (connectorAddress == null) {
            // need to load the management-agent and get the address
            final String javaHome = vm.getSystemProperties().getProperty("java.home");
            // assume java.home is JDK and look in JRE for agent
            String managementAgentPath = javaHome + File.separator + "jre" + File.separator + "lib" + File.separator + "management-agent.jar";
            File managementAgent = new File(managementAgentPath);
            if (!managementAgent.exists()) {
                // assume java.home is JRE and look in lib for agent
                managementAgentPath = javaHome + File.separator + "lib" + File.separator + "management-agent.jar";
                managementAgent = new File(managementAgentPath);
                if (!managementAgent.exists()) {
                    throw new IOException("JDK management agent not found");
                }
            }
            // attempt to load the management agent
            managementAgentPath = managementAgent.getCanonicalPath();
            try {
                vm.loadAgent(managementAgentPath, "com.sun.management.jmxremote");
            } catch (AgentLoadException e) {
                IOException ioe = new IOException(e.getMessage());
                ioe.initCause(e);
                throw ioe;
            } catch (AgentInitializationException e) {
                IOException ioe = new IOException(e.getMessage());
                ioe.initCause(e);
                throw ioe;
            }
            // get the connector address
            agentProps = vm.getAgentProperties();
            connectorAddress = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
        }
    } finally {
        vm.detach();
    }
    if (connectorAddress == null) {
        // should never reach here
        throw new IOException("Failed to find address to attach to process");
    }
    return new JMXServiceURL(connectorAddress);
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) AgentLoadException(com.sun.tools.attach.AgentLoadException) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) AgentInitializationException(com.sun.tools.attach.AgentInitializationException) VirtualMachine(com.sun.tools.attach.VirtualMachine)

Example 59 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project geode by apache.

the class LocalProcessController method connect.

/**
   * Connects to the JMX agent in the local process.
   * 
   * @throws ConnectionFailedException if there was a failure to connect to the local JMX connector
   *         in the process
   * @throws IOException if the JDK management agent cannot be found and loaded
   */
void connect() throws ConnectionFailedException, IOException {
    try {
        final JMXServiceURL jmxUrl = getJMXServiceURL();
        this.jmxc = JMXConnectorFactory.connect(jmxUrl);
        this.server = this.jmxc.getMBeanServerConnection();
    } catch (AttachNotSupportedException e) {
        throw new ConnectionFailedException("Failed to connect to process '" + this.pid + "'", e);
    }
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException)

Example 60 with JMXServiceURL

use of javax.management.remote.JMXServiceURL in project geode by apache.

the class LocalProcessController method getJMXServiceURL.

/**
   * Uses the Attach API to connect to the local process and ensures that it has loaded the JMX
   * management agent. The JMXServiceURL identifying the local connector address for the JMX agent
   * in the process is returned.
   * 
   * @return the address of the JMX API connector server for connecting to the local process
   * 
   * @throws AttachNotSupportedException if unable to use the Attach API to connect to the process
   * @throws IOException if the JDK management agent cannot be found and loaded
   */
private JMXServiceURL getJMXServiceURL() throws AttachNotSupportedException, IOException {
    String connectorAddress = null;
    final VirtualMachine vm = VirtualMachine.attach(String.valueOf(this.pid));
    try {
        Properties agentProps = vm.getAgentProperties();
        connectorAddress = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
        if (connectorAddress == null) {
            // need to load the management-agent and get the address
            final String javaHome = vm.getSystemProperties().getProperty("java.home");
            // assume java.home is JDK and look in JRE for agent
            String managementAgentPath = javaHome + File.separator + "jre" + File.separator + "lib" + File.separator + "management-agent.jar";
            File managementAgent = new File(managementAgentPath);
            if (!managementAgent.exists()) {
                // assume java.home is JRE and look in lib for agent
                managementAgentPath = javaHome + File.separator + "lib" + File.separator + "management-agent.jar";
                managementAgent = new File(managementAgentPath);
                if (!managementAgent.exists()) {
                    throw new IOException("JDK management agent not found");
                }
            }
            // attempt to load the management agent
            managementAgentPath = managementAgent.getCanonicalPath();
            try {
                vm.loadAgent(managementAgentPath, "com.sun.management.jmxremote");
            } catch (AgentLoadException e) {
                IOException ioe = new IOException(e.getMessage());
                ioe.initCause(e);
                throw ioe;
            } catch (AgentInitializationException e) {
                IOException ioe = new IOException(e.getMessage());
                ioe.initCause(e);
                throw ioe;
            }
            // get the connector address
            agentProps = vm.getAgentProperties();
            connectorAddress = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
        }
    } finally {
        vm.detach();
    }
    if (connectorAddress == null) {
        // should never reach here
        throw new IOException("Failed to find address to attach to process");
    }
    return new JMXServiceURL(connectorAddress);
}
Also used : JMXServiceURL(javax.management.remote.JMXServiceURL) AgentLoadException(com.sun.tools.attach.AgentLoadException) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) AgentInitializationException(com.sun.tools.attach.AgentInitializationException) VirtualMachine(com.sun.tools.attach.VirtualMachine)

Aggregations

JMXServiceURL (javax.management.remote.JMXServiceURL)280 JMXConnector (javax.management.remote.JMXConnector)150 MBeanServerConnection (javax.management.MBeanServerConnection)107 ObjectName (javax.management.ObjectName)90 IOException (java.io.IOException)78 HashMap (java.util.HashMap)75 MBeanServer (javax.management.MBeanServer)71 JMXConnectorServer (javax.management.remote.JMXConnectorServer)64 MalformedURLException (java.net.MalformedURLException)45 RemoteException (java.rmi.RemoteException)22 Test (org.junit.Test)22 Map (java.util.Map)16 UnknownHostException (java.net.UnknownHostException)14 Properties (java.util.Properties)14 Notification (javax.management.Notification)14 NotificationListener (javax.management.NotificationListener)14 MalformedObjectNameException (javax.management.MalformedObjectNameException)13 LocateRegistry (java.rmi.registry.LocateRegistry)12 Registry (java.rmi.registry.Registry)12 ArrayList (java.util.ArrayList)11