author | egahlin |
Fri, 25 May 2012 12:24:57 +0200 | |
changeset 12851 | 3334e1c781d0 |
parent 10419 | 12c063b39232 |
child 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2005, 2006, 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.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 |
} |
|
62 |
||
63 |
private static String getDisplayName(String commandLine) { |
|
64 |
// trim the pathname of jar file if it's a jar |
|
65 |
String[] res = commandLine.split(" ", 2); |
|
66 |
if (res[0].endsWith(".jar")) { |
|
67 |
File jarfile = new File(res[0]); |
|
68 |
String displayName = jarfile.getName(); |
|
69 |
if (res.length == 2) { |
|
70 |
displayName += " " + res[1]; |
|
71 |
} |
|
72 |
return displayName; |
|
73 |
} |
|
74 |
return commandLine; |
|
75 |
} |
|
76 |
||
77 |
public int vmid() { |
|
78 |
return vmid; |
|
79 |
} |
|
80 |
||
81 |
public boolean isManageable() { |
|
82 |
return (address != null); |
|
83 |
} |
|
84 |
||
85 |
public boolean isAttachable() { |
|
86 |
return isAttachSupported; |
|
87 |
} |
|
88 |
||
89 |
public void startManagementAgent() throws IOException { |
|
90 |
if (address != null) { |
|
91 |
// already started |
|
92 |
return; |
|
93 |
} |
|
94 |
||
95 |
if (!isAttachable()) { |
|
96 |
throw new IOException("This virtual machine \"" + vmid + |
|
97 |
"\" does not support dynamic attach."); |
|
98 |
} |
|
99 |
||
100 |
loadManagementAgent(); |
|
101 |
// fails to load or start the management agent |
|
102 |
if (address == null) { |
|
103 |
// should never reach here |
|
104 |
throw new IOException("Fails to find connector address"); |
|
105 |
} |
|
106 |
} |
|
107 |
||
108 |
public String connectorAddress() { |
|
109 |
// return null if not available or no JMX agent |
|
110 |
return address; |
|
111 |
} |
|
112 |
||
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; |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
10419
diff
changeset
|
133 |
Set<Integer> vms; |
2 | 134 |
try { |
135 |
host = MonitoredHost.getMonitoredHost(new HostIdentifier((String)null)); |
|
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 | 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) { |
|
154 |
// ignore |
|
155 |
} |
|
156 |
map.put((Integer) vmid, |
|
157 |
new LocalVirtualMachine(pid, name, attachable, address)); |
|
158 |
} |
|
159 |
} |
|
160 |
} |
|
161 |
||
162 |
private static final String LOCAL_CONNECTOR_ADDRESS_PROP = |
|
163 |
"com.sun.management.jmxremote.localConnectorAddress"; |
|
164 |
||
165 |
private static void getAttachableVMs(Map<Integer, LocalVirtualMachine> map) { |
|
166 |
List<VirtualMachineDescriptor> vms = VirtualMachine.list(); |
|
167 |
for (VirtualMachineDescriptor vmd : vms) { |
|
168 |
try { |
|
169 |
Integer vmid = Integer.valueOf(vmd.id()); |
|
170 |
if (!map.containsKey(vmid)) { |
|
171 |
boolean attachable = false; |
|
172 |
String address = null; |
|
173 |
try { |
|
174 |
VirtualMachine vm = VirtualMachine.attach(vmd); |
|
175 |
attachable = true; |
|
176 |
Properties agentProps = vm.getAgentProperties(); |
|
177 |
address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); |
|
178 |
vm.detach(); |
|
179 |
} catch (AttachNotSupportedException x) { |
|
180 |
// not attachable |
|
181 |
} catch (IOException x) { |
|
182 |
// ignore |
|
183 |
} |
|
184 |
map.put(vmid, new LocalVirtualMachine(vmid.intValue(), |
|
185 |
vmd.displayName(), |
|
186 |
attachable, |
|
187 |
address)); |
|
188 |
} |
|
189 |
} catch (NumberFormatException e) { |
|
190 |
// do not support vmid different than pid |
|
191 |
} |
|
192 |
} |
|
193 |
} |
|
194 |
||
195 |
public static LocalVirtualMachine getLocalVirtualMachine(int vmid) { |
|
196 |
Map<Integer, LocalVirtualMachine> map = getAllVirtualMachines(); |
|
197 |
LocalVirtualMachine lvm = map.get(vmid); |
|
198 |
if (lvm == null) { |
|
199 |
// Check if the VM is attachable but not included in the list |
|
200 |
// if it's running with a different security context. |
|
201 |
// For example, Windows services running |
|
202 |
// local SYSTEM account are attachable if you have Adminstrator |
|
203 |
// privileges. |
|
204 |
boolean attachable = false; |
|
205 |
String address = null; |
|
206 |
String name = String.valueOf(vmid); // default display name to pid |
|
207 |
try { |
|
208 |
VirtualMachine vm = VirtualMachine.attach(name); |
|
209 |
attachable = true; |
|
210 |
Properties agentProps = vm.getAgentProperties(); |
|
211 |
address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); |
|
212 |
vm.detach(); |
|
213 |
lvm = new LocalVirtualMachine(vmid, name, attachable, address); |
|
214 |
} catch (AttachNotSupportedException x) { |
|
215 |
// not attachable |
|
216 |
if (JConsole.isDebug()) { |
|
217 |
x.printStackTrace(); |
|
218 |
} |
|
219 |
} catch (IOException x) { |
|
220 |
// ignore |
|
221 |
if (JConsole.isDebug()) { |
|
222 |
x.printStackTrace(); |
|
223 |
} |
|
224 |
} |
|
225 |
} |
|
226 |
return lvm; |
|
227 |
} |
|
228 |
||
229 |
// load the management agent into the target VM |
|
230 |
private void loadManagementAgent() throws IOException { |
|
231 |
VirtualMachine vm = null; |
|
232 |
String name = String.valueOf(vmid); |
|
233 |
try { |
|
234 |
vm = VirtualMachine.attach(name); |
|
235 |
} catch (AttachNotSupportedException x) { |
|
236 |
IOException ioe = new IOException(x.getMessage()); |
|
237 |
ioe.initCause(x); |
|
238 |
throw ioe; |
|
239 |
} |
|
240 |
||
241 |
String home = vm.getSystemProperties().getProperty("java.home"); |
|
242 |
||
243 |
// Normally in ${java.home}/jre/lib/management-agent.jar but might |
|
244 |
// be in ${java.home}/lib in build environments. |
|
245 |
||
246 |
String agent = home + File.separator + "jre" + File.separator + |
|
247 |
"lib" + File.separator + "management-agent.jar"; |
|
248 |
File f = new File(agent); |
|
249 |
if (!f.exists()) { |
|
250 |
agent = home + File.separator + "lib" + File.separator + |
|
251 |
"management-agent.jar"; |
|
252 |
f = new File(agent); |
|
253 |
if (!f.exists()) { |
|
254 |
throw new IOException("Management agent not found"); |
|
255 |
} |
|
256 |
} |
|
257 |
||
258 |
agent = f.getCanonicalPath(); |
|
259 |
try { |
|
260 |
vm.loadAgent(agent, "com.sun.management.jmxremote"); |
|
261 |
} catch (AgentLoadException x) { |
|
262 |
IOException ioe = new IOException(x.getMessage()); |
|
263 |
ioe.initCause(x); |
|
264 |
throw ioe; |
|
265 |
} catch (AgentInitializationException x) { |
|
266 |
IOException ioe = new IOException(x.getMessage()); |
|
267 |
ioe.initCause(x); |
|
268 |
throw ioe; |
|
269 |
} |
|
270 |
||
271 |
// get the connector address |
|
272 |
Properties agentProps = vm.getAgentProperties(); |
|
273 |
address = (String) agentProps.get(LOCAL_CONNECTOR_ADDRESS_PROP); |
|
274 |
||
275 |
vm.detach(); |
|
276 |
} |
|
277 |
} |