jdk/src/share/classes/com/sun/jmx/mbeanserver/MBeanIntrospector.java
author emcmanus
Thu, 05 Jun 2008 13:42:47 +0200
changeset 687 874e25a9844a
parent 287 bff5501b2a02
child 833 bfa2bef7517c
child 715 f16baef3a20e
permissions -rw-r--r--
6562936: Support custom type mappings in MXBeans Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.jmx.mbeanserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import static com.sun.jmx.mbeanserver.Util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.Array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.Constructor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.Type;
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
    37
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.Descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.ImmutableDescriptor;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
    43
import javax.management.IntrospectionException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.InvalidAttributeValueException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.MBeanAttributeInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.MBeanConstructorInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.MBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.MBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.MBeanNotificationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.MBeanOperationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.NotCompliantMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.management.NotificationBroadcaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import javax.management.ReflectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * An introspector for MBeans of a certain type.  There is one instance
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
    57
 * of this class for Standard MBeans, and one for every MXBeanMappingFactory;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
    58
 * these two cases correspond to the two concrete subclasses of this abstract
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
    59
 * class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @param <M> the representation of methods for this kind of MBean:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Method for Standard MBeans, ConvertingMethod for MXBeans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Using a type parameter <M> allows us to deal with the fact that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Method and ConvertingMethod have no useful common ancestor, on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * which we could call getName, getGenericReturnType, etc.  A simpler approach
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * would be to wrap every Method in an object that does have a common
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * ancestor with ConvertingMethod.  But that would mean an extra object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * for every Method in every Standard MBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
abstract class MBeanIntrospector<M> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    static final class PerInterfaceMap<M>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            extends WeakHashMap<Class<?>, WeakReference<PerInterface<M>>> {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /** The map from interface to PerInterface for this type of MBean. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    abstract PerInterfaceMap<M> getPerInterfaceMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * The map from concrete implementation class and interface to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * MBeanInfo for this type of MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    abstract MBeanInfoMap getMBeanInfoMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /** Make an interface analyzer for this type of MBean. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    abstract MBeanAnalyzer<M> getAnalyzer(Class<?> mbeanInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    throws NotCompliantMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /** True if MBeans with this kind of introspector are MXBeans. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    abstract boolean isMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /** Find the M corresponding to the given Method. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    abstract M mFrom(Method m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /** Get the name of this method. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    abstract String getName(M m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Get the return type of this method.  This is the return type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * of a method in a Java interface, so for MXBeans it is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * declared Java type, not the mapped Open Type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    abstract Type getGenericReturnType(M m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * Get the parameter types of this method in the Java interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * it came from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    abstract Type[] getGenericParameterTypes(M m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Get the signature of this method as a caller would have to supply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * it in MBeanServer.invoke.  For MXBeans, the named types will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * the mapped Open Types for the parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    abstract String[] getSignature(M m);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Check that this method is valid.  For example, a method in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * MXBean interface is not valid if one of its parameters cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * mapped to an Open Type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   124
    abstract void checkMethod(M m);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * Invoke the method with the given target and arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @param cookie Additional information about the target.  For an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * MXBean, this is the MXBeanLookup associated with the MXBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * It would be cleaner if the type of the cookie were a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * type parameter to this class, but that would involve a lot of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * messy type parameter propagation just to avoid a couple of casts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    abstract Object invokeM2(M m, Object target, Object[] args, Object cookie)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    throws InvocationTargetException, IllegalAccessException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            MBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Test whether the given value is valid for the given parameter of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * M.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    abstract boolean validParameter(M m, Object value, int paramNo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            Object cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Construct an MBeanAttributeInfo for the given attribute based on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * given getter and setter.  One but not both of the getter and setter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * may be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    abstract MBeanAttributeInfo getMBeanAttributeInfo(String attributeName,
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   154
            M getter, M setter) throws IntrospectionException;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   155
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Construct an MBeanOperationInfo for the given operation based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * the M it was derived from.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    abstract MBeanOperationInfo getMBeanOperationInfo(String operationName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            M operation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Get a Descriptor containing fields that MBeans of this kind will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * always have.  For example, MXBeans will always have "mxbean=true".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    abstract Descriptor getBasicMBeanDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Get a Descriptor containing additional fields beyond the ones
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * from getBasicMBeanDescriptor that MBeans whose concrete class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * is resourceClass will always have.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    abstract Descriptor getMBeanDescriptor(Class<?> resourceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   176
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   177
     * Get any additional Descriptor entries for this introspector instance.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   178
     * If there is a non-default MXBeanMappingFactory, it will appear in
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   179
     * this Descriptor.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   180
     * @return Additional Descriptor entries, or an empty Descriptor if none.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   181
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   182
    Descriptor getSpecificMBeanDescriptor() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   183
        return ImmutableDescriptor.EMPTY_DESCRIPTOR;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   184
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   185
287
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   186
    void checkCompliance(Class<?> mbeanType) throws NotCompliantMBeanException {
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   187
        if (!mbeanType.isInterface()) {
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   188
            throw new NotCompliantMBeanException("Not an interface: " +
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   189
                    mbeanType.getName());
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   190
        }
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   191
    }
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   192
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   193
    /**
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   194
     * Get the methods to be analyzed to build the MBean interface.
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   195
     */
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   196
    List<Method> getMethods(final Class<?> mbeanType) throws Exception {
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   197
        return Arrays.asList(mbeanType.getMethods());
bff5501b2a02 6610917: Define a generic NotificationFilter
emcmanus
parents: 2
diff changeset
   198
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    final PerInterface<M> getPerInterface(Class<?> mbeanInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        PerInterfaceMap<M> map = getPerInterfaceMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        synchronized (map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            WeakReference<PerInterface<M>> wr = map.get(mbeanInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            PerInterface<M> pi = (wr == null) ? null : wr.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (pi == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    MBeanAnalyzer<M> analyzer = getAnalyzer(mbeanInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    MBeanInfo mbeanInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                            makeInterfaceMBeanInfo(mbeanInterface, analyzer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    pi = new PerInterface<M>(mbeanInterface, this, analyzer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                            mbeanInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    wr = new WeakReference<PerInterface<M>>(pi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    map.put(mbeanInterface, wr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                } catch (Exception x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    throw Introspector.throwException(mbeanInterface,x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return pi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Make the MBeanInfo skeleton for the given MBean interface using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * the given analyzer.  This will never be the MBeanInfo of any real
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * MBean (because the getClassName() must be a concrete class), but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * its MBeanAttributeInfo[] and MBeanOperationInfo[] can be inserted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * into such an MBeanInfo, and its Descriptor can be the basis for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * the MBeanInfo's Descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private MBeanInfo makeInterfaceMBeanInfo(Class<?> mbeanInterface,
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   232
            MBeanAnalyzer<M> analyzer) throws IntrospectionException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        final MBeanInfoMaker maker = new MBeanInfoMaker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        analyzer.visit(maker);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        final String description =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                "Information on the management interface of the MBean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        return maker.makeMBeanInfo(mbeanInterface, description);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    /** True if the given getter and setter are consistent. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    final boolean consistent(M getter, M setter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        return (getter == null || setter == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                getGenericReturnType(getter).equals(getGenericParameterTypes(setter)[0]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Invoke the given M on the given target with the given args and cookie.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Wrap exceptions appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    final Object invokeM(M m, Object target, Object[] args, Object cookie)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    throws MBeanException, ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            return invokeM2(m, target, args, cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            unwrapInvocationTargetException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            throw new RuntimeException(e); // not reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            throw new ReflectionException(e, e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        /* We do not catch and wrap RuntimeException or Error,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
         * because we're in a DynamicMBean, so the logic for DynamicMBeans
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
         * will do the wrapping.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * Invoke the given setter on the given target with the given argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * and cookie.  Wrap exceptions appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /* If the value is of the wrong type for the method we are about to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * invoke, we are supposed to throw an InvalidAttributeValueException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * Rather than making the check always, we invoke the method, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * if it throws an exception we check the type to see if that was
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * what caused the exception.  The assumption is that an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * from an invalid type will arise before any user method is ever
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * called (either in reflection or in OpenConverter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    final void invokeSetter(String name, M setter, Object target, Object arg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            Object cookie)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            throws MBeanException, ReflectionException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            InvalidAttributeValueException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            invokeM2(setter, target, new Object[] {arg}, cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        } catch (IllegalAccessException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            throw new ReflectionException(e, e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } catch (RuntimeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            maybeInvalidParameter(name, setter, arg, cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        } catch (InvocationTargetException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            maybeInvalidParameter(name, setter, arg, cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            unwrapInvocationTargetException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    private void maybeInvalidParameter(String name, M setter, Object arg,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            Object cookie)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            throws InvalidAttributeValueException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (!validParameter(setter, arg, 0, cookie)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    "Invalid value for attribute " + name + ": " + arg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            throw new InvalidAttributeValueException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    static boolean isValidParameter(Method m, Object value, int paramNo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        Class<?> c = m.getParameterTypes()[paramNo];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            // Following is expensive but we only call this method to determine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            // if an exception is due to an incompatible parameter type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            // Plain old c.isInstance doesn't work for primitive types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            Object a = Array.newInstance(c, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            Array.set(a, 0, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            unwrapInvocationTargetException(InvocationTargetException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            throws MBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        Throwable t = e.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        if (t instanceof RuntimeException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throw (RuntimeException) t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        else if (t instanceof Error)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            throw (Error) t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            throw new MBeanException((Exception) t,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                    (t == null ? null : t.toString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    /** A visitor that constructs the per-interface MBeanInfo. */
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   333
    private class MBeanInfoMaker
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   334
            implements MBeanAnalyzer.MBeanVisitor<M, IntrospectionException> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        public void visitAttribute(String attributeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                M getter,
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   338
                M setter) throws IntrospectionException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            MBeanAttributeInfo mbai =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    getMBeanAttributeInfo(attributeName, getter, setter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            attrs.add(mbai);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        public void visitOperation(String operationName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                M operation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            MBeanOperationInfo mboi =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    getMBeanOperationInfo(operationName, operation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            ops.add(mboi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        /** Make an MBeanInfo based on the attributes and operations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
         *  found in the interface. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        MBeanInfo makeMBeanInfo(Class<?> mbeanInterface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                String description) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            final MBeanAttributeInfo[] attrArray =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    attrs.toArray(new MBeanAttributeInfo[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            final MBeanOperationInfo[] opArray =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    ops.toArray(new MBeanOperationInfo[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            final String interfaceClassName =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    "interfaceClassName=" + mbeanInterface.getName();
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   363
            final Descriptor classNameDescriptor =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    new ImmutableDescriptor(interfaceClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            final Descriptor mbeanDescriptor = getBasicMBeanDescriptor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            final Descriptor annotatedDescriptor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    Introspector.descriptorForElement(mbeanInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            final Descriptor descriptor =
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   369
                DescriptorCache.getInstance().union(
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   370
                    classNameDescriptor,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    mbeanDescriptor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    annotatedDescriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return new MBeanInfo(mbeanInterface.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    attrArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                    opArray,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                    descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        private final List<MBeanAttributeInfo> attrs = newList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        private final List<MBeanOperationInfo> ops = newList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * Looking up the MBeanInfo for a given base class (implementation class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * is complicated by the fact that we may use the same base class with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * several different explicit MBean interfaces via the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * javax.management.StandardMBean class.  It is further complicated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * by the fact that we have to be careful not to retain a strong reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * to any Class object for fear we would prevent a ClassLoader from being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * garbage-collected.  So we have a first lookup from the base class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * to a map for each interface that base class might specify giving
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * the MBeanInfo constructed for that base class and interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    static class MBeanInfoMap
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            extends WeakHashMap<Class<?>, WeakHashMap<Class<?>, MBeanInfo>> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Return the MBeanInfo for the given resource, based on the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * per-interface data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     */
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   406
    final MBeanInfo getMBeanInfo(Object resource, PerInterface<M> perInterface)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   407
    throws NotCompliantMBeanException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        MBeanInfo mbi =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                getClassMBeanInfo(resource.getClass(), perInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        MBeanNotificationInfo[] notifs = findNotifications(resource);
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   411
        Descriptor d = getSpecificMBeanDescriptor();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   412
        boolean anyNotifs = (notifs != null && notifs.length > 0);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   413
        if (!anyNotifs && ImmutableDescriptor.EMPTY_DESCRIPTOR.equals(d))
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            return mbi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        else {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   416
            d = ImmutableDescriptor.union(d, mbi.getDescriptor());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            return new MBeanInfo(mbi.getClassName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    mbi.getDescription(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                    mbi.getAttributes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    mbi.getConstructors(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    mbi.getOperations(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    notifs,
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   423
                    d);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Return the basic MBeanInfo for resources of the given class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * per-interface data.  This MBeanInfo might not be the final MBeanInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * for instances of the class, because if the class is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * NotificationBroadcaster then each instance gets to decide what
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * MBeanNotificationInfo[] to put in its own MBeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    final MBeanInfo getClassMBeanInfo(Class<?> resourceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            PerInterface<M> perInterface) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        MBeanInfoMap map = getMBeanInfoMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        synchronized (map) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            WeakHashMap<Class<?>, MBeanInfo> intfMap = map.get(resourceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            if (intfMap == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                intfMap = new WeakHashMap<Class<?>, MBeanInfo>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                map.put(resourceClass, intfMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            Class<?> intfClass = perInterface.getMBeanInterface();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            MBeanInfo mbi = intfMap.get(intfClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            if (mbi == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                MBeanInfo imbi = perInterface.getMBeanInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                Descriptor descriptor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                        ImmutableDescriptor.union(imbi.getDescriptor(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                        getMBeanDescriptor(resourceClass));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                mbi = new MBeanInfo(resourceClass.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                        imbi.getDescription(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        imbi.getAttributes(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                        findConstructors(resourceClass),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                        imbi.getOperations(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                        (MBeanNotificationInfo[]) null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                        descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                intfMap.put(intfClass, mbi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            return mbi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    static MBeanNotificationInfo[] findNotifications(Object moi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        if (!(moi instanceof NotificationBroadcaster))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        MBeanNotificationInfo[] mbn =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                ((NotificationBroadcaster) moi).getNotificationInfo();
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 287
diff changeset
   468
        if (mbn == null || mbn.length == 0)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        MBeanNotificationInfo[] result =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                new MBeanNotificationInfo[mbn.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        for (int i = 0; i < mbn.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            MBeanNotificationInfo ni = mbn[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            if (ni.getClass() != MBeanNotificationInfo.class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                ni = (MBeanNotificationInfo) ni.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            result[i] = ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    private static MBeanConstructorInfo[] findConstructors(Class<?> c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        Constructor[] cons = c.getConstructors();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        MBeanConstructorInfo[] mbc = new MBeanConstructorInfo[cons.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        for (int i = 0; i < cons.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            final String descr = "Public constructor of the MBean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            mbc[i] = new MBeanConstructorInfo(descr, cons[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        return mbc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
}