jdk/src/share/classes/com/sun/jmx/mbeanserver/StandardMBeanIntrospector.java
author sjiang
Tue, 02 Apr 2013 10:38:51 +0200
changeset 18220 1d724730bd2c
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
8007467: Better JMX type conversion Reviewed-by: dfuchs, mchung, skoivu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.jmx.mbeanserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.WeakHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.management.Descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.management.ImmutableDescriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.management.IntrospectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.management.MBeanAttributeInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.MBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.MBeanOperationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.NotCompliantMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.NotificationBroadcaster;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.NotificationBroadcasterSupport;
18220
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
    41
import sun.reflect.misc.MethodUtil;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class StandardMBeanIntrospector extends MBeanIntrospector<Method> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final StandardMBeanIntrospector instance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        new StandardMBeanIntrospector();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    static StandardMBeanIntrospector getInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        return instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    PerInterfaceMap<Method> getPerInterfaceMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        return perInterfaceMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    MBeanInfoMap getMBeanInfoMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return mbeanInfoMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    MBeanAnalyzer<Method> getAnalyzer(Class<?> mbeanInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return MBeanAnalyzer.analyzer(mbeanInterface, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    boolean isMXBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    Method mFrom(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    String getName(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return m.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    Type getGenericReturnType(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return m.getGenericReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    Type[] getGenericParameterTypes(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        return m.getGenericParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    String[] getSignature(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        Class<?>[] params = m.getParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        String[] sig = new String[params.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        for (int i = 0; i < params.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            sig[i] = params[i].getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return sig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    void checkMethod(Method m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    Object invokeM2(Method m, Object target, Object[] args, Object cookie)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            throws InvocationTargetException, IllegalAccessException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                   MBeanException {
18220
1d724730bd2c 8007467: Better JMX type conversion
sjiang
parents: 5506
diff changeset
   112
        return MethodUtil.invoke(m, target, args);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    boolean validParameter(Method m, Object value, int paramNo, Object cookie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return isValidParameter(m, value, paramNo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    MBeanAttributeInfo getMBeanAttributeInfo(String attributeName,
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   122
            Method getter, Method setter) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   124
        final String description = "Attribute exposed for management";
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   125
        try {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   126
            return new MBeanAttributeInfo(attributeName, description,
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   127
                                          getter, setter);
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   128
        } catch (IntrospectionException e) {
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   129
            throw new RuntimeException(e); // should not happen
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   130
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    MBeanOperationInfo getMBeanOperationInfo(String operationName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            Method operation) {
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   136
        final String description = "Operation exposed for management";
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1247
diff changeset
   137
        return new MBeanOperationInfo(description, operation);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    Descriptor getBasicMBeanDescriptor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        /* We don't bother saying mxbean=false, and we can't know whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
           the info is immutable until we know whether the MBean class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
           (not interface) is a NotificationBroadcaster. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return ImmutableDescriptor.EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    Descriptor getMBeanDescriptor(Class<?> resourceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        boolean immutable = isDefinitelyImmutableInfo(resourceClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return new ImmutableDescriptor("mxbean=false",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                       "immutableInfo=" + immutable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /* Return true if and only if we can be sure that the given MBean implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * class has immutable MBeanInfo.  A Standard MBean that is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * NotificationBroadcaster is allowed to return different values at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * different times from its getNotificationInfo() method, which is when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * we might not know if it is immutable.  But if it is a subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * NotificationBroadcasterSupport and does not override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * getNotificationInfo(), then we know it won't change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    static boolean isDefinitelyImmutableInfo(Class<?> implClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (!NotificationBroadcaster.class.isAssignableFrom(implClass))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        synchronized (definitelyImmutable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            Boolean immutable = definitelyImmutable.get(implClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            if (immutable == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                final Class<NotificationBroadcasterSupport> nbs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        NotificationBroadcasterSupport.class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                if (nbs.isAssignableFrom(implClass)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        Method m = implClass.getMethod("getNotificationInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        immutable = (m.getDeclaringClass() == nbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                        // Too bad, we'll say no for now.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    immutable = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                definitelyImmutable.put(implClass, immutable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            return immutable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    private static final WeakHashMap<Class<?>, Boolean> definitelyImmutable =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            new WeakHashMap<Class<?>, Boolean>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private static final PerInterfaceMap<Method>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        perInterfaceMap = new PerInterfaceMap<Method>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private static final MBeanInfoMap mbeanInfoMap = new MBeanInfoMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
}