jdk/src/share/classes/javax/management/MBeanServerInvocationHandler.java
author emcmanus
Mon, 27 Oct 2008 14:02:40 +0100
changeset 1510 e747d3193ef2
parent 715 f16baef3a20e
child 1512 0f8eb7ad4ef5
permissions -rw-r--r--
6763639: Remove "rawtypes" warnings from JMX code Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 687
diff changeset
     2
 * Copyright 2002-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package 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;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    36
import javax.management.openmbean.MXBeanMappingFactory;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    37
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    38
import static javax.management.JMX.MBeanOptions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>{@link InvocationHandler} that forwards methods in an MBean's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * management interface through the MBean server to the MBean.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>Given an {@link MBeanServerConnection}, the {@link ObjectName}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * of an MBean within that MBean server, and a Java interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <code>Intf</code> that describes the management interface of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * MBean using the patterns for a Standard MBean or an MXBean, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * class can be used to construct a proxy for the MBean.  The proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * implements the interface <code>Intf</code> such that all of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * methods are forwarded through the MBean server to the MBean.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>If the {@code InvocationHandler} is for an MXBean, then the parameters of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * a method are converted from the type declared in the MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * interface into the corresponding mapped type, and the return value
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * is converted from the mapped type into the declared type.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * example, with the method<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * {@code public List<String> reverse(List<String> list);}<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * and given that the mapped type for {@code List<String>} is {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * String[]}, a call to {@code proxy.reverse(someList)} will convert
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * {@code someList} from a {@code List<String>} to a {@code String[]},
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * call the MBean operation {@code reverse}, then convert the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * {@code String[]} into a {@code List<String>}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <p>The method Object.toString(), Object.hashCode(), or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Object.equals(Object), when invoked on a proxy using this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * invocation handler, is forwarded to the MBean server as a method on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * the proxied MBean only if it appears in one of the proxy's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * interfaces.  For a proxy created with {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * JMX.newMBeanProxy} or {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * JMX#newMXBeanProxy(MBeanServerConnection, ObjectName, Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * JMX.newMXBeanProxy}, this means that the method must appear in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * Standard MBean or MXBean interface.  Otherwise these methods have
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * the following behavior:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <li>toString() returns a string representation of the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <li>hashCode() returns a hash code for the proxy such
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * that two equal proxies have the same hash code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * <li>equals(Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * returns true if and only if the Object argument is of the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * proxy class as this proxy, with an MBeanServerInvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * that has the same MBeanServerConnection and ObjectName; if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * of the {@code MBeanServerInvocationHandler}s was constructed with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * a {@code Class} argument then the other must have been constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * with the same {@code Class} for {@code equals} to return true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
public class MBeanServerInvocationHandler implements InvocationHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <p>Invocation handler that forwards methods through an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * server to a Standard MBean.  This constructor may be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * instead of relying on {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * JMX.newMBeanProxy}, for instance if you need to supply a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * different {@link ClassLoader} to {@link Proxy#newProxyInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Proxy.newProxyInstance}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <p>This constructor is not appropriate for an MXBean.  Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * {@link #MBeanServerInvocationHandler(MBeanServerConnection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * ObjectName, boolean)} for that.  This constructor is equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * to {@code new MBeanServerInvocationHandler(connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * objectName, false)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @param connection the MBean server connection through which all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * methods of a proxy using this handler will be forwarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param objectName the name of the MBean within the MBean server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * to which methods will be forwarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public MBeanServerInvocationHandler(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                        ObjectName objectName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   117
        this(connection, objectName, null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * <p>Invocation handler that can forward methods through an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * server to a Standard MBean or MXBean.  This constructor may be called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * instead of relying on {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * JMX#newMXBeanProxy(MBeanServerConnection, ObjectName, Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * JMX.newMXBeanProxy}, for instance if you need to supply a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * different {@link ClassLoader} to {@link Proxy#newProxyInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Proxy.newProxyInstance}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param connection the MBean server connection through which all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * methods of a proxy using this handler will be forwarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param objectName the name of the MBean within the MBean server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * to which methods will be forwarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @param isMXBean if true, the proxy is for an {@link MXBean}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * appropriate mappings will be applied to method parameters and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public MBeanServerInvocationHandler(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                        ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                        boolean isMXBean) {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   144
        this(connection, objectName, isMXBean ? MBeanOptions.MXBEAN : null);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   145
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   146
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   147
    public MBeanServerInvocationHandler(MBeanServerConnection connection,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   148
                                        ObjectName objectName,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   149
                                        MBeanOptions options) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   150
        if (options == null)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   151
            options = new MBeanOptions();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (connection == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            throw new IllegalArgumentException("Null connection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (objectName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new IllegalArgumentException("Null object name");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        this.connection = connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        this.objectName = objectName;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   160
        this.options = options.canonical();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * <p>The MBean server connection through which the methods of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * a proxy using this handler are forwarded.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @return the MBean server connection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public MBeanServerConnection getMBeanServerConnection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        return connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * <p>The name of the MBean within the MBean server to which methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * are forwarded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @return the object name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public ObjectName getObjectName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        return objectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * <p>If true, the proxy is for an MXBean, and appropriate mappings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * are applied to method parameters and return values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @return whether the proxy is for an MXBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    public boolean isMXBean() {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   196
        return options.isMXBean();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   197
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   198
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   199
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   200
     * <p>Return the {@link MBeanOptions} used for this proxy.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   201
     *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   202
     * @return the MBeanOptions.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   203
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   204
    public MBeanOptions getMBeanOptions() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   205
        return options.uncanonical();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * <p>Return a proxy that implements the given interface by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * forwarding its methods through the given MBean server to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * named MBean.  As of 1.6, the methods {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class)} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * {@link JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * boolean)} are preferred to this method.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <p>This method is equivalent to {@link Proxy#newProxyInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Proxy.newProxyInstance}<code>(interfaceClass.getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * interfaces, handler)</code>.  Here <code>handler</code> is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * result of {@link #MBeanServerInvocationHandler new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * MBeanServerInvocationHandler(connection, objectName)}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * <code>interfaces</code> is an array that has one element if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * <code>notificationBroadcaster</code> is false and two if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * true.  The first element of <code>interfaces</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * <code>interfaceClass</code> and the second, if present, is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <code>NotificationEmitter.class</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param connection the MBean server to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param objectName the name of the MBean within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <code>connection</code> to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @param interfaceClass the management interface that the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * exports, which will also be implemented by the returned proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param notificationBroadcaster make the returned proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * implement {@link NotificationEmitter} by forwarding its methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * via <code>connection</code>. A call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * NotificationBroadcaster#addNotificationListener} on the proxy will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * result in a call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * MBeanServerConnection#addNotificationListener(ObjectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * NotificationListener, NotificationFilter, Object)}, and likewise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * for the other methods of {@link NotificationBroadcaster} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * NotificationEmitter}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param <T> allows the compiler to know that if the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * interfaceClass} parameter is {@code MyMBean.class}, for example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * then the return type is {@code MyMBean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @return the new proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @see JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public static <T> T newProxyInstance(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                         ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                                         Class<T> interfaceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                         boolean notificationBroadcaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        final InvocationHandler handler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            new MBeanServerInvocationHandler(connection, objectName);
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   256
        final Class<?>[] interfaces;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (notificationBroadcaster) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            interfaces =
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   259
                new Class<?>[] {interfaceClass, NotificationEmitter.class};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        } else
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   261
            interfaces = new Class<?>[] {interfaceClass};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        Object proxy =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            Proxy.newProxyInstance(interfaceClass.getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                   interfaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                   handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return interfaceClass.cast(proxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public Object invoke(Object proxy, Method method, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            throws Throwable {
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   272
        final Class<?> methodClass = method.getDeclaringClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        if (methodClass.equals(NotificationBroadcaster.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            || methodClass.equals(NotificationEmitter.class))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            return invokeBroadcasterMethod(proxy, method, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        // local or not: equals, toString, hashCode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        if (shouldDoLocally(proxy, method))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            return doLocally(proxy, method, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        try {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   283
            if (isMXBean()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                MXBeanProxy p = findMXBeanProxy(methodClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                return p.invoke(connection, objectName, method, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                final String methodName = method.getName();
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   288
                final Class<?>[] paramTypes = method.getParameterTypes();
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   289
                final Class<?> returnType = method.getReturnType();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                /* Inexplicably, InvocationHandler specifies that args is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                   when the method takes no arguments rather than a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                   zero-length array.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                final int nargs = (args == null) ? 0 : args.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                if (methodName.startsWith("get")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    && methodName.length() > 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    && nargs == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    && !returnType.equals(Void.TYPE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    return connection.getAttribute(objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        methodName.substring(3));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                if (methodName.startsWith("is")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    && methodName.length() > 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    && nargs == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    && (returnType.equals(Boolean.TYPE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    || returnType.equals(Boolean.class))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    return connection.getAttribute(objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                        methodName.substring(2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                if (methodName.startsWith("set")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                    && methodName.length() > 3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                    && nargs == 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    && returnType.equals(Void.TYPE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    Attribute attr = new Attribute(methodName.substring(3), args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    connection.setAttribute(objectName, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                final String[] signature = new String[paramTypes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                for (int i = 0; i < paramTypes.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    signature[i] = paramTypes[i].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                return connection.invoke(objectName, methodName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                         args, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        } catch (MBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            throw e.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } catch (RuntimeMBeanException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            throw re.getTargetException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        } catch (RuntimeErrorException rre) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            throw rre.getTargetError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        /* The invoke may fail because it can't get to the MBean, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
           one of the these exceptions declared by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
           MBeanServerConnection.invoke:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
           - RemoteException: can't talk to MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
           - InstanceNotFoundException: objectName is not registered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
           - ReflectionException: objectName is registered but does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
             have the method being invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
           In all of these cases, the exception will be wrapped by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
           proxy mechanism in an UndeclaredThrowableException unless
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
           it happens to be declared in the "throws" clause of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
           method being invoked on the proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   349
    private MXBeanProxy findMXBeanProxy(Class<?> mxbeanInterface) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   350
        MXBeanMappingFactory mappingFactory = options.getMXBeanMappingFactory();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        synchronized (mxbeanProxies) {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   352
            ClassToProxy classToProxy = mxbeanProxies.get(mappingFactory);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   353
            if (classToProxy == null) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   354
                classToProxy = new ClassToProxy();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   355
                mxbeanProxies.put(mappingFactory, classToProxy);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   357
            WeakReference<MXBeanProxy> wr = classToProxy.get(mxbeanInterface);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   358
            MXBeanProxy p;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   359
            if (wr != null) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   360
                p = wr.get();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   361
                if (p != null)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   362
                    return p;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   363
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   364
            p = new MXBeanProxy(mxbeanInterface, mappingFactory);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   365
            classToProxy.put(mxbeanInterface, new WeakReference<MXBeanProxy>(p));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   369
    private static final WeakHashMap<MXBeanMappingFactory, ClassToProxy>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   370
            mxbeanProxies = newWeakHashMap();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   371
    private static class ClassToProxy
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   372
            extends WeakHashMap<Class<?>, WeakReference<MXBeanProxy>> {}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   373
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   374
    private static <K, V> WeakHashMap<K, V> newWeakHashMap() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   375
        return new WeakHashMap<K, V>();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   376
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    private Object invokeBroadcasterMethod(Object proxy, Method method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                                           Object[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        final String methodName = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        final int nargs = (args == null) ? 0 : args.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (methodName.equals("addNotificationListener")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            /* The various throws of IllegalArgumentException here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
               should not happen, since we know what the methods in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
               NotificationBroadcaster and NotificationEmitter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
               are.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            if (nargs != 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    "Bad arg count to addNotificationListener: " + nargs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            /* Other inconsistencies will produce ClassCastException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
               below.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            NotificationListener listener = (NotificationListener) args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            NotificationFilter filter = (NotificationFilter) args[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            Object handback = args[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            connection.addNotificationListener(objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                                               listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                                               filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                                               handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        } else if (methodName.equals("removeNotificationListener")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            /* NullPointerException if method with no args, but that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
               shouldn't happen because removeNL does have args.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            NotificationListener listener = (NotificationListener) args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            switch (nargs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                connection.removeNotificationListener(objectName, listener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                NotificationFilter filter = (NotificationFilter) args[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                Object handback = args[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                connection.removeNotificationListener(objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                                                      listener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                                                      filter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                                                      handback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    "Bad arg count to removeNotificationListener: " + nargs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                throw new IllegalArgumentException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        } else if (methodName.equals("getNotificationInfo")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            if (args != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                throw new IllegalArgumentException("getNotificationInfo has " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                                                   "args");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            MBeanInfo info = connection.getMBeanInfo(objectName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            return info.getNotifications();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            throw new IllegalArgumentException("Bad method name: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                                               methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    private boolean shouldDoLocally(Object proxy, Method method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        final String methodName = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        if ((methodName.equals("hashCode") || methodName.equals("toString"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            && method.getParameterTypes().length == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            && isLocal(proxy, method))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (methodName.equals("equals")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            && Arrays.equals(method.getParameterTypes(),
1510
e747d3193ef2 6763639: Remove "rawtypes" warnings from JMX code
emcmanus
parents: 715
diff changeset
   455
                             new Class<?>[] {Object.class})
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            && isLocal(proxy, method))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    private Object doLocally(Object proxy, Method method, Object[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        final String methodName = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (methodName.equals("equals")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            if (this == args[0]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if (!(args[0] instanceof Proxy)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            final InvocationHandler ihandler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                Proxy.getInvocationHandler(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            if (ihandler == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                !(ihandler instanceof MBeanServerInvocationHandler)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            final MBeanServerInvocationHandler handler =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                (MBeanServerInvocationHandler)ihandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            return connection.equals(handler.connection) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                objectName.equals(handler.objectName) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                proxy.getClass().equals(args[0].getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        } else if (methodName.equals("toString")) {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   489
            return (isMXBean() ? "MX" : "M") + "BeanProxy(" +
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                connection + "[" + objectName + "])";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        } else if (methodName.equals("hashCode")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            return objectName.hashCode()+connection.hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        throw new RuntimeException("Unexpected method name: " + methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    private static boolean isLocal(Object proxy, Method method) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        final Class<?>[] interfaces = proxy.getClass().getInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        if(interfaces == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        final String methodName = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        final Class<?>[] params = method.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        for (Class<?> intf : interfaces) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                intf.getMethod(methodName, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                return false; // found method in one of our interfaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            } catch (NoSuchMethodException nsme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                // OK.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        return true;  // did not find in any interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    private final MBeanServerConnection connection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    private final ObjectName objectName;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   520
    private final MBeanOptions options;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
}