gov.nasa.arc.brahms.vm.api.convert
Class ConverterManager

java.lang.Object
  extended by gov.nasa.arc.brahms.vm.api.convert.ConverterManager

public class ConverterManager
extends java.lang.Object

ConverterManager is a manager responsible for supporting the conversion between Brahms objects and Java objects. It maintains a list of the supported Brahms/Java converters responsible for properly converting between Brahms objects/types and Java objects/types.

There are a couple of guidelines to follow to have the conversion work properly:

  1. Only Brahms objects can be converted, not agents, conceptual objects, or areas.
  2. Java classes and Brahms classes that are converted to one another must have the same package and class names.
  3. Java classes to be converted must comply with the Java Bean specification, i.e. each property is identified by its getters and setters: get, set and for boolean values the getter is: is. In the Brahms classes the attribute/relation names must match the Property Names.
  4. Java classes for which no conversion is possible are ignored and not mapped to attributes of a Java type at the moment.
The converter converts the following special types: You can write and register your own converters in case you need to convert your classes in a different way than is supported. The ConverterManager accessible via the gov.nasa.arc.brahms.vm.api.JAPI allows you to register converter types and converters.

To convert a Java object to Brahms or vice versa:

  // create a conversion context (used to ensure that we
  // don't get into cycles with nested objects and don't
  // reconvert an already converted reference
  ConverterManager oCM = JAPI.getConverterManager();
  ConversionContext oContext = new ConversionContext();
  oContext.setBrahmsContext(getContext()); 

  // convert to Brahms
  MyJavaClass oJavaObject = ...
  IObject oConvertedBrahmsObject = oCM.toBrahms(oJavaObject, oContext);
  // clear the conversion cache
  oContext.clearBrahmsConverted();
  oContext.clearJavaConverted();

  // convert to Java
  IObject oBrahmsObject = ...
  MyJavaClass oConvertedJavaObject = (MyJavaClass)oCM.toJava(oBrahmsObject, oContext);
  // clear the conversion cache
  oContext.clearBrahmsConverted();
  oContext.clearJavaConverted();
 

Version:
$Revision: 1.4 $ $Date: 2009/11/23 21:13:34 $ $Author: rnado $
Author:
Ron van Hoof

Constructor Summary
ConverterManager()
          Constructor, creates a new ConverterManager.
 
Method Summary
 void addBrahmsConverter(IType brahmsType, java.lang.String type)
          Adds the specified converter supporting conversion from/to the specified brahmsType.
 void addConverterType(java.lang.String name, IConverter converter)
          Adds the specified converter identified by the specified name.
 void addJavaConverter(java.lang.Class<?> javaType, java.lang.String type)
          Adds the specified converter type supporting conversion from/to the specified javaType.
 BrahmsClassDescriptor getBrahmsClassDescriptor(java.lang.String qualifiedName)
          Returns the BrahmsClassDescriptor for the Brahms class with the specified qualified name.
 IConverter getBrahmsConverter(IType brahmsType)
          Returns the converter supporting conversion from/to the specified brahmsType.
 BrahmsGroupDescriptor getBrahmsGroupDescriptor(java.lang.String qualifiedName)
          Returns the BrahmsGroupDescriptor for the Brahms group with the specified qualified name.
 IConverter getConverterType(java.lang.String name)
          Returns the IConverter with the specified name.
 IConverter getJavaConverter(java.lang.Class<?> javaType)
          Returns the converter supporting conversion from/to the specified javaType.
protected  void initialize()
          Create the base set of converters.
 boolean isPrimitive(java.lang.Class<?> c)
          Indicates whether the type identified by the specified class is a primitive type.
 IObject toBrahms(java.lang.Object obj, ConversionContext ctx)
          Converts the specified Java object into its matching Brahms object representation.
 java.lang.Object toJava(IObject obj, ConversionContext ctx)
          Converts the specified Brahms object into its matching Java object representation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConverterManager

public ConverterManager()
Constructor, creates a new ConverterManager.

Method Detail

initialize

protected void initialize()
Create the base set of converters.


addConverterType

public void addConverterType(java.lang.String name,
                             IConverter converter)
Adds the specified converter identified by the specified name.

Parameters:
name - the name of the converter type
converter - the IConverter

getConverterType

public IConverter getConverterType(java.lang.String name)
Returns the IConverter with the specified name.

Parameters:
name - the name of the converter type
Returns:
IConverter the converter

addJavaConverter

public void addJavaConverter(java.lang.Class<?> javaType,
                             java.lang.String type)
Adds the specified converter type supporting conversion from/to the specified javaType.

Parameters:
javaType - the Class with the java type
type - the name of the converter type

addBrahmsConverter

public void addBrahmsConverter(IType brahmsType,
                               java.lang.String type)
Adds the specified converter supporting conversion from/to the specified brahmsType.

Parameters:
brahmsType - the IType with the brahms type
type - the name of the converter type

getJavaConverter

public IConverter getJavaConverter(java.lang.Class<?> javaType)
Returns the converter supporting conversion from/to the specified javaType.

Parameters:
javaType - the Class for which the converter is to be returned
Returns:
IConverter the converter, or null if no converter found

getBrahmsConverter

public IConverter getBrahmsConverter(IType brahmsType)
Returns the converter supporting conversion from/to the specified brahmsType.

Parameters:
brahmsType - the IType for which the converter is to be returned
Returns:
IConverter the converter, or null if no converter found

getBrahmsClassDescriptor

public BrahmsClassDescriptor getBrahmsClassDescriptor(java.lang.String qualifiedName)
Returns the BrahmsClassDescriptor for the Brahms class with the specified qualified name.

Parameters:
qualifiedName - the class' qualified name
Returns:
BrahmsClassDescriptor the descriptor

getBrahmsGroupDescriptor

public BrahmsGroupDescriptor getBrahmsGroupDescriptor(java.lang.String qualifiedName)
Returns the BrahmsGroupDescriptor for the Brahms group with the specified qualified name.

Parameters:
qualifiedName - the class' qualified name
Returns:
BrahmsGroupDescriptor the descriptor

isPrimitive

public boolean isPrimitive(java.lang.Class<?> c)
Indicates whether the type identified by the specified class is a primitive type.

Parameters:
c - the Class to test
Returns:
boolean true if primitive, false otherwise

toJava

public java.lang.Object toJava(IObject obj,
                               ConversionContext ctx)
                        throws ExternalException
Converts the specified Brahms object into its matching Java object representation.

Parameters:
obj - the Brahms IObject to convert
ctx - the IContext used to interact with the Brahms object
Returns:
Object the Java object representation of the Brahms object
Throws:
ExternalException - if there was a problem converting the object

toBrahms

public IObject toBrahms(java.lang.Object obj,
                        ConversionContext ctx)
                 throws ExternalException
Converts the specified Java object into its matching Brahms object representation.

Parameters:
obj - the java Object to convert
ctx - the IContext used to interact with the Brahms object
Returns:
IObject the Brahms object representation of the java object
Throws:
ExternalException - if there was a problem converting the object


Copyright © 1997-2012 All Rights Reserved.