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 *
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.
24 */
25
26 package sun.tools.jconsole;
27
28 import com.sun.management.HotSpotDiagnosticMXBean;
29 import com.sun.tools.jconsole.JConsoleContext;
30 import com.sun.tools.jconsole.JConsoleContext.ConnectionState;
31 import java.beans.PropertyChangeListener;
32 import java.beans.PropertyChangeEvent;
33 import java.io.IOException;
34 import java.lang.management.*;
35 import static java.lang.management.ManagementFactory.*;
36 import java.lang.ref.WeakReference;
37 import java.lang.reflect.*;
38 import java.rmi.*;
39 import java.rmi.registry.*;
40 import java.rmi.server.*;
41 import java.util.*;
42 import javax.management.*;
43 import javax.management.remote.*;
44 import javax.management.remote.rmi.*;
45 import javax.rmi.ssl.SslRMIClientSocketFactory;
46 import javax.swing.event.SwingPropertyChangeSupport;
47 import sun.rmi.server.UnicastRef2;
48 import sun.rmi.transport.LiveRef;
49
50 public class ProxyClient implements JConsoleContext {
51
52 private ConnectionState connectionState = ConnectionState.DISCONNECTED;
53
54 // The SwingPropertyChangeSupport will fire events on the EDT
55 private SwingPropertyChangeSupport propertyChangeSupport =
56 new SwingPropertyChangeSupport(this, true);
57
58 private static Map<String, ProxyClient> cache =
59 Collections.synchronizedMap(new HashMap<String, ProxyClient>());
60
61 private volatile boolean isDead = true;
62 private String hostName = null;
63 private int port = 0;
64 private String userName = null;
65 private String password = null;
66 private boolean hasPlatformMXBeans = false;
67 private boolean hasHotSpotDiagnosticMXBean= false;
68 private boolean hasCompilationMXBean = false;
487 }
488
489 private static String getCacheKey(String hostName, int port,
490 String userName, String password) {
491 return (hostName == null ? "" : hostName) + ":" +
492 port + ":" +
493 (userName == null ? "" : userName) + ":" +
494 (password == null ? "" : password);
495 }
496
497 public String connectionName() {
498 return connectionName;
499 }
500
501 public String getDisplayName() {
502 return displayName;
503 }
504
505 public String toString() {
506 if (!isConnected()) {
507 return Resources.getText("ConnectionName (disconnected)", displayName);
508 } else {
509 return displayName;
510 }
511 }
512
513 public MBeanServerConnection getMBeanServerConnection() {
514 return mbsc;
515 }
516
517 public SnapshotMBeanServerConnection getSnapshotMBeanServerConnection() {
518 return server;
519 }
520
521 public String getUrl() {
522 return advancedUrl;
523 }
524
525 public String getHostName() {
526 return hostName;
527 }
578 }
579
580 /**
581 * Returns a map of MBeans with ObjectName as the key and MBeanInfo value
582 * of a given domain. If domain is <tt>null</tt>, all MBeans
583 * are returned. If no MBean found, an empty map is returned.
584 *
585 */
586 public Map<ObjectName, MBeanInfo> getMBeans(String domain)
587 throws IOException {
588
589 ObjectName name = null;
590 if (domain != null) {
591 try {
592 name = new ObjectName(domain + ":*");
593 } catch (MalformedObjectNameException e) {
594 // should not reach here
595 assert(false);
596 }
597 }
598 Set mbeans = server.queryNames(name, null);
599 Map<ObjectName,MBeanInfo> result =
600 new HashMap<ObjectName,MBeanInfo>(mbeans.size());
601 Iterator iterator = mbeans.iterator();
602 while (iterator.hasNext()) {
603 Object object = iterator.next();
604 if (object instanceof ObjectName) {
605 ObjectName o = (ObjectName)object;
606 try {
607 MBeanInfo info = server.getMBeanInfo(o);
608 result.put(o, info);
609 } catch (IntrospectionException e) {
610 // TODO: should log the error
611 } catch (InstanceNotFoundException e) {
612 // TODO: should log the error
613 } catch (ReflectionException e) {
614 // TODO: should log the error
615 }
616 }
617 }
618 return result;
619 }
620
621 /**
687 if (hasCompilationMXBean && compilationMBean == null) {
688 compilationMBean =
689 newPlatformMXBeanProxy(server, COMPILATION_MXBEAN_NAME,
690 CompilationMXBean.class);
691 }
692 return compilationMBean;
693 }
694
695 public Collection<MemoryPoolProxy> getMemoryPoolProxies()
696 throws IOException {
697
698 // TODO: How to deal with changes to the list??
699 if (memoryPoolProxies == null) {
700 ObjectName poolName = null;
701 try {
702 poolName = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*");
703 } catch (MalformedObjectNameException e) {
704 // should not reach here
705 assert(false);
706 }
707 Set mbeans = server.queryNames(poolName, null);
708 if (mbeans != null) {
709 memoryPoolProxies = new ArrayList<MemoryPoolProxy>();
710 Iterator iterator = mbeans.iterator();
711 while (iterator.hasNext()) {
712 ObjectName objName = (ObjectName) iterator.next();
713 MemoryPoolProxy p = new MemoryPoolProxy(this, objName);
714 memoryPoolProxies.add(p);
715 }
716 }
717 }
718 return memoryPoolProxies;
719 }
720
721 public synchronized Collection<GarbageCollectorMXBean> getGarbageCollectorMXBeans()
722 throws IOException {
723
724 // TODO: How to deal with changes to the list??
725 if (garbageCollectorMBeans == null) {
726 ObjectName gcName = null;
727 try {
728 gcName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*");
729 } catch (MalformedObjectNameException e) {
730 // should not reach here
731 assert(false);
732 }
733 Set mbeans = server.queryNames(gcName, null);
734 if (mbeans != null) {
735 garbageCollectorMBeans = new ArrayList<GarbageCollectorMXBean>();
736 Iterator iterator = mbeans.iterator();
737 while (iterator.hasNext()) {
738 ObjectName on = (ObjectName) iterator.next();
739 String name = GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE +
740 ",name=" + on.getKeyProperty("name");
741
742 GarbageCollectorMXBean mBean =
743 newPlatformMXBeanProxy(server, name,
744 GarbageCollectorMXBean.class);
745 garbageCollectorMBeans.add(mBean);
746 }
747 }
748 }
749 return garbageCollectorMBeans;
750 }
751
752 public synchronized MemoryMXBean getMemoryMXBean() throws IOException {
753 if (hasPlatformMXBeans && memoryMBean == null) {
754 memoryMBean =
755 newPlatformMXBeanProxy(server, MEMORY_MXBEAN_NAME,
756 MemoryMXBean.class);
|
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 *
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.
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 import sun.tools.jconsole.resources.Messages;
49
50 public class ProxyClient implements JConsoleContext {
51
52 private ConnectionState connectionState = ConnectionState.DISCONNECTED;
53
54 // The SwingPropertyChangeSupport will fire events on the EDT
55 private SwingPropertyChangeSupport propertyChangeSupport =
56 new SwingPropertyChangeSupport(this, true);
57
58 private static Map<String, ProxyClient> cache =
59 Collections.synchronizedMap(new HashMap<String, ProxyClient>());
60
61 private volatile boolean isDead = true;
62 private String hostName = null;
63 private int port = 0;
64 private String userName = null;
65 private String password = null;
66 private boolean hasPlatformMXBeans = false;
67 private boolean hasHotSpotDiagnosticMXBean= false;
68 private boolean hasCompilationMXBean = false;
487 }
488
489 private static String getCacheKey(String hostName, int port,
490 String userName, String password) {
491 return (hostName == null ? "" : hostName) + ":" +
492 port + ":" +
493 (userName == null ? "" : userName) + ":" +
494 (password == null ? "" : password);
495 }
496
497 public String connectionName() {
498 return connectionName;
499 }
500
501 public String getDisplayName() {
502 return displayName;
503 }
504
505 public String toString() {
506 if (!isConnected()) {
507 return Resources.format(Messages.CONNECTION_NAME__DISCONNECTED_, displayName);
508 } else {
509 return displayName;
510 }
511 }
512
513 public MBeanServerConnection getMBeanServerConnection() {
514 return mbsc;
515 }
516
517 public SnapshotMBeanServerConnection getSnapshotMBeanServerConnection() {
518 return server;
519 }
520
521 public String getUrl() {
522 return advancedUrl;
523 }
524
525 public String getHostName() {
526 return hostName;
527 }
578 }
579
580 /**
581 * Returns a map of MBeans with ObjectName as the key and MBeanInfo value
582 * of a given domain. If domain is <tt>null</tt>, all MBeans
583 * are returned. If no MBean found, an empty map is returned.
584 *
585 */
586 public Map<ObjectName, MBeanInfo> getMBeans(String domain)
587 throws IOException {
588
589 ObjectName name = null;
590 if (domain != null) {
591 try {
592 name = new ObjectName(domain + ":*");
593 } catch (MalformedObjectNameException e) {
594 // should not reach here
595 assert(false);
596 }
597 }
598 Set<ObjectName> mbeans = server.queryNames(name, null);
599 Map<ObjectName,MBeanInfo> result =
600 new HashMap<ObjectName,MBeanInfo>(mbeans.size());
601 Iterator<ObjectName> iterator = mbeans.iterator();
602 while (iterator.hasNext()) {
603 Object object = iterator.next();
604 if (object instanceof ObjectName) {
605 ObjectName o = (ObjectName)object;
606 try {
607 MBeanInfo info = server.getMBeanInfo(o);
608 result.put(o, info);
609 } catch (IntrospectionException e) {
610 // TODO: should log the error
611 } catch (InstanceNotFoundException e) {
612 // TODO: should log the error
613 } catch (ReflectionException e) {
614 // TODO: should log the error
615 }
616 }
617 }
618 return result;
619 }
620
621 /**
687 if (hasCompilationMXBean && compilationMBean == null) {
688 compilationMBean =
689 newPlatformMXBeanProxy(server, COMPILATION_MXBEAN_NAME,
690 CompilationMXBean.class);
691 }
692 return compilationMBean;
693 }
694
695 public Collection<MemoryPoolProxy> getMemoryPoolProxies()
696 throws IOException {
697
698 // TODO: How to deal with changes to the list??
699 if (memoryPoolProxies == null) {
700 ObjectName poolName = null;
701 try {
702 poolName = new ObjectName(MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",*");
703 } catch (MalformedObjectNameException e) {
704 // should not reach here
705 assert(false);
706 }
707 Set<ObjectName> mbeans = server.queryNames(poolName, null);
708 if (mbeans != null) {
709 memoryPoolProxies = new ArrayList<MemoryPoolProxy>();
710 Iterator<ObjectName> iterator = mbeans.iterator();
711 while (iterator.hasNext()) {
712 ObjectName objName = (ObjectName) iterator.next();
713 MemoryPoolProxy p = new MemoryPoolProxy(this, objName);
714 memoryPoolProxies.add(p);
715 }
716 }
717 }
718 return memoryPoolProxies;
719 }
720
721 public synchronized Collection<GarbageCollectorMXBean> getGarbageCollectorMXBeans()
722 throws IOException {
723
724 // TODO: How to deal with changes to the list??
725 if (garbageCollectorMBeans == null) {
726 ObjectName gcName = null;
727 try {
728 gcName = new ObjectName(GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*");
729 } catch (MalformedObjectNameException e) {
730 // should not reach here
731 assert(false);
732 }
733 Set<ObjectName> mbeans = server.queryNames(gcName, null);
734 if (mbeans != null) {
735 garbageCollectorMBeans = new ArrayList<GarbageCollectorMXBean>();
736 Iterator<ObjectName> iterator = mbeans.iterator();
737 while (iterator.hasNext()) {
738 ObjectName on = (ObjectName) iterator.next();
739 String name = GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE +
740 ",name=" + on.getKeyProperty("name");
741
742 GarbageCollectorMXBean mBean =
743 newPlatformMXBeanProxy(server, name,
744 GarbageCollectorMXBean.class);
745 garbageCollectorMBeans.add(mBean);
746 }
747 }
748 }
749 return garbageCollectorMBeans;
750 }
751
752 public synchronized MemoryMXBean getMemoryMXBean() throws IOException {
753 if (hasPlatformMXBeans && memoryMBean == null) {
754 memoryMBean =
755 newPlatformMXBeanProxy(server, MEMORY_MXBEAN_NAME,
756 MemoryMXBean.class);
|