32 import java.io.InputStream; |
32 import java.io.InputStream; |
33 import java.lang.management.ManagementFactory; |
33 import java.lang.management.ManagementFactory; |
34 import java.lang.reflect.InvocationTargetException; |
34 import java.lang.reflect.InvocationTargetException; |
35 import java.lang.reflect.Method; |
35 import java.lang.reflect.Method; |
36 import java.net.InetAddress; |
36 import java.net.InetAddress; |
37 import java.net.Socket; |
|
38 import java.net.UnknownHostException; |
37 import java.net.UnknownHostException; |
39 import java.text.MessageFormat; |
38 import java.text.MessageFormat; |
40 import java.util.MissingResourceException; |
39 import java.util.MissingResourceException; |
41 import java.util.Properties; |
40 import java.util.Properties; |
42 import java.util.ResourceBundle; |
41 import java.util.ResourceBundle; |
86 // Parse string com.sun.management.prop=xxx,com.sun.management.prop=yyyy |
85 // Parse string com.sun.management.prop=xxx,com.sun.management.prop=yyyy |
87 // and return property set if args is null or empty |
86 // and return property set if args is null or empty |
88 // return empty property set |
87 // return empty property set |
89 private static Properties parseString(String args) { |
88 private static Properties parseString(String args) { |
90 Properties argProps = new Properties(); |
89 Properties argProps = new Properties(); |
91 if (args != null) { |
90 if (args != null && !args.trim().equals("")) { |
92 for (String option : args.split(",")) { |
91 for (String option : args.split(",")) { |
93 String s[] = option.split("=", 2); |
92 String s[] = option.split("=", 2); |
94 String name = s[0].trim(); |
93 String name = s[0].trim(); |
95 String value = (s.length > 1) ? s[1].trim() : ""; |
94 String value = (s.length > 1) ? s[1].trim() : ""; |
96 |
95 |
158 private static synchronized void startRemoteManagementAgent(String args) throws Exception { |
157 private static synchronized void startRemoteManagementAgent(String args) throws Exception { |
159 if (jmxServer != null) { |
158 if (jmxServer != null) { |
160 throw new RuntimeException(getText(INVALID_STATE, "Agent already started")); |
159 throw new RuntimeException(getText(INVALID_STATE, "Agent already started")); |
161 } |
160 } |
162 |
161 |
163 Properties argProps = parseString(args); |
162 try { |
164 Properties configProps = new Properties(); |
163 Properties argProps = parseString(args); |
165 |
164 Properties configProps = new Properties(); |
166 // Load the management properties from the config file |
165 |
167 // if config file is not specified readConfiguration implicitly |
166 // Load the management properties from the config file |
168 // reads <java.home>/lib/management/management.properties |
167 // if config file is not specified readConfiguration implicitly |
169 |
168 // reads <java.home>/lib/management/management.properties |
170 String fname = System.getProperty(CONFIG_FILE); |
169 |
171 readConfiguration(fname, configProps); |
170 String fname = System.getProperty(CONFIG_FILE); |
172 |
171 readConfiguration(fname, configProps); |
173 // management properties can be overridden by system properties |
172 |
174 // which take precedence |
173 // management properties can be overridden by system properties |
175 Properties sysProps = System.getProperties(); |
174 // which take precedence |
176 synchronized (sysProps) { |
175 Properties sysProps = System.getProperties(); |
177 configProps.putAll(sysProps); |
176 synchronized (sysProps) { |
178 } |
177 configProps.putAll(sysProps); |
179 |
178 } |
180 // if user specifies config file into command line for either |
179 |
181 // jcmd utilities or attach command it overrides properties set in |
180 // if user specifies config file into command line for either |
182 // command line at the time of VM start |
181 // jcmd utilities or attach command it overrides properties set in |
183 String fnameUser = argProps.getProperty(CONFIG_FILE); |
182 // command line at the time of VM start |
184 if (fnameUser != null) { |
183 String fnameUser = argProps.getProperty(CONFIG_FILE); |
185 readConfiguration(fnameUser, configProps); |
184 if (fnameUser != null) { |
186 } |
185 readConfiguration(fnameUser, configProps); |
187 |
186 } |
188 // arguments specified in command line of jcmd utilities |
187 |
189 // override both system properties and one set by config file |
188 // arguments specified in command line of jcmd utilities |
190 // specified in jcmd command line |
189 // override both system properties and one set by config file |
191 configProps.putAll(argProps); |
190 // specified in jcmd command line |
192 |
191 configProps.putAll(argProps); |
193 // jcmd doesn't allow to change ThreadContentionMonitoring, but user |
192 |
194 // can specify this property inside config file, so enable optional |
193 // jcmd doesn't allow to change ThreadContentionMonitoring, but user |
195 // monitoring functionality if this property is set |
194 // can specify this property inside config file, so enable optional |
196 final String enableThreadContentionMonitoring = |
195 // monitoring functionality if this property is set |
197 configProps.getProperty(ENABLE_THREAD_CONTENTION_MONITORING); |
196 final String enableThreadContentionMonitoring = |
198 |
197 configProps.getProperty(ENABLE_THREAD_CONTENTION_MONITORING); |
199 if (enableThreadContentionMonitoring != null) { |
198 |
200 ManagementFactory.getThreadMXBean(). |
199 if (enableThreadContentionMonitoring != null) { |
201 setThreadContentionMonitoringEnabled(true); |
200 ManagementFactory.getThreadMXBean(). |
202 } |
201 setThreadContentionMonitoringEnabled(true); |
203 |
202 } |
204 String jmxremotePort = configProps.getProperty(JMXREMOTE_PORT); |
203 |
205 if (jmxremotePort != null) { |
204 String jmxremotePort = configProps.getProperty(JMXREMOTE_PORT); |
206 jmxServer = ConnectorBootstrap. |
205 if (jmxremotePort != null) { |
207 startRemoteConnectorServer(jmxremotePort, configProps); |
206 jmxServer = ConnectorBootstrap. |
208 |
207 startRemoteConnectorServer(jmxremotePort, configProps); |
209 startDiscoveryService(configProps); |
208 |
|
209 startDiscoveryService(configProps); |
|
210 } else { |
|
211 throw new AgentConfigurationError(INVALID_JMXREMOTE_PORT, "No port specified"); |
|
212 } |
|
213 } catch (AgentConfigurationError err) { |
|
214 error(err.getError(), err.getParams()); |
210 } |
215 } |
211 } |
216 } |
212 |
217 |
213 private static synchronized void stopRemoteManagementAgent() throws Exception { |
218 private static synchronized void stopRemoteManagementAgent() throws Exception { |
214 |
219 |