jdk/src/share/classes/sun/tools/jconsole/LocalVirtualMachine.java
author alanb
Fri, 02 Nov 2012 15:50:11 +0000
changeset 14342 8435a30053c1
parent 12851 3334e1c781d0
child 24870 5d567113d043
permissions -rw-r--r--
7197491: update copyright year to match last edit in jdk8 jdk repository Reviewed-by: chegar, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12851
diff changeset
     2
 * Copyright (c) 2005, 2012, 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.MonitoredHost;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.jvmstat.monitor.MonitoredVm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.jvmstat.monitor.MonitoredVmUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.jvmstat.monitor.MonitorException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.jvmstat.monitor.VmIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public class LocalVirtualMachine {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private String address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private String commandLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private String displayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int vmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private boolean isAttachSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public LocalVirtualMachine(int vmid, String commandLine, boolean canAttach, String connectorAddress) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        this.vmid = vmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        this.commandLine = commandLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        this.address = connectorAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        this.isAttachSupported = canAttach;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        this.displayName = getDisplayName(commandLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static String getDisplayName(String commandLine) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        // trim the pathname of jar file if it's a jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        String[] res = commandLine.split(" ", 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (res[0].endsWith(".jar")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
           File jarfile = new File(res[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
           String displayName = jarfile.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
           if (res.length == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
               displayName += " " + res[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
           return displayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        return commandLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public int vmid() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        return vmid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public boolean isManageable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return (address != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public boolean isAttachable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        return isAttachSupported;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public void startManagementAgent() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (address != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            // already started
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (!isAttachable()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new IOException("This virtual machine \"" + vmid +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                "\" does not support dynamic attach.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        loadManagementAgent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // fails to load or start the management agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (address == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            // should never reach here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new IOException("Fails to find connector address");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public String connectorAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // return null if not available or no JMX agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public String displayName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        return displayName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return commandLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    // This method returns the list of all virtual machines currently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    // running on the machine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static Map<Integer, LocalVirtualMachine> getAllVirtualMachines() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        Map<Integer, LocalVirtualMachine> map =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            new HashMap<Integer, LocalVirtualMachine>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        getMonitoredVMs(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        getAttachableVMs(map);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    private static void getMonitoredVMs(Map<Integer, LocalVirtualMachine> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        MonitoredHost host;
12851
3334e1c781d0 7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents: 10419
diff changeset
   133
        Set<Integer> vms;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            vms = host.activeVms();
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   137
        } catch (java.net.URISyntaxException | MonitorException x) {
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   138
            throw new InternalError(x.getMessage(), x);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for (Object vmid: vms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            if (vmid instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                int pid = ((Integer) vmid).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                String name = vmid.toString(); // default to pid if name not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                boolean attachable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                String address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                     MonitoredVm mvm = host.getMonitoredVm(new VmIdentifier(name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                     // use the command line as the display name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                     name =  MonitoredVmUtil.commandLine(mvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                     attachable = MonitoredVmUtil.isAttachable(mvm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                     address = ConnectorAddressLink.importFrom(pid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                     mvm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                     // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                map.put((Integer) vmid,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        new LocalVirtualMachine(pid, name, attachable, address));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
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
    private static final String LOCAL_CONNECTOR_ADDRESS_PROP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        "com.sun.management.jmxremote.localConnectorAddress";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private static void getAttachableVMs(Map<Integer, LocalVirtualMachine> map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        List<VirtualMachineDescriptor> vms = VirtualMachine.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        for (VirtualMachineDescriptor vmd : vms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                Integer vmid = Integer.valueOf(vmd.id());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                if (!map.containsKey(vmid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    boolean attachable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    String address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        VirtualMachine vm = VirtualMachine.attach(vmd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        attachable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        Properties agentProps = vm.getAgentProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        vm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    } catch (AttachNotSupportedException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        // not attachable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    map.put(vmid, new LocalVirtualMachine(vmid.intValue(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                                                          vmd.displayName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                                          attachable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                                                          address));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                // do not support vmid different than pid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            }
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
    public static LocalVirtualMachine getLocalVirtualMachine(int vmid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        Map<Integer, LocalVirtualMachine> map = getAllVirtualMachines();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        LocalVirtualMachine lvm = map.get(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (lvm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            // Check if the VM is attachable but not included in the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            // if it's running with a different security context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            // For example, Windows services running
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            // local SYSTEM account are attachable if you have Adminstrator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            // privileges.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            boolean attachable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            String address = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            String name = String.valueOf(vmid); // default display name to pid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                VirtualMachine vm = VirtualMachine.attach(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                attachable = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                Properties agentProps = vm.getAgentProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                vm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                lvm = new LocalVirtualMachine(vmid, name, attachable, address);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            } catch (AttachNotSupportedException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                // not attachable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                if (JConsole.isDebug()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    x.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            } catch (IOException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if (JConsole.isDebug()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    x.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        return lvm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    // load the management agent into the target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    private void loadManagementAgent() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        VirtualMachine vm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        String name = String.valueOf(vmid);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            vm = VirtualMachine.attach(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } catch (AttachNotSupportedException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            IOException ioe = new IOException(x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            ioe.initCause(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        String home = vm.getSystemProperties().getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        // Normally in ${java.home}/jre/lib/management-agent.jar but might
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        // be in ${java.home}/lib in build environments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        String agent = home + File.separator + "jre" + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                           "lib" + File.separator + "management-agent.jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        File f = new File(agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (!f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            agent = home + File.separator +  "lib" + File.separator +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                        "management-agent.jar";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            f = new File(agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (!f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                throw new IOException("Management agent not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        agent = f.getCanonicalPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            vm.loadAgent(agent, "com.sun.management.jmxremote");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        } catch (AgentLoadException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            IOException ioe = new IOException(x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            ioe.initCause(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        } catch (AgentInitializationException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            IOException ioe = new IOException(x.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            ioe.initCause(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            throw ioe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // get the connector address
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        Properties agentProps = vm.getAgentProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        vm.detach();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
}