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