author | egahlin |
Fri, 25 May 2012 12:24:57 +0200 | |
changeset 12851 | 3334e1c781d0 |
parent 10355 | a976ff46116b |
child 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2004, 2008, 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 com.sun.management.HotSpotDiagnosticMXBean; |
|
29 |
import com.sun.tools.jconsole.JConsoleContext; |
|
30 |
import java.beans.PropertyChangeListener; |
|
31 |
import java.beans.PropertyChangeEvent; |
|
32 |
import java.io.IOException; |
|
33 |
import java.lang.management.*; |
|
34 |
import static java.lang.management.ManagementFactory.*; |
|
35 |
import java.lang.ref.WeakReference; |
|
36 |
import java.lang.reflect.*; |
|
37 |
import java.rmi.*; |
|
38 |
import java.rmi.registry.*; |
|
39 |
import java.rmi.server.*; |
|
40 |
import java.util.*; |
|
41 |
import javax.management.*; |
|
42 |
import javax.management.remote.*; |
|
43 |
import javax.management.remote.rmi.*; |
|
44 |
import javax.rmi.ssl.SslRMIClientSocketFactory; |
|
45 |
import javax.swing.event.SwingPropertyChangeSupport; |
|
46 |
import sun.rmi.server.UnicastRef2; |
|
47 |
import sun.rmi.transport.LiveRef; |
|
48 |
||
49 |
public class ProxyClient implements JConsoleContext { |
|
50 |
||
51 |
private ConnectionState connectionState = ConnectionState.DISCONNECTED; |
|
52 |
||
53 |
// The SwingPropertyChangeSupport will fire events on the EDT |
|
54 |
private SwingPropertyChangeSupport propertyChangeSupport = |
|
55 |
new SwingPropertyChangeSupport(this, true); |
|
56 |
||
57 |
private static Map<String, ProxyClient> cache = |
|
58 |
Collections.synchronizedMap(new HashMap<String, ProxyClient>()); |
|
59 |
||
60 |
private volatile boolean isDead = true; |
|
61 |
private String hostName = null; |
|
62 |
private int port = 0; |
|
63 |
private String userName = null; |
|
64 |
private String password = null; |
|
65 |
private boolean hasPlatformMXBeans = false; |
|
66 |
private boolean hasHotSpotDiagnosticMXBean= false; |
|
67 |
private boolean hasCompilationMXBean = false; |
|
68 |
private boolean supportsLockUsage = false; |
|
69 |
||
70 |
// REVISIT: VMPanel and other places relying using getUrl(). |
|
71 |
||
72 |
// set only if it's created for local monitoring |
|
73 |
private LocalVirtualMachine lvm; |
|
74 |
||
75 |
// set only if it's created from a given URL via the Advanced tab |
|
76 |
private String advancedUrl = null; |
|
77 |
||
78 |
private JMXServiceURL jmxUrl = null; |
|
697
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
79 |
private MBeanServerConnection mbsc = null; |
2 | 80 |
private SnapshotMBeanServerConnection server = null; |
81 |
private JMXConnector jmxc = null; |
|
82 |
private RMIServer stub = null; |
|
83 |
private static final SslRMIClientSocketFactory sslRMIClientSocketFactory = |
|
84 |
new SslRMIClientSocketFactory(); |
|
85 |
private String registryHostName = null; |
|
86 |
private int registryPort = 0; |
|
87 |
private boolean vmConnector = false; |
|
88 |
private boolean sslRegistry = false; |
|
89 |
private boolean sslStub = false; |
|
90 |
final private String connectionName; |
|
91 |
final private String displayName; |
|
92 |
||
93 |
private ClassLoadingMXBean classLoadingMBean = null; |
|
94 |
private CompilationMXBean compilationMBean = null; |
|
95 |
private MemoryMXBean memoryMBean = null; |
|
96 |
private OperatingSystemMXBean operatingSystemMBean = null; |
|
97 |
private RuntimeMXBean runtimeMBean = null; |
|
98 |
private ThreadMXBean threadMBean = null; |
|
99 |
||
100 |
private com.sun.management.OperatingSystemMXBean sunOperatingSystemMXBean = null; |
|
101 |
private HotSpotDiagnosticMXBean hotspotDiagnosticMXBean = null; |
|
102 |
||
103 |
private List<MemoryPoolProxy> memoryPoolProxies = null; |
|
104 |
private List<GarbageCollectorMXBean> garbageCollectorMBeans = null; |
|
105 |
||
106 |
final static private String HOTSPOT_DIAGNOSTIC_MXBEAN_NAME = |
|
107 |
"com.sun.management:type=HotSpotDiagnostic"; |
|
108 |
||
109 |
private ProxyClient(String hostName, int port, |
|
110 |
String userName, String password) throws IOException { |
|
111 |
this.connectionName = getConnectionName(hostName, port, userName); |
|
112 |
this.displayName = connectionName; |
|
113 |
if (hostName.equals("localhost") && port == 0) { |
|
114 |
// Monitor self |
|
115 |
this.hostName = hostName; |
|
116 |
this.port = port; |
|
117 |
} else { |
|
118 |
// Create an RMI connector client and connect it to |
|
119 |
// the RMI connector server |
|
120 |
final String urlPath = "/jndi/rmi://" + hostName + ":" + port + |
|
121 |
"/jmxrmi"; |
|
122 |
JMXServiceURL url = new JMXServiceURL("rmi", "", 0, urlPath); |
|
123 |
setParameters(url, userName, password); |
|
124 |
vmConnector = true; |
|
125 |
registryHostName = hostName; |
|
126 |
registryPort = port; |
|
127 |
checkSslConfig(); |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
private ProxyClient(String url, |
|
132 |
String userName, String password) throws IOException { |
|
133 |
this.advancedUrl = url; |
|
134 |
this.connectionName = getConnectionName(url, userName); |
|
135 |
this.displayName = connectionName; |
|
136 |
setParameters(new JMXServiceURL(url), userName, password); |
|
137 |
} |
|
138 |
||
139 |
private ProxyClient(LocalVirtualMachine lvm) throws IOException { |
|
140 |
this.lvm = lvm; |
|
141 |
this.connectionName = getConnectionName(lvm); |
|
142 |
this.displayName = "pid: " + lvm.vmid() + " " + lvm.displayName(); |
|
143 |
} |
|
144 |
||
145 |
private void setParameters(JMXServiceURL url, String userName, String password) { |
|
146 |
this.jmxUrl = url; |
|
147 |
this.hostName = jmxUrl.getHost(); |
|
148 |
this.port = jmxUrl.getPort(); |
|
149 |
this.userName = userName; |
|
150 |
this.password = password; |
|
151 |
} |
|
152 |
||
153 |
private static void checkStub(Remote stub, |
|
154 |
Class<? extends Remote> stubClass) { |
|
155 |
// Check remote stub is from the expected class. |
|
156 |
// |
|
157 |
if (stub.getClass() != stubClass) { |
|
158 |
if (!Proxy.isProxyClass(stub.getClass())) { |
|
159 |
throw new SecurityException( |
|
160 |
"Expecting a " + stubClass.getName() + " stub!"); |
|
161 |
} else { |
|
162 |
InvocationHandler handler = Proxy.getInvocationHandler(stub); |
|
163 |
if (handler.getClass() != RemoteObjectInvocationHandler.class) { |
|
164 |
throw new SecurityException( |
|
165 |
"Expecting a dynamic proxy instance with a " + |
|
166 |
RemoteObjectInvocationHandler.class.getName() + |
|
167 |
" invocation handler!"); |
|
168 |
} else { |
|
169 |
stub = (Remote) handler; |
|
170 |
} |
|
171 |
} |
|
172 |
} |
|
173 |
// Check RemoteRef in stub is from the expected class |
|
174 |
// "sun.rmi.server.UnicastRef2". |
|
175 |
// |
|
176 |
RemoteRef ref = ((RemoteObject)stub).getRef(); |
|
177 |
if (ref.getClass() != UnicastRef2.class) { |
|
178 |
throw new SecurityException( |
|
179 |
"Expecting a " + UnicastRef2.class.getName() + |
|
180 |
" remote reference in stub!"); |
|
181 |
} |
|
182 |
// Check RMIClientSocketFactory in stub is from the expected class |
|
183 |
// "javax.rmi.ssl.SslRMIClientSocketFactory". |
|
184 |
// |
|
185 |
LiveRef liveRef = ((UnicastRef2)ref).getLiveRef(); |
|
186 |
RMIClientSocketFactory csf = liveRef.getClientSocketFactory(); |
|
187 |
if (csf == null || csf.getClass() != SslRMIClientSocketFactory.class) { |
|
188 |
throw new SecurityException( |
|
189 |
"Expecting a " + SslRMIClientSocketFactory.class.getName() + |
|
190 |
" RMI client socket factory in stub!"); |
|
191 |
} |
|
192 |
} |
|
193 |
||
194 |
private static final String rmiServerImplStubClassName = |
|
195 |
"javax.management.remote.rmi.RMIServerImpl_Stub"; |
|
196 |
private static final Class<? extends Remote> rmiServerImplStubClass; |
|
197 |
||
198 |
static { |
|
199 |
// FIXME: RMIServerImpl_Stub is generated at build time |
|
200 |
// after jconsole is built. We need to investigate if |
|
201 |
// the Makefile can be fixed to build jconsole in the |
|
202 |
// right order. As a workaround for now, we dynamically |
|
203 |
// load RMIServerImpl_Stub class instead of statically |
|
204 |
// referencing it. |
|
205 |
Class<? extends Remote> serverStubClass = null; |
|
206 |
try { |
|
207 |
serverStubClass = Class.forName(rmiServerImplStubClassName).asSubclass(Remote.class); |
|
208 |
} catch (ClassNotFoundException e) { |
|
209 |
// should never reach here |
|
10355
a976ff46116b
7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents:
5506
diff
changeset
|
210 |
throw new InternalError(e.getMessage(), e); |
2 | 211 |
} |
212 |
rmiServerImplStubClass = serverStubClass; |
|
213 |
} |
|
214 |
||
215 |
private void checkSslConfig() throws IOException { |
|
216 |
// Get the reference to the RMI Registry and lookup RMIServer stub |
|
217 |
// |
|
218 |
Registry registry; |
|
219 |
try { |
|
220 |
registry = |
|
221 |
LocateRegistry.getRegistry(registryHostName, registryPort, |
|
222 |
sslRMIClientSocketFactory); |
|
223 |
try { |
|
224 |
stub = (RMIServer) registry.lookup("jmxrmi"); |
|
225 |
} catch (NotBoundException nbe) { |
|
226 |
throw (IOException) |
|
227 |
new IOException(nbe.getMessage()).initCause(nbe); |
|
228 |
} |
|
229 |
sslRegistry = true; |
|
230 |
} catch (IOException e) { |
|
231 |
registry = |
|
232 |
LocateRegistry.getRegistry(registryHostName, registryPort); |
|
233 |
try { |
|
234 |
stub = (RMIServer) registry.lookup("jmxrmi"); |
|
235 |
} catch (NotBoundException nbe) { |
|
236 |
throw (IOException) |
|
237 |
new IOException(nbe.getMessage()).initCause(nbe); |
|
238 |
} |
|
239 |
sslRegistry = false; |
|
240 |
} |
|
241 |
// Perform the checks for secure stub |
|
242 |
// |
|
243 |
try { |
|
244 |
checkStub(stub, rmiServerImplStubClass); |
|
245 |
sslStub = true; |
|
246 |
} catch (SecurityException e) { |
|
247 |
sslStub = false; |
|
248 |
} |
|
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
* Returns true if the underlying RMI registry is SSL-protected. |
|
253 |
* |
|
254 |
* @exception UnsupportedOperationException If this {@code ProxyClient} |
|
255 |
* does not denote a JMX connector for a JMX VM agent. |
|
256 |
*/ |
|
257 |
public boolean isSslRmiRegistry() { |
|
258 |
// Check for VM connector |
|
259 |
// |
|
260 |
if (!isVmConnector()) { |
|
261 |
throw new UnsupportedOperationException( |
|
262 |
"ProxyClient.isSslRmiRegistry() is only supported if this " + |
|
263 |
"ProxyClient is a JMX connector for a JMX VM agent"); |
|
264 |
} |
|
265 |
return sslRegistry; |
|
266 |
} |
|
267 |
||
268 |
/** |
|
269 |
* Returns true if the retrieved RMI stub is SSL-protected. |
|
270 |
* |
|
271 |
* @exception UnsupportedOperationException If this {@code ProxyClient} |
|
272 |
* does not denote a JMX connector for a JMX VM agent. |
|
273 |
*/ |
|
274 |
public boolean isSslRmiStub() { |
|
275 |
// Check for VM connector |
|
276 |
// |
|
277 |
if (!isVmConnector()) { |
|
278 |
throw new UnsupportedOperationException( |
|
279 |
"ProxyClient.isSslRmiStub() is only supported if this " + |
|
280 |
"ProxyClient is a JMX connector for a JMX VM agent"); |
|
281 |
} |
|
282 |
return sslStub; |
|
283 |
} |
|
284 |
||
285 |
/** |
|
286 |
* Returns true if this {@code ProxyClient} denotes |
|
287 |
* a JMX connector for a JMX VM agent. |
|
288 |
*/ |
|
289 |
public boolean isVmConnector() { |
|
290 |
return vmConnector; |
|
291 |
} |
|
292 |
||
293 |
private void setConnectionState(ConnectionState state) { |
|
294 |
ConnectionState oldState = this.connectionState; |
|
295 |
this.connectionState = state; |
|
296 |
propertyChangeSupport.firePropertyChange(CONNECTION_STATE_PROPERTY, |
|
297 |
oldState, state); |
|
298 |
} |
|
299 |
||
300 |
public ConnectionState getConnectionState() { |
|
301 |
return this.connectionState; |
|
302 |
} |
|
303 |
||
304 |
void flush() { |
|
305 |
if (server != null) { |
|
306 |
server.flush(); |
|
307 |
} |
|
308 |
} |
|
309 |
||
310 |
void connect() { |
|
311 |
setConnectionState(ConnectionState.CONNECTING); |
|
312 |
try { |
|
313 |
tryConnect(); |
|
314 |
setConnectionState(ConnectionState.CONNECTED); |
|
315 |
} catch (Exception e) { |
|
316 |
if (JConsole.isDebug()) { |
|
317 |
e.printStackTrace(); |
|
318 |
} |
|
319 |
setConnectionState(ConnectionState.DISCONNECTED); |
|
320 |
} |
|
321 |
} |
|
322 |
||
323 |
private void tryConnect() throws IOException { |
|
324 |
if (jmxUrl == null && "localhost".equals(hostName) && port == 0) { |
|
325 |
// Monitor self |
|
326 |
this.jmxc = null; |
|
697
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
327 |
this.mbsc = ManagementFactory.getPlatformMBeanServer(); |
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
328 |
this.server = Snapshot.newSnapshot(mbsc); |
2 | 329 |
} else { |
330 |
// Monitor another process |
|
331 |
if (lvm != null) { |
|
332 |
if (!lvm.isManageable()) { |
|
333 |
lvm.startManagementAgent(); |
|
334 |
if (!lvm.isManageable()) { |
|
335 |
// FIXME: what to throw |
|
336 |
throw new IOException(lvm + "not manageable"); |
|
337 |
} |
|
338 |
} |
|
339 |
if (this.jmxUrl == null) { |
|
340 |
this.jmxUrl = new JMXServiceURL(lvm.connectorAddress()); |
|
341 |
} |
|
342 |
} |
|
343 |
// Need to pass in credentials ? |
|
344 |
if (userName == null && password == null) { |
|
345 |
if (isVmConnector()) { |
|
346 |
// Check for SSL config on reconnection only |
|
347 |
if (stub == null) { |
|
348 |
checkSslConfig(); |
|
349 |
} |
|
350 |
this.jmxc = new RMIConnector(stub, null); |
|
351 |
jmxc.connect(); |
|
352 |
} else { |
|
353 |
this.jmxc = JMXConnectorFactory.connect(jmxUrl); |
|
354 |
} |
|
355 |
} else { |
|
356 |
Map<String, String[]> env = new HashMap<String, String[]>(); |
|
357 |
env.put(JMXConnector.CREDENTIALS, |
|
358 |
new String[] {userName, password}); |
|
359 |
if (isVmConnector()) { |
|
360 |
// Check for SSL config on reconnection only |
|
361 |
if (stub == null) { |
|
362 |
checkSslConfig(); |
|
363 |
} |
|
364 |
this.jmxc = new RMIConnector(stub, null); |
|
365 |
jmxc.connect(env); |
|
366 |
} else { |
|
367 |
this.jmxc = JMXConnectorFactory.connect(jmxUrl, env); |
|
368 |
} |
|
369 |
} |
|
697
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
370 |
this.mbsc = jmxc.getMBeanServerConnection(); |
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
371 |
this.server = Snapshot.newSnapshot(mbsc); |
2 | 372 |
} |
373 |
this.isDead = false; |
|
374 |
||
375 |
try { |
|
376 |
ObjectName on = new ObjectName(THREAD_MXBEAN_NAME); |
|
377 |
this.hasPlatformMXBeans = server.isRegistered(on); |
|
378 |
this.hasHotSpotDiagnosticMXBean = |
|
379 |
server.isRegistered(new ObjectName(HOTSPOT_DIAGNOSTIC_MXBEAN_NAME)); |
|
380 |
// check if it has 6.0 new APIs |
|
381 |
if (this.hasPlatformMXBeans) { |
|
382 |
MBeanOperationInfo[] mopis = server.getMBeanInfo(on).getOperations(); |
|
383 |
// look for findDeadlockedThreads operations; |
|
384 |
for (MBeanOperationInfo op : mopis) { |
|
385 |
if (op.getName().equals("findDeadlockedThreads")) { |
|
386 |
this.supportsLockUsage = true; |
|
387 |
break; |
|
388 |
} |
|
389 |
} |
|
390 |
||
391 |
on = new ObjectName(COMPILATION_MXBEAN_NAME); |
|
392 |
this.hasCompilationMXBean = server.isRegistered(on); |
|
393 |
} |
|
394 |
} catch (MalformedObjectNameException e) { |
|
395 |
// should not reach here |
|
396 |
throw new InternalError(e.getMessage()); |
|
10355
a976ff46116b
7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents:
5506
diff
changeset
|
397 |
} catch (IntrospectionException | |
a976ff46116b
7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents:
5506
diff
changeset
|
398 |
InstanceNotFoundException | |
a976ff46116b
7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents:
5506
diff
changeset
|
399 |
ReflectionException e) { |
a976ff46116b
7080020: Add conventional constructors to InternalError and VirtualMachineError
darcy
parents:
5506
diff
changeset
|
400 |
throw new InternalError(e.getMessage(), e); |
2 | 401 |
} |
402 |
||
403 |
if (hasPlatformMXBeans) { |
|
404 |
// WORKAROUND for bug 5056632 |
|
405 |
// Check if the access role is correct by getting a RuntimeMXBean |
|
406 |
getRuntimeMXBean(); |
|
407 |
} |
|
408 |
} |
|
409 |
||
410 |
/** |
|
411 |
* Gets a proxy client for a given local virtual machine. |
|
412 |
*/ |
|
413 |
public static ProxyClient getProxyClient(LocalVirtualMachine lvm) |
|
414 |
throws IOException { |
|
415 |
final String key = getCacheKey(lvm); |
|
416 |
ProxyClient proxyClient = cache.get(key); |
|
417 |
if (proxyClient == null) { |
|
418 |
proxyClient = new ProxyClient(lvm); |
|
419 |
cache.put(key, proxyClient); |
|
420 |
} |
|
421 |
return proxyClient; |
|
422 |
} |
|
423 |
||
424 |
public static String getConnectionName(LocalVirtualMachine lvm) { |
|
425 |
return Integer.toString(lvm.vmid()); |
|
426 |
} |
|
427 |
||
428 |
private static String getCacheKey(LocalVirtualMachine lvm) { |
|
429 |
return Integer.toString(lvm.vmid()); |
|
430 |
} |
|
431 |
||
432 |
/** |
|
433 |
* Gets a proxy client for a given JMXServiceURL. |
|
434 |
*/ |
|
435 |
public static ProxyClient getProxyClient(String url, |
|
436 |
String userName, String password) |
|
437 |
throws IOException { |
|
438 |
final String key = getCacheKey(url, userName, password); |
|
439 |
ProxyClient proxyClient = cache.get(key); |
|
440 |
if (proxyClient == null) { |
|
441 |
proxyClient = new ProxyClient(url, userName, password); |
|
442 |
cache.put(key, proxyClient); |
|
443 |
} |
|
444 |
return proxyClient; |
|
445 |
} |
|
446 |
||
447 |
public static String getConnectionName(String url, |
|
448 |
String userName) { |
|
449 |
if (userName != null && userName.length() > 0) { |
|
450 |
return userName + "@" + url; |
|
451 |
} else { |
|
452 |
return url; |
|
453 |
} |
|
454 |
} |
|
455 |
||
456 |
private static String getCacheKey(String url, |
|
457 |
String userName, String password) { |
|
458 |
return (url == null ? "" : url) + ":" + |
|
459 |
(userName == null ? "" : userName) + ":" + |
|
460 |
(password == null ? "" : password); |
|
461 |
} |
|
462 |
||
463 |
/** |
|
464 |
* Gets a proxy client for a given "hostname:port". |
|
465 |
*/ |
|
466 |
public static ProxyClient getProxyClient(String hostName, int port, |
|
467 |
String userName, String password) |
|
468 |
throws IOException { |
|
469 |
final String key = getCacheKey(hostName, port, userName, password); |
|
470 |
ProxyClient proxyClient = cache.get(key); |
|
471 |
if (proxyClient == null) { |
|
472 |
proxyClient = new ProxyClient(hostName, port, userName, password); |
|
473 |
cache.put(key, proxyClient); |
|
474 |
} |
|
475 |
return proxyClient; |
|
476 |
} |
|
477 |
||
478 |
public static String getConnectionName(String hostName, int port, |
|
479 |
String userName) { |
|
480 |
String name = hostName + ":" + port; |
|
481 |
if (userName != null && userName.length() > 0) { |
|
482 |
return userName + "@" + name; |
|
483 |
} else { |
|
484 |
return name; |
|
485 |
} |
|
486 |
} |
|
487 |
||
488 |
private static String getCacheKey(String hostName, int port, |
|
489 |
String userName, String password) { |
|
490 |
return (hostName == null ? "" : hostName) + ":" + |
|
491 |
port + ":" + |
|
492 |
(userName == null ? "" : userName) + ":" + |
|
493 |
(password == null ? "" : password); |
|
494 |
} |
|
495 |
||
496 |
public String connectionName() { |
|
497 |
return connectionName; |
|
498 |
} |
|
499 |
||
500 |
public String getDisplayName() { |
|
501 |
return displayName; |
|
502 |
} |
|
503 |
||
504 |
public String toString() { |
|
505 |
if (!isConnected()) { |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
10355
diff
changeset
|
506 |
return Resources.format(Messages.CONNECTION_NAME__DISCONNECTED_, displayName); |
2 | 507 |
} else { |
508 |
return displayName; |
|
509 |
} |
|
510 |
} |
|
511 |
||
697
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
512 |
public MBeanServerConnection getMBeanServerConnection() { |
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
513 |
return mbsc; |
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
514 |
} |
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
515 |
|
fb86c99b92a2
6711106: REGRESSION: Bad usage of SnapshotMBeanServerConnection in MBeans tab and JConsole plugins.
lmalvent
parents:
2
diff
changeset
|
516 |
public SnapshotMBeanServerConnection getSnapshotMBeanServerConnection() { |
2 | 517 |
return server; |
518 |
} |
|
519 |
||
520 |
public String getUrl() { |
|
521 |
return advancedUrl; |
|
522 |
} |
|
523 |
||
524 |
public String getHostName() { |
|
525 |
return hostName; |
|
526 |
} |
|
527 |
||
528 |
public int getPort() { |
|
529 |
return port; |
|
530 |
} |
|
531 |
||
532 |
public int getVmid() { |
|
533 |
return (lvm != null) ? lvm.vmid() : 0; |
|
534 |
} |
|
535 |
||
536 |
public String getUserName() { |
|
537 |
return userName; |
|
538 |
} |
|
539 |
||
540 |
public String getPassword() { |
|
541 |
return password; |
|
542 |
} |
|
543 |
||
544 |
public void disconnect() { |
|
545 |
// Reset remote stub |
|
546 |
stub = null; |
|
547 |
// Close MBeanServer connection |
|
548 |
if (jmxc != null) { |
|
549 |
try { |
|
550 |
jmxc.close(); |
|
551 |
} catch (IOException e) { |
|
552 |
// Ignore ??? |
|
553 |
} |
|
554 |
} |
|
555 |
// Reset platform MBean references |
|
556 |
classLoadingMBean = null; |
|
557 |
compilationMBean = null; |
|
558 |
memoryMBean = null; |
|
559 |
operatingSystemMBean = null; |
|
560 |
runtimeMBean = null; |
|
561 |
threadMBean = null; |
|
562 |
sunOperatingSystemMXBean = null; |
|
563 |
garbageCollectorMBeans = null; |
|
564 |
// Set connection state to DISCONNECTED |
|
565 |
if (!isDead) { |
|
566 |
isDead = true; |
|
567 |
setConnectionState(ConnectionState.DISCONNECTED); |
|
568 |
} |
|
569 |
} |
|
570 |
||
571 |
/** |
|
572 |
* Returns the list of domains in which any MBean is |
|
573 |
* currently registered. |
|
574 |
*/ |
|
575 |
public String[] getDomains() throws IOException { |
|
576 |
return server.getDomains(); |
|
577 |
} |
|
578 |
||
579 |
/** |
|
580 |
* Returns a map of MBeans with ObjectName as the key and MBeanInfo value |
|
581 |
* of a given domain. If domain is <tt>null</tt>, all MBeans |
|
582 |
* are returned. If no MBean found, an empty map is returned. |
|
583 |
* |
|
584 |
*/ |
|
585 |
public Map<ObjectName, MBeanInfo> getMBeans(String domain) |
|
586 |
throws IOException { |
|
587 |
||
588 |
ObjectName name = null; |
|
589 |
if (domain != null) { |
|
590 |
try { |
|
591 |
name = new ObjectName(domain + ":*"); |
|
592 |
} catch (MalformedObjectNameException e) { |
|
593 |
// should not reach here |
|
594 |
assert(false); |
|
595 |
} |
|
596 |
} |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
10355
diff
changeset
|
597 |
Set<ObjectName> mbeans = server.queryNames(name, null); |
2 | 598 |
Map<ObjectName,MBeanInfo> result = |
599 |
new HashMap<ObjectName,MBeanInfo>(mbeans.size()); |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
10355
diff
changeset
|
600 |
Iterator<ObjectName> iterator = mbeans.iterator(); |
2 | 601 |
while (iterator.hasNext()) { |
602 |
Object object = iterator.next(); |
|
603 |
if (object instanceof ObjectName) { |
|
604 |
ObjectName o = (ObjectName)object; |
|
605 |
try { |
|
606 |
MBeanInfo info = server.getMBeanInfo(o); |
|
607 |
result.put(o, info); |
|
608 |
} catch (IntrospectionException e) { |
|
609 |
// TODO: should log the error |
|
610 |
} catch (InstanceNotFoundException e) { |
|
611 |
// TODO: should log the error |
|
612 |
} catch (ReflectionException e) { |
|
613 |
// TODO: should log the error |
|
614 |
} |
|
615 |
} |
|
616 |
} |
|
617 |
return result; |
|
618 |
} |
|
619 |
||
620 |
/** |
|
621 |
* Returns a list of attributes of a named MBean. |
|
622 |
* |
|
623 |
*/ |
|
624 |
public AttributeList getAttributes(ObjectName name, String[] attributes) |
|
625 |
throws IOException { |
|
626 |
AttributeList list = null; |
|
627 |
try { |
|
628 |
list = server.getAttributes(name, attributes); |
|
629 |
} catch (InstanceNotFoundException e) { |
|
630 |
// TODO: A MBean may have been unregistered. |
|
631 |
// need to set up listener to listen for MBeanServerNotification. |
|
632 |
} catch (ReflectionException e) { |
|
633 |
// TODO: should log the error |
|
634 |
} |
|
635 |
return list; |
|
636 |
} |
|
637 |
||
638 |
/** |
|
639 |
* Set the value of a specific attribute of a named MBean. |
|
640 |
*/ |
|
641 |
public void setAttribute(ObjectName name, Attribute attribute) |
|
642 |
throws InvalidAttributeValueException, |
|
643 |
MBeanException, |
|
644 |
IOException { |
|
645 |
try { |
|
646 |
server.setAttribute(name, attribute); |
|
647 |
} catch (InstanceNotFoundException e) { |
|
648 |
// TODO: A MBean may have been unregistered. |
|
649 |
} catch (AttributeNotFoundException e) { |
|
650 |
assert(false); |
|
651 |
} catch (ReflectionException e) { |
|
652 |
// TODO: should log the error |
|
653 |
} |
|
654 |
} |
|
655 |
||
656 |
/** |
|
657 |
* Invokes an operation of a named MBean. |
|
658 |
* |
|
659 |
* @throws MBeanException Wraps an exception thrown by |
|
660 |
* the MBean's invoked method. |
|
661 |
*/ |
|
662 |
public Object invoke(ObjectName name, String operationName, |
|
663 |
Object[] params, String[] signature) |
|
664 |
throws IOException, MBeanException { |
|
665 |
Object result = null; |
|
666 |
try { |
|
667 |
result = server.invoke(name, operationName, params, signature); |
|
668 |
} catch (InstanceNotFoundException e) { |
|
669 |
// TODO: A MBean may have been unregistered. |
|
670 |
} catch (ReflectionException e) { |
|
671 |
// TODO: should log the error |
|
672 |
} |
|
673 |
return result; |
|
674 |
} |
|
675 |
||
676 |
public synchronized ClassLoadingMXBean getClassLoadingMXBean() throws IOException { |
|
677 |
if (hasPlatformMXBeans && classLoadingMBean == null) { |
|
678 |
classLoadingMBean = |
|
679 |
newPlatformMXBeanProxy(server, CLASS_LOADING_MXBEAN_NAME, |
|
680 |
ClassLoadingMXBean.class); |
|
681 |
} |
|
682 |
return classLoadingMBean; |
|
683 |
} |
|
684 |
||
685 |
public synchronized CompilationMXBean getCompilationMXBean() throws IOException { |
|
686 |
if (hasCompilationMXBean && compilationMBean == null) { |
|
687 |
compilationMBean = |
|
688 |
newPlatformMXBeanProxy(server, COMPILATION_MXBEAN_NAME, |
|
689 |
CompilationMXBean.class); |
|
690 |
} |
|
691 |
return compilationMBean; |
|
692 |
} |
|
693 |
||
694 |
public Collection<MemoryPoolProxy> getMemoryPoolProxies() |
|
695 |
throws IOException { |
|
696 |
||
697 |
// TODO: How to deal with changes to the list?? |
|
698 |
if (memoryPoolProxies == null) { |
|
699 |
ObjectName poolName = null; |
|
700 |
try { |
|
701 |
poolName = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*"); |
|
702 |
} catch (MalformedObjectNameException e) { |
|
703 |
// should not reach here |
|
704 |
assert(false); |
|
705 |
} |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
10355
diff
changeset
|
706 |
Set<ObjectName> mbeans = server.queryNames(poolName, null); |
2 | 707 |
if (mbeans != null) { |
708 |
memoryPoolProxies = new ArrayList<MemoryPoolProxy>(); |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
10355
diff
changeset
|
709 |
Iterator<ObjectName> iterator = mbeans.iterator(); |
2 | 710 |
while (iterator.hasNext()) { |
711 |
ObjectName objName = (ObjectName) iterator.next(); |
|
712 |
MemoryPoolProxy p = new MemoryPoolProxy(this, objName); |
|
713 |
memoryPoolProxies.add(p); |
|
714 |
} |
|
715 |
} |
|
716 |
} |
|
717 |
return memoryPoolProxies; |
|
718 |
} |
|
719 |
||
720 |
public synchronized Collection<GarbageCollectorMXBean> getGarbageCollectorMXBeans() |
|
721 |
throws IOException { |
|
722 |
||
723 |
// TODO: How to deal with changes to the list?? |
|
724 |
if (garbageCollectorMBeans == null) { |
|
725 |
ObjectName gcName = null; |
|
726 |
try { |
|
727 |
gcName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*"); |
|
728 |
} catch (MalformedObjectNameException e) { |
|
729 |
// should not reach here |
|
730 |
assert(false); |
|
731 |
} |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
10355
diff
changeset
|
732 |
Set<ObjectName> mbeans = server.queryNames(gcName, null); |
2 | 733 |
if (mbeans != null) { |
734 |
garbageCollectorMBeans = new ArrayList<GarbageCollectorMXBean>(); |
|
12851
3334e1c781d0
7017818: NLS: JConsoleResources.java cannot be handled by translation team
egahlin
parents:
10355
diff
changeset
|
735 |
Iterator<ObjectName> iterator = mbeans.iterator(); |
2 | 736 |
while (iterator.hasNext()) { |
737 |
ObjectName on = (ObjectName) iterator.next(); |
|
738 |
String name = GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + |
|
739 |
",name=" + on.getKeyProperty("name"); |
|
740 |
||
741 |
GarbageCollectorMXBean mBean = |
|
742 |
newPlatformMXBeanProxy(server, name, |
|
743 |
GarbageCollectorMXBean.class); |
|
744 |
garbageCollectorMBeans.add(mBean); |
|
745 |
} |
|
746 |
} |
|
747 |
} |
|
748 |
return garbageCollectorMBeans; |
|
749 |
} |
|
750 |
||
751 |
public synchronized MemoryMXBean getMemoryMXBean() throws IOException { |
|
752 |
if (hasPlatformMXBeans && memoryMBean == null) { |
|
753 |
memoryMBean = |
|
754 |
newPlatformMXBeanProxy(server, MEMORY_MXBEAN_NAME, |
|
755 |
MemoryMXBean.class); |
|
756 |
} |
|
757 |
return memoryMBean; |
|
758 |
} |
|
759 |
||
760 |
public synchronized RuntimeMXBean getRuntimeMXBean() throws IOException { |
|
761 |
if (hasPlatformMXBeans && runtimeMBean == null) { |
|
762 |
runtimeMBean = |
|
763 |
newPlatformMXBeanProxy(server, RUNTIME_MXBEAN_NAME, |
|
764 |
RuntimeMXBean.class); |
|
765 |
} |
|
766 |
return runtimeMBean; |
|
767 |
} |
|
768 |
||
769 |
||
770 |
public synchronized ThreadMXBean getThreadMXBean() throws IOException { |
|
771 |
if (hasPlatformMXBeans && threadMBean == null) { |
|
772 |
threadMBean = |
|
773 |
newPlatformMXBeanProxy(server, THREAD_MXBEAN_NAME, |
|
774 |
ThreadMXBean.class); |
|
775 |
} |
|
776 |
return threadMBean; |
|
777 |
} |
|
778 |
||
779 |
public synchronized OperatingSystemMXBean getOperatingSystemMXBean() throws IOException { |
|
780 |
if (hasPlatformMXBeans && operatingSystemMBean == null) { |
|
781 |
operatingSystemMBean = |
|
782 |
newPlatformMXBeanProxy(server, OPERATING_SYSTEM_MXBEAN_NAME, |
|
783 |
OperatingSystemMXBean.class); |
|
784 |
} |
|
785 |
return operatingSystemMBean; |
|
786 |
} |
|
787 |
||
788 |
public synchronized com.sun.management.OperatingSystemMXBean |
|
789 |
getSunOperatingSystemMXBean() throws IOException { |
|
790 |
||
791 |
try { |
|
792 |
ObjectName on = new ObjectName(OPERATING_SYSTEM_MXBEAN_NAME); |
|
793 |
if (sunOperatingSystemMXBean == null) { |
|
794 |
if (server.isInstanceOf(on, |
|
795 |
"com.sun.management.OperatingSystemMXBean")) { |
|
796 |
sunOperatingSystemMXBean = |
|
797 |
newPlatformMXBeanProxy(server, |
|
798 |
OPERATING_SYSTEM_MXBEAN_NAME, |
|
799 |
com.sun.management.OperatingSystemMXBean.class); |
|
800 |
} |
|
801 |
} |
|
802 |
} catch (InstanceNotFoundException e) { |
|
803 |
return null; |
|
804 |
} catch (MalformedObjectNameException e) { |
|
805 |
return null; // should never reach here |
|
806 |
} |
|
807 |
return sunOperatingSystemMXBean; |
|
808 |
} |
|
809 |
||
810 |
public synchronized HotSpotDiagnosticMXBean getHotSpotDiagnosticMXBean() throws IOException { |
|
811 |
if (hasHotSpotDiagnosticMXBean && hotspotDiagnosticMXBean == null) { |
|
812 |
hotspotDiagnosticMXBean = |
|
813 |
newPlatformMXBeanProxy(server, HOTSPOT_DIAGNOSTIC_MXBEAN_NAME, |
|
814 |
HotSpotDiagnosticMXBean.class); |
|
815 |
} |
|
816 |
return hotspotDiagnosticMXBean; |
|
817 |
} |
|
818 |
||
819 |
public <T> T getMXBean(ObjectName objName, Class<T> interfaceClass) |
|
820 |
throws IOException { |
|
821 |
return newPlatformMXBeanProxy(server, |
|
822 |
objName.toString(), |
|
823 |
interfaceClass); |
|
824 |
||
825 |
} |
|
826 |
||
827 |
// Return thread IDs of deadlocked threads or null if any. |
|
828 |
// It finds deadlocks involving only monitors if it's a Tiger VM. |
|
829 |
// Otherwise, it finds deadlocks involving both monitors and |
|
830 |
// the concurrent locks. |
|
831 |
public long[] findDeadlockedThreads() throws IOException { |
|
832 |
ThreadMXBean tm = getThreadMXBean(); |
|
833 |
if (supportsLockUsage && tm.isSynchronizerUsageSupported()) { |
|
834 |
return tm.findDeadlockedThreads(); |
|
835 |
} else { |
|
836 |
return tm.findMonitorDeadlockedThreads(); |
|
837 |
} |
|
838 |
} |
|
839 |
||
840 |
public synchronized void markAsDead() { |
|
841 |
disconnect(); |
|
842 |
} |
|
843 |
||
844 |
public boolean isDead() { |
|
845 |
return isDead; |
|
846 |
} |
|
847 |
||
848 |
boolean isConnected() { |
|
849 |
return !isDead(); |
|
850 |
} |
|
851 |
||
852 |
boolean hasPlatformMXBeans() { |
|
853 |
return this.hasPlatformMXBeans; |
|
854 |
} |
|
855 |
||
856 |
boolean hasHotSpotDiagnosticMXBean() { |
|
857 |
return this.hasHotSpotDiagnosticMXBean; |
|
858 |
} |
|
859 |
||
860 |
boolean isLockUsageSupported() { |
|
861 |
return supportsLockUsage; |
|
862 |
} |
|
863 |
||
864 |
public boolean isRegistered(ObjectName name) throws IOException { |
|
865 |
return server.isRegistered(name); |
|
866 |
} |
|
867 |
||
868 |
public void addPropertyChangeListener(PropertyChangeListener listener) { |
|
869 |
propertyChangeSupport.addPropertyChangeListener(listener); |
|
870 |
} |
|
871 |
||
872 |
public void addWeakPropertyChangeListener(PropertyChangeListener listener) { |
|
873 |
if (!(listener instanceof WeakPCL)) { |
|
874 |
listener = new WeakPCL(listener); |
|
875 |
} |
|
876 |
propertyChangeSupport.addPropertyChangeListener(listener); |
|
877 |
} |
|
878 |
||
879 |
public void removePropertyChangeListener(PropertyChangeListener listener) { |
|
880 |
if (!(listener instanceof WeakPCL)) { |
|
881 |
// Search for the WeakPCL holding this listener (if any) |
|
882 |
for (PropertyChangeListener pcl : propertyChangeSupport.getPropertyChangeListeners()) { |
|
883 |
if (pcl instanceof WeakPCL && ((WeakPCL)pcl).get() == listener) { |
|
884 |
listener = pcl; |
|
885 |
break; |
|
886 |
} |
|
887 |
} |
|
888 |
} |
|
889 |
propertyChangeSupport.removePropertyChangeListener(listener); |
|
890 |
} |
|
891 |
||
892 |
/** |
|
893 |
* The PropertyChangeListener is handled via a WeakReference |
|
894 |
* so as not to pin down the listener. |
|
895 |
*/ |
|
896 |
private class WeakPCL extends WeakReference<PropertyChangeListener> |
|
897 |
implements PropertyChangeListener { |
|
898 |
WeakPCL(PropertyChangeListener referent) { |
|
899 |
super(referent); |
|
900 |
} |
|
901 |
||
902 |
public void propertyChange(PropertyChangeEvent pce) { |
|
903 |
PropertyChangeListener pcl = get(); |
|
904 |
||
905 |
if (pcl == null) { |
|
906 |
// The referent listener was GC'ed, we're no longer |
|
907 |
// interested in PropertyChanges, remove the listener. |
|
908 |
dispose(); |
|
909 |
} else { |
|
910 |
pcl.propertyChange(pce); |
|
911 |
} |
|
912 |
} |
|
913 |
||
914 |
private void dispose() { |
|
915 |
removePropertyChangeListener(this); |
|
916 |
} |
|
917 |
} |
|
918 |
||
919 |
// |
|
920 |
// Snapshot MBeanServerConnection: |
|
921 |
// |
|
922 |
// This is an object that wraps an existing MBeanServerConnection and adds |
|
923 |
// caching to it, as follows: |
|
924 |
// |
|
925 |
// - The first time an attribute is called in a given MBean, the result is |
|
926 |
// cached. Every subsequent time getAttribute is called for that attribute |
|
927 |
// the cached result is returned. |
|
928 |
// |
|
929 |
// - Before every call to VMPanel.update() or when the Refresh button in the |
|
930 |
// Attributes table is pressed down the attributes cache is flushed. Then |
|
931 |
// any subsequent call to getAttribute will retrieve all the values for |
|
932 |
// the attributes that are known to the cache. |
|
933 |
// |
|
934 |
// - The attributes cache uses a learning approach and only the attributes |
|
935 |
// that are in the cache will be retrieved between two subsequent updates. |
|
936 |
// |
|
937 |
||
938 |
public interface SnapshotMBeanServerConnection |
|
939 |
extends MBeanServerConnection { |
|
940 |
/** |
|
941 |
* Flush all cached values of attributes. |
|
942 |
*/ |
|
943 |
public void flush(); |
|
944 |
} |
|
945 |
||
946 |
public static class Snapshot { |
|
947 |
private Snapshot() { |
|
948 |
} |
|
949 |
public static SnapshotMBeanServerConnection |
|
950 |
newSnapshot(MBeanServerConnection mbsc) { |
|
951 |
final InvocationHandler ih = new SnapshotInvocationHandler(mbsc); |
|
952 |
return (SnapshotMBeanServerConnection) Proxy.newProxyInstance( |
|
953 |
Snapshot.class.getClassLoader(), |
|
954 |
new Class[] {SnapshotMBeanServerConnection.class}, |
|
955 |
ih); |
|
956 |
} |
|
957 |
} |
|
958 |
||
959 |
static class SnapshotInvocationHandler implements InvocationHandler { |
|
960 |
||
961 |
private final MBeanServerConnection conn; |
|
962 |
private Map<ObjectName, NameValueMap> cachedValues = newMap(); |
|
963 |
private Map<ObjectName, Set<String>> cachedNames = newMap(); |
|
964 |
||
965 |
@SuppressWarnings("serial") |
|
966 |
private static final class NameValueMap |
|
967 |
extends HashMap<String, Object> {} |
|
968 |
||
969 |
SnapshotInvocationHandler(MBeanServerConnection conn) { |
|
970 |
this.conn = conn; |
|
971 |
} |
|
972 |
||
973 |
synchronized void flush() { |
|
974 |
cachedValues = newMap(); |
|
975 |
} |
|
976 |
||
977 |
public Object invoke(Object proxy, Method method, Object[] args) |
|
978 |
throws Throwable { |
|
979 |
final String methodName = method.getName(); |
|
980 |
if (methodName.equals("getAttribute")) { |
|
981 |
return getAttribute((ObjectName) args[0], (String) args[1]); |
|
982 |
} else if (methodName.equals("getAttributes")) { |
|
983 |
return getAttributes((ObjectName) args[0], (String[]) args[1]); |
|
984 |
} else if (methodName.equals("flush")) { |
|
985 |
flush(); |
|
986 |
return null; |
|
987 |
} else { |
|
988 |
try { |
|
989 |
return method.invoke(conn, args); |
|
990 |
} catch (InvocationTargetException e) { |
|
991 |
throw e.getCause(); |
|
992 |
} |
|
993 |
} |
|
994 |
} |
|
995 |
||
996 |
private Object getAttribute(ObjectName objName, String attrName) |
|
997 |
throws MBeanException, InstanceNotFoundException, |
|
998 |
AttributeNotFoundException, ReflectionException, IOException { |
|
999 |
final NameValueMap values = getCachedAttributes( |
|
1000 |
objName, Collections.singleton(attrName)); |
|
1001 |
Object value = values.get(attrName); |
|
1002 |
if (value != null || values.containsKey(attrName)) { |
|
1003 |
return value; |
|
1004 |
} |
|
1005 |
// Not in cache, presumably because it was omitted from the |
|
1006 |
// getAttributes result because of an exception. Following |
|
1007 |
// call will probably provoke the same exception. |
|
1008 |
return conn.getAttribute(objName, attrName); |
|
1009 |
} |
|
1010 |
||
1011 |
private AttributeList getAttributes( |
|
1012 |
ObjectName objName, String[] attrNames) throws |
|
1013 |
InstanceNotFoundException, ReflectionException, IOException { |
|
1014 |
final NameValueMap values = getCachedAttributes( |
|
1015 |
objName, |
|
1016 |
new TreeSet<String>(Arrays.asList(attrNames))); |
|
1017 |
final AttributeList list = new AttributeList(); |
|
1018 |
for (String attrName : attrNames) { |
|
1019 |
final Object value = values.get(attrName); |
|
1020 |
if (value != null || values.containsKey(attrName)) { |
|
1021 |
list.add(new Attribute(attrName, value)); |
|
1022 |
} |
|
1023 |
} |
|
1024 |
return list; |
|
1025 |
} |
|
1026 |
||
1027 |
private synchronized NameValueMap getCachedAttributes( |
|
1028 |
ObjectName objName, Set<String> attrNames) throws |
|
1029 |
InstanceNotFoundException, ReflectionException, IOException { |
|
1030 |
NameValueMap values = cachedValues.get(objName); |
|
1031 |
if (values != null && values.keySet().containsAll(attrNames)) { |
|
1032 |
return values; |
|
1033 |
} |
|
1034 |
attrNames = new TreeSet<String>(attrNames); |
|
1035 |
Set<String> oldNames = cachedNames.get(objName); |
|
1036 |
if (oldNames != null) { |
|
1037 |
attrNames.addAll(oldNames); |
|
1038 |
} |
|
1039 |
values = new NameValueMap(); |
|
1040 |
final AttributeList attrs = conn.getAttributes( |
|
1041 |
objName, |
|
1042 |
attrNames.toArray(new String[attrNames.size()])); |
|
1043 |
for (Attribute attr : attrs.asList()) { |
|
1044 |
values.put(attr.getName(), attr.getValue()); |
|
1045 |
} |
|
1046 |
cachedValues.put(objName, values); |
|
1047 |
cachedNames.put(objName, attrNames); |
|
1048 |
return values; |
|
1049 |
} |
|
1050 |
||
1051 |
// See http://www.artima.com/weblogs/viewpost.jsp?thread=79394 |
|
1052 |
private static <K, V> Map<K, V> newMap() { |
|
1053 |
return new HashMap<K, V>(); |
|
1054 |
} |
|
1055 |
} |
|
1056 |
} |