jdk/src/share/classes/com/sun/jmx/mbeanserver/MXBeanProxy.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 static com.sun.jmx.mbeanserver.Util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.management.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.management.MBeanServerConnection;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    35
import javax.management.MalformedObjectNameException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.NotCompliantMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.ObjectName;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    38
import javax.management.openmbean.MXBeanMappingFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
   <p>Helper class for an {@link InvocationHandler} that forwards methods from an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
   MXBean interface to a named
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
   MXBean in an MBean Server and handles translation between the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
   arbitrary Java types in the interface and the Open Types used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
   by the MXBean.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
   @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class MXBeanProxy {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    50
    public MXBeanProxy(Class<?> mxbeanInterface, MXBeanMappingFactory factory) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (mxbeanInterface == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            throw new IllegalArgumentException("Null parameter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        final MBeanAnalyzer<ConvertingMethod> analyzer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            analyzer =
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    58
                MXBeanIntrospector.getInstance(factory).getAnalyzer(mxbeanInterface);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new IllegalArgumentException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        analyzer.visit(new Visitor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    65
    private class Visitor
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    66
            implements MBeanAnalyzer.MBeanVisitor<ConvertingMethod, RuntimeException> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        public void visitAttribute(String attributeName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                                   ConvertingMethod getter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                                   ConvertingMethod setter) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            if (getter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                getter.checkCallToOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                Method getterMethod = getter.getMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                handlerMap.put(getterMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                               new GetHandler(attributeName, getter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            if (setter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                // return type is void, no need for checkCallToOpen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                Method setterMethod = setter.getMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                handlerMap.put(setterMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                               new SetHandler(attributeName, setter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        public void visitOperation(String operationName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                   ConvertingMethod operation) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            operation.checkCallToOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            Method operationMethod = operation.getMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            String[] sig = operation.getOpenSignature();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            handlerMap.put(operationMethod,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                           new InvokeHandler(operationName, sig, operation));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static abstract class Handler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Handler(String name, ConvertingMethod cm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            this.name = name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            this.convertingMethod = cm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        ConvertingMethod getConvertingMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            return convertingMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        abstract Object invoke(MBeanServerConnection mbsc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                               ObjectName name, Object[] args) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        private final String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        private final ConvertingMethod convertingMethod;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    private static class GetHandler extends Handler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        GetHandler(String attributeName, ConvertingMethod cm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            super(attributeName, cm);
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
        Object invoke(MBeanServerConnection mbsc, ObjectName name, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            assert(args == null || args.length == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            return mbsc.getAttribute(name, getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private static class SetHandler extends Handler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        SetHandler(String attributeName, ConvertingMethod cm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            super(attributeName, cm);
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
        Object invoke(MBeanServerConnection mbsc, ObjectName name, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            assert(args.length == 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            Attribute attr = new Attribute(getName(), args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            mbsc.setAttribute(name, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private static class InvokeHandler extends Handler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        InvokeHandler(String operationName, String[] signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                      ConvertingMethod cm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            super(operationName, cm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            this.signature = signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        Object invoke(MBeanServerConnection mbsc, ObjectName name, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            return mbsc.invoke(name, getName(), args, signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        private final String[] signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public Object invoke(MBeanServerConnection mbsc, ObjectName name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                         Method method, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        Handler handler = handlerMap.get(method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        ConvertingMethod cm = handler.getConvertingMethod();
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   164
        String prefix = extractPrefix(name);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   165
        MXBeanLookup lookup = MXBeanLookup.lookupFor(mbsc, prefix);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   166
        MXBeanLookup oldLookup = MXBeanLookup.getLookup();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   167
        try {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   168
            MXBeanLookup.setLookup(lookup);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   169
            Object[] openArgs = cm.toOpenParameters(lookup, args);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   170
            Object result = handler.invoke(mbsc, name, openArgs);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   171
            return cm.fromOpenReturnValue(lookup, result);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   172
        } finally {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   173
            MXBeanLookup.setLookup(oldLookup);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   174
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   175
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   176
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   177
    private static String extractPrefix(ObjectName name)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   178
            throws MalformedObjectNameException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   179
        String domain = name.getDomain();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   180
        int slashslash = domain.lastIndexOf("//");
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   181
        if (slashslash > 0 && domain.charAt(slashslash - 1) == '/')
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   182
            slashslash--;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   183
        if (slashslash >= 0)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   184
            return domain.substring(0, slashslash + 2);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   185
        else
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   186
            return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private final Map<Method, Handler> handlerMap = newMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
}