jdk/src/share/classes/com/sun/jmx/mbeanserver/MXBeanIntrospector.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 687 874e25a9844a
child 834 dc74d4ddc28e
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
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 2005-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 com.sun.jmx.mbeanserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    28
import com.sun.jmx.mbeanserver.MBeanIntrospector.MBeanInfoMap;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    29
import com.sun.jmx.mbeanserver.MBeanIntrospector.PerInterfaceMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.annotation.Annotation;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    31
import java.lang.ref.WeakReference;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.reflect.GenericArrayType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.reflect.InvocationTargetException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.reflect.Type;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    36
import java.util.Map;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    37
import java.util.WeakHashMap;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.Descriptor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.ImmutableDescriptor;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    40
import javax.management.IntrospectionException;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    41
import javax.management.JMX;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.MBeanAttributeInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.MBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.MBeanOperationInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.MBeanParameterInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.NotCompliantMBeanException;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    47
import javax.management.openmbean.MXBeanMappingFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.management.openmbean.OpenMBeanAttributeInfoSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.management.openmbean.OpenMBeanOperationInfoSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.management.openmbean.OpenMBeanParameterInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.management.openmbean.OpenMBeanParameterInfoSupport;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import javax.management.openmbean.OpenType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Introspector for MXBeans.  There is exactly one instance of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
class MXBeanIntrospector extends MBeanIntrospector<ConvertingMethod> {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    60
    /* We keep one MXBeanIntrospector per MXBeanMappingFactory, since the results
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    61
     * of the introspection depend on the factory.  The MXBeanIntrospector
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    62
     * has a reference back to the factory, so we wrap it in a WeakReference.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    63
     * It will be strongly referenced by any MXBeanSupport instances using it;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    64
     * if there are none then it is OK to gc it.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    65
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    66
    private static final
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    67
            Map<MXBeanMappingFactory, WeakReference<MXBeanIntrospector>> map =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    68
            new WeakHashMap<MXBeanMappingFactory, WeakReference<MXBeanIntrospector>>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    70
    static MXBeanIntrospector getInstance(MXBeanMappingFactory factory) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    71
        if (factory == null)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    72
            factory = MXBeanMappingFactory.DEFAULT;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    73
        synchronized (map) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    74
            MXBeanIntrospector intro;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    75
            WeakReference<MXBeanIntrospector> wr = map.get(factory);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    76
            if (wr != null) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    77
                intro = wr.get();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    78
                if (intro != null)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    79
                    return intro;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    80
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    81
            intro = new MXBeanIntrospector(factory);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    82
            wr = new WeakReference<MXBeanIntrospector>(intro);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    83
            map.put(factory, wr);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    84
            return intro;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    85
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    86
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    87
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    88
    private MXBeanIntrospector(MXBeanMappingFactory factory) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    89
        this.mappingFactory = factory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    PerInterfaceMap<ConvertingMethod> getPerInterfaceMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        return perInterfaceMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    MBeanInfoMap getMBeanInfoMap() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        return mbeanInfoMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    MBeanAnalyzer<ConvertingMethod> getAnalyzer(Class<?> mbeanInterface)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throws NotCompliantMBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return MBeanAnalyzer.analyzer(mbeanInterface, this);
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
    boolean isMXBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    ConvertingMethod mFrom(Method m) {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   115
        return ConvertingMethod.from(m, mappingFactory);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    String getName(ConvertingMethod m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return m.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    Type getGenericReturnType(ConvertingMethod m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return m.getGenericReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    Type[] getGenericParameterTypes(ConvertingMethod m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        return m.getGenericParameterTypes();
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
    String[] getSignature(ConvertingMethod m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return m.getOpenSignature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    void checkMethod(ConvertingMethod m) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        m.checkCallFromOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    Object invokeM2(ConvertingMethod m, Object target, Object[] args,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    Object cookie)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            throws InvocationTargetException, IllegalAccessException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                   MBeanException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return m.invokeWithOpenReturn((MXBeanLookup) cookie, target, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    boolean validParameter(ConvertingMethod m, Object value, int paramNo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                           Object cookie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            // Null is a valid value for all OpenTypes, even though
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            // OpenType.isValue(null) will return false.  It can always be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            // matched to the corresponding Java type, except when that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            // type is primitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            Type t = m.getGenericParameterTypes()[paramNo];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            return (!(t instanceof Class) || !((Class) t).isPrimitive());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            Object v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                v = m.fromOpenParameter((MXBeanLookup) cookie, value, paramNo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                // Ignore the exception and let MBeanIntrospector.invokeSetter()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                // throw the initial exception.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return isValidParameter(m.getMethod(), v, paramNo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    MBeanAttributeInfo getMBeanAttributeInfo(String attributeName,
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   176
            ConvertingMethod getter, ConvertingMethod setter)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   177
            throws IntrospectionException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        final boolean isReadable = (getter != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        final boolean isWritable = (setter != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        final boolean isIs = isReadable && getName(getter).startsWith("is");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        final String description = attributeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        final OpenType<?> openType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        final Type originalType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (isReadable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            openType = getter.getOpenReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            originalType = getter.getGenericReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            openType = setter.getOpenParameterTypes()[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            originalType = setter.getGenericParameterTypes()[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        Descriptor descriptor = typeDescriptor(openType, originalType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (isReadable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            descriptor = ImmutableDescriptor.union(descriptor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    getter.getDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (isWritable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            descriptor = ImmutableDescriptor.union(descriptor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    setter.getDescriptor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        final MBeanAttributeInfo ai;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (canUseOpenInfo(originalType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            ai = new OpenMBeanAttributeInfoSupport(attributeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                                                   description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                                   openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                                                   isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                                                   isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                                   isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                                   descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            ai = new MBeanAttributeInfo(attributeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                        originalTypeString(originalType),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                        description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                        isReadable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                        isWritable,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                        isIs,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                                        descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        // could also consult annotations for defaultValue,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // minValue, maxValue, legalValues
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        return ai;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    MBeanOperationInfo getMBeanOperationInfo(String operationName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            ConvertingMethod operation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        final Method method = operation.getMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        final String description = operationName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        /* Ideally this would be an empty string, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
           OMBOperationInfo constructor forbids that.  Also, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
           could consult an annotation to get a useful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
           description.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        final int impact = MBeanOperationInfo.UNKNOWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        final OpenType<?> returnType = operation.getOpenReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        final Type originalReturnType = operation.getGenericReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        final OpenType<?>[] paramTypes = operation.getOpenParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        final Type[] originalParamTypes = operation.getGenericParameterTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        final MBeanParameterInfo[] params =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            new MBeanParameterInfo[paramTypes.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        boolean openReturnType = canUseOpenInfo(originalReturnType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        boolean openParameterTypes = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        Annotation[][] annots = method.getParameterAnnotations();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        for (int i = 0; i < paramTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            final String paramName = "p" + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            final String paramDescription = paramName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            final OpenType<?> openType = paramTypes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            final Type originalType = originalParamTypes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            Descriptor descriptor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                typeDescriptor(openType, originalType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            descriptor = ImmutableDescriptor.union(descriptor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    Introspector.descriptorForAnnotations(annots[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            final MBeanParameterInfo pi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (canUseOpenInfo(originalType)) {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   260
                pi = new OpenMBeanParameterInfoSupport(paramName,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                                                       paramDescription,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                                       openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                                       descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                openParameterTypes = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                pi = new MBeanParameterInfo(
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   267
                    paramName,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    originalTypeString(originalType),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    paramDescription,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            params[i] = pi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        Descriptor descriptor =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            typeDescriptor(returnType, originalReturnType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        descriptor = ImmutableDescriptor.union(descriptor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                Introspector.descriptorForElement(method));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        final MBeanOperationInfo oi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (openReturnType && openParameterTypes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            /* If the return value and all the parameters can be faithfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
             * represented as OpenType then we return an OpenMBeanOperationInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
             * If any of them is a primitive type, we can't.  Compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
             * with JSR 174 means that we must return an MBean*Info where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
             * the getType() is the primitive type, not its wrapped type as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
             * we would get with an OpenMBean*Info.  The OpenType is available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
             * in the Descriptor in either case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            final OpenMBeanParameterInfo[] oparams =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                new OpenMBeanParameterInfo[params.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            System.arraycopy(params, 0, oparams, 0, params.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            oi = new OpenMBeanOperationInfoSupport(operationName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                                                   description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                                                   oparams,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                                   returnType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                                   impact,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                                   descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            oi = new MBeanOperationInfo(operationName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                                        description,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                                        params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                                        openReturnType ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                                        returnType.getClassName() :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                                        originalTypeString(originalReturnType),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                                        impact,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                                        descriptor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return oi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    Descriptor getBasicMBeanDescriptor() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        return new ImmutableDescriptor("mxbean=true",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                                       "immutableInfo=true");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    Descriptor getMBeanDescriptor(Class<?> resourceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        /* We already have immutableInfo=true in the Descriptor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
         * included in the MBeanInfo for the MXBean interface.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
         * method is being called for the MXBean *class* to add any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
         * new items beyond those in the interface Descriptor, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         * currently it does not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return ImmutableDescriptor.EMPTY_DESCRIPTOR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   329
    @Override
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   330
    Descriptor getSpecificMBeanDescriptor() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   331
        if (mappingFactory == MXBeanMappingFactory.DEFAULT)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   332
            return ImmutableDescriptor.EMPTY_DESCRIPTOR;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   333
        else {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   334
            return new ImmutableDescriptor(
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   335
                    JMX.MXBEAN_MAPPING_FACTORY_CLASS_FIELD + "=" +
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   336
                    mappingFactory.getClass().getName());
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   337
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   338
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   339
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    private static Descriptor typeDescriptor(OpenType openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                             Type originalType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        return new ImmutableDescriptor(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            new String[] {"openType",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                          "originalType"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            new Object[] {openType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                          originalTypeString(originalType)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * <p>True if this type can be faithfully represented in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * OpenMBean*Info.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * <p>Compatibility with JSR 174 means that primitive types must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * represented by an MBean*Info whose getType() is the primitive type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * string, e.g. "int".  If we used an OpenMBean*Info then this string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * would be the wrapped type, e.g. "java.lang.Integer".</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * <p>Compatibility with JMX 1.2 (including J2SE 5.0) means that arrays
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * of primitive types cannot use an ArrayType representing an array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * primitives, because that didn't exist in JMX 1.2.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private static boolean canUseOpenInfo(Type type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        if (type instanceof GenericArrayType) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            return canUseOpenInfo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                ((GenericArrayType) type).getGenericComponentType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        } else if (type instanceof Class && ((Class<?>) type).isArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            return canUseOpenInfo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                ((Class<?>) type).getComponentType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        return (!(type instanceof Class && ((Class<?>) type).isPrimitive()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    private static String originalTypeString(Type type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        if (type instanceof Class)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            return ((Class) type).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return type.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   380
    private final PerInterfaceMap<ConvertingMethod>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        perInterfaceMap = new PerInterfaceMap<ConvertingMethod>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   383
    private final MBeanInfoMap mbeanInfoMap = new MBeanInfoMap();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   384
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   385
    private final MXBeanMappingFactory mappingFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
}