diff a/jdk/src/share/classes/sun/management/AgentConfigurationError.java b/jdk/src/share/classes/sun/management/AgentConfigurationError.java --- a/jdk/src/share/classes/sun/management/AgentConfigurationError.java +++ b/jdk/src/share/classes/sun/management/AgentConfigurationError.java @@ -1,7 +1,7 @@ /* - * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2004, 2020, 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. Oracle designates this @@ -53,10 +53,12 @@ "agent.err.invalid.agentclass"; public static final String INVALID_JMXREMOTE_PORT = "agent.err.invalid.jmxremote.port"; public static final String INVALID_JMXREMOTE_RMI_PORT = "agent.err.invalid.jmxremote.rmi.port"; + public static final String INVALID_JMXREMOTE_LOCAL_PORT = + "agent.err.invalid.jmxremote.local.port"; public static final String PASSWORD_FILE_NOT_SET = "agent.err.password.file.notset"; public static final String PASSWORD_FILE_NOT_READABLE = "agent.err.password.file.not.readable"; public static final String PASSWORD_FILE_READ_FAILED = diff a/jdk/src/share/classes/sun/management/jmxremote/ConnectorBootstrap.java b/jdk/src/share/classes/sun/management/jmxremote/ConnectorBootstrap.java --- a/jdk/src/share/classes/sun/management/jmxremote/ConnectorBootstrap.java +++ b/jdk/src/share/classes/sun/management/jmxremote/ConnectorBootstrap.java @@ -1,7 +1,7 @@ /* - * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2020, 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. Oracle designates this @@ -115,10 +115,12 @@ "com.sun.management.jmxremote.port"; public static final String HOST = "com.sun.management.jmxremote.host"; public static final String RMI_PORT = "com.sun.management.jmxremote.rmi.port"; + public static final String LOCAL_PORT = + "com.sun.management.jmxremote.local.port"; public static final String CONFIG_FILE_NAME = "com.sun.management.config.file"; public static final String USE_LOCAL_ONLY = "com.sun.management.jmxremote.local.only"; public static final String USE_SSL = @@ -529,17 +531,39 @@ if (lh == null || !lh.isLoopbackAddress()) { localhost = "127.0.0.1"; } MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); + + Properties props = null; try { - JMXServiceURL url = new JMXServiceURL("rmi", localhost, 0); - // Do we accept connections from local interfaces only? - Properties props = Agent.getManagementProperties(); + props = Agent.getManagementProperties(); if (props == null) { props = new Properties(); } + } catch (Exception e) { + throw new AgentConfigurationError(AGENT_EXCEPTION, e, e.toString()); + } + + // User can specify a port to be used to start local connector server. + // Random one will be allocated if port is not specified. + int localPort = 0; + String localPortStr = props.getProperty(PropertyNames.LOCAL_PORT); + try { + if (localPortStr != null) { + localPort = Integer.parseInt(localPortStr); + } + } catch (NumberFormatException x) { + throw new AgentConfigurationError(INVALID_JMXREMOTE_LOCAL_PORT, x, localPortStr); + } + if (localPort < 0) { + throw new AgentConfigurationError(INVALID_JMXREMOTE_LOCAL_PORT, localPortStr); + } + + try { + JMXServiceURL url = new JMXServiceURL("rmi", localhost, localPort); + // Do we accept connections from local interfaces only? String useLocalOnlyStr = props.getProperty( PropertyNames.USE_LOCAL_ONLY, DefaultValues.USE_LOCAL_ONLY); boolean useLocalOnly = Boolean.valueOf(useLocalOnlyStr).booleanValue(); if (useLocalOnly) { env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE, diff a/jdk/src/share/lib/management/management.properties b/jdk/src/share/lib/management/management.properties --- a/jdk/src/share/lib/management/management.properties +++ b/jdk/src/share/lib/management/management.properties @@ -24,10 +24,12 @@ # ################ Management Agent Port ######################### # # For setting the JMX RMI agent port use the following line # com.sun.management.jmxremote.port= # +# For setting the JMX local server port use the following line +# com.sun.management.jmxremote.local.port= # For setting the SNMP agent port use the following line # com.sun.management.snmp.port= ##################################################################### # Optional Instrumentation