author | sla |
Wed, 11 Jun 2014 15:47:53 +0200 | |
changeset 24870 | 5d567113d043 |
parent 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
24870
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
14342
diff
changeset
|
2 |
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 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.AttachNotSupportedException; |
|
36 |
||
37 |
// Sun private |
|
38 |
import sun.management.ConnectorAddressLink; |
|
39 |
import sun.jvmstat.monitor.HostIdentifier; |
|
40 |
import sun.jvmstat.monitor.MonitoredHost; |
|
41 |
import sun.jvmstat.monitor.MonitoredVm; |
|
42 |
import sun.jvmstat.monitor.MonitoredVmUtil; |
|
43 |
import sun.jvmstat.monitor.MonitorException; |
|
44 |
import sun.jvmstat.monitor.VmIdentifier; |
|
45 |
||
46 |
public class LocalVirtualMachine { |
|
47 |
private String address; |
|
48 |
private String commandLine; |
|
49 |
private String displayName; |
|
50 |
private int vmid; |
|
51 |
private boolean isAttachSupported; |
|
52 |
||
53 |
public LocalVirtualMachine(int vmid, String commandLine, boolean canAttach, String connectorAddress) { |
|
54 |
this.vmid = vmid; |
|
55 |
this.commandLine = commandLine; |
|
56 |
this.address = connectorAddress; |
|
57 |
this.isAttachSupported = canAttach; |
|
58 |
this.displayName = getDisplayName(commandLine); |
|
59 |
} |
|
60 |
||
61 |
private static String getDisplayName(String commandLine) { |
|
62 |
// trim the pathname of jar file if it's a jar |
|
63 |
String[] res = commandLine.split(" ", 2); |
|
64 |
if (res[0].endsWith(".jar")) { |
|
65 |
File jarfile = new File(res[0]); |
|
66 |
String displayName = jarfile.getName(); |
|
67 |
if (res.length == 2) { |
|
68 |
displayName += " " + res[1]; |
|
69 |
} |
|
70 |
return displayName; |
|
71 |
} |
|
72 |
return commandLine; |
|
73 |
} |
|
74 |
||
75 |
public int vmid() { |
|
76 |
return vmid; |
|
77 |
} |
|
78 |
||
79 |
public boolean isManageable() { |
|
80 |
return (address != null); |
|
81 |
} |
|
82 |
||
83 |
public boolean isAttachable() { |
|
84 |
return isAttachSupported; |
|
85 |
} |
|
86 |
||
87 |
public void startManagementAgent() throws IOException { |
|
88 |
if (address != null) { |
|
89 |
// already started |
|
90 |
return; |
|
91 |
} |
|
92 |
||
93 |
if (!isAttachable()) { |
|
94 |
throw new IOException("This virtual machine \"" + vmid + |
|
95 |
"\" does not support dynamic attach."); |
|
96 |
} |
|
97 |
||
98 |
loadManagementAgent(); |
|
99 |
// fails to load or start the management agent |
|
100 |
if (address == null) { |
|
101 |
// should never reach here |
|
102 |
throw new IOException("Fails to find connector address"); |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
public String connectorAddress() { |
|
107 |
// return null if not available or no JMX agent |
|
108 |
return address; |
|
109 |
} |
|
110 |
||
111 |
public String displayName() { |
|
112 |
return displayName; |
|
113 |
} |
|
114 |
||
115 |
public String toString() { |
|
116 |
return commandLine; |
|
117 |
} |
|
118 |
||
119 |
// This method returns the list of all virtual machines currently |
|
120 |
// running on the machine |
|
121 |
public static Map<Integer, LocalVirtualMachine> getAllVirtualMachines() { |
|
122 |
Map<Integer, LocalVirtualMachine> map = |
|
123 |
new HashMap<Integer, LocalVirtualMachine>(); |
|
124 |
getMonitoredVMs(map); |
|
125 |
getAttachableVMs(map); |
|
126 |
return map; |
|
127 |
} |
|
128 |
||
129 |
private static void getMonitoredVMs(Map<Integer, LocalVirtualMachine> map) { |
|
130 |
MonitoredHost host; |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
10419
diff
changeset
|
131 |
Set<Integer> vms; |
2 | 132 |
try { |
133 |
host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null)); |
|
134 |
vms = host.activeVms(); |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
5506
diff
changeset
|
135 |
} catch (java.net.URISyntaxException | MonitorException x) { |
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
5506
diff
changeset
|
136 |
throw new InternalError(x.getMessage(), x); |
2 | 137 |
} |
138 |
for (Object vmid: vms) { |
|
139 |
if (vmid instanceof Integer) { |
|
140 |
int pid = ((Integer) vmid).intValue(); |
|
141 |
String name = vmid.toString(); // default to pid if name not available |
|
142 |
boolean attachable = false; |
|
143 |
String address = null; |
|
144 |
try { |
|
145 |
MonitoredVm mvm = host.getMonitoredVm(new VmIdentifier(name)); |
|
146 |
// use the command line as the display name |
|
147 |
name = MonitoredVmUtil.commandLine(mvm); |
|
148 |
attachable = MonitoredVmUtil.isAttachable(mvm); |
|
149 |
address = ConnectorAddressLink.importFrom(pid); |
|
150 |
mvm.detach(); |
|
151 |
} catch (Exception x) { |
|
152 |
// ignore |
|
153 |
} |
|
154 |
map.put((Integer) vmid, |
|
155 |
new LocalVirtualMachine(pid, name, attachable, address)); |
|
156 |
} |
|
157 |
} |
|
158 |
} |
|
159 |
||
160 |
private static final String LOCAL_CONNECTOR_ADDRESS_PROP = |
|
161 |
"com.sun.management.jmxremote.localConnectorAddress"; |
|
162 |
||
163 |
private static void getAttachableVMs(Map<Integer, LocalVirtualMachine> map) { |
|
164 |
List<VirtualMachineDescriptor> vms = VirtualMachine.list(); |
|
165 |
for (VirtualMachineDescriptor vmd : vms) { |
|
166 |
try { |
|
167 |
Integer vmid = Integer.valueOf(vmd.id()); |
|
168 |
if (!map.containsKey(vmid)) { |
|
169 |
boolean attachable = false; |
|
170 |
String address = null; |
|
171 |
try { |
|
172 |
VirtualMachine vm = VirtualMachine.attach(vmd); |
|
173 |
attachable = true; |
|
174 |
Properties agentProps = vm.getAgentProperties(); |
|
175 |
address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); |
|
176 |
vm.detach(); |
|
177 |
} catch (AttachNotSupportedException x) { |
|
178 |
// not attachable |
|
179 |
} catch (IOException x) { |
|
180 |
// ignore |
|
181 |
} |
|
182 |
map.put(vmid, new LocalVirtualMachine(vmid.intValue(), |
|
183 |
vmd.displayName(), |
|
184 |
attachable, |
|
185 |
address)); |
|
186 |
} |
|
187 |
} catch (NumberFormatException e) { |
|
188 |
// do not support vmid different than pid |
|
189 |
} |
|
190 |
} |
|
191 |
} |
|
192 |
||
193 |
public static LocalVirtualMachine getLocalVirtualMachine(int vmid) { |
|
194 |
Map<Integer, LocalVirtualMachine> map = getAllVirtualMachines(); |
|
195 |
LocalVirtualMachine lvm = map.get(vmid); |
|
196 |
if (lvm == null) { |
|
197 |
// Check if the VM is attachable but not included in the list |
|
198 |
// if it's running with a different security context. |
|
199 |
// For example, Windows services running |
|
200 |
// local SYSTEM account are attachable if you have Adminstrator |
|
201 |
// privileges. |
|
202 |
boolean attachable = false; |
|
203 |
String address = null; |
|
204 |
String name = String.valueOf(vmid); // default display name to pid |
|
205 |
try { |
|
206 |
VirtualMachine vm = VirtualMachine.attach(name); |
|
207 |
attachable = true; |
|
208 |
Properties agentProps = vm.getAgentProperties(); |
|
209 |
address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); |
|
210 |
vm.detach(); |
|
211 |
lvm = new LocalVirtualMachine(vmid, name, attachable, address); |
|
212 |
} catch (AttachNotSupportedException x) { |
|
213 |
// not attachable |
|
214 |
if (JConsole.isDebug()) { |
|
215 |
x.printStackTrace(); |
|
216 |
} |
|
217 |
} catch (IOException x) { |
|
218 |
// ignore |
|
219 |
if (JConsole.isDebug()) { |
|
220 |
x.printStackTrace(); |
|
221 |
} |
|
222 |
} |
|
223 |
} |
|
224 |
return lvm; |
|
225 |
} |
|
226 |
||
227 |
// load the management agent into the target VM |
|
228 |
private void loadManagementAgent() throws IOException { |
|
229 |
VirtualMachine vm = null; |
|
230 |
String name = String.valueOf(vmid); |
|
231 |
try { |
|
232 |
vm = VirtualMachine.attach(name); |
|
233 |
} catch (AttachNotSupportedException x) { |
|
234 |
IOException ioe = new IOException(x.getMessage()); |
|
235 |
ioe.initCause(x); |
|
236 |
throw ioe; |
|
237 |
} |
|
238 |
||
24870
5d567113d043
8044135: Add API to start JMX agent from attach framework
sla
parents:
14342
diff
changeset
|
239 |
vm.startLocalManagementAgent(); |
2 | 240 |
|
241 |
// get the connector address |
|
242 |
Properties agentProps = vm.getAgentProperties(); |
|
243 |
address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); |
|
244 |
||
245 |
vm.detach(); |
|
246 |
} |
|
247 |
} |