jdk/src/share/classes/com/sun/jmx/mbeanserver/PerInterface.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 687 874e25a9844a
child 4156 acaa49a2768a
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.management.AttributeNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.management.InvalidAttributeValueException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.management.MBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.MBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.ReflectionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import static com.sun.jmx.mbeanserver.Util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * Per-MBean-interface behavior.  A single instance of this class can be shared
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * by all MBeans of the same kind (Standard MBean or MXBean) that have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * MBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
final class PerInterface<M> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    PerInterface(Class<?> mbeanInterface, MBeanIntrospector<M> introspector,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                 MBeanAnalyzer<M> analyzer, MBeanInfo mbeanInfo) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        this.mbeanInterface = mbeanInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        this.introspector = introspector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.mbeanInfo = mbeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        analyzer.visit(new InitMaps());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    Class<?> getMBeanInterface() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return mbeanInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    MBeanInfo getMBeanInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return mbeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    boolean isMXBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        return introspector.isMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    Object getAttribute(Object resource, String attribute, Object cookie)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            throws AttributeNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                   MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                   ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        final M cm = getters.get(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (cm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            final String msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if (setters.containsKey(attribute))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                msg = "Write-only attribute: " + attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                msg = "No such attribute: " + attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new AttributeNotFoundException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        return introspector.invokeM(cm, resource, (Object[]) null, cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    void setAttribute(Object resource, String attribute, Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                      Object cookie)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            throws AttributeNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                   InvalidAttributeValueException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                   MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                   ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        final M cm = setters.get(attribute);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (cm == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            final String msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            if (getters.containsKey(attribute))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                msg = "Read-only attribute: " + attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                msg = "No such attribute: " + attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new AttributeNotFoundException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        introspector.invokeSetter(attribute, cm, resource, value, cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    Object invoke(Object resource, String operation, Object[] params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                  String[] signature, Object cookie)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            throws MBeanException, ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        final List<MethodAndSig> list = ops.get(operation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        if (list == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            final String msg = "No such operation: " + operation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return noSuchMethod(msg, resource, operation, params, signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (signature == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            signature = new String[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        MethodAndSig found = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (MethodAndSig mas : list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            if (Arrays.equals(mas.signature, signature)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                found = mas;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (found == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            final String badSig = sigString(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            final String msg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (list.size() == 1) {  // helpful exception message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                msg = "Signature mismatch for operation " + operation +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        ": " + badSig + " should be " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                        sigString(list.get(0).signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                msg = "Operation " + operation + " exists but not with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        "this signature: " + badSig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            return noSuchMethod(msg, resource, operation, params, signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return introspector.invokeM(found.method, resource, params, cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * This method is called when invoke doesn't find the named method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Before throwing an exception, we check to see whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * jmx.invoke.getters property is set, and if so whether the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * being invoked might be a getter or a setter.  If so we invoke it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * and return the result.  This is for compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * with code based on JMX RI 1.0 or 1.1 which allowed invoking getters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * and setters.  It is *not* recommended that new code use this feature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * Since this method is either going to throw an exception or use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * functionality that is strongly discouraged, we consider that its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * performance is not very important.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * A simpler way to implement the functionality would be to add the getters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * and setters to the operations map when jmx.invoke.getters is set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * However, that means that the property is consulted when an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * interface is being introspected and not thereafter.  Previously,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * the property was consulted on every invocation.  So this simpler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * implementation could potentially break code that sets and unsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * the property at different times.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private Object noSuchMethod(String msg, Object resource, String operation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                Object[] params, String[] signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                                Object cookie)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throws MBeanException, ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        // Construct the exception that we will probably throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        final NoSuchMethodException nsme =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            new NoSuchMethodException(operation + sigString(signature));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        final ReflectionException exception =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            new ReflectionException(nsme, msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (introspector.isMXBean())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            throw exception; // No compatibility requirement here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        // Is the compatibility property set?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        GetPropertyAction act = new GetPropertyAction("jmx.invoke.getters");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        String invokeGettersS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            invokeGettersS = AccessController.doPrivileged(act);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            // We don't expect an exception here but if we get one then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            // we'll simply assume that the property is not set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            invokeGettersS = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (invokeGettersS == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throw exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        int rest = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        Map<String, M> methods = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if (signature == null || signature.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (operation.startsWith("get"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                rest = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            else if (operation.startsWith("is"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                rest = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            if (rest != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                methods = getters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        } else if (signature.length == 1 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                   operation.startsWith("set")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            rest = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            methods = setters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (rest != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            String attrName = operation.substring(rest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            M method = methods.get(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if (method != null && introspector.getName(method).equals(operation)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                String[] msig = introspector.getSignature(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                if ((signature == null && msig.length == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        Arrays.equals(signature, msig)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    return introspector.invokeM(method, resource, params, cookie);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        throw exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    private String sigString(String[] signature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        StringBuilder b = new StringBuilder("(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        if (signature != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            for (String s : signature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                if (b.length() > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    b.append(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                b.append(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return b.append(")").toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Visitor that sets up the method maps (operations, getters, setters).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   234
    private class InitMaps implements MBeanAnalyzer.MBeanVisitor<M, RuntimeException> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        public void visitAttribute(String attributeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                                   M getter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                                   M setter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if (getter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                introspector.checkMethod(getter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                final Object old = getters.put(attributeName, getter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                assert(old == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            if (setter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                introspector.checkMethod(setter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                final Object old = setters.put(attributeName, setter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                assert(old == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        public void visitOperation(String operationName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                                   M operation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            introspector.checkMethod(operation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            final String[] sig = introspector.getSignature(operation);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            final MethodAndSig mas = new MethodAndSig();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            mas.method = operation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            mas.signature = sig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            List<MethodAndSig> list = ops.get(operationName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            if (list == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                list = Collections.singletonList(mas);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                if (list.size() == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                    list = newList(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                list.add(mas);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            ops.put(operationName, list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private class MethodAndSig {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        M method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        String[] signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    private final Class<?> mbeanInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private final MBeanIntrospector<M> introspector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private final MBeanInfo mbeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    private final Map<String, M> getters = newMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private final Map<String, M> setters = newMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    private final Map<String, List<MethodAndSig>> ops = newMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
}