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