src/java.management/share/classes/javax/management/MBeanServerInvocationHandler.java
author darcy
Wed, 23 Oct 2019 13:01:40 -0700
changeset 58766 54ffb15c4839
parent 47216 71c04702a3d5
permissions -rw-r--r--
8232442: Suppress warnings on non-serializable non-transient instance fields in java.management.* Reviewed-by: rriggs, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18805
diff changeset
     2
 * Copyright (c) 2002, 2013, 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: 4156
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: 4156
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: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
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 javax.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jmx.mbeanserver.MXBeanProxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.reflect.InvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>{@link InvocationHandler} that forwards methods in an MBean's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * management interface through the MBean server to the MBean.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>Given an {@link MBeanServerConnection}, the {@link ObjectName}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * of an MBean within that MBean server, and a Java interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>Intf</code> that describes the management interface of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * MBean using the patterns for a Standard MBean or an MXBean, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * class can be used to construct a proxy for the MBean.  The proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * implements the interface <code>Intf</code> such that all of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * methods are forwarded through the MBean server to the MBean.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <p>If the {@code InvocationHandler} is for an MXBean, then the parameters of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * a method are converted from the type declared in the MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * interface into the corresponding mapped type, and the return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * is converted from the mapped type into the declared type.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * example, with the method<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * {@code public List<String> reverse(List<String> list);}<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * and given that the mapped type for {@code List<String>} is {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * String[]}, a call to {@code proxy.reverse(someList)} will convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * {@code someList} from a {@code List<String>} to a {@code String[]},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * call the MBean operation {@code reverse}, then convert the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * {@code String[]} into a {@code List<String>}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p>The method Object.toString(), Object.hashCode(), or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * Object.equals(Object), when invoked on a proxy using this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * invocation handler, is forwarded to the MBean server as a method on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * the proxied MBean only if it appears in one of the proxy's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * interfaces.  For a proxy created with {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * JMX.newMBeanProxy} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * JMX#newMXBeanProxy(MBeanServerConnection, ObjectName, Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * JMX.newMXBeanProxy}, this means that the method must appear in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * Standard MBean or MXBean interface.  Otherwise these methods have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * the following behavior:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <li>toString() returns a string representation of the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <li>hashCode() returns a hash code for the proxy such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * that two equal proxies have the same hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <li>equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * returns true if and only if the Object argument is of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * proxy class as this proxy, with an MBeanServerInvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * that has the same MBeanServerConnection and ObjectName; if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * of the {@code MBeanServerInvocationHandler}s was constructed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * a {@code Class} argument then the other must have been constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * with the same {@code Class} for {@code equals} to return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
public class MBeanServerInvocationHandler implements InvocationHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * <p>Invocation handler that forwards methods through an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * server to a Standard MBean.  This constructor may be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * instead of relying on {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * JMX.newMBeanProxy}, for instance if you need to supply a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * different {@link ClassLoader} to {@link Proxy#newProxyInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * Proxy.newProxyInstance}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * <p>This constructor is not appropriate for an MXBean.  Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * {@link #MBeanServerInvocationHandler(MBeanServerConnection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * ObjectName, boolean)} for that.  This constructor is equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * to {@code new MBeanServerInvocationHandler(connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * objectName, false)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param connection the MBean server connection through which all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * methods of a proxy using this handler will be forwarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param objectName the name of the MBean within the MBean server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * to which methods will be forwarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public MBeanServerInvocationHandler(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                        ObjectName objectName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   114
        this(connection, objectName, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p>Invocation handler that can forward methods through an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * server to a Standard MBean or MXBean.  This constructor may be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * instead of relying on {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * JMX#newMXBeanProxy(MBeanServerConnection, ObjectName, Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * JMX.newMXBeanProxy}, for instance if you need to supply a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * different {@link ClassLoader} to {@link Proxy#newProxyInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Proxy.newProxyInstance}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @param connection the MBean server connection through which all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * methods of a proxy using this handler will be forwarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param objectName the name of the MBean within the MBean server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * to which methods will be forwarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param isMXBean if true, the proxy is for an {@link MXBean}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * appropriate mappings will be applied to method parameters and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public MBeanServerInvocationHandler(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                        ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                                        boolean isMXBean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (connection == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new IllegalArgumentException("Null connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
31701
b4d934543e0a 8077407: Even better MBean connections
jbachorik
parents: 31700
diff changeset
   144
        if (Proxy.isProxyClass(connection.getClass())) {
b4d934543e0a 8077407: Even better MBean connections
jbachorik
parents: 31700
diff changeset
   145
            if (MBeanServerInvocationHandler.class.isAssignableFrom(
b4d934543e0a 8077407: Even better MBean connections
jbachorik
parents: 31700
diff changeset
   146
                    Proxy.getInvocationHandler(connection).getClass())) {
b4d934543e0a 8077407: Even better MBean connections
jbachorik
parents: 31700
diff changeset
   147
                throw new IllegalArgumentException("Wrapping MBeanServerInvocationHandler");
b4d934543e0a 8077407: Even better MBean connections
jbachorik
parents: 31700
diff changeset
   148
            }
b4d934543e0a 8077407: Even better MBean connections
jbachorik
parents: 31700
diff changeset
   149
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (objectName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw new IllegalArgumentException("Null object name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        this.connection = connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        this.objectName = objectName;
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   155
        this.isMXBean = isMXBean;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <p>The MBean server connection through which the methods of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * a proxy using this handler are forwarded.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return the MBean server connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public MBeanServerConnection getMBeanServerConnection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * <p>The name of the MBean within the MBean server to which methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * are forwarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @return the object name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public ObjectName getObjectName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        return objectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * <p>If true, the proxy is for an MXBean, and appropriate mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * are applied to method parameters and return values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * @return whether the proxy is for an MXBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public boolean isMXBean() {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   191
        return isMXBean;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * <p>Return a proxy that implements the given interface by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * forwarding its methods through the given MBean server to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * named MBean.  As of 1.6, the methods {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * {@link JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * boolean)} are preferred to this method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <p>This method is equivalent to {@link Proxy#newProxyInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * Proxy.newProxyInstance}<code>(interfaceClass.getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * interfaces, handler)</code>.  Here <code>handler</code> is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * result of {@link #MBeanServerInvocationHandler new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * MBeanServerInvocationHandler(connection, objectName)}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * <code>interfaces</code> is an array that has one element if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * <code>notificationBroadcaster</code> is false and two if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * true.  The first element of <code>interfaces</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <code>interfaceClass</code> and the second, if present, is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <code>NotificationEmitter.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @param connection the MBean server to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @param objectName the name of the MBean within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * <code>connection</code> to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * @param interfaceClass the management interface that the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * exports, which will also be implemented by the returned proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @param notificationBroadcaster make the returned proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * implement {@link NotificationEmitter} by forwarding its methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * via <code>connection</code>. A call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * NotificationBroadcaster#addNotificationListener} on the proxy will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * result in a call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * MBeanServerConnection#addNotificationListener(ObjectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * NotificationListener, NotificationFilter, Object)}, and likewise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * for the other methods of {@link NotificationBroadcaster} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * NotificationEmitter}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param <T> allows the compiler to know that if the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * interfaceClass} parameter is {@code MyMBean.class}, for example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * then the return type is {@code MyMBean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @return the new proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents: 18213
diff changeset
   234
     * @see JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class, boolean)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    public static <T> T newProxyInstance(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                         ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                                         Class<T> interfaceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                                         boolean notificationBroadcaster) {
18213
45c8ed869a1b 8008982: Adjust JMX for underlying interface changes
jbachorik
parents: 5506
diff changeset
   240
        return JMX.newMBeanProxy(connection, objectName, interfaceClass, notificationBroadcaster);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    public Object invoke(Object proxy, Method method, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            throws Throwable {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   245
        final Class<?> methodClass = method.getDeclaringClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        if (methodClass.equals(NotificationBroadcaster.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            || methodClass.equals(NotificationEmitter.class))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            return invokeBroadcasterMethod(proxy, method, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        // local or not: equals, toString, hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (shouldDoLocally(proxy, method))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return doLocally(proxy, method, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        try {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   256
            if (isMXBean()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                MXBeanProxy p = findMXBeanProxy(methodClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                return p.invoke(connection, objectName, method, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                final String methodName = method.getName();
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   261
                final Class<?>[] paramTypes = method.getParameterTypes();
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   262
                final Class<?> returnType = method.getReturnType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                /* Inexplicably, InvocationHandler specifies that args is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                   when the method takes no arguments rather than a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                   zero-length array.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                final int nargs = (args == null) ? 0 : args.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                if (methodName.startsWith("get")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    && methodName.length() > 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    && nargs == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    && !returnType.equals(Void.TYPE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    return connection.getAttribute(objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        methodName.substring(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                if (methodName.startsWith("is")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    && methodName.length() > 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    && nargs == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                    && (returnType.equals(Boolean.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    || returnType.equals(Boolean.class))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    return connection.getAttribute(objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        methodName.substring(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                if (methodName.startsWith("set")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    && methodName.length() > 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    && nargs == 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    && returnType.equals(Void.TYPE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    Attribute attr = new Attribute(methodName.substring(3), args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                    connection.setAttribute(objectName, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                final String[] signature = new String[paramTypes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                for (int i = 0; i < paramTypes.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    signature[i] = paramTypes[i].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                return connection.invoke(objectName, methodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                         args, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        } catch (MBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            throw e.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        } catch (RuntimeMBeanException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            throw re.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        } catch (RuntimeErrorException rre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            throw rre.getTargetError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        /* The invoke may fail because it can't get to the MBean, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
           one of the these exceptions declared by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
           MBeanServerConnection.invoke:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
           - RemoteException: can't talk to MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
           - InstanceNotFoundException: objectName is not registered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
           - ReflectionException: objectName is registered but does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
             have the method being invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
           In all of these cases, the exception will be wrapped by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
           proxy mechanism in an UndeclaredThrowableException unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
           it happens to be declared in the "throws" clause of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
           method being invoked on the proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   322
    private static MXBeanProxy findMXBeanProxy(Class<?> mxbeanInterface) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        synchronized (mxbeanProxies) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   324
            WeakReference<MXBeanProxy> proxyRef =
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   325
                    mxbeanProxies.get(mxbeanInterface);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   326
            MXBeanProxy p = (proxyRef == null) ? null : proxyRef.get();
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   327
            if (p == null) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   328
                try {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   329
                    p = new MXBeanProxy(mxbeanInterface);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   330
                } catch (IllegalArgumentException e) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   331
                    String msg = "Cannot make MXBean proxy for " +
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   332
                            mxbeanInterface.getName() + ": " + e.getMessage();
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   333
                    IllegalArgumentException iae =
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   334
                            new IllegalArgumentException(msg, e.getCause());
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   335
                    iae.setStackTrace(e.getStackTrace());
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   336
                    throw iae;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   337
                }
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   338
                mxbeanProxies.put(mxbeanInterface,
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   339
                                  new WeakReference<MXBeanProxy>(p));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    }
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   344
    private static final WeakHashMap<Class<?>, WeakReference<MXBeanProxy>>
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   345
            mxbeanProxies = new WeakHashMap<Class<?>, WeakReference<MXBeanProxy>>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    private Object invokeBroadcasterMethod(Object proxy, Method method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                           Object[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        final String methodName = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        final int nargs = (args == null) ? 0 : args.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        if (methodName.equals("addNotificationListener")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            /* The various throws of IllegalArgumentException here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
               should not happen, since we know what the methods in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
               NotificationBroadcaster and NotificationEmitter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
               are.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            if (nargs != 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    "Bad arg count to addNotificationListener: " + nargs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            /* Other inconsistencies will produce ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
               below.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            NotificationListener listener = (NotificationListener) args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            NotificationFilter filter = (NotificationFilter) args[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            Object handback = args[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            connection.addNotificationListener(objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                                               listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                                               filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                                               handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        } else if (methodName.equals("removeNotificationListener")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            /* NullPointerException if method with no args, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
               shouldn't happen because removeNL does have args.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            NotificationListener listener = (NotificationListener) args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            switch (nargs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                connection.removeNotificationListener(objectName, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                NotificationFilter filter = (NotificationFilter) args[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                Object handback = args[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                connection.removeNotificationListener(objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                                                      listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                                                      filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                                                      handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    "Bad arg count to removeNotificationListener: " + nargs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        } else if (methodName.equals("getNotificationInfo")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            if (args != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                throw new IllegalArgumentException("getNotificationInfo has " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                                   "args");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            MBeanInfo info = connection.getMBeanInfo(objectName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            return info.getNotifications();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            throw new IllegalArgumentException("Bad method name: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                                               methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    private boolean shouldDoLocally(Object proxy, Method method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        final String methodName = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        if ((methodName.equals("hashCode") || methodName.equals("toString"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            && method.getParameterTypes().length == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            && isLocal(proxy, method))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        if (methodName.equals("equals")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            && Arrays.equals(method.getParameterTypes(),
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   424
                             new Class<?>[] {Object.class})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            && isLocal(proxy, method))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            return true;
31701
b4d934543e0a 8077407: Even better MBean connections
jbachorik
parents: 31700
diff changeset
   427
        if (methodName.equals("finalize")
b4d934543e0a 8077407: Even better MBean connections
jbachorik
parents: 31700
diff changeset
   428
            && method.getParameterTypes().length == 0) {
b4d934543e0a 8077407: Even better MBean connections
jbachorik
parents: 31700
diff changeset
   429
            return true;
b4d934543e0a 8077407: Even better MBean connections
jbachorik
parents: 31700
diff changeset
   430
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    private Object doLocally(Object proxy, Method method, Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        final String methodName = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        if (methodName.equals("equals")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            if (this == args[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            if (!(args[0] instanceof Proxy)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            final InvocationHandler ihandler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                Proxy.getInvocationHandler(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (ihandler == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                !(ihandler instanceof MBeanServerInvocationHandler)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            final MBeanServerInvocationHandler handler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                (MBeanServerInvocationHandler)ihandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            return connection.equals(handler.connection) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                objectName.equals(handler.objectName) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                proxy.getClass().equals(args[0].getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        } else if (methodName.equals("toString")) {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   462
            return (isMXBean() ? "MX" : "M") + "BeanProxy(" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                connection + "[" + objectName + "])";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        } else if (methodName.equals("hashCode")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            return objectName.hashCode()+connection.hashCode();
31700
e803266598cb 8076397: Better MBean connections
jbachorik
parents: 25859
diff changeset
   466
        } else if (methodName.equals("finalize")) {
e803266598cb 8076397: Better MBean connections
jbachorik
parents: 25859
diff changeset
   467
            // ignore the finalizer invocation via proxy
e803266598cb 8076397: Better MBean connections
jbachorik
parents: 25859
diff changeset
   468
            return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        throw new RuntimeException("Unexpected method name: " + methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    private static boolean isLocal(Object proxy, Method method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        final Class<?>[] interfaces = proxy.getClass().getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        if(interfaces == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        final String methodName = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        final Class<?>[] params = method.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        for (Class<?> intf : interfaces) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                intf.getMethod(methodName, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                return false; // found method in one of our interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            } catch (NoSuchMethodException nsme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                // OK.
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
        return true;  // did not find in any interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    private final MBeanServerConnection connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    private final ObjectName objectName;
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1512
diff changeset
   496
    private final boolean isMXBean;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
}