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