com.endeca.portal.util
Class JAXBHelper

java.lang.Object
  extended by com.endeca.portal.util.JAXBHelper

public class JAXBHelper
extends java.lang.Object

Manages caching and retrieval of JAXBContext instances for specified classes. JAXBContext instances should be reused. This class keeps an in-memory cache of Class:JAXBContext instances. Also contains useful utilities for actually marshalling JAXB objects to XML strings. *

Author:
Endeca Technologies, Inc.

Constructor Summary
JAXBHelper()
           
 
Method Summary
protected static boolean cacheKeyExists(java.lang.Class<?> klass)
          This method exists to support unit testing.
protected static javax.xml.bind.JAXBContext getCachedValue(java.lang.Class<?> klass)
          This method exists to support unit testing.
protected static int getCacheSize()
          This method exists to support unit testing.
static javax.xml.bind.JAXBContext getContext(java.lang.Class<?> klass)
          Retrieves the JAXBContext instance for the specified class.
protected static javax.xml.bind.JAXBContext removeCachedValue(java.lang.Class<?> klass)
          This method exists to support unit testing.
static
<T> java.lang.String
toXML(java.lang.Class<T> klass, T objWithXmlRoot)
          Utility function for using the cached JAXB context to write out the object's XML representation.
static
<T> java.lang.String
toXML(javax.xml.bind.JAXBElement<T> element)
          Utility function for using the cached JAXB context to write out the object's XML representation.
static
<T> java.lang.String
TryToXML(java.lang.Class<T> klass, T objWithXmlRoot, java.lang.String error)
          Utility function for using the cached JAXB context to write out the object's XML representation.
static
<T> java.lang.String
TryToXML(javax.xml.bind.JAXBElement<T> element, java.lang.String error)
          Utility function for using the cached JAXB context to write out the object's XML.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JAXBHelper

public JAXBHelper()
Method Detail

getContext

public static javax.xml.bind.JAXBContext getContext(java.lang.Class<?> klass)
                                             throws javax.xml.bind.JAXBException
Retrieves the JAXBContext instance for the specified class. Creates the JAXBContext instance if necessary, otherwise retrieves it from cache.

Parameters:
klass -
Returns:
the JAXBContext for the given class
Throws:
javax.xml.bind.JAXBException

toXML

public static <T> java.lang.String toXML(javax.xml.bind.JAXBElement<T> element)
                              throws javax.xml.bind.JAXBException
Utility function for using the cached JAXB context to write out the object's XML representation. Use your specific ObjectFactory's createElementName to create a JAXBElement to pass in. For example in conversation service: com.endeca.mdex.conversation.ObjectFactory of = new com.endeca.mdex.conversation.ObjectFactory(); of.createRequest(obj); If the object is annotated with XmlRootElement you should use the overload which takes a Class and an Object.

Parameters:
element - The element to marshal
Returns:
a string representing the XML
Throws:
javax.xml.bind.JAXBException

TryToXML

public static <T> java.lang.String TryToXML(javax.xml.bind.JAXBElement<T> element,
                                            java.lang.String error)
Utility function for using the cached JAXB context to write out the object's XML. If any errors occur in marshaling the errorMessage string is returned.

Parameters:
element - The element to marshal
error - An error string to return if unable to marshal
Returns:
a string representing the XML
See Also:
toXML(JAXBElement)

toXML

public static <T> java.lang.String toXML(java.lang.Class<T> klass,
                                         T objWithXmlRoot)
                              throws javax.xml.bind.JAXBException
Utility function for using the cached JAXB context to write out the object's XML representation. The object that should be passed should be have an XmlRootElement annotation on it. If the element does not have an XmlRootElement annotation then you should use the JAXBElement<> overloads of this function. Design note: Pass Class as a separate parameter for cleaner null handling.

Parameters:
klass - the type of the object passed. Separate parameter for cleaner null handling.
objWithXmlRoot - object that is an instance of a class with XmlRootElement annotation
Returns:
a string representing the XML
Throws:
javax.xml.bind.JAXBException

TryToXML

public static <T> java.lang.String TryToXML(java.lang.Class<T> klass,
                                            T objWithXmlRoot,
                                            java.lang.String error)
Utility function for using the cached JAXB context to write out the object's XML representation.

Parameters:
klass - the type of the object passed. Separate parameter for cleaner null handling.
objWithXmlRoot - object that is an instance of a class with XmlRootElement annotation
Returns:
a string representing the XML
See Also:
toXML(Class, Object)

getCacheSize

protected static int getCacheSize()
This method exists to support unit testing. It returns the number of unique contexts saved in the cache. Callers should not need to care about the size of the cache.

Returns:
the current size of the cache

removeCachedValue

protected static javax.xml.bind.JAXBContext removeCachedValue(java.lang.Class<?> klass)
This method exists to support unit testing. It removes an item from the cache. Callers should not manage the cache directly.

Parameters:
klass -
Returns:
the context removed from the cache

cacheKeyExists

protected static boolean cacheKeyExists(java.lang.Class<?> klass)
This method exists to support unit testing. It indicates whether or not a specific context exists in the cache. Callers should not need to know this; they should call getContext(Class) directly, which will always return a context.

Parameters:
klass -
Returns:
true if the given class exists in the cache

getCachedValue

protected static javax.xml.bind.JAXBContext getCachedValue(java.lang.Class<?> klass)
This method exists to support unit testing. It returns a context only if that context already exists in the cache. Callers should not use this method; they should call getContext(Class) directly, which will always return a context.

Parameters:
klass -
Returns:
the cached JAXBContext