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