Search in sources :

Example 1 with AgentInitializationException

use of com.sun.tools.attach.AgentInitializationException in project druid by alibaba.

the class DruidStat method loadManagementAgentAndGetAddress.

private static String loadManagementAgentAndGetAddress(int vmid) throws IOException {
    VirtualMachine vm = null;
    String name = String.valueOf(vmid);
    try {
        vm = VirtualMachine.attach(name);
    } catch (AttachNotSupportedException x) {
        throw new IOException(x.getMessage(), x);
    }
    String home = vm.getSystemProperties().getProperty("java.home");
    // Normally in ${java.home}/jre/lib/management-agent.jar but might
    // be in ${java.home}/lib in build environments.
    String agent = home + File.separator + "jre" + File.separator + "lib" + File.separator + "management-agent.jar";
    File f = new File(agent);
    if (!f.exists()) {
        agent = home + File.separator + "lib" + File.separator + "management-agent.jar";
        f = new File(agent);
        if (!f.exists()) {
            throw new IOException("Management agent not found");
        }
    }
    agent = f.getCanonicalPath();
    try {
        vm.loadAgent(agent, "com.sun.management.jmxremote");
    } catch (AgentLoadException x) {
        throw new IOException(x.getMessage(), x);
    } catch (AgentInitializationException x) {
        throw new IOException(x.getMessage(), x);
    }
    // get the connector address
    Properties agentProps = vm.getAgentProperties();
    String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
    vm.detach();
    return address;
}
Also used : AgentLoadException(com.sun.tools.attach.AgentLoadException) IOException(java.io.IOException) AttachNotSupportedException(com.sun.tools.attach.AttachNotSupportedException) Properties(java.util.Properties) File(java.io.File) AgentInitializationException(com.sun.tools.attach.AgentInitializationException) VirtualMachine(com.sun.tools.attach.VirtualMachine)

Example 2 with AgentInitializationException

use of com.sun.tools.attach.AgentInitializationException 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 3 with AgentInitializationException

use of com.sun.tools.attach.AgentInitializationException 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

AgentInitializationException (com.sun.tools.attach.AgentInitializationException)3 AgentLoadException (com.sun.tools.attach.AgentLoadException)3 VirtualMachine (com.sun.tools.attach.VirtualMachine)3 File (java.io.File)3 IOException (java.io.IOException)3 Properties (java.util.Properties)3 JMXServiceURL (javax.management.remote.JMXServiceURL)2 AttachNotSupportedException (com.sun.tools.attach.AttachNotSupportedException)1