--- old/src/hotspot/share/runtime/arguments.cpp 2018-01-24 16:40:45.395085323 +0530 +++ new/src/hotspot/share/runtime/arguments.cpp 2018-01-24 16:40:45.191084034 +0530 @@ -3211,6 +3211,20 @@ "ManagementServer is not supported in this VM.\n"); return JNI_ERR; #endif // INCLUDE_MANAGEMENT + } else if (match_option(option, "-Xmanagement", &tail)) { +#if INCLUDE_MANAGEMENT + if (FLAG_SET_CMDLINE(bool, ManagementServer, true) != Flag::SUCCESS) { + return JNI_EINVAL; + } + // management agent in module jdk.management.agent + if (!create_numbered_property("jdk.module.addmods", "jdk.management.agent", addmods_count++)) { + return JNI_ENOMEM; + } +#else + jio_fprintf(defaultStream::output_stream(), + "-Xmanagement is not supported in this VM.\n"); + return JNI_ERR; +#endif } else if (match_option(option, "-XX:", &tail)) { // -XX:xxxx // Skip -XX:Flags= and -XX:VMOptionsFile= since those cases have // already been handled --- old/src/jdk.management.agent/share/classes/jdk/internal/agent/Agent.java 2018-01-24 16:40:46.011089218 +0530 +++ new/src/jdk.management.agent/share/classes/jdk/internal/agent/Agent.java 2018-01-24 16:40:45.807087928 +0530 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2018, 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 @@ -38,12 +38,17 @@ import java.security.AccessController; import java.security.PrivilegedAction; import java.text.MessageFormat; +import java.util.Collections; import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.MissingResourceException; +import java.util.Optional; import java.util.Properties; import java.util.ResourceBundle; import java.util.ServiceLoader; +import java.util.Set; import java.util.function.Function; import java.util.function.Predicate; @@ -53,6 +58,8 @@ import static jdk.internal.agent.AgentConfigurationError.*; import jdk.internal.agent.spi.AgentProvider; import jdk.internal.vm.VMSupport; +import sun.management.ManagementFactoryHelper; +import sun.management.VMManagement; import sun.management.jdp.JdpController; import sun.management.jdp.JdpException; import sun.management.jmxremote.ConnectorBootstrap; @@ -262,10 +269,28 @@ // The properties used to configure the server private static Properties configProps = null; + private static final Map XMANAGEMENTFLAS = Collections.unmodifiableMap(new LinkedHashMap<>() {{ + put("config_file", "com.sun.management.config.file"); + put("local", "com.sun.management.jmxremote"); + put("port", "com.sun.management.jmxremote.port"); + put("host", "com.sun.management.jmxremote.host"); + put("rmiserver_port", "com.sun.management.jmxremote.rmi.port"); + put("rmi_registry_ssl", "com.sun.management.jmxremote.registry.ssl"); + put("ssl", "com.sun.management.jmxremote.ssl"); + put("ssl_config_file", "com.sun.management.jmxremote.ssl.config.file"); + put("ssl_client_auth", "com.sun.management.jmxremote.ssl.need.client.auth"); + put("password_file", "com.sun.management.jmxremote.password.file"); + put("authenticate", "com.sun.management.jmxremote.authenticate"); + put("access_file", "com.sun.management.jmxremote.access.file"); + }}); + // Parse string com.sun.management.prop=xxx,com.sun.management.prop=yyyy // and return property set if args is null or empty // return empty property set private static Properties parseString(String args) { + if(args.startsWith("-Xmanagement")) { + return parseXmgmtArgs(args); + } Properties argProps = new Properties(); if (args != null && !args.trim().equals("")) { for (String option : args.split(",")) { @@ -609,12 +634,48 @@ } } + private static Properties parseXmgmtArgs(String args) { + Properties props = new Properties(); + if(args != null && !args.trim().isEmpty()) { + args = args.trim(); + if (args.equals("-Xmanagement") || args.equals("-Xmanagement:")) { + props.setProperty("com.sun.management.jmxremote", "true"); + return props; + } + + args = args.replaceFirst("^-Xmanagement:", ""); + Set keys = XMANAGEMENTFLAS.keySet(); + for (String param : args.split(",")) { + String[] tokens = param.trim().split("=",2); + String name = tokens[0].trim(); + if(keys.contains(name)) { + name = XMANAGEMENTFLAS.get(name); + String value = (tokens.length > 1) ? tokens[1].trim() : ""; + props.setProperty(name, value); + } else { + error(INVALID_OPTION, name); + } + } + } + return props; + } + + public static void startAgent() throws Exception { String prop = System.getProperty("com.sun.management.agent.class"); // -Dcom.sun.management.agent.class not set so read management // properties and start agent if (prop == null) { + VMManagement vmManagement = ManagementFactoryHelper.getVMManagement(); + if (vmManagement != null) { + List vmArguments = vmManagement.getVmArguments(); + Optional argStr = vmArguments.stream().filter(a -> a.startsWith("-Xmanagement")).findFirst(); + if (argStr.isPresent()) { + Properties props = parseXmgmtArgs(argStr.get()); + props.forEach((a,b) -> System.setProperty((String) a,(String) b)); + } + } // initialize management properties Properties props = getManagementProperties(); if (props != null) { --- /dev/null 2018-01-24 16:13:40.816006525 +0530 +++ new/test/jdk/sun/management/jmxremote/XmanagementAgentTest.java 2018-01-24 16:40:46.251090743 +0530 @@ -0,0 +1,107 @@ +//package jdk.sun.management.jmxremote; + +/* + * Copyright (c) 2018, 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 java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import jdk.test.lib.process.ProcessTools; + +/** + * @test + * @bug 8187498 + * @summary Unit test for -Xmanagement Flags for JMX agent + * @library /test/lib + * @run main XmanagementAgentTest + */ +public class XmanagementAgentTest { + + static final Map fragments = new HashMap<>(); + + static { + fragments.put("config_file", System.getProperty("test.src") + "/bootstrap/management_ssltest06_ok.properties.in"); + fragments.put("port", "2345"); + fragments.put("local", "false"); + fragments.put("host", "localhost"); + fragments.put("rmiserver_port", "4456"); + fragments.put("ssl", "true"); + fragments.put("rmi_registry_ssl", "false"); +// fragments.put("ssl_config_file", System.getProperty("test.src") + "/bootstrap/jmxremote_ssltest12_ok.ssl.in"); + fragments.put("ssl_client_auth", "true"); + fragments.put("authenticate", "false"); + fragments.put("password_file", System.getProperty("test.src") + "/bootstrap/jmxremote_ssltest06_ok.password.in"); + fragments.put("access_file", System.getProperty("test.src") + "/bootstrap/jmxremote_ssltest10_ok.access.in"); + } + + public static void main(String[] args) throws Exception { + List keys = new ArrayList<>(fragments.keySet()); + Collections.shuffle(keys); + StringBuilder stringBuffer = new StringBuilder(); + stringBuffer.append("-Xmanagement:"); + + keys.forEach(k -> stringBuffer.append(k).append("=").append(fragments.get(k)).append(",")); + String commandLine = stringBuffer.toString().replaceAll(",$", ""); + + List pbArgs = new ArrayList<>(); + pbArgs.add(commandLine); + pbArgs.add(XmgmtVerfifier.class.getName()); + + int exitValue = ProcessTools.executeTestJava(pbArgs.toArray(new String[0])) + .outputTo(System.out) + .errorTo(System.err) + .getExitValue(); + if (exitValue != 0) { + throw new RuntimeException("Test Failed!!"); + } + } +} + +class XmgmtVerfifier { + + private static final Map XMANAGEMENTFLAS = Collections.unmodifiableMap(new LinkedHashMap<>() { + { + put("config_file", "com.sun.management.config.file"); + put("local", "com.sun.management.jmxremote"); + put("port", "com.sun.management.jmxremote.port"); + put("host", "com.sun.management.jmxremote.host"); + put("rmiserver_port", "com.sun.management.jmxremote.rmi.port"); + put("rmi_registry_ssl", "com.sun.management.jmxremote.registry.ssl"); + put("ssl", "com.sun.management.jmxremote.ssl"); + put("ssl_config_file", "com.sun.management.jmxremote.ssl.config.file"); + put("ssl_client_auth", "com.sun.management.jmxremote.ssl.need.client.auth"); + put("password_file", "com.sun.management.jmxremote.password.file"); + put("authenticate", "com.sun.management.jmxremote.authenticate"); + put("access_file", "com.sun.management.jmxremote.access.file"); + } + }); + + public static void main(String[] args) { + XMANAGEMENTFLAS.values().forEach(a -> System.out.println(a + " -> " + System.getProperty(a))); + } +} +