2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2005, 2006, 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
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/*
|
|
25 |
*
|
|
26 |
*
|
|
27 |
* A test "management tool" used by unit test LocalManagementTest.sh.
|
|
28 |
*
|
|
29 |
* Usage: java TestManager <pid> <port>
|
|
30 |
*
|
|
31 |
* where <pid> is the process-id of the test application, and <port> is
|
|
32 |
* TCP port is used to shutdown the application.
|
|
33 |
*/
|
|
34 |
import javax.management.MBeanServerConnection;
|
|
35 |
import javax.management.MBeanServerInvocationHandler;
|
|
36 |
import javax.management.ObjectName;
|
|
37 |
import javax.management.remote.JMXServiceURL;
|
|
38 |
import javax.management.remote.JMXConnectorFactory;
|
|
39 |
import javax.management.remote.JMXConnector;
|
|
40 |
import java.lang.management.RuntimeMXBean;
|
|
41 |
import static java.lang.management.ManagementFactory.*;
|
|
42 |
import java.net.Socket;
|
|
43 |
import java.net.InetSocketAddress;
|
|
44 |
import java.io.File;
|
|
45 |
import java.io.IOException;
|
|
46 |
import java.util.Properties;
|
|
47 |
|
|
48 |
// Sun specific
|
|
49 |
import com.sun.tools.attach.VirtualMachine;
|
|
50 |
|
|
51 |
// Sun implementation specific
|
|
52 |
import sun.management.ConnectorAddressLink;
|
|
53 |
|
|
54 |
public class TestManager {
|
|
55 |
|
|
56 |
/*
|
|
57 |
* Starts the management agent in the target VM
|
|
58 |
*/
|
|
59 |
private static void startManagementAgent(String pid) throws IOException {
|
|
60 |
/*
|
|
61 |
* JAR file normally in ${java.home}/jre/lib but may be in ${java.home}/lib
|
|
62 |
* with development/non-images builds
|
|
63 |
*/
|
|
64 |
String home = System.getProperty("java.home");
|
|
65 |
String agent = home + File.separator + "jre" + File.separator + "lib"
|
|
66 |
+ File.separator + "management-agent.jar";
|
|
67 |
File f = new File(agent);
|
|
68 |
if (!f.exists()) {
|
|
69 |
agent = home + File.separator + "lib" + File.separator +
|
|
70 |
"management-agent.jar";
|
|
71 |
f = new File(agent);
|
|
72 |
if (!f.exists()) {
|
|
73 |
throw new RuntimeException("management-agent.jar missing");
|
|
74 |
}
|
|
75 |
}
|
|
76 |
agent = f.getCanonicalPath();
|
|
77 |
|
|
78 |
System.out.println("Loading " + agent + " into target VM ...");
|
|
79 |
|
|
80 |
try {
|
|
81 |
VirtualMachine.attach(pid).loadAgent(agent);
|
|
82 |
} catch (Exception x) {
|
|
83 |
throw new IOException(x.getMessage());
|
|
84 |
}
|
|
85 |
}
|
|
86 |
|
|
87 |
private static void connect(String pid, String address) throws Exception {
|
|
88 |
if (address == null) {
|
|
89 |
throw new RuntimeException("Local connector address for " +
|
|
90 |
pid + " is null");
|
|
91 |
}
|
|
92 |
|
|
93 |
System.out.println("Connect to process " + pid + " via: " + address);
|
|
94 |
|
|
95 |
JMXServiceURL url = new JMXServiceURL(address);
|
|
96 |
JMXConnector c = JMXConnectorFactory.connect(url);
|
|
97 |
MBeanServerConnection server = c.getMBeanServerConnection();
|
|
98 |
|
|
99 |
System.out.println("Connected.");
|
|
100 |
|
|
101 |
RuntimeMXBean rt = newPlatformMXBeanProxy(server,
|
|
102 |
RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
|
|
103 |
System.out.println(rt.getName());
|
|
104 |
|
|
105 |
// close the connection
|
|
106 |
c.close();
|
|
107 |
}
|
|
108 |
|
|
109 |
|
|
110 |
private final static String LOCAL_CONNECTOR_ADDRESS_PROP =
|
|
111 |
"com.sun.management.jmxremote.localConnectorAddress";
|
|
112 |
public static void main(String[] args) throws Exception {
|
|
113 |
String pid = args[0]; // pid as a string
|
|
114 |
VirtualMachine vm = VirtualMachine.attach(pid);
|
|
115 |
|
|
116 |
String agentPropLocalConnectorAddress = (String)
|
|
117 |
vm.getAgentProperties().get(LOCAL_CONNECTOR_ADDRESS_PROP);
|
|
118 |
|
|
119 |
int vmid = Integer.parseInt(pid);
|
|
120 |
String jvmstatLocalConnectorAddress =
|
|
121 |
ConnectorAddressLink.importFrom(vmid);
|
|
122 |
|
|
123 |
if (agentPropLocalConnectorAddress == null &&
|
|
124 |
jvmstatLocalConnectorAddress == null) {
|
|
125 |
// No JMX Connector address so attach to VM, and load
|
|
126 |
// management-agent.jar
|
|
127 |
startManagementAgent(pid);
|
|
128 |
agentPropLocalConnectorAddress = (String)
|
|
129 |
vm.getAgentProperties().get(LOCAL_CONNECTOR_ADDRESS_PROP);
|
|
130 |
jvmstatLocalConnectorAddress =
|
|
131 |
ConnectorAddressLink.importFrom(vmid);
|
|
132 |
}
|
|
133 |
|
|
134 |
|
|
135 |
// Test address obtained from agent properties
|
|
136 |
System.out.println("Testing the connector address from agent properties");
|
|
137 |
connect(pid, agentPropLocalConnectorAddress);
|
|
138 |
|
|
139 |
// Test address obtained from jvmstat buffer
|
|
140 |
System.out.println("Testing the connector address from jvmstat buffer");
|
|
141 |
connect(pid, jvmstatLocalConnectorAddress);
|
|
142 |
|
|
143 |
|
|
144 |
// Shutdown application
|
|
145 |
int port = Integer.parseInt(args[1]);
|
|
146 |
System.out.println("Shutdown process via TCP port: " + port);
|
|
147 |
Socket s = new Socket();
|
|
148 |
s.connect(new InetSocketAddress(port));
|
|
149 |
s.close();
|
|
150 |
}
|
|
151 |
}
|