author | lana |
Thu, 26 Dec 2013 12:04:16 -0800 | |
changeset 23010 | 6dadb192ad81 |
parent 21298 | d8a11a962ee3 |
child 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.Socket; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
38 |
import java.net.UnknownHostException; |
2 | 39 |
import java.text.MessageFormat; |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
40 |
import java.util.MissingResourceException; |
2 | 41 |
import java.util.Properties; |
42 |
import java.util.ResourceBundle; |
|
43 |
||
44 |
import javax.management.remote.JMXConnectorServer; |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
45 |
import javax.management.remote.JMXServiceURL; |
2 | 46 |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
47 |
import static sun.management.AgentConfigurationError.*; |
2 | 48 |
import sun.management.jmxremote.ConnectorBootstrap; |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
49 |
import sun.management.jdp.JdpController; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
50 |
import sun.management.jdp.JdpException; |
2 | 51 |
import sun.misc.VMSupport; |
52 |
||
53 |
/** |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
54 |
* 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
|
55 |
* -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
|
56 |
* system class loader. Also jmx framework could be started by jcmd |
2 | 57 |
*/ |
58 |
public class Agent { |
|
59 |
// management properties |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
60 |
|
2 | 61 |
private static Properties mgmtProps; |
62 |
private static ResourceBundle messageRB; |
|
63 |
private static final String CONFIG_FILE = |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
64 |
"com.sun.management.config.file"; |
2 | 65 |
private static final String SNMP_PORT = |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
66 |
"com.sun.management.snmp.port"; |
2 | 67 |
private static final String JMXREMOTE = |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
68 |
"com.sun.management.jmxremote"; |
2 | 69 |
private static final String JMXREMOTE_PORT = |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
70 |
"com.sun.management.jmxremote.port"; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
71 |
private static final String RMI_PORT = |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
72 |
"com.sun.management.jmxremote.rmi.port"; |
2 | 73 |
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
|
74 |
"com.sun.management.enableThreadContentionMonitoring"; |
2 | 75 |
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
|
76 |
"com.sun.management.jmxremote.localConnectorAddress"; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
77 |
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
|
78 |
"sun.management.snmp.AdaptorBootstrap"; |
2 | 79 |
|
17721
1d6516794d05
8014420: Default JDP address does not match the one assigned by IANA
dsamersoff
parents:
15531
diff
changeset
|
80 |
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
|
81 |
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
|
82 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
83 |
// The only active agent allowed |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
84 |
private static JMXConnectorServer jmxServer = null; |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
85 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
86 |
// 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
|
87 |
// 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
|
88 |
// return empty property set |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
89 |
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
|
90 |
Properties argProps = new Properties(); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
91 |
if (args != null) { |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
92 |
for (String option : args.split(",")) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
93 |
String s[] = option.split("=", 2); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
94 |
String name = s[0].trim(); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
95 |
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
|
96 |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
97 |
if (!name.startsWith("com.sun.management.")) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
98 |
error(INVALID_OPTION, name); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
99 |
} |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
100 |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
101 |
argProps.setProperty(name, value); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
102 |
} |
11991
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 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
105 |
return argProps; |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
106 |
} |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
107 |
|
2 | 108 |
// invoked by -javaagent or -Dcom.sun.management.agent.class |
109 |
public static void premain(String args) throws Exception { |
|
110 |
agentmain(args); |
|
111 |
} |
|
112 |
||
113 |
// invoked by attach mechanism |
|
114 |
public static void agentmain(String args) throws Exception { |
|
115 |
if (args == null || args.length() == 0) { |
|
116 |
args = JMXREMOTE; // default to local management |
|
117 |
} |
|
118 |
||
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
119 |
Properties arg_props = parseString(args); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
120 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
121 |
// Read properties from the config file |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
122 |
Properties config_props = new Properties(); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
123 |
String fname = arg_props.getProperty(CONFIG_FILE); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
124 |
readConfiguration(fname, config_props); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
125 |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
126 |
// Arguments override config file |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
127 |
config_props.putAll(arg_props); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
128 |
startAgent(config_props); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
129 |
} |
2 | 130 |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
131 |
// 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
|
132 |
// 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
|
133 |
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
|
134 |
Properties agentProps = VMSupport.getAgentProperties(); |
2 | 135 |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
136 |
// 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
|
137 |
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
|
138 |
JMXConnectorServer cs = ConnectorBootstrap.startLocalConnectorServer(); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
139 |
String address = cs.getAddress().toString(); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
140 |
// 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
|
141 |
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
|
142 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
143 |
try { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
144 |
// 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
|
145 |
ConnectorAddressLink.export(address); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
146 |
} catch (Exception x) { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
147 |
// 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
|
148 |
// 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
|
149 |
warning(EXPORT_ADDRESS_FAILED, x.getMessage()); |
2 | 150 |
} |
151 |
} |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
152 |
} |
2 | 153 |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
154 |
// jcmd ManagementAgent.start entry point |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
155 |
// 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
|
156 |
// 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
|
157 |
// @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
|
158 |
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
|
159 |
if (jmxServer != null) { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
160 |
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
|
161 |
} |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
162 |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
163 |
Properties argProps = parseString(args); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
164 |
Properties configProps = new Properties(); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
165 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
166 |
// Load the management properties from the config file |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
167 |
// if config file is not specified readConfiguration implicitly |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
168 |
// reads <java.home>/lib/management/management.properties |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
169 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
170 |
String fname = System.getProperty(CONFIG_FILE); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
171 |
readConfiguration(fname, configProps); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
172 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
173 |
// management properties can be overridden by system properties |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
174 |
// which take precedence |
12694
543c5e6c5ed0
7164191: properties.putAll API may fail with ConcurrentModifcationException on multi-thread scenario
dsamersoff
parents:
11991
diff
changeset
|
175 |
Properties sysProps = System.getProperties(); |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
176 |
synchronized (sysProps) { |
12694
543c5e6c5ed0
7164191: properties.putAll API may fail with ConcurrentModifcationException on multi-thread scenario
dsamersoff
parents:
11991
diff
changeset
|
177 |
configProps.putAll(sysProps); |
543c5e6c5ed0
7164191: properties.putAll API may fail with ConcurrentModifcationException on multi-thread scenario
dsamersoff
parents:
11991
diff
changeset
|
178 |
} |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
179 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
180 |
// if user specifies config file into command line for either |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
181 |
// jcmd utilities or attach command it overrides properties set in |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
182 |
// command line at the time of VM start |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
183 |
String fnameUser = argProps.getProperty(CONFIG_FILE); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
184 |
if (fnameUser != null) { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
185 |
readConfiguration(fnameUser, configProps); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
186 |
} |
2 | 187 |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
188 |
// arguments specified in command line of jcmd utilities |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
189 |
// override both system properties and one set by config file |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
190 |
// specified in jcmd command line |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
191 |
configProps.putAll(argProps); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
192 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
193 |
// jcmd doesn't allow to change ThreadContentionMonitoring, but user |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
194 |
// can specify this property inside config file, so enable optional |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
195 |
// monitoring functionality if this property is set |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
196 |
final String enableThreadContentionMonitoring = |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
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 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
199 |
if (enableThreadContentionMonitoring != null) { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
200 |
ManagementFactory.getThreadMXBean(). |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
201 |
setThreadContentionMonitoringEnabled(true); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
202 |
} |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
203 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
204 |
String jmxremotePort = configProps.getProperty(JMXREMOTE_PORT); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
205 |
if (jmxremotePort != null) { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
206 |
jmxServer = ConnectorBootstrap. |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
207 |
startRemoteConnectorServer(jmxremotePort, configProps); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
208 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
209 |
startDiscoveryService(configProps); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
210 |
} |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
211 |
} |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
212 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
213 |
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
|
214 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
215 |
JdpController.stopDiscoveryService(); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
216 |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
217 |
if (jmxServer != null) { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
218 |
ConnectorBootstrap.unexportRegistry(); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
219 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
220 |
// 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
|
221 |
// Don't cause any errors. |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
222 |
jmxServer.stop(); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
223 |
jmxServer = null; |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
224 |
} |
2 | 225 |
} |
226 |
||
227 |
private static void startAgent(Properties props) throws Exception { |
|
228 |
String snmpPort = props.getProperty(SNMP_PORT); |
|
229 |
String jmxremote = props.getProperty(JMXREMOTE); |
|
230 |
String jmxremotePort = props.getProperty(JMXREMOTE_PORT); |
|
231 |
||
232 |
// Enable optional monitoring functionality if requested |
|
233 |
final String enableThreadContentionMonitoring = |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
234 |
props.getProperty(ENABLE_THREAD_CONTENTION_MONITORING); |
2 | 235 |
if (enableThreadContentionMonitoring != null) { |
236 |
ManagementFactory.getThreadMXBean(). |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
237 |
setThreadContentionMonitoringEnabled(true); |
2 | 238 |
} |
239 |
||
240 |
try { |
|
241 |
if (snmpPort != null) { |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
242 |
loadSnmpAgent(snmpPort, props); |
2 | 243 |
} |
244 |
||
245 |
/* |
|
246 |
* If the jmxremote.port property is set then we start the |
|
247 |
* RMIConnectorServer for remote M&M. |
|
248 |
* |
|
249 |
* If the jmxremote or jmxremote.port properties are set then |
|
250 |
* we start a RMIConnectorServer for local M&M. The address |
|
251 |
* of this "local" server is exported as a counter to the jstat |
|
252 |
* instrumentation buffer. |
|
253 |
*/ |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
254 |
if (jmxremote != null || jmxremotePort != null) { |
2 | 255 |
if (jmxremotePort != null) { |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
256 |
jmxServer = ConnectorBootstrap. |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
257 |
startRemoteConnectorServer(jmxremotePort, props); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
258 |
startDiscoveryService(props); |
2 | 259 |
} |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
260 |
startLocalManagementAgent(); |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
261 |
} |
2 | 262 |
|
263 |
} catch (AgentConfigurationError e) { |
|
264 |
error(e.getError(), e.getParams()); |
|
265 |
} catch (Exception e) { |
|
266 |
error(e); |
|
267 |
} |
|
268 |
} |
|
269 |
||
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
270 |
private static void startDiscoveryService(Properties props) |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
271 |
throws IOException { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
272 |
// Start discovery service if requested |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
273 |
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
|
274 |
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
|
275 |
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
|
276 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
277 |
// Decide whether we should start autodicovery service. |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
278 |
// 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
|
279 |
// 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
|
280 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
281 |
boolean shouldStart = false; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
282 |
if (discoveryShouldStart == null){ |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
283 |
shouldStart = (discoveryPort != null); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
284 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
285 |
else{ |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
286 |
try{ |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
287 |
shouldStart = Boolean.parseBoolean(discoveryShouldStart); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
288 |
} catch (NumberFormatException e) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
289 |
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
|
290 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
291 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
292 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
293 |
if (shouldStart) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
294 |
// 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
|
295 |
InetAddress address; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
296 |
try { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
297 |
address = (discoveryAddress == null) ? |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
298 |
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
|
299 |
} catch (UnknownHostException e) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
300 |
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
|
301 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
302 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
303 |
int port = JDP_DEFAULT_PORT; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
304 |
if (discoveryPort != null) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
305 |
try { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
306 |
port = Integer.parseInt(discoveryPort); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
307 |
} catch (NumberFormatException e) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
308 |
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
|
309 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
310 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
311 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
312 |
// Rebuilding service URL to broadcast it |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
313 |
String jmxremotePort = props.getProperty(JMXREMOTE_PORT); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
314 |
String rmiPort = props.getProperty(RMI_PORT); |
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 |
JMXServiceURL url = jmxServer.getAddress(); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
317 |
String hostname = url.getHost(); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
318 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
319 |
String jmxUrlStr = (rmiPort != null) |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
320 |
? String.format( |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
321 |
"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
|
322 |
hostname, rmiPort, hostname, jmxremotePort) |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
323 |
: String.format( |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
324 |
"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
|
325 |
|
21298
d8a11a962ee3
8024071: In ManagementAgent.start it should be possible to set the jdp.name parameter.
dsamersoff
parents:
17721
diff
changeset
|
326 |
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
|
327 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
328 |
try{ |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
329 |
JdpController.startDiscoveryService(address, port, instanceName, jmxUrlStr); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
330 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
331 |
catch(JdpException e){ |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
332 |
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
|
333 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
334 |
} |
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 |
|
2 | 337 |
public static Properties loadManagementProperties() { |
338 |
Properties props = new Properties(); |
|
339 |
||
340 |
// Load the management properties from the config file |
|
341 |
||
342 |
String fname = System.getProperty(CONFIG_FILE); |
|
343 |
readConfiguration(fname, props); |
|
344 |
||
345 |
// management properties can be overridden by system properties |
|
346 |
// which take precedence |
|
12694
543c5e6c5ed0
7164191: properties.putAll API may fail with ConcurrentModifcationException on multi-thread scenario
dsamersoff
parents:
11991
diff
changeset
|
347 |
Properties sysProps = System.getProperties(); |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
348 |
synchronized (sysProps) { |
12694
543c5e6c5ed0
7164191: properties.putAll API may fail with ConcurrentModifcationException on multi-thread scenario
dsamersoff
parents:
11991
diff
changeset
|
349 |
props.putAll(sysProps); |
543c5e6c5ed0
7164191: properties.putAll API may fail with ConcurrentModifcationException on multi-thread scenario
dsamersoff
parents:
11991
diff
changeset
|
350 |
} |
2 | 351 |
|
352 |
return props; |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
353 |
} |
2 | 354 |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
355 |
public static synchronized Properties getManagementProperties() { |
2 | 356 |
if (mgmtProps == null) { |
357 |
String configFile = System.getProperty(CONFIG_FILE); |
|
358 |
String snmpPort = System.getProperty(SNMP_PORT); |
|
359 |
String jmxremote = System.getProperty(JMXREMOTE); |
|
360 |
String jmxremotePort = System.getProperty(JMXREMOTE_PORT); |
|
361 |
||
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
362 |
if (configFile == null && snmpPort == null |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
363 |
&& jmxremote == null && jmxremotePort == null) { |
2 | 364 |
// return if out-of-the-management option is not specified |
365 |
return null; |
|
366 |
} |
|
367 |
mgmtProps = loadManagementProperties(); |
|
368 |
} |
|
369 |
return mgmtProps; |
|
370 |
} |
|
371 |
||
2285
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
372 |
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
|
373 |
try { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
374 |
// 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
|
375 |
// AdaptorBootstrap.initialize(snmpPort, props); |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
376 |
final Class<?> adaptorClass = |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
377 |
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
|
378 |
final Method initializeMethod = |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
379 |
adaptorClass.getMethod("initialize", |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
380 |
String.class, Properties.class); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
381 |
initializeMethod.invoke(null, snmpPort, props); |
11530
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
382 |
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException x) { |
a9d059c15b80
7117570: Warnings in sun.mangement.* and its subpackages
mchung
parents:
5506
diff
changeset
|
383 |
// 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
|
384 |
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
|
385 |
} catch (InvocationTargetException x) { |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
386 |
final Throwable cause = x.getCause(); |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
387 |
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
|
388 |
throw (RuntimeException) cause; |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
389 |
} 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
|
390 |
throw (Error) cause; |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
391 |
} |
2285
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
392 |
// should not happen... |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
393 |
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
|
394 |
} |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
395 |
} |
37fdbed8178f
6661448: Make the SNMP agent optional when OPENJDK=true and IMPORT_BINARY_PLUGS=false
dfuchs
parents:
2
diff
changeset
|
396 |
|
2 | 397 |
// read config file and initialize the properties |
398 |
private static void readConfiguration(String fname, Properties p) { |
|
399 |
if (fname == null) { |
|
400 |
String home = System.getProperty("java.home"); |
|
401 |
if (home == null) { |
|
402 |
throw new Error("Can't find java.home ??"); |
|
403 |
} |
|
404 |
StringBuffer defaultFileName = new StringBuffer(home); |
|
405 |
defaultFileName.append(File.separator).append("lib"); |
|
406 |
defaultFileName.append(File.separator).append("management"); |
|
407 |
defaultFileName.append(File.separator).append("management.properties"); |
|
408 |
// Set file name |
|
409 |
fname = defaultFileName.toString(); |
|
410 |
} |
|
411 |
final File configFile = new File(fname); |
|
412 |
if (!configFile.exists()) { |
|
413 |
error(CONFIG_FILE_NOT_FOUND, fname); |
|
414 |
} |
|
415 |
||
416 |
InputStream in = null; |
|
417 |
try { |
|
418 |
in = new FileInputStream(configFile); |
|
419 |
BufferedInputStream bin = new BufferedInputStream(in); |
|
420 |
p.load(bin); |
|
421 |
} catch (FileNotFoundException e) { |
|
422 |
error(CONFIG_FILE_OPEN_FAILED, e.getMessage()); |
|
423 |
} catch (IOException e) { |
|
424 |
error(CONFIG_FILE_OPEN_FAILED, e.getMessage()); |
|
425 |
} catch (SecurityException e) { |
|
426 |
error(CONFIG_FILE_ACCESS_DENIED, fname); |
|
427 |
} finally { |
|
428 |
if (in != null) { |
|
429 |
try { |
|
430 |
in.close(); |
|
431 |
} catch (IOException e) { |
|
432 |
error(CONFIG_FILE_CLOSE_FAILED, fname); |
|
433 |
} |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
434 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
435 |
} |
2 | 436 |
} |
437 |
||
438 |
public static void startAgent() throws Exception { |
|
439 |
String prop = System.getProperty("com.sun.management.agent.class"); |
|
440 |
||
441 |
// -Dcom.sun.management.agent.class not set so read management |
|
442 |
// properties and start agent |
|
443 |
if (prop == null) { |
|
444 |
// initialize management properties |
|
445 |
Properties props = getManagementProperties(); |
|
446 |
if (props != null) { |
|
447 |
startAgent(props); |
|
448 |
} |
|
449 |
return; |
|
450 |
} |
|
451 |
||
452 |
// -Dcom.sun.management.agent.class=<agent classname>:<agent args> |
|
453 |
String[] values = prop.split(":"); |
|
454 |
if (values.length < 1 || values.length > 2) { |
|
455 |
error(AGENT_CLASS_INVALID, "\"" + prop + "\""); |
|
456 |
} |
|
457 |
String cname = values[0]; |
|
458 |
String args = (values.length == 2 ? values[1] : null); |
|
459 |
||
460 |
if (cname == null || cname.length() == 0) { |
|
461 |
error(AGENT_CLASS_INVALID, "\"" + prop + "\""); |
|
462 |
} |
|
463 |
||
464 |
if (cname != null) { |
|
465 |
try { |
|
466 |
// Instantiate the named class. |
|
467 |
// invoke the premain(String args) method |
|
468 |
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cname); |
|
469 |
Method premain = clz.getMethod("premain", |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
470 |
new Class<?>[]{String.class}); |
2 | 471 |
premain.invoke(null, /* static */ |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
472 |
new Object[]{args}); |
2 | 473 |
} catch (ClassNotFoundException ex) { |
474 |
error(AGENT_CLASS_NOT_FOUND, "\"" + cname + "\""); |
|
475 |
} catch (NoSuchMethodException ex) { |
|
476 |
error(AGENT_CLASS_PREMAIN_NOT_FOUND, "\"" + cname + "\""); |
|
477 |
} catch (SecurityException ex) { |
|
478 |
error(AGENT_CLASS_ACCESS_DENIED); |
|
479 |
} catch (Exception ex) { |
|
480 |
String msg = (ex.getCause() == null |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
481 |
? ex.getMessage() |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
482 |
: ex.getCause().getMessage()); |
2 | 483 |
error(AGENT_CLASS_FAILED, msg); |
484 |
} |
|
485 |
} |
|
486 |
} |
|
487 |
||
488 |
public static void error(String key) { |
|
489 |
String keyText = getText(key); |
|
490 |
System.err.print(getText("agent.err.error") + ": " + keyText); |
|
491 |
throw new RuntimeException(keyText); |
|
492 |
} |
|
493 |
||
494 |
public static void error(String key, String[] params) { |
|
495 |
if (params == null || params.length == 0) { |
|
496 |
error(key); |
|
497 |
} else { |
|
498 |
StringBuffer message = new StringBuffer(params[0]); |
|
499 |
for (int i = 1; i < params.length; i++) { |
|
500 |
message.append(" " + params[i]); |
|
501 |
} |
|
502 |
error(key, message.toString()); |
|
503 |
} |
|
504 |
} |
|
505 |
||
506 |
public static void error(String key, String message) { |
|
507 |
String keyText = getText(key); |
|
508 |
System.err.print(getText("agent.err.error") + ": " + keyText); |
|
509 |
System.err.println(": " + message); |
|
510 |
throw new RuntimeException(keyText); |
|
511 |
} |
|
512 |
||
513 |
public static void error(Exception e) { |
|
514 |
e.printStackTrace(); |
|
515 |
System.err.println(getText(AGENT_EXCEPTION) + ": " + e.toString()); |
|
516 |
throw new RuntimeException(e); |
|
517 |
} |
|
518 |
||
519 |
public static void warning(String key, String message) { |
|
520 |
System.err.print(getText("agent.err.warning") + ": " + getText(key)); |
|
521 |
System.err.println(": " + message); |
|
522 |
} |
|
523 |
||
524 |
private static void initResource() { |
|
525 |
try { |
|
526 |
messageRB = |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
527 |
ResourceBundle.getBundle("sun.management.resources.agent"); |
2 | 528 |
} catch (MissingResourceException e) { |
529 |
throw new Error("Fatal: Resource for management agent is missing"); |
|
530 |
} |
|
531 |
} |
|
532 |
||
533 |
public static String getText(String key) { |
|
534 |
if (messageRB == null) { |
|
535 |
initResource(); |
|
536 |
} |
|
537 |
try { |
|
538 |
return messageRB.getString(key); |
|
539 |
} catch (MissingResourceException e) { |
|
540 |
return "Missing management agent resource bundle: key = \"" + key + "\""; |
|
541 |
} |
|
542 |
} |
|
543 |
||
544 |
public static String getText(String key, String... args) { |
|
545 |
if (messageRB == null) { |
|
546 |
initResource(); |
|
547 |
} |
|
548 |
String format = messageRB.getString(key); |
|
549 |
if (format == null) { |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
550 |
format = "missing resource key: key = \"" + key + "\", " |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
551 |
+ "arguments = \"{0}\", \"{1}\", \"{2}\""; |
2 | 552 |
} |
553 |
return MessageFormat.format(format, (Object[]) args); |
|
554 |
} |
|
555 |
} |