Path: blob/master/src/java.management/share/classes/com/sun/jmx/mbeanserver/MXBeanMapping.java
41161 views
/*1* Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.jmx.mbeanserver;2627import java.io.InvalidObjectException;28import java.lang.reflect.Type;29import javax.management.openmbean.OpenDataException;30import javax.management.openmbean.OpenType;3132/**33* <p>A custom mapping between Java types and Open types for use in MXBeans.34* To define such a mapping, subclass this class and define at least the35* {@link #fromOpenValue fromOpenValue} and {@link #toOpenValue toOpenValue}36* methods, and optionally the {@link #checkReconstructible} method.37* Then either use an {@link MXBeanMappingClass} annotation on your custom38* Java types, or include this MXBeanMapping in an39* {@link MXBeanMappingFactory}.</p>40*41* <p>For example, suppose we have a class {@code MyLinkedList}, which looks42* like this:</p>43*44* <pre>45* public class MyLinkedList {46* public MyLinkedList(String name, MyLinkedList next) {...}47* public String getName() {...}48* public MyLinkedList getNext() {...}49* }50* </pre>51*52* <p>This is not a valid type for MXBeans, because it contains a53* self-referential property "next" defined by the {@code getNext()}54* method. MXBeans do not support recursive types. So we would like55* to specify a mapping for {@code MyLinkedList} explicitly. When an56* MXBean interface contains {@code MyLinkedList}, that will be mapped57* into a {@code String[]}, which is a valid Open Type.</p>58*59* <p>To define this mapping, we first subclass {@code MXBeanMapping}:</p>60*61* <pre>62* public class MyLinkedListMapping extends MXBeanMapping {63* public MyLinkedListMapping(Type type) throws OpenDataException {64* super(MyLinkedList.class, ArrayType.getArrayType(SimpleType.STRING));65* if (type != MyLinkedList.class)66* throw new OpenDataException("Mapping only valid for MyLinkedList");67* }68*69* {@literal @Override}70* public Object fromOpenValue(Object openValue) throws InvalidObjectException {71* String[] array = (String[]) openValue;72* MyLinkedList list = null;73* for (int i = array.length - 1; i >= 0; i--)74* list = new MyLinkedList(array[i], list);75* return list;76* }77*78* {@literal @Override}79* public Object toOpenValue(Object javaValue) throws OpenDataException {80* ArrayList<String> array = new ArrayList<String>();81* for (MyLinkedList list = (MyLinkedList) javaValue; list != null;82* list = list.getNext())83* array.add(list.getName());84* return array.toArray(new String[0]);85* }86* }87* </pre>88*89* <p>The call to the superclass constructor specifies what the90* original Java type is ({@code MyLinkedList.class}) and what Open91* Type it is mapped to ({@code92* ArrayType.getArrayType(SimpleType.STRING)}). The {@code93* fromOpenValue} method says how we go from the Open Type ({@code94* String[]}) to the Java type ({@code MyLinkedList}), and the {@code95* toOpenValue} method says how we go from the Java type to the Open96* Type.</p>97*98* <p>With this mapping defined, we can annotate the {@code MyLinkedList}99* class appropriately:</p>100*101* <pre>102* {@literal @MXBeanMappingClass}(MyLinkedListMapping.class)103* public class MyLinkedList {...}104* </pre>105*106* <p>Now we can use {@code MyLinkedList} in an MXBean interface and it107* will work.</p>108*109* <p>If we are unable to modify the {@code MyLinkedList} class,110* we can define an {@link MXBeanMappingFactory}. See the documentation111* of that class for further details.</p>112*113* @see <a href="../MXBean.html#custom">MXBean specification, section114* "Custom MXBean type mappings"</a>115*/116public abstract class MXBeanMapping {117private final Type javaType;118private final OpenType<?> openType;119private final Class<?> openClass;120121/**122* <p>Construct a mapping between the given Java type and the given123* Open Type.</p>124*125* @param javaType the Java type (for example, {@code MyLinkedList}).126* @param openType the Open Type (for example, {@code127* ArrayType.getArrayType(SimpleType.STRING)})128*129* @throws NullPointerException if either argument is null.130*/131protected MXBeanMapping(Type javaType, OpenType<?> openType) {132if (javaType == null || openType == null)133throw new NullPointerException("Null argument");134this.javaType = javaType;135this.openType = openType;136this.openClass = makeOpenClass(javaType, openType);137}138139/**140* <p>The Java type that was supplied to the constructor.</p>141* @return the Java type that was supplied to the constructor.142*/143public final Type getJavaType() {144return javaType;145}146147/**148* <p>The Open Type that was supplied to the constructor.</p>149* @return the Open Type that was supplied to the constructor.150*/151public final OpenType<?> getOpenType() {152return openType;153}154155/**156* <p>The Java class that corresponds to instances of the157* {@linkplain #getOpenType() Open Type} for this mapping.</p>158* @return the Java class that corresponds to instances of the159* Open Type for this mapping.160* @see OpenType#getClassName161*/162public final Class<?> getOpenClass() {163return openClass;164}165166private static Class<?> makeOpenClass(Type javaType, OpenType<?> openType) {167if (javaType instanceof Class<?> && ((Class<?>) javaType).isPrimitive())168return (Class<?>) javaType;169try {170String className = openType.getClassName();171return Class.forName(className, false, MXBeanMapping.class.getClassLoader());172} catch (ClassNotFoundException e) {173throw new RuntimeException(e); // should not happen174}175}176177/**178* <p>Convert an instance of the Open Type into the Java type.179* @param openValue the value to be converted.180* @return the converted value.181* @throws InvalidObjectException if the value cannot be converted.182*/183public abstract Object fromOpenValue(Object openValue)184throws InvalidObjectException;185186/**187* <p>Convert an instance of the Java type into the Open Type.188* @param javaValue the value to be converted.189* @return the converted value.190* @throws OpenDataException if the value cannot be converted.191*/192public abstract Object toOpenValue(Object javaValue)193throws OpenDataException;194195196/**197* <p>Throw an appropriate InvalidObjectException if we will not198* be able to convert back from the open data to the original Java199* object. The {@link #fromOpenValue fromOpenValue} throws an200* exception if a given open data value cannot be converted. This201* method throws an exception if <em>no</em> open data values can202* be converted. The default implementation of this method never203* throws an exception. Subclasses can override it as204* appropriate.</p>205* @throws InvalidObjectException if {@code fromOpenValue} will throw206* an exception no matter what its argument is.207*/208public void checkReconstructible() throws InvalidObjectException {}209}210211212