687
|
1 |
/*
|
715
|
2 |
* Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
|
687
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package javax.management.openmbean;
|
|
27 |
|
|
28 |
import com.sun.jmx.mbeanserver.DefaultMXBeanMappingFactory;
|
|
29 |
import java.lang.reflect.Type;
|
|
30 |
|
|
31 |
/**
|
|
32 |
* <p>Defines how types are mapped for a given MXBean or set of MXBeans.
|
|
33 |
* An {@code MXBeanMappingFactory} can be specified either through the
|
|
34 |
* {@link MXBeanMappingFactoryClass} annotation, or through the
|
|
35 |
* {@link javax.management.JMX.MBeanOptions JMX.MBeanOptions} argument to a
|
|
36 |
* {@link javax.management.StandardMBean StandardMBean} constructor or MXBean
|
|
37 |
* proxy.</p>
|
|
38 |
*
|
|
39 |
* <p>An {@code MXBeanMappingFactory} must return an {@code MXBeanMapping}
|
|
40 |
* for any Java type that appears in the MXBeans that the factory is being
|
|
41 |
* used for. Usually it does that by handling any custom types, and
|
|
42 |
* forwarding everything else to the {@linkplain #DEFAULT default mapping
|
|
43 |
* factory}.</p>
|
|
44 |
*
|
|
45 |
* <p>Consider the {@code MyLinkedList} example from the {@link MXBeanMapping}
|
|
46 |
* documentation. If we are unable to change the {@code MyLinkedList} class
|
|
47 |
* to add an {@link MXBeanMappingClass} annotation, we could achieve the same
|
|
48 |
* effect by defining {@code MyLinkedListMappingFactory} as follows:</p>
|
|
49 |
*
|
|
50 |
* <pre>
|
|
51 |
* public class MyLinkedListMappingFactory implements MXBeanMappingFactory {
|
|
52 |
* public MyLinkedListMappingFactory() {}
|
|
53 |
*
|
|
54 |
* public MXBeanMapping mappingForType(Type t, MXBeanMappingFactory f)
|
|
55 |
* throws OpenDataException {
|
|
56 |
* if (t == MyLinkedList.class)
|
|
57 |
* return new MyLinkedListMapping(t);
|
|
58 |
* else
|
|
59 |
* return MXBeanMappingFactory.DEFAULT.mappingForType(t, f);
|
|
60 |
* }
|
|
61 |
* }
|
|
62 |
* </pre>
|
|
63 |
*
|
|
64 |
* <p>The mapping factory handles only the {@code MyLinkedList} class.
|
|
65 |
* Every other type is forwarded to the default mapping factory.
|
|
66 |
* This includes types such as {@code MyLinkedList[]} and
|
|
67 |
* {@code List<MyLinkedList>}; the default mapping factory will recursively
|
|
68 |
* invoke {@code MyLinkedListMappingFactory} to map the contained
|
|
69 |
* {@code MyLinkedList} type.</p>
|
|
70 |
*
|
|
71 |
* <p>Once we have defined {@code MyLinkedListMappingFactory}, we can use
|
|
72 |
* it in an MXBean interface like this:</p>
|
|
73 |
*
|
|
74 |
* <pre>
|
|
75 |
* {@literal @MXBeanMappingFactoryClass}(MyLinkedListMappingFactory.class)
|
|
76 |
* public interface SomethingMXBean {
|
|
77 |
* public MyLinkedList getSomething();
|
|
78 |
* }
|
|
79 |
* </pre>
|
|
80 |
*
|
|
81 |
* <p>Alternatively we can annotate the package that {@code SomethingMXBean}
|
|
82 |
* appears in, or we can supply the factory to a {@link
|
|
83 |
* javax.management.StandardMBean StandardMBean} constructor or MXBean
|
|
84 |
* proxy.</p>
|
|
85 |
*/
|
|
86 |
public abstract class MXBeanMappingFactory {
|
|
87 |
/**
|
|
88 |
* <p>Construct an instance of this class.</p>
|
|
89 |
*/
|
|
90 |
protected MXBeanMappingFactory() {}
|
|
91 |
|
|
92 |
/**
|
|
93 |
* <p>Mapping factory that applies the default rules for MXBean
|
|
94 |
* mappings, as described in the <a
|
|
95 |
* href="../MXBean.html#MXBean-spec">MXBean specification</a>.</p>
|
|
96 |
*/
|
|
97 |
public static final MXBeanMappingFactory DEFAULT =
|
|
98 |
new DefaultMXBeanMappingFactory();
|
|
99 |
|
|
100 |
/**
|
|
101 |
* <p>Determine the appropriate MXBeanMappingFactory to use for the given
|
|
102 |
* MXBean interface, based on its annotations. If the interface has an
|
|
103 |
* {@link MXBeanMappingFactoryClass @MXBeanMappingFactoryClass} annotation,
|
|
104 |
* that is used to determine the MXBeanMappingFactory. Otherwise, if the
|
|
105 |
* package containing the interface has such an annotation, that is used.
|
|
106 |
* Otherwise the MXBeanMappingFactory is the {@linkplain #DEFAULT default}
|
|
107 |
* one.</p>
|
|
108 |
*
|
|
109 |
* @param intf the MXBean interface for which to determine the
|
|
110 |
* MXBeanMappingFactory.
|
|
111 |
*
|
|
112 |
* @return the MXBeanMappingFactory for the given MXBean interface.
|
|
113 |
*
|
|
114 |
* @throws IllegalArgumentException if {@code intf} is null, or if an
|
|
115 |
* exception occurs while trying constructing an MXBeanMappingFactory
|
|
116 |
* based on an annotation. In the second case, the exception will appear
|
|
117 |
* in the {@linkplain Throwable#getCause() cause chain} of the
|
|
118 |
* {@code IllegalArgumentException}.
|
|
119 |
*/
|
|
120 |
public static MXBeanMappingFactory forInterface(Class<?> intf) {
|
|
121 |
if (intf == null)
|
|
122 |
throw new IllegalArgumentException("Null interface");
|
|
123 |
MXBeanMappingFactoryClass annot =
|
|
124 |
intf.getAnnotation(MXBeanMappingFactoryClass.class);
|
|
125 |
if (annot == null) {
|
|
126 |
Package p = intf.getPackage();
|
|
127 |
if (p != null)
|
|
128 |
annot = p.getAnnotation(MXBeanMappingFactoryClass.class);
|
|
129 |
}
|
|
130 |
if (annot == null)
|
|
131 |
return MXBeanMappingFactory.DEFAULT;
|
|
132 |
Class<? extends MXBeanMappingFactory> factoryClass = annot.value();
|
|
133 |
try {
|
|
134 |
return annot.value().newInstance();
|
|
135 |
} catch (Exception e) {
|
|
136 |
throw new IllegalArgumentException(
|
|
137 |
"Could not instantiate MXBeanMappingFactory " +
|
|
138 |
factoryClass.getName() +
|
|
139 |
" from @MXBeanMappingFactoryClass", e);
|
|
140 |
}
|
|
141 |
}
|
|
142 |
|
|
143 |
/**
|
|
144 |
* <p>Return the mapping for the given Java type. Typically, a
|
|
145 |
* mapping factory will return mappings for types it handles, and
|
|
146 |
* forward other types to another mapping factory, most often
|
|
147 |
* the {@linkplain #DEFAULT default one}.</p>
|
|
148 |
* @param t the Java type to be mapped.
|
|
149 |
* @param f the original mapping factory that was consulted to do
|
|
150 |
* the mapping. A mapping factory should pass this parameter intact
|
|
151 |
* if it forwards a type to another mapping factory. In the example,
|
|
152 |
* this is how {@code MyLinkedListMappingFactory} works for types
|
|
153 |
* like {@code MyLinkedList[]} and {@code List<MyLinkedList>}.
|
|
154 |
* @return the mapping for the given type.
|
|
155 |
* @throws OpenDataException if this type cannot be mapped. This
|
|
156 |
* exception is appropriate if the factory is supposed to handle
|
|
157 |
* all types of this sort (for example, all linked lists), but
|
|
158 |
* cannot handle this particular type.
|
|
159 |
*/
|
|
160 |
public abstract MXBeanMapping mappingForType(Type t, MXBeanMappingFactory f)
|
|
161 |
throws OpenDataException;
|
|
162 |
}
|