From eefda671b6dedf3ad08c5a8adaff08ea440abf27 Mon Sep 17 00:00:00 2001
From: sla In this example we attach to a Java virtual machine that is identified by
- * the process identifier 2177
. The system properties from the target
- * VM are then used to construct the path to a management agent which is then
- * loaded into the target VM. Once loaded the client detaches from the target VM. 2177
. Then the JMX management agent is
+ * started in the target process using the supplied arguments. Finally, the
+ * client detaches from the target VM.
A VirtualMachine is safe for use by multiple concurrent threads.
* @@ -610,6 +604,68 @@ public abstract class VirtualMachine { */ public abstract Properties getAgentProperties() throws IOException; + /** + * Starts the JMX management agent in the target virtual machine. + * + *The configuration properties are the same as those specified on + * the command line when starting the JMX management agent. In the same + * way as on the command line, you need to specify at least the + * {@code com.sun.management.jmxremote.port} property. + * + *
See the online documentation for + * Monitoring and Management Using JMX Technology for further details. + * + * @param agentProperties + * A Properties object containing the configuration properties + * for the agent. + * + * @throws AttachOperationFailedException + * If the target virtual machine is unable to complete the + * attach operation. A more specific error message will be + * given by {@link AttachOperationFailedException#getMessage()}. + * + * @throws IOException + * If an I/O error occurs, a communication error for example, + * that cannot be identified as an error to indicate that the + * operation failed in the target VM. + * + * @throws IllegalArgumentException + * If keys or values in agentProperties are invalid. + * + * @throws NullPointerException + * If agentProperties is null. + * + * @since 1.8 + */ + public abstract void startManagementAgent(Properties agentProperties) throws IOException; + + /** + * Starts the local JMX management agent in the target virtual machine. + * + *
See the online documentation for
+ * Monitoring and Management Using JMX Technology for further details.
+ *
+ * @return The String representation of the local connector's service address.
+ * The value can be parsed by the
+ * {@link javax.management.remote.JMXServiceURL#JMXServiceURL(String)}
+ * constructor.
+ *
+ * @throws AttachOperationFailedException
+ * If the target virtual machine is unable to complete the
+ * attach operation. A more specific error message will be
+ * given by {@link AttachOperationFailedException#getMessage()}.
+ *
+ * @throws IOException
+ * If an I/O error occurs, a communication error for example,
+ * that cannot be identified as an error to indicate that the
+ * operation failed in the target VM.
+ *
+ * @since 1.8
+ */
+ public abstract String startLocalManagementAgent() throws IOException;
+
/**
* Returns a hash-code value for this VirtualMachine. The hash
* code is based upon the VirtualMachine's components, and satifies
diff --git a/src/share/classes/sun/tools/attach/HotSpotVirtualMachine.java b/src/share/classes/sun/tools/attach/HotSpotVirtualMachine.java
index 862b07cf4..bf2e95165 100644
--- a/src/share/classes/sun/tools/attach/HotSpotVirtualMachine.java
+++ b/src/share/classes/sun/tools/attach/HotSpotVirtualMachine.java
@@ -33,7 +33,7 @@ import com.sun.tools.attach.spi.AttachProvider;
import java.io.InputStream;
import java.io.IOException;
import java.util.Properties;
-import java.util.Map;
+import java.util.stream.Collectors;
/*
* The HotSpot implementation of com.sun.tools.attach.VirtualMachine.
@@ -161,6 +161,50 @@ public abstract class HotSpotVirtualMachine extends VirtualMachine {
return props;
}
+ private static final String MANAGMENT_PREFIX = "com.sun.management.";
+
+ private static boolean checkedKeyName(Object key) {
+ if (!(key instanceof String)) {
+ throw new IllegalArgumentException("Invalid option (not a String): "+key);
+ }
+ if (!((String)key).startsWith(MANAGMENT_PREFIX)) {
+ throw new IllegalArgumentException("Invalid option: "+key);
+ }
+ return true;
+ }
+
+ private static String stripKeyName(Object key) {
+ return ((String)key).substring(MANAGMENT_PREFIX.length());
+ }
+
+ @Override
+ public void startManagementAgent(Properties agentProperties) throws IOException {
+ if (agentProperties == null) {
+ throw new NullPointerException("agentProperties cannot be null");
+ }
+ // Convert the arguments into arguments suitable for the Diagnostic Command:
+ // "ManagementAgent.start jmxremote.port=5555 jmxremote.authenticate=false"
+ String args = agentProperties.entrySet().stream()
+ .filter(entry -> checkedKeyName(entry.getKey()))
+ .map(entry -> stripKeyName(entry.getKey()) + "=" + escape(entry.getValue()))
+ .collect(Collectors.joining(" "));
+ executeJCmd("ManagementAgent.start " + args);
+ }
+
+ private String escape(Object arg) {
+ String value = arg.toString();
+ if (value.contains(" ")) {
+ return "'" + value + "'";
+ }
+ return value;
+ }
+
+ @Override
+ public String startLocalManagementAgent() throws IOException {
+ executeJCmd("ManagementAgent.start_local");
+ return getAgentProperties().getProperty("com.sun.management.jmxremote.localConnectorAddress");
+ }
+
// --- HotSpot specific methods ---
// same as SIGQUIT
diff --git a/src/share/classes/sun/tools/jconsole/LocalVirtualMachine.java b/src/share/classes/sun/tools/jconsole/LocalVirtualMachine.java
index 65b03e0fa..e6c827b70 100644
--- a/src/share/classes/sun/tools/jconsole/LocalVirtualMachine.java
+++ b/src/share/classes/sun/tools/jconsole/LocalVirtualMachine.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -32,8 +32,6 @@ import java.io.File;
// Sun specific
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;
-import com.sun.tools.attach.AgentInitializationException;
-import com.sun.tools.attach.AgentLoadException;
import com.sun.tools.attach.AttachNotSupportedException;
// Sun private
@@ -238,35 +236,7 @@ public class LocalVirtualMachine {
throw ioe;
}
- 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) {
- IOException ioe = new IOException(x.getMessage());
- ioe.initCause(x);
- throw ioe;
- } catch (AgentInitializationException x) {
- IOException ioe = new IOException(x.getMessage());
- ioe.initCause(x);
- throw ioe;
- }
+ vm.startLocalManagementAgent();
// get the connector address
Properties agentProps = vm.getAgentProperties();
diff --git a/test/com/sun/tools/attach/SimpleProvider.java b/test/com/sun/tools/attach/SimpleProvider.java
index 2a380f562..54f3dadf0 100644
--- a/test/com/sun/tools/attach/SimpleProvider.java
+++ b/test/com/sun/tools/attach/SimpleProvider.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -100,4 +100,12 @@ class SimpleVirtualMachine extends VirtualMachine {
public void dataDumpRequest() throws IOException {
}
+
+ public String startLocalManagementAgent() {
+ return null;
+ }
+
+ public void startManagementAgent(Properties agentProperties) {
+ }
+
}
diff --git a/test/com/sun/tools/attach/StartManagementAgent.java b/test/com/sun/tools/attach/StartManagementAgent.java
new file mode 100644
index 000000000..16e701ede
--- /dev/null
+++ b/test/com/sun/tools/attach/StartManagementAgent.java
@@ -0,0 +1,211 @@
+/*
+ * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import com.sun.tools.attach.AttachOperationFailedException;
+import com.sun.tools.attach.VirtualMachine;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.util.Properties;
+import java.util.HashMap;
+
+import javax.management.remote.JMXServiceURL;
+import javax.management.remote.JMXConnector;
+import javax.management.remote.JMXConnectorFactory;
+
+import jdk.testlibrary.ProcessThread;
+import jdk.testlibrary.Utils;
+
+/*
+ * @test
+ * @summary Test for VirtualMachine.startManagementAgent and VirtualMachine.startLocalManagementAgent
+ * @library /lib/testlibrary
+ * @run build Application Shutdown SimpleProvider jdk.testlibrary.*
+ * @run main StartManagementAgent
+ */
+
+/*
+ * This test is not meant to test all possible configuration parameters to
+ * the JMX agent, there are other tests for that. This test makes sure it is
+ * possible to start the agent via attach.
+ */
+public class StartManagementAgent {
+ public static void main(String[] args) throws Throwable {
+ final String pidFile = "StartManagementAgent.Application.pid";
+ ProcessThread processThread = null;
+ RunnerUtil.ProcessInfo info = null;
+ try {
+ processThread = RunnerUtil.startApplication(pidFile);
+ info = RunnerUtil.readProcessInfo(pidFile);
+ runTests(info.pid);
+ } catch (Throwable t) {
+ System.out.println("StartManagementAgent got unexpected exception: " + t);
+ t.printStackTrace();
+ throw t;
+ } finally {
+ // Make sure the Application process is stopped.
+ RunnerUtil.stopApplication(info.shutdownPort, processThread);
+ }
+ }
+
+ private static void basicTests(VirtualMachine vm) throws Exception {
+
+ // Try calling with null argument
+ boolean exception = false;
+ try {
+ vm.startManagementAgent(null);
+ } catch (NullPointerException e) {
+ exception = true;
+ }
+ if (!exception) {
+ throw new Exception("startManagementAgent(null) should throw NPE");
+ }
+
+ // Try calling with a property value with a space in it
+ Properties p = new Properties();
+ File f = new File("file with space");
+ try (FileWriter fw = new FileWriter(f)) {
+ fw.write("com.sun.management.jmxremote.port=apa");
+ }
+ p.put("com.sun.management.config.file", f.getAbsolutePath());
+ try {
+ vm.startManagementAgent(p);
+ } catch(AttachOperationFailedException ex) {
+ // We expect parsing of "apa" above to fail, but if the file path
+ // can't be read we get a different exception message
+ if (!ex.getMessage().contains("java.lang.NumberFormatException")) {
+ throw ex;
+ }
+ }
+ }
+
+ private static final String LOCAL_CONNECTOR_ADDRESS_PROP =
+ "com.sun.management.jmxremote.localConnectorAddress";
+
+ private static final int MAX_RETRIES = 10;
+
+ public static void runTests(int pid) throws Exception {
+ VirtualMachine vm = VirtualMachine.attach(""+pid);
+ try {
+
+ basicTests(vm);
+
+ testLocalAgent(vm);
+
+ // we retry the remote case several times in case the error
+ // was caused by a port conflict
+ int i = 0;
+ boolean success = false;
+ do {
+ try {
+ System.err.println("Trying remote agent. Try #" + i);
+ testRemoteAgent(vm);
+ success = true;
+ } catch(Exception ex) {
+ System.err.println("testRemoteAgent failed with exception:");
+ ex.printStackTrace();
+ System.err.println("Retrying.");
+ }
+ i++;
+ } while(!success && i < MAX_RETRIES);
+ if (!success) {
+ throw new Exception("testRemoteAgent failed after " + MAX_RETRIES + " tries");
+ }
+ } finally {
+ vm.detach();
+ }
+ }
+
+ public static void testLocalAgent(VirtualMachine vm) throws Exception {
+ Properties agentProps = vm.getAgentProperties();
+ String address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
+ if (address != null) {
+ throw new Exception("Local management agent already started");
+ }
+
+ String result = vm.startLocalManagementAgent();
+
+ // try to parse the return value as a JMXServiceURL
+ new JMXServiceURL(result);
+
+ agentProps = vm.getAgentProperties();
+ address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
+ if (address == null) {
+ throw new Exception("Local management agent could not be started");
+ }
+ }
+
+ public static void testRemoteAgent(VirtualMachine vm) throws Exception {
+ int port = Utils.getFreePort();
+
+ // try to connect - should fail
+ tryConnect(port, false);
+
+ // start agent
+ System.out.println("Starting agent on port: " + port);
+ Properties mgmtProps = new Properties();
+ mgmtProps.put("com.sun.management.jmxremote.port", port);
+ mgmtProps.put("com.sun.management.jmxremote.authenticate", "false");
+ mgmtProps.put("com.sun.management.jmxremote.ssl", "false");
+ vm.startManagementAgent(mgmtProps);
+
+ // try to connect - should work
+ tryConnect(port, true);
+
+ // try to start again - should fail
+ boolean exception = false;
+ try {
+ vm.startManagementAgent(mgmtProps);
+ } catch(AttachOperationFailedException ex) {
+ // expected
+ exception = true;
+ }
+ if (!exception) {
+ throw new Exception("Expected the second call to vm.startManagementAgent() to fail");
+ }
+ }
+
+ private static void tryConnect(int port, boolean shouldSucceed) throws Exception {
+ String jmxUrlStr =
+ String.format(
+ "service:jmx:rmi:///jndi/rmi://localhost:%d/jmxrmi",
+ port);
+ JMXServiceURL url = new JMXServiceURL(jmxUrlStr);
+ HashMap