0N/A/*
2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A *
0N/A *
0N/A * A test "management tool" used by unit test LocalManagementTest.sh.
0N/A *
0N/A * Usage: java TestManager <pid> <port>
0N/A *
0N/A * where <pid> is the process-id of the test application, and <port> is
0N/A * TCP port is used to shutdown the application.
0N/A */
0N/Aimport javax.management.MBeanServerConnection;
0N/Aimport javax.management.MBeanServerInvocationHandler;
0N/Aimport javax.management.ObjectName;
0N/Aimport javax.management.remote.JMXServiceURL;
0N/Aimport javax.management.remote.JMXConnectorFactory;
0N/Aimport javax.management.remote.JMXConnector;
0N/Aimport java.lang.management.RuntimeMXBean;
0N/Aimport static java.lang.management.ManagementFactory.*;
0N/Aimport java.net.Socket;
0N/Aimport java.net.InetSocketAddress;
0N/Aimport java.io.File;
0N/Aimport java.io.IOException;
0N/Aimport java.util.Properties;
0N/A
0N/A// Sun specific
0N/Aimport com.sun.tools.attach.VirtualMachine;
0N/A
0N/A// Sun implementation specific
0N/Aimport sun.management.ConnectorAddressLink;
0N/A
0N/Apublic class TestManager {
0N/A
0N/A /*
0N/A * Starts the management agent in the target VM
0N/A */
0N/A private static void startManagementAgent(String pid) throws IOException {
0N/A /*
0N/A * JAR file normally in ${java.home}/jre/lib but may be in ${java.home}/lib
0N/A * with development/non-images builds
0N/A */
0N/A String home = System.getProperty("java.home");
0N/A String agent = home + File.separator + "jre" + File.separator + "lib"
0N/A + File.separator + "management-agent.jar";
0N/A File f = new File(agent);
0N/A if (!f.exists()) {
0N/A agent = home + File.separator + "lib" + File.separator +
0N/A "management-agent.jar";
0N/A f = new File(agent);
0N/A if (!f.exists()) {
0N/A throw new RuntimeException("management-agent.jar missing");
0N/A }
0N/A }
0N/A agent = f.getCanonicalPath();
0N/A
0N/A System.out.println("Loading " + agent + " into target VM ...");
0N/A
0N/A try {
0N/A VirtualMachine.attach(pid).loadAgent(agent);
0N/A } catch (Exception x) {
0N/A throw new IOException(x.getMessage());
0N/A }
0N/A }
0N/A
0N/A private static void connect(String pid, String address) throws Exception {
0N/A if (address == null) {
0N/A throw new RuntimeException("Local connector address for " +
0N/A pid + " is null");
0N/A }
0N/A
0N/A System.out.println("Connect to process " + pid + " via: " + address);
0N/A
0N/A JMXServiceURL url = new JMXServiceURL(address);
0N/A JMXConnector c = JMXConnectorFactory.connect(url);
0N/A MBeanServerConnection server = c.getMBeanServerConnection();
0N/A
0N/A System.out.println("Connected.");
0N/A
0N/A RuntimeMXBean rt = newPlatformMXBeanProxy(server,
0N/A RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
0N/A System.out.println(rt.getName());
0N/A
0N/A // close the connection
0N/A c.close();
0N/A }
0N/A
0N/A
0N/A private final static String LOCAL_CONNECTOR_ADDRESS_PROP =
0N/A "com.sun.management.jmxremote.localConnectorAddress";
0N/A public static void main(String[] args) throws Exception {
0N/A String pid = args[0]; // pid as a string
0N/A VirtualMachine vm = VirtualMachine.attach(pid);
0N/A
0N/A String agentPropLocalConnectorAddress = (String)
0N/A vm.getAgentProperties().get(LOCAL_CONNECTOR_ADDRESS_PROP);
0N/A
0N/A int vmid = Integer.parseInt(pid);
0N/A String jvmstatLocalConnectorAddress =
0N/A ConnectorAddressLink.importFrom(vmid);
0N/A
0N/A if (agentPropLocalConnectorAddress == null &&
0N/A jvmstatLocalConnectorAddress == null) {
0N/A // No JMX Connector address so attach to VM, and load
0N/A // management-agent.jar
0N/A startManagementAgent(pid);
0N/A agentPropLocalConnectorAddress = (String)
0N/A vm.getAgentProperties().get(LOCAL_CONNECTOR_ADDRESS_PROP);
0N/A jvmstatLocalConnectorAddress =
0N/A ConnectorAddressLink.importFrom(vmid);
0N/A }
0N/A
0N/A
0N/A // Test address obtained from agent properties
0N/A System.out.println("Testing the connector address from agent properties");
0N/A connect(pid, agentPropLocalConnectorAddress);
0N/A
0N/A // Test address obtained from jvmstat buffer
0N/A System.out.println("Testing the connector address from jvmstat buffer");
0N/A connect(pid, jvmstatLocalConnectorAddress);
0N/A
0N/A
0N/A // Shutdown application
0N/A int port = Integer.parseInt(args[1]);
0N/A System.out.println("Shutdown process via TCP port: " + port);
0N/A Socket s = new Socket();
0N/A s.connect(new InetSocketAddress(port));
0N/A s.close();
0N/A }
0N/A}