Please note that this documentation is not final and is subject to change.

Java™ Platform
Standard Ed. 7

DRAFT ea-b34

javax.management.openmbean
Class MXBeanMapping

java.lang.Object
  extended by javax.management.openmbean.MXBeanMapping

public abstract class MXBeanMapping
extends Object

A custom mapping between Java types and Open types for use in MXBeans. To define such a mapping, subclass this class and define at least the fromOpenValue and toOpenValue methods, and optionally the checkReconstructible() method. Then either use an MXBeanMappingClass annotation on your custom Java types, or include this MXBeanMapping in an MXBeanMappingFactory.

For example, suppose we have a class MyLinkedList, which looks like this:

 public class MyLinkedList {
     public MyLinkedList(String name, MyLinkedList next) {...}
     public String getName() {...}
     public MyLinkedList getNext() {...}
 }
 

This is not a valid type for MXBeans, because it contains a self-referential property "next" defined by the getNext() method. MXBeans do not support recursive types. So we would like to specify a mapping for MyLinkedList explicitly. When an MXBean interface contains MyLinkedList, that will be mapped into a String[], which is a valid Open Type.

To define this mapping, we first subclass MXBeanMapping:

 public class MyLinkedListMapping extends MXBeanMapping {
     public MyLinkedListMapping(Type type) throws OpenDataException {
         super(MyLinkedList.class, ArrayType.getArrayType(SimpleType.STRING));
         if (type != MyLinkedList.class)
             throw new OpenDataException("Mapping only valid for MyLinkedList");
     }

     @Override
     public Object fromOpenValue(Object openValue) throws InvalidObjectException {
         String[] array = (String[]) openValue;
         MyLinkedList list = null;
         for (int i = array.length - 1; i >= 0; i--)
             list = new MyLinkedList(array[i], list);
         return list;
     }

     @Override
     public Object toOpenValue(Object javaValue) throws OpenDataException {
         ArrayList<String> array = new ArrayList<String>();
         for (MyLinkedList list = (MyLinkedList) javaValue; list != null;
              list = list.getNext())
             array.add(list.getName());
         return array.toArray(new String[0]);
     }
 }
 

The call to the superclass constructor specifies what the original Java type is (MyLinkedList.class) and what Open Type it is mapped to (ArrayType.getArrayType(SimpleType.STRING)). The fromOpenValue method says how we go from the Open Type (String[]) to the Java type (MyLinkedList), and the toOpenValue method says how we go from the Java type to the Open Type.

With this mapping defined, we can annotate the MyLinkedList class appropriately:

 @MXBeanMappingClass(MyLinkedListMapping.class)
 public class MyLinkedList {...}
 

Now we can use MyLinkedList in an MXBean interface and it will work.

If we are unable to modify the MyLinkedList class, we can define an MXBeanMappingFactory. See the documentation of that class for further details.

See Also:
MXBean specification, section "Custom MXBean type mappings"

Constructor Summary
protected MXBeanMapping(Type javaType, OpenType<?> openType)
          Construct a mapping between the given Java type and the given Open Type.
 
Method Summary
 void checkReconstructible()
          Throw an appropriate InvalidObjectException if we will not be able to convert back from the open data to the original Java object.
abstract  Object fromOpenValue(Object openValue)
          Convert an instance of the Open Type into the Java type.
 Type getJavaType()
          The Java type that was supplied to the constructor.
 Class<?> getOpenClass()
          The Java class that corresponds to instances of the Open Type for this mapping.
 OpenType<?> getOpenType()
          The Open Type that was supplied to the constructor.
abstract  Object toOpenValue(Object javaValue)
          Convert an instance of the Java type into the Open Type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MXBeanMapping

protected MXBeanMapping(Type javaType,
                        OpenType<?> openType)

Construct a mapping between the given Java type and the given Open Type.

Parameters:
javaType - the Java type (for example, MyLinkedList).
openType - the Open Type (for example, ArrayType.getArrayType(SimpleType.STRING))
Throws:
NullPointerException - if either argument is null.
Method Detail

getJavaType

public final Type getJavaType()

The Java type that was supplied to the constructor.

Returns:
the Java type that was supplied to the constructor.

getOpenType

public final OpenType<?> getOpenType()

The Open Type that was supplied to the constructor.

Returns:
the Open Type that was supplied to the constructor.

getOpenClass

public final Class<?> getOpenClass()

The Java class that corresponds to instances of the Open Type for this mapping.

Returns:
the Java class that corresponds to instances of the Open Type for this mapping.
See Also:
OpenType.getClassName()

fromOpenValue

public abstract Object fromOpenValue(Object openValue)
                              throws InvalidObjectException

Convert an instance of the Open Type into the Java type.

Parameters:
openValue - the value to be converted.
Returns:
the converted value.
Throws:
InvalidObjectException - if the value cannot be converted.

toOpenValue

public abstract Object toOpenValue(Object javaValue)
                            throws OpenDataException

Convert an instance of the Java type into the Open Type.

Parameters:
javaValue - the value to be converted.
Returns:
the converted value.
Throws:
OpenDataException - if the value cannot be converted.

checkReconstructible

public void checkReconstructible()
                          throws InvalidObjectException

Throw an appropriate InvalidObjectException if we will not be able to convert back from the open data to the original Java object. The fromOpenValue throws an exception if a given open data value cannot be converted. This method throws an exception if no open data values can be converted. The default implementation of this method never throws an exception. Subclasses can override it as appropriate.

Throws:
InvalidObjectException - if fromOpenValue will throw an exception no matter what its argument is.

Java™ Platform
Standard Ed. 7

DRAFT ea-b34

Submit a bug or feature

Copyright 2008 Sun Microsystems, Inc. All Rights Reserved. Use is subject to license terms.