jdk/src/share/classes/sun/tools/jconsole/LocalVirtualMachine.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 5506 202f599c92aa
child 12851 3334e1c781d0
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
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();
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   138
        } catch (java.net.URISyntaxException | MonitorException x) {
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   139
            throw new InternalError(x.getMessage(), x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        for (Object vmid: vms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            if (vmid instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                int pid = ((Integer) vmid).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                String name = vmid.toString(); // default to pid if name not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                boolean attachable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                String address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                     MonitoredVm mvm = host.getMonitoredVm(new VmIdentifier(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                     // use the command line as the display name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                     name =  MonitoredVmUtil.commandLine(mvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                     attachable = MonitoredVmUtil.isAttachable(mvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                     address = ConnectorAddressLink.importFrom(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                     mvm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                     // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                map.put((Integer) vmid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        new LocalVirtualMachine(pid, name, attachable, address));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private static final String LOCAL_CONNECTOR_ADDRESS_PROP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        "com.sun.management.jmxremote.localConnectorAddress";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    private static void getAttachableVMs(Map<Integer, LocalVirtualMachine> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        List<VirtualMachineDescriptor> vms = VirtualMachine.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        for (VirtualMachineDescriptor vmd : vms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                Integer vmid = Integer.valueOf(vmd.id());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                if (!map.containsKey(vmid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    boolean attachable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    String address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        VirtualMachine vm = VirtualMachine.attach(vmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        attachable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        Properties agentProps = vm.getAgentProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        vm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    } catch (AttachNotSupportedException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        // not attachable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                    map.put(vmid, new LocalVirtualMachine(vmid.intValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                                          vmd.displayName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                                          attachable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                                                          address));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                // do not support vmid different than pid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public static LocalVirtualMachine getLocalVirtualMachine(int vmid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        Map<Integer, LocalVirtualMachine> map = getAllVirtualMachines();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        LocalVirtualMachine lvm = map.get(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (lvm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // Check if the VM is attachable but not included in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // if it's running with a different security context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            // For example, Windows services running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            // local SYSTEM account are attachable if you have Adminstrator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            // privileges.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            boolean attachable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            String address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            String name = String.valueOf(vmid); // default display name to pid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                VirtualMachine vm = VirtualMachine.attach(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                attachable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                Properties agentProps = vm.getAgentProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                vm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                lvm = new LocalVirtualMachine(vmid, name, attachable, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            } catch (AttachNotSupportedException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                // not attachable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                if (JConsole.isDebug()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                    x.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                if (JConsole.isDebug()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                    x.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return lvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    // load the management agent into the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private void loadManagementAgent() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        VirtualMachine vm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        String name = String.valueOf(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            vm = VirtualMachine.attach(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        } catch (AttachNotSupportedException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            IOException ioe = new IOException(x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            ioe.initCause(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        String home = vm.getSystemProperties().getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // Normally in ${java.home}/jre/lib/management-agent.jar but might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        // be in ${java.home}/lib in build environments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        String agent = home + File.separator + "jre" + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                           "lib" + File.separator + "management-agent.jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        File f = new File(agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        if (!f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            agent = home + File.separator +  "lib" + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                        "management-agent.jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            f = new File(agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            if (!f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                throw new IOException("Management agent not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        agent = f.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            vm.loadAgent(agent, "com.sun.management.jmxremote");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        } catch (AgentLoadException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            IOException ioe = new IOException(x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            ioe.initCause(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        } catch (AgentInitializationException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            IOException ioe = new IOException(x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            ioe.initCause(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        // get the connector address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        Properties agentProps = vm.getAgentProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        vm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
}