22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.tools.jconsole;
27
28 import java.util.*;
29 import java.io.IOException;
30 import java.io.File;
31
32 // Sun specific
33 import com.sun.tools.attach.VirtualMachine;
34 import com.sun.tools.attach.VirtualMachineDescriptor;
35 import com.sun.tools.attach.AgentInitializationException;
36 import com.sun.tools.attach.AgentLoadException;
37 import com.sun.tools.attach.AttachNotSupportedException;
38
39 // Sun private
40 import sun.management.ConnectorAddressLink;
41 import sun.jvmstat.monitor.HostIdentifier;
42 import sun.jvmstat.monitor.Monitor;
43 import sun.jvmstat.monitor.MonitoredHost;
44 import sun.jvmstat.monitor.MonitoredVm;
45 import sun.jvmstat.monitor.MonitoredVmUtil;
46 import sun.jvmstat.monitor.MonitorException;
47 import sun.jvmstat.monitor.VmIdentifier;
48
49 public class LocalVirtualMachine {
50 private String address;
51 private String commandLine;
52 private String displayName;
53 private int vmid;
54 private boolean isAttachSupported;
55
56 public LocalVirtualMachine(int vmid, String commandLine, boolean canAttach, String connectorAddress) {
57 this.vmid = vmid;
58 this.commandLine = commandLine;
59 this.address = connectorAddress;
60 this.isAttachSupported = canAttach;
61 this.displayName = getDisplayName(commandLine);
62 }
114 public String displayName() {
115 return displayName;
116 }
117
118 public String toString() {
119 return commandLine;
120 }
121
122 // This method returns the list of all virtual machines currently
123 // running on the machine
124 public static Map<Integer, LocalVirtualMachine> getAllVirtualMachines() {
125 Map<Integer, LocalVirtualMachine> map =
126 new HashMap<Integer, LocalVirtualMachine>();
127 getMonitoredVMs(map);
128 getAttachableVMs(map);
129 return map;
130 }
131
132 private static void getMonitoredVMs(Map<Integer, LocalVirtualMachine> map) {
133 MonitoredHost host;
134 Set vms;
135 try {
136 host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null));
137 vms = host.activeVms();
138 } catch (java.net.URISyntaxException | MonitorException x) {
139 throw new InternalError(x.getMessage(), x);
140 }
141 for (Object vmid: vms) {
142 if (vmid instanceof Integer) {
143 int pid = ((Integer) vmid).intValue();
144 String name = vmid.toString(); // default to pid if name not available
145 boolean attachable = false;
146 String address = null;
147 try {
148 MonitoredVm mvm = host.getMonitoredVm(new VmIdentifier(name));
149 // use the command line as the display name
150 name = MonitoredVmUtil.commandLine(mvm);
151 attachable = MonitoredVmUtil.isAttachable(mvm);
152 address = ConnectorAddressLink.importFrom(pid);
153 mvm.detach();
154 } catch (Exception x) {
|
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.tools.jconsole;
27
28 import java.util.*;
29 import java.io.IOException;
30 import java.io.File;
31
32 // Sun specific
33 import com.sun.tools.attach.VirtualMachine;
34 import com.sun.tools.attach.VirtualMachineDescriptor;
35 import com.sun.tools.attach.AgentInitializationException;
36 import com.sun.tools.attach.AgentLoadException;
37 import com.sun.tools.attach.AttachNotSupportedException;
38
39 // Sun private
40 import sun.management.ConnectorAddressLink;
41 import sun.jvmstat.monitor.HostIdentifier;
42 import sun.jvmstat.monitor.MonitoredHost;
43 import sun.jvmstat.monitor.MonitoredVm;
44 import sun.jvmstat.monitor.MonitoredVmUtil;
45 import sun.jvmstat.monitor.MonitorException;
46 import sun.jvmstat.monitor.VmIdentifier;
47
48 public class LocalVirtualMachine {
49 private String address;
50 private String commandLine;
51 private String displayName;
52 private int vmid;
53 private boolean isAttachSupported;
54
55 public LocalVirtualMachine(int vmid, String commandLine, boolean canAttach, String connectorAddress) {
56 this.vmid = vmid;
57 this.commandLine = commandLine;
58 this.address = connectorAddress;
59 this.isAttachSupported = canAttach;
60 this.displayName = getDisplayName(commandLine);
61 }
113 public String displayName() {
114 return displayName;
115 }
116
117 public String toString() {
118 return commandLine;
119 }
120
121 // This method returns the list of all virtual machines currently
122 // running on the machine
123 public static Map<Integer, LocalVirtualMachine> getAllVirtualMachines() {
124 Map<Integer, LocalVirtualMachine> map =
125 new HashMap<Integer, LocalVirtualMachine>();
126 getMonitoredVMs(map);
127 getAttachableVMs(map);
128 return map;
129 }
130
131 private static void getMonitoredVMs(Map<Integer, LocalVirtualMachine> map) {
132 MonitoredHost host;
133 Set<Integer> vms;
134 try {
135 host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null));
136 vms = host.activeVms();
137 } catch (java.net.URISyntaxException | MonitorException x) {
138 throw new InternalError(x.getMessage(), x);
139 }
140 for (Object vmid: vms) {
141 if (vmid instanceof Integer) {
142 int pid = ((Integer) vmid).intValue();
143 String name = vmid.toString(); // default to pid if name not available
144 boolean attachable = false;
145 String address = null;
146 try {
147 MonitoredVm mvm = host.getMonitoredVm(new VmIdentifier(name));
148 // use the command line as the display name
149 name = MonitoredVmUtil.commandLine(mvm);
150 attachable = MonitoredVmUtil.isAttachable(mvm);
151 address = ConnectorAddressLink.importFrom(pid);
152 mvm.detach();
153 } catch (Exception x) {
|