jdk/src/share/classes/sun/tools/jconsole/LocalVirtualMachine.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 10419 12c063b39232
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.tools.jconsole;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
// Sun specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.attach.VirtualMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.attach.VirtualMachineDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.attach.AgentInitializationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.tools.attach.AgentLoadException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.attach.AttachNotSupportedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
// Sun private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.management.ConnectorAddressLink;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import sun.jvmstat.monitor.HostIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.jvmstat.monitor.Monitor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.jvmstat.monitor.MonitoredHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.jvmstat.monitor.MonitoredVm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.jvmstat.monitor.MonitoredVmUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.jvmstat.monitor.MonitorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.jvmstat.monitor.VmIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class LocalVirtualMachine {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private String address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private String commandLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private String displayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private int vmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private boolean isAttachSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public LocalVirtualMachine(int vmid, String commandLine, boolean canAttach, String connectorAddress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.vmid = vmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.commandLine = commandLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.address = connectorAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.isAttachSupported = canAttach;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        this.displayName = getDisplayName(commandLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private static String getDisplayName(String commandLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // trim the pathname of jar file if it's a jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        String[] res = commandLine.split(" ", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (res[0].endsWith(".jar")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
           File jarfile = new File(res[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
           String displayName = jarfile.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
           if (res.length == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
               displayName += " " + res[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
           return displayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return commandLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public int vmid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        return vmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public boolean isManageable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return (address != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public boolean isAttachable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return isAttachSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public void startManagementAgent() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (address != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            // already started
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (!isAttachable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new IOException("This virtual machine \"" + vmid +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                "\" does not support dynamic attach.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        loadManagementAgent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        // fails to load or start the management agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (address == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            // should never reach here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new IOException("Fails to find connector address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public String connectorAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        // return null if not available or no JMX agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public String displayName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        return displayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return commandLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    // This method returns the list of all virtual machines currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // running on the machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public static Map<Integer, LocalVirtualMachine> getAllVirtualMachines() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        Map<Integer, LocalVirtualMachine> map =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            new HashMap<Integer, LocalVirtualMachine>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        getMonitoredVMs(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        getAttachableVMs(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private static void getMonitoredVMs(Map<Integer, LocalVirtualMachine> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        MonitoredHost host;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        Set vms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            vms = host.activeVms();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        } catch (java.net.URISyntaxException sx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new InternalError(sx.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        } catch (MonitorException mx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throw new InternalError(mx.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        for (Object vmid: vms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            if (vmid instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                int pid = ((Integer) vmid).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                String name = vmid.toString(); // default to pid if name not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                boolean attachable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                String address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                     MonitoredVm mvm = host.getMonitoredVm(new VmIdentifier(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                     // use the command line as the display name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                     name =  MonitoredVmUtil.commandLine(mvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                     attachable = MonitoredVmUtil.isAttachable(mvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                     address = ConnectorAddressLink.importFrom(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                     mvm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                     // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                map.put((Integer) vmid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        new LocalVirtualMachine(pid, name, attachable, address));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private static final String LOCAL_CONNECTOR_ADDRESS_PROP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        "com.sun.management.jmxremote.localConnectorAddress";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    private static void getAttachableVMs(Map<Integer, LocalVirtualMachine> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        List<VirtualMachineDescriptor> vms = VirtualMachine.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        for (VirtualMachineDescriptor vmd : vms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                Integer vmid = Integer.valueOf(vmd.id());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                if (!map.containsKey(vmid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    boolean attachable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    String address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        VirtualMachine vm = VirtualMachine.attach(vmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        attachable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        Properties agentProps = vm.getAgentProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        vm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    } catch (AttachNotSupportedException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        // not attachable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                        // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    map.put(vmid, new LocalVirtualMachine(vmid.intValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                                          vmd.displayName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                                          attachable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                                                          address));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                // do not support vmid different than pid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public static LocalVirtualMachine getLocalVirtualMachine(int vmid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        Map<Integer, LocalVirtualMachine> map = getAllVirtualMachines();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        LocalVirtualMachine lvm = map.get(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (lvm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            // Check if the VM is attachable but not included in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            // if it's running with a different security context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            // For example, Windows services running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            // local SYSTEM account are attachable if you have Adminstrator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            // privileges.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            boolean attachable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            String address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            String name = String.valueOf(vmid); // default display name to pid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                VirtualMachine vm = VirtualMachine.attach(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                attachable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                Properties agentProps = vm.getAgentProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                vm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                lvm = new LocalVirtualMachine(vmid, name, attachable, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            } catch (AttachNotSupportedException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                // not attachable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                if (JConsole.isDebug()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    x.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                if (JConsole.isDebug()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                    x.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        return lvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    // load the management agent into the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private void loadManagementAgent() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        VirtualMachine vm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        String name = String.valueOf(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            vm = VirtualMachine.attach(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        } catch (AttachNotSupportedException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            IOException ioe = new IOException(x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            ioe.initCause(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        String home = vm.getSystemProperties().getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // Normally in ${java.home}/jre/lib/management-agent.jar but might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // be in ${java.home}/lib in build environments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        String agent = home + File.separator + "jre" + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                           "lib" + File.separator + "management-agent.jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        File f = new File(agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (!f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            agent = home + File.separator +  "lib" + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        "management-agent.jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            f = new File(agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if (!f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                throw new IOException("Management agent not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        agent = f.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            vm.loadAgent(agent, "com.sun.management.jmxremote");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        } catch (AgentLoadException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            IOException ioe = new IOException(x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            ioe.initCause(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } catch (AgentInitializationException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            IOException ioe = new IOException(x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            ioe.initCause(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        // get the connector address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        Properties agentProps = vm.getAgentProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        vm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
}