author | sla |
Wed, 18 Jun 2014 09:04:20 +0200 | |
changeset 24980 | dbf4ae2cafa4 |
parent 24969 | afa6934dd8e8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
21298
diff
changeset
|
2 |
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
package sun.management; |
|
26 |
||
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
27 |
import java.io.BufferedInputStream; |
2 | 28 |
import java.io.File; |
29 |
import java.io.FileInputStream; |
|
30 |
import java.io.FileNotFoundException; |
|
31 |
import java.io.IOException; |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
32 |
import java.io.InputStream; |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
33 |
import java.lang.management.ManagementFactory; |
2285
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
34 |
import java.lang.reflect.InvocationTargetException; |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
35 |
import java.lang.reflect.Method; |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
36 |
import java.net.InetAddress; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
37 |
import java.net.UnknownHostException; |
2 | 38 |
import java.text.MessageFormat; |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
39 |
import java.util.MissingResourceException; |
2 | 40 |
import java.util.Properties; |
41 |
import java.util.ResourceBundle; |
|
42 |
||
43 |
import javax.management.remote.JMXConnectorServer; |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
44 |
import javax.management.remote.JMXServiceURL; |
2 | 45 |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
46 |
import static sun.management.AgentConfigurationError.*; |
2 | 47 |
import sun.management.jmxremote.ConnectorBootstrap; |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
48 |
import sun.management.jdp.JdpController; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
49 |
import sun.management.jdp.JdpException; |
2 | 50 |
import sun.misc.VMSupport; |
51 |
||
52 |
/** |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
53 |
* This Agent is started by the VM when -Dcom.sun.management.snmp or |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
54 |
* -Dcom.sun.management.jmxremote is set. This class will be loaded by the |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
55 |
* system class loader. Also jmx framework could be started by jcmd |
2 | 56 |
*/ |
57 |
public class Agent { |
|
58 |
// management properties |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
59 |
|
2 | 60 |
private static Properties mgmtProps; |
61 |
private static ResourceBundle messageRB; |
|
62 |
private static final String CONFIG_FILE = |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
63 |
"com.sun.management.config.file"; |
2 | 64 |
private static final String SNMP_PORT = |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
65 |
"com.sun.management.snmp.port"; |
2 | 66 |
private static final String JMXREMOTE = |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
67 |
"com.sun.management.jmxremote"; |
2 | 68 |
private static final String JMXREMOTE_PORT = |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
69 |
"com.sun.management.jmxremote.port"; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
70 |
private static final String RMI_PORT = |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
71 |
"com.sun.management.jmxremote.rmi.port"; |
2 | 72 |
private static final String ENABLE_THREAD_CONTENTION_MONITORING = |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
73 |
"com.sun.management.enableThreadContentionMonitoring"; |
2 | 74 |
private static final String LOCAL_CONNECTOR_ADDRESS_PROP = |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
75 |
"com.sun.management.jmxremote.localConnectorAddress"; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
76 |
private static final String SNMP_ADAPTOR_BOOTSTRAP_CLASS_NAME = |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
77 |
"sun.management.snmp.AdaptorBootstrap"; |
2 | 78 |
|
17721
1d6516794d05
8014420: Default JDP address does not match the one assigned by IANA
dsamersoff
parents:
15531
diff
changeset
|
79 |
private static final String JDP_DEFAULT_ADDRESS = "224.0.23.178"; |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
80 |
private static final int JDP_DEFAULT_PORT = 7095; |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
81 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
82 |
// The only active agent allowed |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
83 |
private static JMXConnectorServer jmxServer = null; |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
84 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
85 |
// Parse string com.sun.management.prop=xxx,com.sun.management.prop=yyyy |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
86 |
// and return property set if args is null or empty |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
87 |
// return empty property set |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
88 |
private static Properties parseString(String args) { |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
89 |
Properties argProps = new Properties(); |
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
90 |
if (args != null && !args.trim().equals("")) { |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
91 |
for (String option : args.split(",")) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
92 |
String s[] = option.split("=", 2); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
93 |
String name = s[0].trim(); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
94 |
String value = (s.length > 1) ? s[1].trim() : ""; |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
95 |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
96 |
if (!name.startsWith("com.sun.management.")) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
97 |
error(INVALID_OPTION, name); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
98 |
} |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
99 |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
100 |
argProps.setProperty(name, value); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
101 |
} |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
102 |
} |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
103 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
104 |
return argProps; |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
105 |
} |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
106 |
|
2 | 107 |
// invoked by -javaagent or -Dcom.sun.management.agent.class |
108 |
public static void premain(String args) throws Exception { |
|
109 |
agentmain(args); |
|
110 |
} |
|
111 |
||
112 |
// invoked by attach mechanism |
|
113 |
public static void agentmain(String args) throws Exception { |
|
114 |
if (args == null || args.length() == 0) { |
|
115 |
args = JMXREMOTE; // default to local management |
|
116 |
} |
|
117 |
||
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
118 |
Properties arg_props = parseString(args); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
119 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
120 |
// Read properties from the config file |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
121 |
Properties config_props = new Properties(); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
122 |
String fname = arg_props.getProperty(CONFIG_FILE); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
123 |
readConfiguration(fname, config_props); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
124 |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
125 |
// Arguments override config file |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
126 |
config_props.putAll(arg_props); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
127 |
startAgent(config_props); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
128 |
} |
2 | 129 |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
130 |
// jcmd ManagementAgent.start_local entry point |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
131 |
// Also called due to command-line via startAgent() |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
132 |
private static synchronized void startLocalManagementAgent() { |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
133 |
Properties agentProps = VMSupport.getAgentProperties(); |
2 | 134 |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
135 |
// start local connector if not started |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
136 |
if (agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP) == null) { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
137 |
JMXConnectorServer cs = ConnectorBootstrap.startLocalConnectorServer(); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
138 |
String address = cs.getAddress().toString(); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
139 |
// Add the local connector address to the agent properties |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
140 |
agentProps.put(LOCAL_CONNECTOR_ADDRESS_PROP, address); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
141 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
142 |
try { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
143 |
// export the address to the instrumentation buffer |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
144 |
ConnectorAddressLink.export(address); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
145 |
} catch (Exception x) { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
146 |
// Connector server started but unable to export address |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
147 |
// to instrumentation buffer - non-fatal error. |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
148 |
warning(EXPORT_ADDRESS_FAILED, x.getMessage()); |
2 | 149 |
} |
150 |
} |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
151 |
} |
2 | 152 |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
153 |
// jcmd ManagementAgent.start entry point |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
154 |
// This method starts the remote JMX agent and starts neither |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
155 |
// the local JMX agent nor the SNMP agent |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
156 |
// @see #startLocalManagementAgent and also @see #startAgent. |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
157 |
private static synchronized void startRemoteManagementAgent(String args) throws Exception { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
158 |
if (jmxServer != null) { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
159 |
throw new RuntimeException(getText(INVALID_STATE, "Agent already started")); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
160 |
} |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
161 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
162 |
try { |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
163 |
Properties argProps = parseString(args); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
164 |
Properties configProps = new Properties(); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
165 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
166 |
// Load the management properties from the config file |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
167 |
// if config file is not specified readConfiguration implicitly |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
168 |
// reads <java.home>/lib/management/management.properties |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
169 |
|
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
170 |
String fname = System.getProperty(CONFIG_FILE); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
171 |
readConfiguration(fname, configProps); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
172 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
173 |
// management properties can be overridden by system properties |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
174 |
// which take precedence |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
175 |
Properties sysProps = System.getProperties(); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
176 |
synchronized (sysProps) { |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
177 |
configProps.putAll(sysProps); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
178 |
} |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
179 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
180 |
// if user specifies config file into command line for either |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
181 |
// jcmd utilities or attach command it overrides properties set in |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
182 |
// command line at the time of VM start |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
183 |
String fnameUser = argProps.getProperty(CONFIG_FILE); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
184 |
if (fnameUser != null) { |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
185 |
readConfiguration(fnameUser, configProps); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
186 |
} |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
187 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
188 |
// arguments specified in command line of jcmd utilities |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
189 |
// override both system properties and one set by config file |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
190 |
// specified in jcmd command line |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
191 |
configProps.putAll(argProps); |
2 | 192 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
193 |
// jcmd doesn't allow to change ThreadContentionMonitoring, but user |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
194 |
// can specify this property inside config file, so enable optional |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
195 |
// monitoring functionality if this property is set |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
196 |
final String enableThreadContentionMonitoring = |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
197 |
configProps.getProperty(ENABLE_THREAD_CONTENTION_MONITORING); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
198 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
199 |
if (enableThreadContentionMonitoring != null) { |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
200 |
ManagementFactory.getThreadMXBean(). |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
201 |
setThreadContentionMonitoringEnabled(true); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
202 |
} |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
203 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
204 |
String jmxremotePort = configProps.getProperty(JMXREMOTE_PORT); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
205 |
if (jmxremotePort != null) { |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
206 |
jmxServer = ConnectorBootstrap. |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
207 |
startRemoteConnectorServer(jmxremotePort, configProps); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
208 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
209 |
startDiscoveryService(configProps); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
210 |
} else { |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
211 |
throw new AgentConfigurationError(INVALID_JMXREMOTE_PORT, "No port specified"); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
212 |
} |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
213 |
} catch (AgentConfigurationError err) { |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
214 |
error(err.getError(), err.getParams()); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
215 |
} |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
216 |
} |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
217 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
218 |
private static synchronized void stopRemoteManagementAgent() throws Exception { |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
219 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
220 |
JdpController.stopDiscoveryService(); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
221 |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
222 |
if (jmxServer != null) { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
223 |
ConnectorBootstrap.unexportRegistry(); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
224 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
225 |
// Attempt to stop already stopped agent |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
226 |
// Don't cause any errors. |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
227 |
jmxServer.stop(); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
228 |
jmxServer = null; |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
229 |
} |
2 | 230 |
} |
231 |
||
232 |
private static void startAgent(Properties props) throws Exception { |
|
233 |
String snmpPort = props.getProperty(SNMP_PORT); |
|
234 |
String jmxremote = props.getProperty(JMXREMOTE); |
|
235 |
String jmxremotePort = props.getProperty(JMXREMOTE_PORT); |
|
236 |
||
237 |
// Enable optional monitoring functionality if requested |
|
238 |
final String enableThreadContentionMonitoring = |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
239 |
props.getProperty(ENABLE_THREAD_CONTENTION_MONITORING); |
2 | 240 |
if (enableThreadContentionMonitoring != null) { |
241 |
ManagementFactory.getThreadMXBean(). |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
242 |
setThreadContentionMonitoringEnabled(true); |
2 | 243 |
} |
244 |
||
245 |
try { |
|
246 |
if (snmpPort != null) { |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
247 |
loadSnmpAgent(snmpPort, props); |
2 | 248 |
} |
249 |
||
250 |
/* |
|
251 |
* If the jmxremote.port property is set then we start the |
|
252 |
* RMIConnectorServer for remote M&M. |
|
253 |
* |
|
254 |
* If the jmxremote or jmxremote.port properties are set then |
|
255 |
* we start a RMIConnectorServer for local M&M. The address |
|
256 |
* of this "local" server is exported as a counter to the jstat |
|
257 |
* instrumentation buffer. |
|
258 |
*/ |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
259 |
if (jmxremote != null || jmxremotePort != null) { |
2 | 260 |
if (jmxremotePort != null) { |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
261 |
jmxServer = ConnectorBootstrap. |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
262 |
startRemoteConnectorServer(jmxremotePort, props); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
263 |
startDiscoveryService(props); |
2 | 264 |
} |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
265 |
startLocalManagementAgent(); |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
266 |
} |
2 | 267 |
|
268 |
} catch (AgentConfigurationError e) { |
|
269 |
error(e.getError(), e.getParams()); |
|
270 |
} catch (Exception e) { |
|
271 |
error(e); |
|
272 |
} |
|
273 |
} |
|
274 |
||
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
275 |
private static void startDiscoveryService(Properties props) |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
276 |
throws IOException { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
277 |
// Start discovery service if requested |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
278 |
String discoveryPort = props.getProperty("com.sun.management.jdp.port"); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
279 |
String discoveryAddress = props.getProperty("com.sun.management.jdp.address"); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
280 |
String discoveryShouldStart = props.getProperty("com.sun.management.jmxremote.autodiscovery"); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
281 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
282 |
// Decide whether we should start autodicovery service. |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
283 |
// To start autodiscovery following conditions should be met: |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
284 |
// autodiscovery==true OR (autodicovery==null AND jdp.port != NULL) |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
285 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
286 |
boolean shouldStart = false; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
287 |
if (discoveryShouldStart == null){ |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
288 |
shouldStart = (discoveryPort != null); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
289 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
290 |
else{ |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
291 |
try{ |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
292 |
shouldStart = Boolean.parseBoolean(discoveryShouldStart); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
293 |
} catch (NumberFormatException e) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
294 |
throw new AgentConfigurationError("Couldn't parse autodiscovery argument"); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
295 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
296 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
297 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
298 |
if (shouldStart) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
299 |
// port and address are required arguments and have no default values |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
300 |
InetAddress address; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
301 |
try { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
302 |
address = (discoveryAddress == null) ? |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
303 |
InetAddress.getByName(JDP_DEFAULT_ADDRESS) : InetAddress.getByName(discoveryAddress); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
304 |
} catch (UnknownHostException e) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
305 |
throw new AgentConfigurationError("Unable to broadcast to requested address", e); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
306 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
307 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
308 |
int port = JDP_DEFAULT_PORT; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
309 |
if (discoveryPort != null) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
310 |
try { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
311 |
port = Integer.parseInt(discoveryPort); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
312 |
} catch (NumberFormatException e) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
313 |
throw new AgentConfigurationError("Couldn't parse JDP port argument"); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
314 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
315 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
316 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
317 |
// Rebuilding service URL to broadcast it |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
318 |
String jmxremotePort = props.getProperty(JMXREMOTE_PORT); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
319 |
String rmiPort = props.getProperty(RMI_PORT); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
320 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
321 |
JMXServiceURL url = jmxServer.getAddress(); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
322 |
String hostname = url.getHost(); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
323 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
324 |
String jmxUrlStr = (rmiPort != null) |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
325 |
? String.format( |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
326 |
"service:jmx:rmi://%s:%s/jndi/rmi://%s:%s/jmxrmi", |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
327 |
hostname, rmiPort, hostname, jmxremotePort) |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
328 |
: String.format( |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
329 |
"service:jmx:rmi:///jndi/rmi://%s:%s/jmxrmi", hostname, jmxremotePort); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
330 |
|
21298
d8a11a962ee3
8024071: In ManagementAgent.start it should be possible to set the jdp.name parameter.
dsamersoff
parents:
17721
diff
changeset
|
331 |
String instanceName = props.getProperty("com.sun.management.jdp.name"); |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
332 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
333 |
try{ |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
334 |
JdpController.startDiscoveryService(address, port, instanceName, jmxUrlStr); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
335 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
336 |
catch(JdpException e){ |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
337 |
throw new AgentConfigurationError("Couldn't start JDP service", e); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
338 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
339 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
340 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
341 |
|
2 | 342 |
public static Properties loadManagementProperties() { |
343 |
Properties props = new Properties(); |
|
344 |
||
345 |
// Load the management properties from the config file |
|
346 |
||
347 |
String fname = System.getProperty(CONFIG_FILE); |
|
348 |
readConfiguration(fname, props); |
|
349 |
||
350 |
// management properties can be overridden by system properties |
|
351 |
// which take precedence |
|
12694
543c5e6c5ed0
7164191: properties.putAll API may fail with ConcurrentModifcationException on multi-thread scenario
dsamersoff
parents:
11991
diff
changeset
|
352 |
Properties sysProps = System.getProperties(); |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
353 |
synchronized (sysProps) { |
12694
543c5e6c5ed0
7164191: properties.putAll API may fail with ConcurrentModifcationException on multi-thread scenario
dsamersoff
parents:
11991
diff
changeset
|
354 |
props.putAll(sysProps); |
543c5e6c5ed0
7164191: properties.putAll API may fail with ConcurrentModifcationException on multi-thread scenario
dsamersoff
parents:
11991
diff
changeset
|
355 |
} |
2 | 356 |
|
357 |
return props; |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
358 |
} |
2 | 359 |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
360 |
public static synchronized Properties getManagementProperties() { |
2 | 361 |
if (mgmtProps == null) { |
362 |
String configFile = System.getProperty(CONFIG_FILE); |
|
363 |
String snmpPort = System.getProperty(SNMP_PORT); |
|
364 |
String jmxremote = System.getProperty(JMXREMOTE); |
|
365 |
String jmxremotePort = System.getProperty(JMXREMOTE_PORT); |
|
366 |
||
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
367 |
if (configFile == null && snmpPort == null |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
368 |
&& jmxremote == null && jmxremotePort == null) { |
2 | 369 |
// return if out-of-the-management option is not specified |
370 |
return null; |
|
371 |
} |
|
372 |
mgmtProps = loadManagementProperties(); |
|
373 |
} |
|
374 |
return mgmtProps; |
|
375 |
} |
|
376 |
||
2285
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
377 |
private static void loadSnmpAgent(String snmpPort, Properties props) { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
378 |
try { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
379 |
// invoke the following through reflection: |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
380 |
// AdaptorBootstrap.initialize(snmpPort, props); |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
381 |
final Class<?> adaptorClass = |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
382 |
Class.forName(SNMP_ADAPTOR_BOOTSTRAP_CLASS_NAME, true, null); |
2285
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
383 |
final Method initializeMethod = |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
384 |
adaptorClass.getMethod("initialize", |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
385 |
String.class, Properties.class); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
386 |
initializeMethod.invoke(null, snmpPort, props); |
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
387 |
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException x) { |
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
388 |
// snmp runtime doesn't exist - initialization fails |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
389 |
throw new UnsupportedOperationException("Unsupported management property: " + SNMP_PORT, x); |
2285
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
390 |
} catch (InvocationTargetException x) { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
391 |
final Throwable cause = x.getCause(); |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
392 |
if (cause instanceof RuntimeException) { |
2285
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
393 |
throw (RuntimeException) cause; |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
394 |
} else if (cause instanceof Error) { |
2285
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
395 |
throw (Error) cause; |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
396 |
} |
2285
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
397 |
// should not happen... |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
398 |
throw new UnsupportedOperationException("Unsupported management property: " + SNMP_PORT, cause); |
2285
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
399 |
} |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
400 |
} |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
401 |
|
2 | 402 |
// read config file and initialize the properties |
403 |
private static void readConfiguration(String fname, Properties p) { |
|
404 |
if (fname == null) { |
|
405 |
String home = System.getProperty("java.home"); |
|
406 |
if (home == null) { |
|
407 |
throw new Error("Can't find java.home ??"); |
|
408 |
} |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
409 |
StringBuilder defaultFileName = new StringBuilder(home); |
2 | 410 |
defaultFileName.append(File.separator).append("lib"); |
411 |
defaultFileName.append(File.separator).append("management"); |
|
412 |
defaultFileName.append(File.separator).append("management.properties"); |
|
413 |
// Set file name |
|
414 |
fname = defaultFileName.toString(); |
|
415 |
} |
|
416 |
final File configFile = new File(fname); |
|
417 |
if (!configFile.exists()) { |
|
418 |
error(CONFIG_FILE_NOT_FOUND, fname); |
|
419 |
} |
|
420 |
||
421 |
InputStream in = null; |
|
422 |
try { |
|
423 |
in = new FileInputStream(configFile); |
|
424 |
BufferedInputStream bin = new BufferedInputStream(in); |
|
425 |
p.load(bin); |
|
426 |
} catch (FileNotFoundException e) { |
|
427 |
error(CONFIG_FILE_OPEN_FAILED, e.getMessage()); |
|
428 |
} catch (IOException e) { |
|
429 |
error(CONFIG_FILE_OPEN_FAILED, e.getMessage()); |
|
430 |
} catch (SecurityException e) { |
|
431 |
error(CONFIG_FILE_ACCESS_DENIED, fname); |
|
432 |
} finally { |
|
433 |
if (in != null) { |
|
434 |
try { |
|
435 |
in.close(); |
|
436 |
} catch (IOException e) { |
|
437 |
error(CONFIG_FILE_CLOSE_FAILED, fname); |
|
438 |
} |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
439 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
440 |
} |
2 | 441 |
} |
442 |
||
443 |
public static void startAgent() throws Exception { |
|
444 |
String prop = System.getProperty("com.sun.management.agent.class"); |
|
445 |
||
446 |
// -Dcom.sun.management.agent.class not set so read management |
|
447 |
// properties and start agent |
|
448 |
if (prop == null) { |
|
449 |
// initialize management properties |
|
450 |
Properties props = getManagementProperties(); |
|
451 |
if (props != null) { |
|
452 |
startAgent(props); |
|
453 |
} |
|
454 |
return; |
|
455 |
} |
|
456 |
||
457 |
// -Dcom.sun.management.agent.class=<agent classname>:<agent args> |
|
458 |
String[] values = prop.split(":"); |
|
459 |
if (values.length < 1 || values.length > 2) { |
|
460 |
error(AGENT_CLASS_INVALID, "\"" + prop + "\""); |
|
461 |
} |
|
462 |
String cname = values[0]; |
|
463 |
String args = (values.length == 2 ? values[1] : null); |
|
464 |
||
465 |
if (cname == null || cname.length() == 0) { |
|
466 |
error(AGENT_CLASS_INVALID, "\"" + prop + "\""); |
|
467 |
} |
|
468 |
||
469 |
if (cname != null) { |
|
470 |
try { |
|
471 |
// Instantiate the named class. |
|
472 |
// invoke the premain(String args) method |
|
473 |
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cname); |
|
474 |
Method premain = clz.getMethod("premain", |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
475 |
new Class<?>[]{String.class}); |
2 | 476 |
premain.invoke(null, /* static */ |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
477 |
new Object[]{args}); |
2 | 478 |
} catch (ClassNotFoundException ex) { |
479 |
error(AGENT_CLASS_NOT_FOUND, "\"" + cname + "\""); |
|
480 |
} catch (NoSuchMethodException ex) { |
|
481 |
error(AGENT_CLASS_PREMAIN_NOT_FOUND, "\"" + cname + "\""); |
|
482 |
} catch (SecurityException ex) { |
|
483 |
error(AGENT_CLASS_ACCESS_DENIED); |
|
484 |
} catch (Exception ex) { |
|
485 |
String msg = (ex.getCause() == null |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
486 |
? ex.getMessage() |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
487 |
: ex.getCause().getMessage()); |
2 | 488 |
error(AGENT_CLASS_FAILED, msg); |
489 |
} |
|
490 |
} |
|
491 |
} |
|
492 |
||
493 |
public static void error(String key) { |
|
494 |
String keyText = getText(key); |
|
495 |
System.err.print(getText("agent.err.error") + ": " + keyText); |
|
496 |
throw new RuntimeException(keyText); |
|
497 |
} |
|
498 |
||
499 |
public static void error(String key, String[] params) { |
|
500 |
if (params == null || params.length == 0) { |
|
501 |
error(key); |
|
502 |
} else { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
503 |
StringBuilder message = new StringBuilder(params[0]); |
2 | 504 |
for (int i = 1; i < params.length; i++) { |
505 |
message.append(" " + params[i]); |
|
506 |
} |
|
507 |
error(key, message.toString()); |
|
508 |
} |
|
509 |
} |
|
510 |
||
511 |
public static void error(String key, String message) { |
|
512 |
String keyText = getText(key); |
|
513 |
System.err.print(getText("agent.err.error") + ": " + keyText); |
|
514 |
System.err.println(": " + message); |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
515 |
throw new RuntimeException(keyText + ": " + message); |
2 | 516 |
} |
517 |
||
518 |
public static void error(Exception e) { |
|
519 |
e.printStackTrace(); |
|
520 |
System.err.println(getText(AGENT_EXCEPTION) + ": " + e.toString()); |
|
521 |
throw new RuntimeException(e); |
|
522 |
} |
|
523 |
||
524 |
public static void warning(String key, String message) { |
|
525 |
System.err.print(getText("agent.err.warning") + ": " + getText(key)); |
|
526 |
System.err.println(": " + message); |
|
527 |
} |
|
528 |
||
529 |
private static void initResource() { |
|
530 |
try { |
|
531 |
messageRB = |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
532 |
ResourceBundle.getBundle("sun.management.resources.agent"); |
2 | 533 |
} catch (MissingResourceException e) { |
534 |
throw new Error("Fatal: Resource for management agent is missing"); |
|
535 |
} |
|
536 |
} |
|
537 |
||
538 |
public static String getText(String key) { |
|
539 |
if (messageRB == null) { |
|
540 |
initResource(); |
|
541 |
} |
|
542 |
try { |
|
543 |
return messageRB.getString(key); |
|
544 |
} catch (MissingResourceException e) { |
|
545 |
return "Missing management agent resource bundle: key = \"" + key + "\""; |
|
546 |
} |
|
547 |
} |
|
548 |
||
549 |
public static String getText(String key, String... args) { |
|
550 |
if (messageRB == null) { |
|
551 |
initResource(); |
|
552 |
} |
|
553 |
String format = messageRB.getString(key); |
|
554 |
if (format == null) { |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
555 |
format = "missing resource key: key = \"" + key + "\", " |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
556 |
+ "arguments = \"{0}\", \"{1}\", \"{2}\""; |
2 | 557 |
} |
558 |
return MessageFormat.format(format, (Object[]) args); |
|
559 |
} |
|
560 |
} |