author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 53760 | 4958b60a70bc |
permissions | -rw-r--r-- |
2 | 1 |
/* |
53760
4958b60a70bc
8209455: [error-prone] JdkObsolete in jdk.management.agent
iignatyev
parents:
52902
diff
changeset
|
2 |
* Copyright (c) 2003, 2019, 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 |
*/ |
43494
11801b2ff456
8173608: Separate JDK management agent from java.management module
mchung
parents:
40743
diff
changeset
|
25 |
package jdk.internal.agent; |
2 | 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; |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
34 |
import java.lang.reflect.Method; |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
35 |
import java.net.InetAddress; |
30334
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
36 |
import java.net.MalformedURLException; |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
37 |
import java.net.UnknownHostException; |
40743
a775e4de954f
8131061: Use of -Dcom.sun.management.snmp needs to be examined for modules
vtewari
parents:
36854
diff
changeset
|
38 |
import java.security.AccessController; |
a775e4de954f
8131061: Use of -Dcom.sun.management.snmp needs to be examined for modules
vtewari
parents:
36854
diff
changeset
|
39 |
import java.security.PrivilegedAction; |
2 | 40 |
import java.text.MessageFormat; |
34540
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
41 |
import java.util.HashMap; |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
42 |
import java.util.Map; |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
43 |
import java.util.MissingResourceException; |
2 | 44 |
import java.util.Properties; |
45 |
import java.util.ResourceBundle; |
|
40743
a775e4de954f
8131061: Use of -Dcom.sun.management.snmp needs to be examined for modules
vtewari
parents:
36854
diff
changeset
|
46 |
import java.util.ServiceLoader; |
34540
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
47 |
import java.util.function.Function; |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
48 |
import java.util.function.Predicate; |
2 | 49 |
|
50 |
import javax.management.remote.JMXConnectorServer; |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
51 |
import javax.management.remote.JMXServiceURL; |
2 | 52 |
|
43494
11801b2ff456
8173608: Separate JDK management agent from java.management module
mchung
parents:
40743
diff
changeset
|
53 |
import static jdk.internal.agent.AgentConfigurationError.*; |
11801b2ff456
8173608: Separate JDK management agent from java.management module
mchung
parents:
40743
diff
changeset
|
54 |
import jdk.internal.agent.spi.AgentProvider; |
11801b2ff456
8173608: Separate JDK management agent from java.management module
mchung
parents:
40743
diff
changeset
|
55 |
import jdk.internal.vm.VMSupport; |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
56 |
import sun.management.jdp.JdpController; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
57 |
import sun.management.jdp.JdpException; |
43494
11801b2ff456
8173608: Separate JDK management agent from java.management module
mchung
parents:
40743
diff
changeset
|
58 |
import sun.management.jmxremote.ConnectorBootstrap; |
2 | 59 |
|
60 |
/** |
|
49542 | 61 |
* This class provides the methods to start the management agent. |
62 |
* 1. {@link #startAgent} method is invoked by the VM if {@code -Dcom.sun.management.*} is set |
|
63 |
* 2. {@link #startLocalManagementAgent} or {@link #startRemoteManagementAgent} |
|
64 |
* is invoked to start the management agent after the VM starts |
|
65 |
* via jcmd ManagementAgent.start and start_local command. |
|
2 | 66 |
*/ |
67 |
public class Agent { |
|
30334
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
68 |
/** |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
69 |
* Agent status collector strategy class |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
70 |
*/ |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
71 |
private static abstract class StatusCollector { |
34540
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
72 |
protected static final Map<String, String> DEFAULT_PROPS = new HashMap<>(); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
73 |
|
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
74 |
static { |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
75 |
DEFAULT_PROPS.put(ConnectorBootstrap.PropertyNames.PORT, |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
76 |
ConnectorBootstrap.DefaultValues.PORT); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
77 |
DEFAULT_PROPS.put(ConnectorBootstrap.PropertyNames.USE_LOCAL_ONLY, |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
78 |
ConnectorBootstrap.DefaultValues.USE_LOCAL_ONLY); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
79 |
DEFAULT_PROPS.put(ConnectorBootstrap.PropertyNames.USE_AUTHENTICATION, |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
80 |
ConnectorBootstrap.DefaultValues.USE_AUTHENTICATION); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
81 |
DEFAULT_PROPS.put(ConnectorBootstrap.PropertyNames.USE_SSL, |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
82 |
ConnectorBootstrap.DefaultValues.USE_SSL); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
83 |
DEFAULT_PROPS.put(ConnectorBootstrap.PropertyNames.USE_REGISTRY_SSL, |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
84 |
ConnectorBootstrap.DefaultValues.USE_REGISTRY_SSL); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
85 |
DEFAULT_PROPS.put(ConnectorBootstrap.PropertyNames.SSL_NEED_CLIENT_AUTH, |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
86 |
ConnectorBootstrap.DefaultValues.SSL_NEED_CLIENT_AUTH); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
87 |
DEFAULT_PROPS.put(ConnectorBootstrap.PropertyNames.CONFIG_FILE_NAME, |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
88 |
ConnectorBootstrap.DefaultValues.CONFIG_FILE_NAME); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
89 |
DEFAULT_PROPS.put(ConnectorBootstrap.PropertyNames.PASSWORD_FILE_NAME, |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
90 |
ConnectorBootstrap.DefaultValues.PASSWORD_FILE_NAME); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
91 |
DEFAULT_PROPS.put(ConnectorBootstrap.PropertyNames.ACCESS_FILE_NAME, |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
92 |
ConnectorBootstrap.DefaultValues.ACCESS_FILE_NAME); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
93 |
|
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
94 |
} |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
95 |
|
30334
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
96 |
final protected StringBuilder sb = new StringBuilder(); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
97 |
final public String collect() { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
98 |
Properties agentProps = VMSupport.getAgentProperties(); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
99 |
String localConnAddr = (String)agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
100 |
if (localConnAddr != null || jmxServer != null) { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
101 |
addAgentStatus(true); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
102 |
appendConnections(localConnAddr); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
103 |
} else { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
104 |
addAgentStatus(false); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
105 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
106 |
return sb.toString(); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
107 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
108 |
|
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
109 |
private void appendConnections(String localConnAddr) { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
110 |
appendConnectionsHeader(); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
111 |
if (localConnAddr != null) { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
112 |
try { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
113 |
JMXServiceURL u = new JMXServiceURL(localConnAddr); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
114 |
addConnection(false, u); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
115 |
} catch (MalformedURLException e) { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
116 |
// will never happen |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
117 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
118 |
|
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
119 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
120 |
if (jmxServer != null) { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
121 |
addConnection(true, jmxServer.getAddress()); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
122 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
123 |
appendConnectionsFooter(); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
124 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
125 |
|
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
126 |
private void addConnection(boolean remote, JMXServiceURL u) { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
127 |
appendConnectionHeader(remote); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
128 |
addConnectionDetails(u); |
34540
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
129 |
addConfigProperties(); |
30334
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
130 |
appendConnectionFooter(remote); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
131 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
132 |
|
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
133 |
private void addConfigProperties() { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
134 |
appendConfigPropsHeader(); |
34540
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
135 |
|
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
136 |
Properties remoteProps = configProps != null ? |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
137 |
configProps : getManagementProperties(); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
138 |
Map<Object, Object> props = new HashMap<>(DEFAULT_PROPS); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
139 |
|
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
140 |
if (remoteProps == null) { |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
141 |
// local connector only |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
142 |
String loc_only = System.getProperty( |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
143 |
ConnectorBootstrap.PropertyNames.USE_LOCAL_ONLY |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
144 |
); |
33267
c9211ad104b5
8138748: ManagementAgent.status DCMD fails with NPE for JMX configured on command line
jbachorik
parents:
30803
diff
changeset
|
145 |
|
34540
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
146 |
if (loc_only != null && |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
147 |
!ConnectorBootstrap.DefaultValues.USE_LOCAL_ONLY.equals(loc_only)) { |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
148 |
props.put( |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
149 |
ConnectorBootstrap.PropertyNames.USE_LOCAL_ONLY, |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
150 |
loc_only |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
151 |
); |
30334
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
152 |
} |
34540
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
153 |
} else { |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
154 |
props.putAll(remoteProps); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
155 |
} |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
156 |
|
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
157 |
props.entrySet().stream() |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
158 |
.filter(preprocess(Map.Entry::getKey, StatusCollector::isManagementProp)) |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
159 |
.forEach(this::addConfigProp); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
160 |
|
30334
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
161 |
appendConfigPropsFooter(); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
162 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
163 |
|
34540
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
164 |
private static boolean isManagementProp(Object pName) { |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
165 |
return pName != null && pName.toString().startsWith("com.sun.management."); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
166 |
} |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
167 |
|
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
168 |
private static <T, V> Predicate<T> preprocess(Function<T, V> f, Predicate<V> p) { |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
169 |
return (T t) -> p.test(f.apply(t)); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
170 |
} |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
171 |
|
30334
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
172 |
abstract protected void addAgentStatus(boolean enabled); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
173 |
abstract protected void appendConnectionsHeader(); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
174 |
abstract protected void appendConnectionsFooter(); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
175 |
abstract protected void addConnectionDetails(JMXServiceURL u); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
176 |
abstract protected void appendConnectionHeader(boolean remote); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
177 |
abstract protected void appendConnectionFooter(boolean remote); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
178 |
abstract protected void appendConfigPropsHeader(); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
179 |
abstract protected void appendConfigPropsFooter(); |
34540
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
180 |
abstract protected void addConfigProp(Map.Entry<?, ?> prop); |
30334
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
181 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
182 |
|
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
183 |
/** |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
184 |
* Free-text status collector strategy implementation |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
185 |
*/ |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
186 |
final private static class TextStatusCollector extends StatusCollector { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
187 |
|
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
188 |
@Override |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
189 |
protected void addAgentStatus(boolean enabled) { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
190 |
sb.append("Agent: ").append(enabled ? "enabled" : "disabled").append('\n'); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
191 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
192 |
|
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
193 |
@Override |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
194 |
protected void appendConnectionsHeader() { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
195 |
sb.append('\n'); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
196 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
197 |
|
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
198 |
@Override |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
199 |
protected void addConnectionDetails(JMXServiceURL u) { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
200 |
sb.append("Protocol : ").append(u.getProtocol()).append('\n') |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
201 |
.append("Host : ").append(u.getHost()).append('\n') |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
202 |
.append("URL : ").append(u).append('\n'); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
203 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
204 |
|
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
205 |
@Override |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
206 |
protected void appendConnectionHeader(boolean remote) { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
207 |
sb.append("Connection Type: ").append(remote ? "remote" : "local").append('\n'); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
208 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
209 |
|
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
210 |
@Override |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
211 |
protected void appendConfigPropsHeader() { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
212 |
sb.append("Properties :\n"); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
213 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
214 |
|
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
215 |
@Override |
34540
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
216 |
protected void addConfigProp(Map.Entry<?, ?> prop) { |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
217 |
sb.append(" ").append(prop.getKey()).append(" = ") |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
218 |
.append(prop.getValue()); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
219 |
Object defVal = DEFAULT_PROPS.get(prop.getKey()); |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
220 |
if (defVal != null && defVal.equals(prop.getValue())) { |
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
221 |
sb.append(" [default]"); |
30334
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
222 |
} |
34540
83f41bd1c3c8
8142398: ManagementAgent.status diagnostic command only outputs the specifically set properties
jbachorik
parents:
33267
diff
changeset
|
223 |
sb.append("\n"); |
30334
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
224 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
225 |
|
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
226 |
@Override |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
227 |
protected void appendConnectionsFooter() {} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
228 |
|
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
229 |
@Override |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
230 |
protected void appendConnectionFooter(boolean remote) { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
231 |
sb.append('\n'); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
232 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
233 |
|
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
234 |
@Override |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
235 |
protected void appendConfigPropsFooter() {} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
236 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
237 |
|
2 | 238 |
// management properties |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
239 |
|
2 | 240 |
private static Properties mgmtProps; |
241 |
private static ResourceBundle messageRB; |
|
242 |
private static final String CONFIG_FILE = |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
243 |
"com.sun.management.config.file"; |
2 | 244 |
private static final String JMXREMOTE = |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
245 |
"com.sun.management.jmxremote"; |
2 | 246 |
private static final String JMXREMOTE_PORT = |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
247 |
"com.sun.management.jmxremote.port"; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
248 |
private static final String RMI_PORT = |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
249 |
"com.sun.management.jmxremote.rmi.port"; |
2 | 250 |
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
|
251 |
"com.sun.management.enableThreadContentionMonitoring"; |
2 | 252 |
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
|
253 |
"com.sun.management.jmxremote.localConnectorAddress"; |
2 | 254 |
|
17721
1d6516794d05
8014420: Default JDP address does not match the one assigned by IANA
dsamersoff
parents:
15531
diff
changeset
|
255 |
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
|
256 |
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
|
257 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
258 |
// The only active agent allowed |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
259 |
private static JMXConnectorServer jmxServer = null; |
30334
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
260 |
// The properties used to configure the server |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
261 |
private static Properties configProps = null; |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
262 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
263 |
// 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
|
264 |
// 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
|
265 |
// return empty property set |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
266 |
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
|
267 |
Properties argProps = new Properties(); |
52902
e3398b2e1ab0
8214971: Replace use of string.equals("") with isEmpty()
rriggs
parents:
49542
diff
changeset
|
268 |
if (args != null && !args.trim().isEmpty()) { |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
269 |
for (String option : args.split(",")) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
270 |
String s[] = option.split("=", 2); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
271 |
String name = s[0].trim(); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
272 |
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
|
273 |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
274 |
if (!name.startsWith("com.sun.management.")) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
275 |
error(INVALID_OPTION, name); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
276 |
} |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
277 |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
278 |
argProps.setProperty(name, value); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
279 |
} |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
280 |
} |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
281 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
282 |
return argProps; |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
283 |
} |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
284 |
|
2 | 285 |
// invoked by -javaagent or -Dcom.sun.management.agent.class |
286 |
public static void premain(String args) throws Exception { |
|
287 |
agentmain(args); |
|
288 |
} |
|
289 |
||
290 |
// invoked by attach mechanism |
|
291 |
public static void agentmain(String args) throws Exception { |
|
292 |
if (args == null || args.length() == 0) { |
|
293 |
args = JMXREMOTE; // default to local management |
|
294 |
} |
|
295 |
||
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
296 |
Properties arg_props = parseString(args); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
297 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
298 |
// Read properties from the config file |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
299 |
Properties config_props = new Properties(); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
300 |
String fname = arg_props.getProperty(CONFIG_FILE); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
301 |
readConfiguration(fname, config_props); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
302 |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
303 |
// Arguments override config file |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
304 |
config_props.putAll(arg_props); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
305 |
startAgent(config_props); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
306 |
} |
2 | 307 |
|
49542 | 308 |
/* |
309 |
* Starts the local management agent. |
|
310 |
* This method is invoked by either startAgent method or |
|
311 |
* by the VM directly via jcmd ManagementAgent.start_local command. |
|
312 |
*/ |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
313 |
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
|
314 |
Properties agentProps = VMSupport.getAgentProperties(); |
2 | 315 |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
316 |
// 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
|
317 |
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
|
318 |
JMXConnectorServer cs = ConnectorBootstrap.startLocalConnectorServer(); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
319 |
String address = cs.getAddress().toString(); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
320 |
// 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
|
321 |
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
|
322 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
323 |
try { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
324 |
// 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
|
325 |
ConnectorAddressLink.export(address); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
326 |
} catch (Exception x) { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
327 |
// 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
|
328 |
// 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
|
329 |
warning(EXPORT_ADDRESS_FAILED, x.getMessage()); |
2 | 330 |
} |
331 |
} |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
332 |
} |
2 | 333 |
|
49542 | 334 |
/* |
335 |
* This method is invoked by the VM to start the remote management agent |
|
336 |
* via jcmd ManagementAgent.start command. |
|
337 |
*/ |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
338 |
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
|
339 |
if (jmxServer != null) { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
340 |
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
|
341 |
} |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
342 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
343 |
try { |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
344 |
Properties argProps = parseString(args); |
30334
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
345 |
configProps = new Properties(); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
346 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
347 |
// 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
|
348 |
// if config file is not specified readConfiguration implicitly |
27565 | 349 |
// reads <java.home>/conf/management/management.properties |
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
350 |
|
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
351 |
String fname = System.getProperty(CONFIG_FILE); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
352 |
readConfiguration(fname, configProps); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
353 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
354 |
// 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
|
355 |
// which take precedence |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
356 |
Properties sysProps = System.getProperties(); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
357 |
synchronized (sysProps) { |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
358 |
configProps.putAll(sysProps); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
359 |
} |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
360 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
361 |
// 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
|
362 |
// 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
|
363 |
// 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
|
364 |
String fnameUser = argProps.getProperty(CONFIG_FILE); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
365 |
if (fnameUser != null) { |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
366 |
readConfiguration(fnameUser, configProps); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
367 |
} |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
368 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
369 |
// 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
|
370 |
// 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
|
371 |
// specified in jcmd command line |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
372 |
configProps.putAll(argProps); |
2 | 373 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
374 |
// 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
|
375 |
// 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
|
376 |
// 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
|
377 |
final String enableThreadContentionMonitoring = |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
378 |
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
|
379 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
380 |
if (enableThreadContentionMonitoring != null) { |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
381 |
ManagementFactory.getThreadMXBean(). |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
382 |
setThreadContentionMonitoringEnabled(true); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
383 |
} |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
384 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
385 |
String jmxremotePort = configProps.getProperty(JMXREMOTE_PORT); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
386 |
if (jmxremotePort != null) { |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
387 |
jmxServer = ConnectorBootstrap. |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
388 |
startRemoteConnectorServer(jmxremotePort, configProps); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
389 |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
390 |
startDiscoveryService(configProps); |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
391 |
} else { |
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
392 |
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
|
393 |
} |
26488
5b5fccd9d016
8057746: Cannot handle JdpException in JMX agent initialization.
dsamersoff
parents:
25859
diff
changeset
|
394 |
} catch (JdpException e) { |
5b5fccd9d016
8057746: Cannot handle JdpException in JMX agent initialization.
dsamersoff
parents:
25859
diff
changeset
|
395 |
error(e); |
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
396 |
} catch (AgentConfigurationError err) { |
45009
c39425655469
8177721: Improve diagnostics in sun.management.Agent#startAgent()
shshahma
parents:
43719
diff
changeset
|
397 |
error(err); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
398 |
} |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
399 |
} |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
400 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
401 |
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
|
402 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
403 |
JdpController.stopDiscoveryService(); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
404 |
|
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
405 |
if (jmxServer != null) { |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
406 |
ConnectorBootstrap.unexportRegistry(); |
30803
17e70318af8b
8075926: Add a sun.management.JMXConnectorServer perf counter to track its state
jbachorik
parents:
30334
diff
changeset
|
407 |
ConnectorAddressLink.unexportRemote(); |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
408 |
|
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
409 |
// 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
|
410 |
// Don't cause any errors. |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
411 |
jmxServer.stop(); |
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
412 |
jmxServer = null; |
30334
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
413 |
configProps = null; |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
414 |
} |
2 | 415 |
} |
416 |
||
30334
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
417 |
private static synchronized String getManagementAgentStatus() throws Exception { |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
418 |
return new TextStatusCollector().collect(); |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
419 |
} |
22a5f09f4da9
8023093: Add ManagementAgent.status diagnostic command
jbachorik
parents:
27957
diff
changeset
|
420 |
|
2 | 421 |
private static void startAgent(Properties props) throws Exception { |
422 |
String jmxremote = props.getProperty(JMXREMOTE); |
|
423 |
String jmxremotePort = props.getProperty(JMXREMOTE_PORT); |
|
424 |
||
425 |
// Enable optional monitoring functionality if requested |
|
426 |
final String enableThreadContentionMonitoring = |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
427 |
props.getProperty(ENABLE_THREAD_CONTENTION_MONITORING); |
2 | 428 |
if (enableThreadContentionMonitoring != null) { |
429 |
ManagementFactory.getThreadMXBean(). |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
430 |
setThreadContentionMonitoringEnabled(true); |
2 | 431 |
} |
432 |
||
433 |
try { |
|
434 |
||
435 |
/* |
|
436 |
* If the jmxremote.port property is set then we start the |
|
437 |
* RMIConnectorServer for remote M&M. |
|
438 |
* |
|
439 |
* If the jmxremote or jmxremote.port properties are set then |
|
440 |
* we start a RMIConnectorServer for local M&M. The address |
|
441 |
* of this "local" server is exported as a counter to the jstat |
|
442 |
* instrumentation buffer. |
|
443 |
*/ |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
444 |
if (jmxremote != null || jmxremotePort != null) { |
2 | 445 |
if (jmxremotePort != null) { |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
446 |
jmxServer = ConnectorBootstrap. |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
447 |
startRemoteConnectorServer(jmxremotePort, props); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
448 |
startDiscoveryService(props); |
2 | 449 |
} |
11991
800d0ff7b043
7110104: It should be possible to stop and start JMX Agent at runtime
dsamersoff
parents:
11530
diff
changeset
|
450 |
startLocalManagementAgent(); |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
451 |
} |
2 | 452 |
|
453 |
} catch (AgentConfigurationError e) { |
|
45009
c39425655469
8177721: Improve diagnostics in sun.management.Agent#startAgent()
shshahma
parents:
43719
diff
changeset
|
454 |
error(e); |
2 | 455 |
} catch (Exception e) { |
456 |
error(e); |
|
457 |
} |
|
458 |
} |
|
459 |
||
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
460 |
private static void startDiscoveryService(Properties props) |
26488
5b5fccd9d016
8057746: Cannot handle JdpException in JMX agent initialization.
dsamersoff
parents:
25859
diff
changeset
|
461 |
throws IOException, JdpException { |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
462 |
// Start discovery service if requested |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
463 |
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
|
464 |
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
|
465 |
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
|
466 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
467 |
// Decide whether we should start autodicovery service. |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
468 |
// 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
|
469 |
// 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
|
470 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
471 |
boolean shouldStart = false; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
472 |
if (discoveryShouldStart == null){ |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
473 |
shouldStart = (discoveryPort != null); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
474 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
475 |
else{ |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
476 |
try{ |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
477 |
shouldStart = Boolean.parseBoolean(discoveryShouldStart); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
478 |
} catch (NumberFormatException e) { |
26488
5b5fccd9d016
8057746: Cannot handle JdpException in JMX agent initialization.
dsamersoff
parents:
25859
diff
changeset
|
479 |
throw new AgentConfigurationError(AGENT_EXCEPTION, "Couldn't parse autodiscovery argument"); |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
480 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
481 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
482 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
483 |
if (shouldStart) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
484 |
// 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
|
485 |
InetAddress address; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
486 |
try { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
487 |
address = (discoveryAddress == null) ? |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
488 |
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
|
489 |
} catch (UnknownHostException e) { |
26488
5b5fccd9d016
8057746: Cannot handle JdpException in JMX agent initialization.
dsamersoff
parents:
25859
diff
changeset
|
490 |
throw new AgentConfigurationError(AGENT_EXCEPTION, e, "Unable to broadcast to requested address"); |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
491 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
492 |
|
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
493 |
int port = JDP_DEFAULT_PORT; |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
494 |
if (discoveryPort != null) { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
495 |
try { |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
496 |
port = Integer.parseInt(discoveryPort); |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
497 |
} catch (NumberFormatException e) { |
26488
5b5fccd9d016
8057746: Cannot handle JdpException in JMX agent initialization.
dsamersoff
parents:
25859
diff
changeset
|
498 |
throw new AgentConfigurationError(AGENT_EXCEPTION, "Couldn't parse JDP port argument"); |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
499 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
500 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
501 |
|
43538 | 502 |
// Get service URL to broadcast it |
503 |
Map<String,String> remoteProps = ConnectorAddressLink.importRemoteFrom(0); |
|
504 |
String jmxUrlStr = remoteProps.get("sun.management.JMXConnectorServer.0.remoteAddress"); |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
505 |
|
21298
d8a11a962ee3
8024071: In ManagementAgent.start it should be possible to set the jdp.name parameter.
dsamersoff
parents:
17721
diff
changeset
|
506 |
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
|
507 |
|
26488
5b5fccd9d016
8057746: Cannot handle JdpException in JMX agent initialization.
dsamersoff
parents:
25859
diff
changeset
|
508 |
JdpController.startDiscoveryService(address, port, instanceName, jmxUrlStr); |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
509 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
510 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
511 |
|
2 | 512 |
public static Properties loadManagementProperties() { |
513 |
Properties props = new Properties(); |
|
514 |
||
515 |
// Load the management properties from the config file |
|
516 |
||
517 |
String fname = System.getProperty(CONFIG_FILE); |
|
518 |
readConfiguration(fname, props); |
|
519 |
||
520 |
// management properties can be overridden by system properties |
|
521 |
// which take precedence |
|
12694
543c5e6c5ed0
7164191: properties.putAll API may fail with ConcurrentModifcationException on multi-thread scenario
dsamersoff
parents:
11991
diff
changeset
|
522 |
Properties sysProps = System.getProperties(); |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
523 |
synchronized (sysProps) { |
12694
543c5e6c5ed0
7164191: properties.putAll API may fail with ConcurrentModifcationException on multi-thread scenario
dsamersoff
parents:
11991
diff
changeset
|
524 |
props.putAll(sysProps); |
543c5e6c5ed0
7164191: properties.putAll API may fail with ConcurrentModifcationException on multi-thread scenario
dsamersoff
parents:
11991
diff
changeset
|
525 |
} |
2 | 526 |
|
527 |
return props; |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
528 |
} |
2 | 529 |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
530 |
public static synchronized Properties getManagementProperties() { |
2 | 531 |
if (mgmtProps == null) { |
532 |
String configFile = System.getProperty(CONFIG_FILE); |
|
533 |
String jmxremote = System.getProperty(JMXREMOTE); |
|
534 |
String jmxremotePort = System.getProperty(JMXREMOTE_PORT); |
|
535 |
||
49542 | 536 |
if (configFile == null && jmxremote == null && jmxremotePort == null) { |
2 | 537 |
// return if out-of-the-management option is not specified |
538 |
return null; |
|
539 |
} |
|
540 |
mgmtProps = loadManagementProperties(); |
|
541 |
} |
|
542 |
return mgmtProps; |
|
543 |
} |
|
544 |
||
545 |
// read config file and initialize the properties |
|
546 |
private static void readConfiguration(String fname, Properties p) { |
|
547 |
if (fname == null) { |
|
548 |
String home = System.getProperty("java.home"); |
|
549 |
if (home == null) { |
|
550 |
throw new Error("Can't find java.home ??"); |
|
551 |
} |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23010
diff
changeset
|
552 |
StringBuilder defaultFileName = new StringBuilder(home); |
27565 | 553 |
defaultFileName.append(File.separator).append("conf"); |
2 | 554 |
defaultFileName.append(File.separator).append("management"); |
555 |
defaultFileName.append(File.separator).append("management.properties"); |
|
556 |
// Set file name |
|
557 |
fname = defaultFileName.toString(); |
|
558 |
} |
|
559 |
final File configFile = new File(fname); |
|
560 |
if (!configFile.exists()) { |
|
561 |
error(CONFIG_FILE_NOT_FOUND, fname); |
|
562 |
} |
|
563 |
||
564 |
InputStream in = null; |
|
565 |
try { |
|
566 |
in = new FileInputStream(configFile); |
|
567 |
BufferedInputStream bin = new BufferedInputStream(in); |
|
568 |
p.load(bin); |
|
569 |
} catch (FileNotFoundException e) { |
|
570 |
error(CONFIG_FILE_OPEN_FAILED, e.getMessage()); |
|
571 |
} catch (IOException e) { |
|
572 |
error(CONFIG_FILE_OPEN_FAILED, e.getMessage()); |
|
573 |
} catch (SecurityException e) { |
|
574 |
error(CONFIG_FILE_ACCESS_DENIED, fname); |
|
575 |
} finally { |
|
576 |
if (in != null) { |
|
577 |
try { |
|
578 |
in.close(); |
|
579 |
} catch (IOException e) { |
|
580 |
error(CONFIG_FILE_CLOSE_FAILED, fname); |
|
581 |
} |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
582 |
} |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
583 |
} |
2 | 584 |
} |
585 |
||
49542 | 586 |
/** |
587 |
* This method is invoked by the VM to start the management agent |
|
588 |
* when -Dcom.sun.management.* is set during startup. |
|
589 |
*/ |
|
2 | 590 |
public static void startAgent() throws Exception { |
591 |
String prop = System.getProperty("com.sun.management.agent.class"); |
|
592 |
||
593 |
// -Dcom.sun.management.agent.class not set so read management |
|
594 |
// properties and start agent |
|
595 |
if (prop == null) { |
|
596 |
// initialize management properties |
|
597 |
Properties props = getManagementProperties(); |
|
598 |
if (props != null) { |
|
599 |
startAgent(props); |
|
600 |
} |
|
601 |
return; |
|
602 |
} |
|
603 |
||
604 |
// -Dcom.sun.management.agent.class=<agent classname>:<agent args> |
|
605 |
String[] values = prop.split(":"); |
|
606 |
if (values.length < 1 || values.length > 2) { |
|
607 |
error(AGENT_CLASS_INVALID, "\"" + prop + "\""); |
|
608 |
} |
|
609 |
String cname = values[0]; |
|
610 |
String args = (values.length == 2 ? values[1] : null); |
|
611 |
||
612 |
if (cname == null || cname.length() == 0) { |
|
613 |
error(AGENT_CLASS_INVALID, "\"" + prop + "\""); |
|
614 |
} |
|
615 |
||
616 |
if (cname != null) { |
|
617 |
try { |
|
618 |
// Instantiate the named class. |
|
619 |
// invoke the premain(String args) method |
|
620 |
Class<?> clz = ClassLoader.getSystemClassLoader().loadClass(cname); |
|
621 |
Method premain = clz.getMethod("premain", |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
622 |
new Class<?>[]{String.class}); |
2 | 623 |
premain.invoke(null, /* static */ |
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
624 |
new Object[]{args}); |
2 | 625 |
} catch (ClassNotFoundException ex) { |
626 |
error(AGENT_CLASS_NOT_FOUND, "\"" + cname + "\""); |
|
627 |
} catch (NoSuchMethodException ex) { |
|
628 |
error(AGENT_CLASS_PREMAIN_NOT_FOUND, "\"" + cname + "\""); |
|
629 |
} catch (SecurityException ex) { |
|
630 |
error(AGENT_CLASS_ACCESS_DENIED); |
|
631 |
} catch (Exception ex) { |
|
632 |
String msg = (ex.getCause() == null |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
633 |
? ex.getMessage() |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
634 |
: ex.getCause().getMessage()); |
2 | 635 |
error(AGENT_CLASS_FAILED, msg); |
636 |
} |
|
637 |
} |
|
638 |
} |
|
639 |
||
640 |
public static void error(String key) { |
|
641 |
String keyText = getText(key); |
|
642 |
System.err.print(getText("agent.err.error") + ": " + keyText); |
|
643 |
throw new RuntimeException(keyText); |
|
644 |
} |
|
645 |
||
646 |
public static void error(String key, String message) { |
|
647 |
String keyText = getText(key); |
|
648 |
System.err.print(getText("agent.err.error") + ": " + keyText); |
|
649 |
System.err.println(": " + message); |
|
24980
dbf4ae2cafa4
8046778: Better error messages when starting JMX agent via attach or jcmd
sla
parents:
24969
diff
changeset
|
650 |
throw new RuntimeException(keyText + ": " + message); |
2 | 651 |
} |
652 |
||
653 |
public static void error(Exception e) { |
|
654 |
e.printStackTrace(); |
|
655 |
System.err.println(getText(AGENT_EXCEPTION) + ": " + e.toString()); |
|
656 |
throw new RuntimeException(e); |
|
657 |
} |
|
658 |
||
45009
c39425655469
8177721: Improve diagnostics in sun.management.Agent#startAgent()
shshahma
parents:
43719
diff
changeset
|
659 |
public static void error(AgentConfigurationError e) { |
c39425655469
8177721: Improve diagnostics in sun.management.Agent#startAgent()
shshahma
parents:
43719
diff
changeset
|
660 |
String keyText = getText(e.getError()); |
c39425655469
8177721: Improve diagnostics in sun.management.Agent#startAgent()
shshahma
parents:
43719
diff
changeset
|
661 |
String[] params = e.getParams(); |
c39425655469
8177721: Improve diagnostics in sun.management.Agent#startAgent()
shshahma
parents:
43719
diff
changeset
|
662 |
|
c39425655469
8177721: Improve diagnostics in sun.management.Agent#startAgent()
shshahma
parents:
43719
diff
changeset
|
663 |
System.err.print(getText("agent.err.error") + ": " + keyText); |
c39425655469
8177721: Improve diagnostics in sun.management.Agent#startAgent()
shshahma
parents:
43719
diff
changeset
|
664 |
|
c39425655469
8177721: Improve diagnostics in sun.management.Agent#startAgent()
shshahma
parents:
43719
diff
changeset
|
665 |
if (params != null && params.length != 0) { |
53760
4958b60a70bc
8209455: [error-prone] JdkObsolete in jdk.management.agent
iignatyev
parents:
52902
diff
changeset
|
666 |
System.err.println(": " + String.join(" ", params)); |
45009
c39425655469
8177721: Improve diagnostics in sun.management.Agent#startAgent()
shshahma
parents:
43719
diff
changeset
|
667 |
} |
c39425655469
8177721: Improve diagnostics in sun.management.Agent#startAgent()
shshahma
parents:
43719
diff
changeset
|
668 |
e.printStackTrace(); |
c39425655469
8177721: Improve diagnostics in sun.management.Agent#startAgent()
shshahma
parents:
43719
diff
changeset
|
669 |
throw new RuntimeException(e); |
c39425655469
8177721: Improve diagnostics in sun.management.Agent#startAgent()
shshahma
parents:
43719
diff
changeset
|
670 |
} |
c39425655469
8177721: Improve diagnostics in sun.management.Agent#startAgent()
shshahma
parents:
43719
diff
changeset
|
671 |
|
2 | 672 |
public static void warning(String key, String message) { |
673 |
System.err.print(getText("agent.err.warning") + ": " + getText(key)); |
|
674 |
System.err.println(": " + message); |
|
675 |
} |
|
676 |
||
677 |
private static void initResource() { |
|
678 |
try { |
|
679 |
messageRB = |
|
43494
11801b2ff456
8173608: Separate JDK management agent from java.management module
mchung
parents:
40743
diff
changeset
|
680 |
ResourceBundle.getBundle("jdk.internal.agent.resources.agent"); |
2 | 681 |
} catch (MissingResourceException e) { |
682 |
throw new Error("Fatal: Resource for management agent is missing"); |
|
683 |
} |
|
684 |
} |
|
685 |
||
686 |
public static String getText(String key) { |
|
687 |
if (messageRB == null) { |
|
688 |
initResource(); |
|
689 |
} |
|
690 |
try { |
|
691 |
return messageRB.getString(key); |
|
692 |
} catch (MissingResourceException e) { |
|
693 |
return "Missing management agent resource bundle: key = \"" + key + "\""; |
|
694 |
} |
|
695 |
} |
|
696 |
||
697 |
public static String getText(String key, String... args) { |
|
698 |
if (messageRB == null) { |
|
699 |
initResource(); |
|
700 |
} |
|
701 |
String format = messageRB.getString(key); |
|
702 |
if (format == null) { |
|
15531
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
703 |
format = "missing resource key: key = \"" + key + "\", " |
071efc9f31ad
8002048: Protocol to discovery of manageable Java processes on a network
dsamersoff
parents:
12694
diff
changeset
|
704 |
+ "arguments = \"{0}\", \"{1}\", \"{2}\""; |
2 | 705 |
} |
706 |
return MessageFormat.format(format, (Object[]) args); |
|
707 |
} |
|
708 |
} |