提交 73b1d35d 编写于 作者: S sla

8046778: Better error messages when starting JMX agent via attach or jcmd

Reviewed-by: dholmes
上级 c647758c
...@@ -34,7 +34,6 @@ import java.lang.management.ManagementFactory; ...@@ -34,7 +34,6 @@ import java.lang.management.ManagementFactory;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.MissingResourceException; import java.util.MissingResourceException;
...@@ -88,7 +87,7 @@ public class Agent { ...@@ -88,7 +87,7 @@ public class Agent {
// return empty property set // return empty property set
private static Properties parseString(String args) { private static Properties parseString(String args) {
Properties argProps = new Properties(); Properties argProps = new Properties();
if (args != null) { if (args != null && !args.trim().equals("")) {
for (String option : args.split(",")) { for (String option : args.split(",")) {
String s[] = option.split("=", 2); String s[] = option.split("=", 2);
String name = s[0].trim(); String name = s[0].trim();
...@@ -160,53 +159,59 @@ public class Agent { ...@@ -160,53 +159,59 @@ public class Agent {
throw new RuntimeException(getText(INVALID_STATE, "Agent already started")); throw new RuntimeException(getText(INVALID_STATE, "Agent already started"));
} }
Properties argProps = parseString(args); try {
Properties configProps = new Properties(); Properties argProps = parseString(args);
Properties configProps = new Properties();
// Load the management properties from the config file // Load the management properties from the config file
// if config file is not specified readConfiguration implicitly // if config file is not specified readConfiguration implicitly
// reads <java.home>/lib/management/management.properties // reads <java.home>/lib/management/management.properties
String fname = System.getProperty(CONFIG_FILE); String fname = System.getProperty(CONFIG_FILE);
readConfiguration(fname, configProps); readConfiguration(fname, configProps);
// management properties can be overridden by system properties // management properties can be overridden by system properties
// which take precedence // which take precedence
Properties sysProps = System.getProperties(); Properties sysProps = System.getProperties();
synchronized (sysProps) { synchronized (sysProps) {
configProps.putAll(sysProps); configProps.putAll(sysProps);
} }
// if user specifies config file into command line for either // if user specifies config file into command line for either
// jcmd utilities or attach command it overrides properties set in // jcmd utilities or attach command it overrides properties set in
// command line at the time of VM start // command line at the time of VM start
String fnameUser = argProps.getProperty(CONFIG_FILE); String fnameUser = argProps.getProperty(CONFIG_FILE);
if (fnameUser != null) { if (fnameUser != null) {
readConfiguration(fnameUser, configProps); readConfiguration(fnameUser, configProps);
} }
// arguments specified in command line of jcmd utilities // arguments specified in command line of jcmd utilities
// override both system properties and one set by config file // override both system properties and one set by config file
// specified in jcmd command line // specified in jcmd command line
configProps.putAll(argProps); configProps.putAll(argProps);
// jcmd doesn't allow to change ThreadContentionMonitoring, but user // jcmd doesn't allow to change ThreadContentionMonitoring, but user
// can specify this property inside config file, so enable optional // can specify this property inside config file, so enable optional
// monitoring functionality if this property is set // monitoring functionality if this property is set
final String enableThreadContentionMonitoring = final String enableThreadContentionMonitoring =
configProps.getProperty(ENABLE_THREAD_CONTENTION_MONITORING); configProps.getProperty(ENABLE_THREAD_CONTENTION_MONITORING);
if (enableThreadContentionMonitoring != null) { if (enableThreadContentionMonitoring != null) {
ManagementFactory.getThreadMXBean(). ManagementFactory.getThreadMXBean().
setThreadContentionMonitoringEnabled(true); setThreadContentionMonitoringEnabled(true);
} }
String jmxremotePort = configProps.getProperty(JMXREMOTE_PORT); String jmxremotePort = configProps.getProperty(JMXREMOTE_PORT);
if (jmxremotePort != null) { if (jmxremotePort != null) {
jmxServer = ConnectorBootstrap. jmxServer = ConnectorBootstrap.
startRemoteConnectorServer(jmxremotePort, configProps); startRemoteConnectorServer(jmxremotePort, configProps);
startDiscoveryService(configProps); startDiscoveryService(configProps);
} else {
throw new AgentConfigurationError(INVALID_JMXREMOTE_PORT, "No port specified");
}
} catch (AgentConfigurationError err) {
error(err.getError(), err.getParams());
} }
} }
...@@ -507,7 +512,7 @@ public class Agent { ...@@ -507,7 +512,7 @@ public class Agent {
String keyText = getText(key); String keyText = getText(key);
System.err.print(getText("agent.err.error") + ": " + keyText); System.err.print(getText("agent.err.error") + ": " + keyText);
System.err.println(": " + message); System.err.println(": " + message);
throw new RuntimeException(keyText); throw new RuntimeException(keyText + ": " + message);
} }
public static void error(Exception e) { public static void error(Exception e) {
......
...@@ -43,7 +43,7 @@ agent.err.agentclass.failed = Management agent class failed ...@@ -43,7 +43,7 @@ agent.err.agentclass.failed = Management agent class failed
agent.err.premain.notfound = premain(String) does not exist in agent class agent.err.premain.notfound = premain(String) does not exist in agent class
agent.err.agentclass.access.denied = Access to premain(String) is denied agent.err.agentclass.access.denied = Access to premain(String) is denied
agent.err.invalid.agentclass = Invalid com.sun.management.agent.class property value agent.err.invalid.agentclass = Invalid com.sun.management.agent.class property value
agent.err.invalid.state = Invalid agent state agent.err.invalid.state = Invalid agent state: {0}
agent.err.invalid.jmxremote.port = Invalid com.sun.management.jmxremote.port number agent.err.invalid.jmxremote.port = Invalid com.sun.management.jmxremote.port number
agent.err.invalid.jmxremote.rmi.port = Invalid com.sun.management.jmxremote.rmi.port number agent.err.invalid.jmxremote.rmi.port = Invalid com.sun.management.jmxremote.rmi.port number
......
...@@ -93,7 +93,7 @@ public class StartManagementAgent { ...@@ -93,7 +93,7 @@ public class StartManagementAgent {
} catch(AttachOperationFailedException ex) { } catch(AttachOperationFailedException ex) {
// We expect parsing of "apa" above to fail, but if the file path // We expect parsing of "apa" above to fail, but if the file path
// can't be read we get a different exception message // can't be read we get a different exception message
if (!ex.getMessage().contains("java.lang.NumberFormatException")) { if (!ex.getMessage().contains("Invalid com.sun.management.jmxremote.port number")) {
throw ex; throw ex;
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册