Path: blob/master/test/jdk/sun/management/jmxremote/bootstrap/TestManager.java
41153 views
/*1* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*25*26* A test "management tool" used by unit tests -27* LocalManagementTest.java, CustomLauncherTest.java28*29* Usage: java TestManager <pid> <port>30*31* where <pid> is the process-id of the test application, and <port> is32* TCP port is used to shutdown the application.33*/34import javax.management.MBeanServerConnection;35import javax.management.remote.JMXServiceURL;36import javax.management.remote.JMXConnectorFactory;37import javax.management.remote.JMXConnector;38import java.lang.management.RuntimeMXBean;39import static java.lang.management.ManagementFactory.*;40import java.net.Socket;41import java.net.InetSocketAddress;42import java.io.IOException;4344// Sun specific45import com.sun.tools.attach.VirtualMachine;4647// Sun implementation specific48import jdk.internal.agent.ConnectorAddressLink;4950public class TestManager {5152/*53* Starts the management agent in the target VM54*/55private static void startManagementAgent(String pid) throws IOException {56try {57VirtualMachine.attach(pid).startLocalManagementAgent();58} catch (Exception x) {59throw new IOException(x.getMessage(), x);60}61}6263private static void connect(String pid, String address) throws Exception {64if (address == null) {65throw new RuntimeException("Local connector address for " +66pid + " is null");67}6869System.out.println("Connect to process " + pid + " via: " + address);7071JMXServiceURL url = new JMXServiceURL(address);72JMXConnector c = JMXConnectorFactory.connect(url);73MBeanServerConnection server = c.getMBeanServerConnection();7475System.out.println("Connected.");7677RuntimeMXBean rt = newPlatformMXBeanProxy(server,78RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);79System.out.println(rt.getName());8081// close the connection82c.close();83}848586private final static String LOCAL_CONNECTOR_ADDRESS_PROP =87"com.sun.management.jmxremote.localConnectorAddress";88public static void main(String[] args) throws Exception {89String pid = args[0]; // pid as a string90System.out.println("Starting TestManager for PID = " + pid);91System.out.flush();92VirtualMachine vm = VirtualMachine.attach(pid);9394String agentPropLocalConnectorAddress = (String)95vm.getAgentProperties().get(LOCAL_CONNECTOR_ADDRESS_PROP);9697int vmid = Integer.parseInt(pid);98String jvmstatLocalConnectorAddress =99ConnectorAddressLink.importFrom(vmid);100101if (agentPropLocalConnectorAddress == null &&102jvmstatLocalConnectorAddress == null) {103// No JMX Connector address so attach to VM, and start local agent104startManagementAgent(pid);105agentPropLocalConnectorAddress = (String)106vm.getAgentProperties().get(LOCAL_CONNECTOR_ADDRESS_PROP);107jvmstatLocalConnectorAddress =108ConnectorAddressLink.importFrom(vmid);109}110111112// Test address obtained from agent properties113System.out.println("Testing the connector address from agent properties");114connect(pid, agentPropLocalConnectorAddress);115116// Test address obtained from jvmstat buffer117System.out.println("Testing the connector address from jvmstat buffer");118connect(pid, jvmstatLocalConnectorAddress);119120// Shutdown application121int port = Integer.parseInt(args[1]);122System.out.println("Shutdown process via TCP port: " + port);123Socket s = new Socket();124s.connect(new InetSocketAddress(port));125s.close();126}127}128129130