jdk/src/share/classes/com/sun/jmx/mbeanserver/MBeanSupport.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
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 javax.management.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.management.AttributeList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.management.AttributeNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.management.InvalidAttributeValueException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import javax.management.MBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.management.MBeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.management.MBeanRegistration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.NotCompliantMBeanException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.ObjectName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.ReflectionException;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    41
import javax.management.openmbean.MXBeanMappingFactory;
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
 * Base class for MBeans.  There is one instance of this class for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * every Standard MBean and every MXBean.  We try to limit the amount
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * of information per instance so we can handle very large numbers of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * MBeans comfortably.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @param <M> either Method or ConvertingMethod, for Standard MBeans
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * and MXBeans respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * We maintain a couple of caches to increase sharing between
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * different MBeans of the same type and also to reduce creation time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * for the second and subsequent instances of the same type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * The first cache maps from an MBean interface to a PerInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * object containing information parsed out of the interface.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * interface is either a Standard MBean interface or an MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * interface, and there is one cache for each case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * The PerInterface includes an MBeanInfo.  This contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * attributes and operations parsed out of the interface's methods,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * plus a basic Descriptor for the interface containing at least the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * interfaceClassName field and any fields derived from annotations on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * the interface.  This MBeanInfo can never be the MBeanInfo for any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * actual MBean, because an MBeanInfo's getClassName() is the name of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * a concrete class and we don't know what the class will be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * Furthermore a real MBeanInfo may need to add constructors and/or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * notifications to the MBeanInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * The PerInterface also contains an MBeanDispatcher which is able to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * route getAttribute, setAttribute, and invoke to the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * method of the interface, including doing any necessary translation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * of parameters and return values for MXBeans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * The PerInterface also contains the original Class for the interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * We need to be careful about references.  When there are no MBeans
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * with a given interface, there must not be any strong references to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * the interface Class.  Otherwise it could never be garbage collected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * and neither could its ClassLoader or any other classes loaded by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * its ClassLoader.  Therefore the cache must wrap the PerInterface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * in a WeakReference.  Each instance of MBeanSupport has a strong
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * reference to its PerInterface, which prevents PerInterface instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * from being garbage-collected prematurely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * The second cache maps from a concrete class and an MBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * that that class implements to the MBeanInfo for that class and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * interface.  (The ability to specify an interface separately comes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * from the class StandardMBean.  MBeans registered directly in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * MBean Server will always have the same interface here.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * The MBeanInfo in this second cache will be the MBeanInfo from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * PerInterface cache for the given itnerface, but with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * getClassName() having the concrete class's name, and the public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * constructors based on the concrete class's constructors.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * MBeanInfo can be shared between all instances of the concrete class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * specifying the same interface, except instances that are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * NotificationBroadcasters.  NotificationBroadcasters supply the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * MBeanNotificationInfo[] in the MBeanInfo based on the instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * method NotificationBroadcaster.getNotificationInfo(), so two
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * instances of the same concrete class do not necessarily have the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * same MBeanNotificationInfo[].  Currently we do not try to detect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * when they do, although it would probably be worthwhile doing that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * since it is a very common case.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * Standard MBeans additionally have the property that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * getNotificationInfo() must in principle be called every time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * getMBeanInfo() is called for the MBean, since the returned array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * allowed to change over time.  We attempt to reduce the cost of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * doing this by detecting when the Standard MBean is a subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * NotificationBroadcasterSupport that does not override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * getNotificationInfo(), meaning that the MBeanNotificationInfo[] is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * the one that was supplied to the constructor.  MXBeans do not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * this problem because their getNotificationInfo() method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * only once.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
public abstract class MBeanSupport<M>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        implements DynamicMBean2, MBeanRegistration {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   125
    <T> MBeanSupport(T resource, Class<T> mbeanInterfaceType,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   126
                     MXBeanMappingFactory mappingFactory)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            throws NotCompliantMBeanException {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   128
        if (mbeanInterfaceType == null)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            throw new NotCompliantMBeanException("Null MBean interface");
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   130
        if (!mbeanInterfaceType.isInstance(resource)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            final String msg =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                "Resource class " + resource.getClass().getName() +
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   133
                " is not an instance of " + mbeanInterfaceType.getName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            throw new NotCompliantMBeanException(msg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        this.resource = resource;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   137
        MBeanIntrospector<M> introspector = getMBeanIntrospector(mappingFactory);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   138
        this.perInterface = introspector.getPerInterface(mbeanInterfaceType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        this.mbeanInfo = introspector.getMBeanInfo(resource, perInterface);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /** Return the appropriate introspector for this type of MBean. */
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   143
    abstract MBeanIntrospector<M>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   144
            getMBeanIntrospector(MXBeanMappingFactory mappingFactory);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Return a cookie for this MBean.  This cookie will be passed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * MBean method invocations where it can supply additional information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * to the invocation.  For example, with MXBeans it can be used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * supply the MXBeanLookup context for resolving inter-MXBean references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    abstract Object getCookie();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public final boolean isMXBean() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        return perInterface.isMXBean();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    // Methods that javax.management.StandardMBean should call from its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    // preRegister and postRegister, given that it is not supposed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    // call the contained object's preRegister etc methods even if it has them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public abstract void register(MBeanServer mbs, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    public abstract void unregister();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public final ObjectName preRegister(MBeanServer server, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        if (resource instanceof MBeanRegistration)
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   168
            name = ((MBeanRegistration) resource).preRegister(server, name);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   169
        return name;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public final void preRegister2(MBeanServer server, ObjectName name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        register(server, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public final void registerFailed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        unregister();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public final void postRegister(Boolean registrationDone) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (resource instanceof MBeanRegistration)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            ((MBeanRegistration) resource).postRegister(registrationDone);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public final void preDeregister() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (resource instanceof MBeanRegistration)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            ((MBeanRegistration) resource).preDeregister();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public final void postDeregister() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        // Undo any work from registration.  We do this in postDeregister
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        // not preDeregister, because if the user preDeregister throws an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // exception then the MBean is not unregistered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            unregister();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            if (resource instanceof MBeanRegistration)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                ((MBeanRegistration) resource).postDeregister();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public final Object getAttribute(String attribute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            throws AttributeNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                   MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                   ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return perInterface.getAttribute(resource, attribute, getCookie());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public final AttributeList getAttributes(String[] attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        final AttributeList result = new AttributeList(attributes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        for (String attrName : attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                final Object attrValue = getAttribute(attrName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                result.add(new Attribute(attrName, attrValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                // OK: attribute is not included in returned list, per spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                // XXX: log the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public final void setAttribute(Attribute attribute)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            throws AttributeNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                   InvalidAttributeValueException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                   MBeanException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                   ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        final String name = attribute.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        final Object value = attribute.getValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        perInterface.setAttribute(resource, name, value, getCookie());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public final AttributeList setAttributes(AttributeList attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        final AttributeList result = new AttributeList(attributes.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        for (Object attrObj : attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            // We can't use AttributeList.asList because it has side-effects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            Attribute attr = (Attribute) attrObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                setAttribute(attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                result.add(new Attribute(attr.getName(), attr.getValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                // OK: attribute is not included in returned list, per spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                // XXX: log the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        return result;
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 final Object invoke(String operation, Object[] params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                         String[] signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            throws MBeanException, ReflectionException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        return perInterface.invoke(resource, operation, params, signature,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                                   getCookie());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    // Overridden by StandardMBeanSupport
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public MBeanInfo getMBeanInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        return mbeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public final String getClassName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        return resource.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public final Object getResource() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        return resource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    public final Class<?> getMBeanInterface() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return perInterface.getMBeanInterface();
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 MBeanInfo mbeanInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    private final Object resource;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private final PerInterface<M> perInterface;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
}