1 /*
   2  * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
   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  *
  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.
  22  */
  23 
  24 /*
  25  *
  26  *
  27  * A test "management tool" used by unit tests -
  28  *   LocalManagementTest.java, CustomLauncherTest.java
  29  *
  30  * Usage:    java TestManager <pid> <port>
  31  *
  32  * where <pid> is the process-id of the test application, and <port> is
  33  * TCP port is used to shutdown the application.
  34  */
  35 import javax.management.MBeanServerConnection;
  36 import javax.management.remote.JMXServiceURL;
  37 import javax.management.remote.JMXConnectorFactory;
  38 import javax.management.remote.JMXConnector;
  39 import java.lang.management.RuntimeMXBean;
  40 import static java.lang.management.ManagementFactory.*;
  41 import java.net.Socket;
  42 import java.net.InetSocketAddress;
  43 import java.io.IOException;
  44 
  45 // Sun specific
  46 import com.sun.tools.attach.VirtualMachine;
  47 
  48 // Sun implementation specific
  49 import jdk.internal.agent.ConnectorAddressLink;
  50 
  51 public class TestManager {
  52 
  53     /*
  54      * Starts the management agent in the target VM
  55      */
  56     private static void startManagementAgent(String pid) throws IOException {
  57         try {
  58             VirtualMachine.attach(pid).startLocalManagementAgent();
  59         } catch (Exception x) {
  60             throw new IOException(x.getMessage(), x);
  61         }
  62     }
  63 
  64     private static void connect(String pid, String address) throws Exception {
  65         if (address == null) {
  66             throw new RuntimeException("Local connector address for " +
  67                                        pid + " is null");
  68         }
  69 
  70         System.out.println("Connect to process " + pid + " via: " + address);
  71 
  72         JMXServiceURL url = new JMXServiceURL(address);
  73         JMXConnector c = JMXConnectorFactory.connect(url);
  74         MBeanServerConnection server = c.getMBeanServerConnection();
  75 
  76         System.out.println("Connected.");
  77 
  78         RuntimeMXBean rt = newPlatformMXBeanProxy(server,
  79             RUNTIME_MXBEAN_NAME, RuntimeMXBean.class);
  80         System.out.println(rt.getName());
  81 
  82         // close the connection
  83         c.close();
  84     }
  85 
  86 
  87     private final static String LOCAL_CONNECTOR_ADDRESS_PROP =
  88         "com.sun.management.jmxremote.localConnectorAddress";
  89     public static void main(String[] args) throws Exception {
  90         String pid = args[0]; // pid as a string
  91         System.out.println("Starting TestManager for PID = " + pid);
  92         System.out.flush();
  93         VirtualMachine vm = VirtualMachine.attach(pid);
  94 
  95         String agentPropLocalConnectorAddress = (String)
  96             vm.getAgentProperties().get(LOCAL_CONNECTOR_ADDRESS_PROP);
  97 
  98         int vmid = Integer.parseInt(pid);
  99         String jvmstatLocalConnectorAddress =
 100             ConnectorAddressLink.importFrom(vmid);
 101 
 102         if (agentPropLocalConnectorAddress == null &&
 103             jvmstatLocalConnectorAddress == null) {
 104             // No JMX Connector address so attach to VM, and start local agent
 105             startManagementAgent(pid);
 106             agentPropLocalConnectorAddress = (String)
 107                 vm.getAgentProperties().get(LOCAL_CONNECTOR_ADDRESS_PROP);
 108             jvmstatLocalConnectorAddress =
 109                 ConnectorAddressLink.importFrom(vmid);
 110         }
 111 
 112 
 113         // Test address obtained from agent properties
 114         System.out.println("Testing the connector address from agent properties");
 115         connect(pid, agentPropLocalConnectorAddress);
 116 
 117         // Test address obtained from jvmstat buffer
 118         System.out.println("Testing the connector address from jvmstat buffer");
 119         connect(pid, jvmstatLocalConnectorAddress);
 120 
 121         // Shutdown application
 122         int port = Integer.parseInt(args[1]);
 123         System.out.println("Shutdown process via TCP port: " + port);
 124         Socket s = new Socket();
 125         s.connect(new InetSocketAddress(port));
 126         s.close();
 127     }
 128 }