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