jdk/src/share/classes/javax/management/JMX.java
author emcmanus
Wed, 09 Jul 2008 10:36:07 +0200
changeset 833 bfa2bef7517c
parent 687 874e25a9844a
child 834 dc74d4ddc28e
permissions -rw-r--r--
6323980: Annotations to simplify MBean development Reviewed-by: jfdenise, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
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 javax.management;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jmx.mbeanserver.Introspector;
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
    29
import com.sun.jmx.mbeanserver.MBeanInjector;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    30
import com.sun.jmx.remote.util.ClassLogger;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    31
import java.beans.BeanInfo;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    32
import java.beans.PropertyDescriptor;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    33
import java.io.Serializable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.reflect.InvocationHandler;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    35
import java.lang.reflect.InvocationTargetException;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    36
import java.lang.reflect.Method;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.reflect.Proxy;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    38
import java.util.Map;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    39
import java.util.TreeMap;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    40
import javax.management.openmbean.MXBeanMappingFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Static methods from the JMX API.  There are no instances of this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class JMX {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /* Code within this package can prove that by providing this instance of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    static final JMX proof = new JMX();
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    52
    private static final ClassLogger logger =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
    53
        new ClassLogger("javax.management.misc", "JMX");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private JMX() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * The name of the <a href="Descriptor.html#defaultValue">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * defaultValue}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static final String DEFAULT_VALUE_FIELD = "defaultValue";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * The name of the <a href="Descriptor.html#immutableInfo">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * immutableInfo}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public static final String IMMUTABLE_INFO_FIELD = "immutableInfo";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * The name of the <a href="Descriptor.html#interfaceClassName">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * interfaceClassName}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public static final String INTERFACE_CLASS_NAME_FIELD = "interfaceClassName";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * The name of the <a href="Descriptor.html#legalValues">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * legalValues}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    public static final String LEGAL_VALUES_FIELD = "legalValues";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * The name of the <a href="Descriptor.html#maxValue">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * maxValue}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public static final String MAX_VALUE_FIELD = "maxValue";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * The name of the <a href="Descriptor.html#minValue">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * minValue}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static final String MIN_VALUE_FIELD = "minValue";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * The name of the <a href="Descriptor.html#mxbean">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * mxbean}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static final String MXBEAN_FIELD = "mxbean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   100
     * The name of the
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   101
     * <a href="Descriptor.html#mxbeanMappingFactoryClass">{@code
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   102
     * mxbeanMappingFactoryClass}</a> field.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   103
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   104
    public static final String MXBEAN_MAPPING_FACTORY_CLASS_FIELD =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   105
            "mxbeanMappingFactoryClass";
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   106
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   107
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * The name of the <a href="Descriptor.html#openType">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * openType}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public static final String OPEN_TYPE_FIELD = "openType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * The name of the <a href="Descriptor.html#originalType">{@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * originalType}</a> field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static final String ORIGINAL_TYPE_FIELD = "originalType";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   120
     * <p>Options to apply to an MBean proxy or to an instance of {@link
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   121
     * StandardMBean}.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   122
     *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   123
     * <p>For example, to specify a custom {@link MXBeanMappingFactory}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   124
     * for a {@code StandardMBean}, you might write this:</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   125
     *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   126
     * <pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   127
     * MXBeanMappingFactory factory = new MyMXBeanMappingFactory();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   128
     * JMX.MBeanOptions opts = new JMX.MBeanOptions();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   129
     * opts.setMXBeanMappingFactory(factory);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   130
     * StandardMBean mbean = new StandardMBean(impl, intf, opts);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   131
     * </pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   132
     *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   133
     * @see javax.management.JMX.ProxyOptions
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   134
     * @see javax.management.StandardMBean.Options
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   135
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   136
    public static class MBeanOptions implements Serializable, Cloneable {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   137
        private static final long serialVersionUID = -6380842449318177843L;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   138
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   139
        static final MBeanOptions MXBEAN = new MBeanOptions();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   140
        static {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   141
            MXBEAN.setMXBeanMappingFactory(MXBeanMappingFactory.DEFAULT);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   142
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   143
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   144
        private MXBeanMappingFactory mappingFactory;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   145
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   146
        /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   147
         * <p>Construct an {@code MBeanOptions} object where all options have
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   148
         * their default values.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   149
         */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   150
        public MBeanOptions() {}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   151
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   152
        @Override
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   153
        public MBeanOptions clone() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   154
            try {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   155
                return (MBeanOptions) super.clone();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   156
            } catch (CloneNotSupportedException e) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   157
                throw new AssertionError(e);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   158
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   159
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   160
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   161
        /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   162
         * <p>True if this is an MXBean proxy or a StandardMBean instance
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   163
         * that is an MXBean.  The default value is false.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   164
         *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   165
         * <p>This method is equivalent to {@link #getMXBeanMappingFactory()
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   166
         * this.getMXBeanMappingFactory()}{@code != null}.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   167
         *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   168
         * @return true if this is an MXBean proxy or a StandardMBean instance
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   169
         * that is an MXBean.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   170
         */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   171
        public boolean isMXBean() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   172
            return (this.mappingFactory != null);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   173
        }
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
         * <p>The mappings between Java types and Open Types to be used in
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   177
         * an MXBean proxy or a StandardMBean instance that is an MXBean,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   178
         * or null if this instance is not for an MXBean.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   179
         * The default value is null.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   180
         *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   181
         * @return the mappings to be used in this proxy or StandardMBean,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   182
         * or null if this instance is not for an MXBean.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   183
         */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   184
        public MXBeanMappingFactory getMXBeanMappingFactory() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   185
            return mappingFactory;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   186
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   187
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   188
        /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   189
         * <p>Set the {@link #getMXBeanMappingFactory() MXBeanMappingFactory} to
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   190
         * the given value.  The value should be null if this instance is not
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   191
         * for an MXBean.  If this instance is for an MXBean, the value should
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   192
         * usually be either a custom mapping factory, or
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   193
         * {@link MXBeanMappingFactory#forInterface
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   194
         * MXBeanMappingFactory.forInterface}{@code (mxbeanInterface)}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   195
         * which signifies
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   196
         * that the {@linkplain MXBeanMappingFactory#DEFAULT default} mapping
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   197
         * factory should be used unless an {@code @}{@link
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   198
         * javax.management.openmbean.MXBeanMappingFactoryClass
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   199
         * MXBeanMappingFactoryClass} annotation on {@code mxbeanInterface}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   200
         * specifies otherwise.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   201
         *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   202
         * <p>Examples:</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   203
         * <pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   204
         * MBeanOptions opts = new MBeanOptions();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   205
         * opts.setMXBeanMappingFactory(myMappingFactory);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   206
         * MyMXBean proxy = JMX.newMBeanProxy(
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   207
         *         mbeanServerConnection, objectName, MyMXBean.class, opts);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   208
         *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   209
         * // ...or...
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   210
         *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   211
         * MBeanOptions opts = new MBeanOptions();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   212
         * MXBeanMappingFactory defaultFactoryForMyMXBean =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   213
         *         MXBeanMappingFactory.forInterface(MyMXBean.class);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   214
         * opts.setMXBeanMappingFactory(defaultFactoryForMyMXBean);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   215
         * MyMXBean proxy = JMX.newMBeanProxy(
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   216
         *         mbeanServerConnection, objectName, MyMXBean.class, opts);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   217
         * </pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   218
         *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   219
         * @param f the new value.  If null, this instance is not for an
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   220
         * MXBean.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   221
         */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   222
        public void setMXBeanMappingFactory(MXBeanMappingFactory f) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   223
            this.mappingFactory = f;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   224
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   225
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   226
        /* To maximise object sharing, classes in this package can replace
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   227
         * a private MBeanOptions with no MXBeanMappingFactory with one
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   228
         * of these shared instances.  But they must be EXTREMELY careful
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   229
         * never to give out the shared instances to user code, which could
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   230
         * modify them.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   231
         */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   232
        private static final MBeanOptions[] CANONICALS = {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   233
            new MBeanOptions(), MXBEAN,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   234
        };
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   235
        // Overridden in local subclasses:
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   236
        MBeanOptions[] canonicals() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   237
            return CANONICALS;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   238
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   239
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   240
        // This is only used by the logic for canonical instances.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   241
        // Overridden in local subclasses:
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   242
        boolean same(MBeanOptions opt) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   243
            return (opt.mappingFactory == mappingFactory);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   244
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   245
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   246
        final MBeanOptions canonical() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   247
            for (MBeanOptions opt : canonicals()) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   248
                if (opt.getClass() == this.getClass() && same(opt))
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   249
                    return opt;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   250
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   251
            return this;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   252
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   253
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   254
        final MBeanOptions uncanonical() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   255
            for (MBeanOptions opt : canonicals()) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   256
                if (this == opt)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   257
                    return clone();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   258
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   259
            return this;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   260
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   261
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   262
        private Map<String, Object> toMap() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   263
            Map<String, Object> map = new TreeMap<String, Object>();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   264
            try {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   265
                BeanInfo bi = java.beans.Introspector.getBeanInfo(getClass());
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   266
                PropertyDescriptor[] pds = bi.getPropertyDescriptors();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   267
                for (PropertyDescriptor pd : pds) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   268
                    String name = pd.getName();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   269
                    if (name.equals("class"))
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   270
                        continue;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   271
                    Method get = pd.getReadMethod();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   272
                    if (get != null)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   273
                        map.put(name, get.invoke(this));
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   274
                }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   275
            } catch (Exception e) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   276
                Throwable t = e;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   277
                if (t instanceof InvocationTargetException)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   278
                    t = t.getCause();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   279
                map.put("Exception", t);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   280
            }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   281
            return map;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   282
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   283
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   284
        @Override
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   285
        public String toString() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   286
            return getClass().getSimpleName() + toMap();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   287
            // For example "MBeanOptions{MXBean=true, <etc>}".
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   288
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   289
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   290
        /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   291
         * <p>Indicates whether some other object is "equal to" this one. The
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   292
         * result is true if and only if the other object is also an instance
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   293
         * of MBeanOptions or a subclass, and has the same properties with
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   294
         * the same values.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   295
         * @return {@inheritDoc}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   296
         */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   297
        @Override
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   298
        public boolean equals(Object obj) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   299
            if (obj == this)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   300
                return true;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   301
            if (obj == null || obj.getClass() != this.getClass())
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   302
                return false;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   303
            return toMap().equals(((MBeanOptions) obj).toMap());
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   304
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   305
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   306
        @Override
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   307
        public int hashCode() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   308
            return toMap().hashCode();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   309
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   310
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   311
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   312
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   313
     * <p>Options to apply to an MBean proxy.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   314
     *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   315
     * @see #newMBeanProxy
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   316
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   317
    public static class ProxyOptions extends MBeanOptions {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   318
        private static final long serialVersionUID = 7238804866098386559L;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   319
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   320
        private boolean notificationEmitter;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   321
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   322
        /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   323
         * <p>Construct a {@code ProxyOptions} object where all options have
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   324
         * their default values.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   325
         */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   326
        public ProxyOptions() {}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   327
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   328
        @Override
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   329
        public ProxyOptions clone() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   330
            return (ProxyOptions) super.clone();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   331
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   332
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   333
        /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   334
         * <p>Defines whether the returned proxy should
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   335
         * implement {@link NotificationEmitter}.  The default value is false.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   336
         *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   337
         * @return true if this proxy will be a NotificationEmitter.
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
         * @see JMX#newMBeanProxy(MBeanServerConnection, ObjectName, Class,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   340
         * MBeanOptions)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   341
         */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   342
        public boolean isNotificationEmitter() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   343
            return this.notificationEmitter;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   344
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   345
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   346
        /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   347
         * <p>Set the {@link #isNotificationEmitter NotificationEmitter} option to
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   348
         * the given value.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   349
         * @param emitter the new value.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   350
         */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   351
        public void setNotificationEmitter(boolean emitter) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   352
            this.notificationEmitter = emitter;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   353
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   354
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   355
        // Canonical objects for each of (MXBean,!MXBean) x (Emitter,!Emitter)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   356
        private static final ProxyOptions[] CANONICALS = {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   357
            new ProxyOptions(), new ProxyOptions(),
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   358
            new ProxyOptions(), new ProxyOptions(),
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   359
        };
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   360
        static {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   361
            CANONICALS[1].setMXBeanMappingFactory(MXBeanMappingFactory.DEFAULT);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   362
            CANONICALS[2].setNotificationEmitter(true);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   363
            CANONICALS[3].setMXBeanMappingFactory(MXBeanMappingFactory.DEFAULT);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   364
            CANONICALS[3].setNotificationEmitter(true);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   365
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   366
        @Override
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   367
        MBeanOptions[] canonicals() {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   368
            return CANONICALS;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   369
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   370
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   371
        @Override
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   372
        boolean same(MBeanOptions opt) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   373
            return (super.same(opt) && opt instanceof ProxyOptions &&
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   374
                    ((ProxyOptions) opt).notificationEmitter == notificationEmitter);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   375
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   376
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   377
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   378
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * <p>Make a proxy for a Standard MBean in a local or remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * MBean Server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * <p>If you have an MBean Server {@code mbs} containing an MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * with {@link ObjectName} {@code name}, and if the MBean's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * management interface is described by the Java interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * {@code MyMBean}, you can construct a proxy for the MBean like
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * this:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * MyMBean proxy = JMX.newMBeanProxy(mbs, name, MyMBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <p>Suppose, for example, {@code MyMBean} looks like this:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * public interface MyMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *     public String getSomeAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *     public void setSomeAttribute(String value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *     public void someOperation(String param1, int param2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * <p>Then you can execute:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * <li>{@code proxy.getSomeAttribute()} which will result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * call to {@code mbs.}{@link MBeanServerConnection#getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * getAttribute}{@code (name, "SomeAttribute")}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * <li>{@code proxy.setSomeAttribute("whatever")} which will result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * in a call to {@code mbs.}{@link MBeanServerConnection#setAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * setAttribute}{@code (name, new Attribute("SomeAttribute", "whatever"))}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * <li>{@code proxy.someOperation("param1", 2)} which will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * translated into a call to {@code mbs.}{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * MBeanServerConnection#invoke invoke}{@code (name, "someOperation", <etc>)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <p>The object returned by this method is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * {@link Proxy} whose {@code InvocationHandler} is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * {@link MBeanServerInvocationHandler}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * <p>This method is equivalent to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * #newMBeanProxy(MBeanServerConnection, ObjectName, Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * boolean) newMBeanProxy(connection, objectName, interfaceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * false)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @param connection the MBean server to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @param objectName the name of the MBean within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * {@code connection} to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @param interfaceClass the management interface that the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * exports, which will also be implemented by the returned proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @param <T> allows the compiler to know that if the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * interfaceClass} parameter is {@code MyMBean.class}, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * example, then the return type is {@code MyMBean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @return the new proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    public static <T> T newMBeanProxy(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                                      ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                                      Class<T> interfaceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        return newMBeanProxy(connection, objectName, interfaceClass, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * <p>Make a proxy for a Standard MBean in a local or remote MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * Server that may also support the methods of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * NotificationEmitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * <p>This method behaves the same as {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * #newMBeanProxy(MBeanServerConnection, ObjectName, Class)}, but
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   454
     * additionally, if {@code notificationEmitter} is {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * true}, then the MBean is assumed to be a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * NotificationBroadcaster} or {@link NotificationEmitter} and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * returned proxy will implement {@link NotificationEmitter} as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * well as {@code interfaceClass}.  A call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * NotificationBroadcaster#addNotificationListener} on the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * will result in a call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * MBeanServerConnection#addNotificationListener(ObjectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * NotificationListener, NotificationFilter, Object)}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * likewise for the other methods of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * NotificationBroadcaster} and {@link NotificationEmitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @param connection the MBean server to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * @param objectName the name of the MBean within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * {@code connection} to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @param interfaceClass the management interface that the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * exports, which will also be implemented by the returned proxy.
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   471
     * @param notificationEmitter make the returned proxy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * implement {@link NotificationEmitter} by forwarding its methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * via {@code connection}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @param <T> allows the compiler to know that if the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * interfaceClass} parameter is {@code MyMBean.class}, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * example, then the return type is {@code MyMBean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * @return the new proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    public static <T> T newMBeanProxy(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                      ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                                      Class<T> interfaceClass,
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   482
                                      boolean notificationEmitter) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   483
        ProxyOptions opts = new ProxyOptions();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   484
        opts.setNotificationEmitter(notificationEmitter);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   485
        return newMBeanProxy(connection, objectName, interfaceClass, opts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * <p>Make a proxy for an MXBean in a local or remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * MBean Server.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * <p>If you have an MBean Server {@code mbs} containing an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * MXBean with {@link ObjectName} {@code name}, and if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * MXBean's management interface is described by the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * interface {@code MyMXBean}, you can construct a proxy for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * the MXBean like this:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * MyMXBean proxy = JMX.newMXBeanProxy(mbs, name, MyMXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * <p>Suppose, for example, {@code MyMXBean} looks like this:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * public interface MyMXBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *     public String getSimpleAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     *     public void setSimpleAttribute(String value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *     public {@link java.lang.management.MemoryUsage} getMappedAttribute();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *     public void setMappedAttribute(MemoryUsage memoryUsage);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     *     public MemoryUsage someOperation(String param1, MemoryUsage param2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * <p>Then:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * <li><p>{@code proxy.getSimpleAttribute()} will result in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * call to {@code mbs.}{@link MBeanServerConnection#getAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * getAttribute}{@code (name, "SimpleAttribute")}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * <li><p>{@code proxy.setSimpleAttribute("whatever")} will result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * in a call to {@code mbs.}{@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * MBeanServerConnection#setAttribute setAttribute}<code>(name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * new Attribute("SimpleAttribute", "whatever"))</code>.<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     *     <p>Because {@code String} is a <em>simple type</em>, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     *     sense of {@link javax.management.openmbean.SimpleType}, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *     is not changed in the context of an MXBean.  The MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *     proxy behaves the same as a Standard MBean proxy (see
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     *     {@link #newMBeanProxy(MBeanServerConnection, ObjectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *     Class) newMBeanProxy}) for the attribute {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     *     SimpleAttribute}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * <li><p>{@code proxy.getMappedAttribute()} will result in a call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * to {@code mbs.getAttribute("MappedAttribute")}.  The MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * mapping rules mean that the actual type of the attribute {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * MappedAttribute} will be {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * javax.management.openmbean.CompositeData CompositeData} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * that is what the {@code mbs.getAttribute} call will return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * The proxy will then convert the {@code CompositeData} back into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * the expected type {@code MemoryUsage} using the MXBean mapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * rules.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * <li><p>Similarly, {@code proxy.setMappedAttribute(memoryUsage)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * will convert the {@code MemoryUsage} argument into a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * CompositeData} before calling {@code mbs.setAttribute}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * <li><p>{@code proxy.someOperation("whatever", memoryUsage)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * will convert the {@code MemoryUsage} argument into a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * CompositeData} and call {@code mbs.invoke}.  The value returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * by {@code mbs.invoke} will be also be a {@code CompositeData},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * and the proxy will convert this into the expected type {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * MemoryUsage} using the MXBean mapping rules.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * <p>The object returned by this method is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * {@link Proxy} whose {@code InvocationHandler} is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * {@link MBeanServerInvocationHandler}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * <p>This method is equivalent to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * #newMXBeanProxy(MBeanServerConnection, ObjectName, Class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * boolean) newMXBeanProxy(connection, objectName, interfaceClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * false)}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * @param connection the MBean server to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * @param objectName the name of the MBean within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * {@code connection} to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * @param interfaceClass the MXBean interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * which will also be implemented by the returned proxy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @param <T> allows the compiler to know that if the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * interfaceClass} parameter is {@code MyMXBean.class}, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * example, then the return type is {@code MyMXBean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * @return the new proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    public static <T> T newMXBeanProxy(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                                       ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                                       Class<T> interfaceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        return newMXBeanProxy(connection, objectName, interfaceClass, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * <p>Make a proxy for an MXBean in a local or remote MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * Server that may also support the methods of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * NotificationEmitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * <p>This method behaves the same as {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * #newMXBeanProxy(MBeanServerConnection, ObjectName, Class)}, but
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   592
     * additionally, if {@code notificationEmitter} is {@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * true}, then the MXBean is assumed to be a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * NotificationBroadcaster} or {@link NotificationEmitter} and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * returned proxy will implement {@link NotificationEmitter} as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * well as {@code interfaceClass}.  A call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * NotificationBroadcaster#addNotificationListener} on the proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * will result in a call to {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * MBeanServerConnection#addNotificationListener(ObjectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * NotificationListener, NotificationFilter, Object)}, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * likewise for the other methods of {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * NotificationBroadcaster} and {@link NotificationEmitter}.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @param connection the MBean server to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @param objectName the name of the MBean within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * {@code connection} to forward to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @param interfaceClass the MXBean interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * which will also be implemented by the returned proxy.
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   609
     * @param notificationEmitter make the returned proxy
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * implement {@link NotificationEmitter} by forwarding its methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * via {@code connection}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @param <T> allows the compiler to know that if the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * interfaceClass} parameter is {@code MyMXBean.class}, for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * example, then the return type is {@code MyMXBean}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @return the new proxy instance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    public static <T> T newMXBeanProxy(MBeanServerConnection connection,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                                       ObjectName objectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                                       Class<T> interfaceClass,
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   620
                                       boolean notificationEmitter) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   621
        ProxyOptions opts = new ProxyOptions();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   622
        MXBeanMappingFactory f = MXBeanMappingFactory.forInterface(interfaceClass);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   623
        opts.setMXBeanMappingFactory(f);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   624
        opts.setNotificationEmitter(notificationEmitter);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   625
        return newMBeanProxy(connection, objectName, interfaceClass, opts);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   626
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   627
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   628
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   629
     * <p>Make a proxy for a Standard MBean or MXBean in a local or remote MBean
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   630
     * Server that may also support the methods of {@link
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   631
     * NotificationEmitter} and (for an MXBean) that may define custom MXBean
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   632
     * type mappings.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   633
     *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   634
     * <p>This method behaves the same as
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   635
     * {@link #newMBeanProxy(MBeanServerConnection, ObjectName, Class)} or
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   636
     * {@link #newMXBeanProxy(MBeanServerConnection, ObjectName, Class)},
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   637
     * according as {@code opts.isMXBean()} is respectively false or true; but
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   638
     * with the following changes based on {@code opts}.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   639
     *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   640
     * <ul>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   641
     *     <li>If {@code opts.isNotificationEmitter()} is {@code
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   642
     *         true}, then the MBean is assumed to be a {@link
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   643
     *         NotificationBroadcaster} or {@link NotificationEmitter} and the
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   644
     *         returned proxy will implement {@link NotificationEmitter} as
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   645
     *         well as {@code interfaceClass}.  A call to {@link
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   646
     *         NotificationBroadcaster#addNotificationListener} on the proxy
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   647
     *         will result in a call to {@link
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   648
     *         MBeanServerConnection#addNotificationListener(ObjectName,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   649
     *         NotificationListener, NotificationFilter, Object)}, and
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   650
     *         likewise for the other methods of {@link
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   651
     *     NotificationBroadcaster} and {@link NotificationEmitter}.</li>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   652
     *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   653
     *     <li>If {@code opts.getMXBeanMappingFactory()} is not null,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   654
     *         then the mappings it defines will be applied to convert between
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   655
     *     arbitrary Java types and Open Types.</li>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   656
     * </ul>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   657
     *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   658
     * @param connection the MBean server to forward to.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   659
     * @param objectName the name of the MBean within
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   660
     * {@code connection} to forward to.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   661
     * @param interfaceClass the Standard MBean or MXBean interface,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   662
     * which will also be implemented by the returned proxy.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   663
     * @param opts the options to apply for this proxy.  Can be null,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   664
     * in which case default options are applied.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   665
     * @param <T> allows the compiler to know that if the {@code
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   666
     * interfaceClass} parameter is {@code MyMXBean.class}, for
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   667
     * example, then the return type is {@code MyMXBean}.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   668
     * @return the new proxy instance.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   669
     *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   670
     * @throws IllegalArgumentException if {@code interfaceClass} is not a
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   671
     * valid MXBean interface.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   672
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   673
    public static <T> T newMBeanProxy(MBeanServerConnection connection,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   674
                                      ObjectName objectName,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   675
                                      Class<T> interfaceClass,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   676
                                      MBeanOptions opts) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        try {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   678
            return newMBeanProxy2(connection, objectName, interfaceClass, opts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        } catch (NotCompliantMBeanException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            throw new IllegalArgumentException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        }
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   682
    }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   683
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   684
    private static <T> T newMBeanProxy2(MBeanServerConnection connection,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   685
                                        ObjectName objectName,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   686
                                        Class<T> interfaceClass,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   687
                                        MBeanOptions opts)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   688
    throws NotCompliantMBeanException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   689
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   690
        if (opts == null)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   691
            opts = new MBeanOptions();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   692
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   693
        boolean notificationEmitter = opts instanceof ProxyOptions &&
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   694
                ((ProxyOptions) opts).isNotificationEmitter();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   695
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   696
        MXBeanMappingFactory mappingFactory = opts.getMXBeanMappingFactory();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   697
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   698
        if (mappingFactory != null) {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   699
            // Check interface for MXBean compliance
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   700
            Introspector.testComplianceMXBeanInterface(interfaceClass,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   701
                    mappingFactory);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   702
        }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   703
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        InvocationHandler handler = new MBeanServerInvocationHandler(
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   705
                connection, objectName, opts);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        final Class[] interfaces;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents: 2
diff changeset
   707
        if (notificationEmitter) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            interfaces =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                new Class<?>[] {interfaceClass, NotificationEmitter.class};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            interfaces = new Class[] {interfaceClass};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        Object proxy = Proxy.newProxyInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                interfaceClass.getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
                interfaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        return interfaceClass.cast(proxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * <p>Test whether an interface is an MXBean interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * An interface is an MXBean interface if it is annotated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * {@link MXBean &#64;MXBean} or {@code @MXBean(true)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * or if it does not have an {@code @MXBean} annotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * and its name ends with "{@code MXBean}".</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * @param interfaceClass The candidate interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * @return true if {@code interfaceClass} is an interface and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * meets the conditions described.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @throws NullPointerException if {@code interfaceClass} is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    public static boolean isMXBeanInterface(Class<?> interfaceClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        if (!interfaceClass.isInterface())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        MXBean a = interfaceClass.getAnnotation(MXBean.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (a != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            return a.value();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        return interfaceClass.getName().endsWith("MXBean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        // We don't bother excluding the case where the name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        // exactly the string "MXBean" since that would mean there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        // was no package name, which is pretty unlikely in practice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
833
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   744
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   745
    /**
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   746
     * <p>Test if an MBean can emit notifications.  An MBean can emit
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   747
     * notifications if either it implements {@link NotificationBroadcaster}
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   748
     * (perhaps through its child interface {@link NotificationEmitter}), or
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   749
     * it uses <a href="MBeanRegistration.html#injection">resource
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   750
     * injection</a> to obtain an instance of {@link SendNotification}
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   751
     * through which it can send notifications.</p>
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   752
     *
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   753
     * @param mbean an MBean object.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   754
     * @return true if the given object is a valid MBean that can emit
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   755
     * notifications; false if the object is a valid MBean but that
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   756
     * cannot emit notifications.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   757
     * @throws NotCompliantMBeanException if the given object is not
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   758
     * a valid MBean.
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   759
     */
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   760
    public static boolean isNotificationSource(Object mbean)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   761
            throws NotCompliantMBeanException {
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   762
        if (mbean instanceof NotificationBroadcaster)
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   763
            return true;
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   764
        Object resource = (mbean instanceof DynamicWrapperMBean) ?
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   765
            ((DynamicWrapperMBean) mbean).getWrappedObject() : mbean;
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   766
        return (MBeanInjector.injectsSendNotification(resource));
bfa2bef7517c 6323980: Annotations to simplify MBean development
emcmanus
parents: 687
diff changeset
   767
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
}