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