Search in sources :

Example 1 with AgentLoadException

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

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

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

Example 4 with AgentLoadException

use of com.sun.tools.attach.AgentLoadException in project jdk8u_jdk by JetBrains.

the class AixVirtualMachine method execute.

/**
     * Execute the given command in the target VM.
     */
InputStream execute(String cmd, Object... args) throws AgentLoadException, IOException {
    // includes null
    assert args.length <= 3;
    // did we detach?
    String p;
    synchronized (this) {
        if (this.path == null) {
            throw new IOException("Detached from target VM");
        }
        p = this.path;
    }
    // create UNIX socket
    int s = socket();
    // connect to target VM
    try {
        connect(s, p);
    } catch (IOException x) {
        close(s);
        throw x;
    }
    IOException ioe = null;
    // <ver> <cmd> <args...>
    try {
        writeString(s, PROTOCOL_VERSION);
        writeString(s, cmd);
        for (int i = 0; i < 3; i++) {
            if (i < args.length && args[i] != null) {
                writeString(s, (String) args[i]);
            } else {
                writeString(s, "");
            }
        }
    } catch (IOException x) {
        ioe = x;
    }
    // Create an input stream to read reply
    SocketInputStream sis = new SocketInputStream(s);
    // Read the command completion status
    int completionStatus;
    try {
        completionStatus = readInt(sis);
    } catch (IOException x) {
        sis.close();
        if (ioe != null) {
            throw ioe;
        } else {
            throw x;
        }
    }
    if (completionStatus != 0) {
        // read from the stream and use that as the error message
        String message = readErrorMessage(sis);
        sis.close();
        // error.
        if (completionStatus == ATTACH_ERROR_BADVERSION) {
            throw new IOException("Protocol mismatch with target VM");
        }
        // thrown.
        if (cmd.equals("load")) {
            throw new AgentLoadException("Failed to load agent library");
        } else {
            if (message == null) {
                throw new AttachOperationFailedException("Command failed in target VM");
            } else {
                throw new AttachOperationFailedException(message);
            }
        }
    }
    // Return the input stream so that the command output can be read
    return sis;
}
Also used : AttachOperationFailedException(com.sun.tools.attach.AttachOperationFailedException) AgentLoadException(com.sun.tools.attach.AgentLoadException) IOException(java.io.IOException)

Example 5 with AgentLoadException

use of com.sun.tools.attach.AgentLoadException in project jdk8u_jdk by JetBrains.

the class BsdVirtualMachine method execute.

/**
     * Execute the given command in the target VM.
     */
InputStream execute(String cmd, Object... args) throws AgentLoadException, IOException {
    // includes null
    assert args.length <= 3;
    // did we detach?
    String p;
    synchronized (this) {
        if (this.path == null) {
            throw new IOException("Detached from target VM");
        }
        p = this.path;
    }
    // create UNIX socket
    int s = socket();
    // connect to target VM
    try {
        connect(s, p);
    } catch (IOException x) {
        close(s);
        throw x;
    }
    IOException ioe = null;
    // <ver> <cmd> <args...>
    try {
        writeString(s, PROTOCOL_VERSION);
        writeString(s, cmd);
        for (int i = 0; i < 3; i++) {
            if (i < args.length && args[i] != null) {
                writeString(s, (String) args[i]);
            } else {
                writeString(s, "");
            }
        }
    } catch (IOException x) {
        ioe = x;
    }
    // Create an input stream to read reply
    SocketInputStream sis = new SocketInputStream(s);
    // Read the command completion status
    int completionStatus;
    try {
        completionStatus = readInt(sis);
    } catch (IOException x) {
        sis.close();
        if (ioe != null) {
            throw ioe;
        } else {
            throw x;
        }
    }
    if (completionStatus != 0) {
        // read from the stream and use that as the error message
        String message = readErrorMessage(sis);
        sis.close();
        // error.
        if (completionStatus == ATTACH_ERROR_BADVERSION) {
            throw new IOException("Protocol mismatch with target VM");
        }
        // thrown.
        if (cmd.equals("load")) {
            throw new AgentLoadException("Failed to load agent library");
        } else {
            if (message == null) {
                throw new AttachOperationFailedException("Command failed in target VM");
            } else {
                throw new AttachOperationFailedException(message);
            }
        }
    }
    // Return the input stream so that the command output can be read
    return sis;
}
Also used : AttachOperationFailedException(com.sun.tools.attach.AttachOperationFailedException) AgentLoadException(com.sun.tools.attach.AgentLoadException) IOException(java.io.IOException)

Aggregations

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