jdk/src/share/classes/com/sun/jmx/mbeanserver/MXBeanMappingFactory.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 4156 acaa49a2768a
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     2
 * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     4
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    10
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    15
 * accompanied this code).
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    16
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4156
diff changeset
    23
 * questions.
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    24
 */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    25
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1322
diff changeset
    26
package com.sun.jmx.mbeanserver;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    27
4156
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1322
diff changeset
    28
import javax.management.openmbean.*;
acaa49a2768a 6851617: Remove JSR 255 (JMX API 2.0) from JDK 7
emcmanus
parents: 1322
diff changeset
    29
import com.sun.jmx.mbeanserver.MXBeanMapping;
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    30
import com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    31
import java.lang.reflect.Type;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    32
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    33
/**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    34
 * <p>Defines how types are mapped for a given MXBean or set of MXBeans.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    35
 * An {@code MXBeanMappingFactory} can be specified either through the
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    36
 * {@link MXBeanMappingFactoryClass} annotation, or through the
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    37
 * {@link javax.management.JMX.MBeanOptions JMX.MBeanOptions} argument to a
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    38
 * {@link javax.management.StandardMBean StandardMBean} constructor or MXBean
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    39
 * proxy.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    40
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    41
 * <p>An {@code MXBeanMappingFactory} must return an {@code MXBeanMapping}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    42
 * for any Java type that appears in the MXBeans that the factory is being
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    43
 * used for.  Usually it does that by handling any custom types, and
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    44
 * forwarding everything else to the {@linkplain #DEFAULT default mapping
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    45
 * factory}.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    46
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    47
 * <p>Consider the {@code MyLinkedList} example from the {@link MXBeanMapping}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    48
 * documentation.  If we are unable to change the {@code MyLinkedList} class
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    49
 * to add an {@link MXBeanMappingClass} annotation, we could achieve the same
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    50
 * effect by defining {@code MyLinkedListMappingFactory} as follows:</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    51
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    52
 * <pre>
1322
d038148778cc 6750935: The expected NotCompliantMBeanException is not thrown for the custom MXBeanMappingFactory
emcmanus
parents: 1011
diff changeset
    53
 * public class MyLinkedListMappingFactory extends MXBeanMappingFactory {
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    54
 *     public MyLinkedListMappingFactory() {}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    55
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    56
 *     public MXBeanMapping mappingForType(Type t, MXBeanMappingFactory f)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    57
 *     throws OpenDataException {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    58
 *         if (t == MyLinkedList.class)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    59
 *             return new MyLinkedListMapping(t);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    60
 *         else
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    61
 *             return MXBeanMappingFactory.DEFAULT.mappingForType(t, f);
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    62
 *     }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    63
 * }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    64
 * </pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    65
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    66
 * <p>The mapping factory handles only the {@code MyLinkedList} class.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    67
 * Every other type is forwarded to the default mapping factory.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    68
 * This includes types such as {@code MyLinkedList[]} and
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    69
 * {@code List<MyLinkedList>}; the default mapping factory will recursively
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    70
 * invoke {@code MyLinkedListMappingFactory} to map the contained
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    71
 * {@code MyLinkedList} type.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    72
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    73
 * <p>Once we have defined {@code MyLinkedListMappingFactory}, we can use
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    74
 * it in an MXBean interface like this:</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    75
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    76
 * <pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    77
 * {@literal @MXBeanMappingFactoryClass}(MyLinkedListMappingFactory.class)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    78
 * public interface SomethingMXBean {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    79
 *     public MyLinkedList getSomething();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    80
 * }
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    81
 * </pre>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    82
 *
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    83
 * <p>Alternatively we can annotate the package that {@code SomethingMXBean}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    84
 * appears in, or we can supply the factory to a {@link
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    85
 * javax.management.StandardMBean StandardMBean} constructor or MXBean
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    86
 * proxy.</p>
1011
2cc4873fa29f 6734273: Minor updates to documentation of Custom MXBean Mappings
emcmanus
parents: 715
diff changeset
    87
 *
2cc4873fa29f 6734273: Minor updates to documentation of Custom MXBean Mappings
emcmanus
parents: 715
diff changeset
    88
 * @see <a href="../MXBean.html#custom">MXBean specification, section
2cc4873fa29f 6734273: Minor updates to documentation of Custom MXBean Mappings
emcmanus
parents: 715
diff changeset
    89
 * "Custom MXBean type mappings"</a>
687
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    90
 */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    91
public abstract class MXBeanMappingFactory {
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    92
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    93
     * <p>Construct an instance of this class.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    94
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    95
    protected MXBeanMappingFactory() {}
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    96
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    97
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    98
     * <p>Mapping factory that applies the default rules for MXBean
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
    99
     * mappings, as described in the <a
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   100
     * href="../MXBean.html#MXBean-spec">MXBean specification</a>.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   101
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   102
    public static final MXBeanMappingFactory DEFAULT =
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   103
            new DefaultMXBeanMappingFactory();
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   104
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   105
    /**
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   106
     * <p>Return the mapping for the given Java type.  Typically, a
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   107
     * mapping factory will return mappings for types it handles, and
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   108
     * forward other types to another mapping factory, most often
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   109
     * the {@linkplain #DEFAULT default one}.</p>
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   110
     * @param t the Java type to be mapped.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   111
     * @param f the original mapping factory that was consulted to do
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   112
     * the mapping.  A mapping factory should pass this parameter intact
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   113
     * if it forwards a type to another mapping factory.  In the example,
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   114
     * this is how {@code MyLinkedListMappingFactory} works for types
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   115
     * like {@code MyLinkedList[]} and {@code List<MyLinkedList>}.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   116
     * @return the mapping for the given type.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   117
     * @throws OpenDataException if this type cannot be mapped.  This
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   118
     * exception is appropriate if the factory is supposed to handle
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   119
     * all types of this sort (for example, all linked lists), but
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   120
     * cannot handle this particular type.
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   121
     */
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   122
    public abstract MXBeanMapping mappingForType(Type t, MXBeanMappingFactory f)
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   123
    throws OpenDataException;
874e25a9844a 6562936: Support custom type mappings in MXBeans
emcmanus
parents:
diff changeset
   124
}