summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFradj Zayen2013-09-27 04:43:09 (EDT)
committer Stephan Born2013-10-23 08:32:46 (EDT)
commit6449cc67596e7dfbba9cda37662878f225ac20b3 (patch)
treece743ae8dad5423afd237ddeb29010ff5ea1f0c5
parent0c2692de38dc0ffb4e4c283cb72721e528d03e3d (diff)
downloadorg.eclipse.stardust.engine-6449cc67596e7dfbba9cda37662878f225ac20b3.zip
org.eclipse.stardust.engine-6449cc67596e7dfbba9cda37662878f225ac20b3.tar.gz
org.eclipse.stardust.engine-6449cc67596e7dfbba9cda37662878f225ac20b3.tar.bz2
CRNT-30661:JaxbUtils fails with JaxbException in certain cases due to using String contextPath for JaxbContext
git-svn-id: https://svn.csa.sungard.com/repos/ipp2/product/branches/b_dev_7_2_x/stardust/engine@67626 8100b5e0-4d52-466c-ae9c-bdeccbdeaf6b Signed-off-by: Fradj Zayen <fradj.zayen@sungard.com>
-rw-r--r--stardust-engine-camel/src/main/java/org/eclipse/stardust/engine/extensions/camel/util/JaxbUtils.java468
1 files changed, 249 insertions, 219 deletions
diff --git a/stardust-engine-camel/src/main/java/org/eclipse/stardust/engine/extensions/camel/util/JaxbUtils.java b/stardust-engine-camel/src/main/java/org/eclipse/stardust/engine/extensions/camel/util/JaxbUtils.java
index 6534e66..8bd29da 100644
--- a/stardust-engine-camel/src/main/java/org/eclipse/stardust/engine/extensions/camel/util/JaxbUtils.java
+++ b/stardust-engine-camel/src/main/java/org/eclipse/stardust/engine/extensions/camel/util/JaxbUtils.java
@@ -1,4 +1,3 @@
-
package org.eclipse.stardust.engine.extensions.camel.util;
import java.io.ByteArrayInputStream;
@@ -25,229 +24,260 @@ import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
-
/**
- * Utility class for marshalling and unmarshalling between XML data and JAXB ojbects.
- * To be used in particular for working with Structured Data.
+ * Utility class for marshalling and unmarshalling between XML data and JAXB ojbects. To
+ * be used in particular for working with Structured Data.
*
* @author JanHendrik.Scheufen
*/
-public final class JaxbUtils {
-
- private static final Logger LOG = LoggerFactory.getLogger(JaxbUtils.class);
-
- private static Map<String,JAXBContext> _jaxbContextCache = new HashMap<String,JAXBContext>();
-
- private JaxbUtils() {}
-
- /**
- * Unmarshals the specified Element into a bean of the specified returnType.
- *
- * @param returnType class of the expected bean
- * @param element XML to umarshal
- *
- * @return <T> instance of the bean
- * @throws JAXBException if there's a problem during unmarshalling
- */
- public static <T> T unmarshal(Class<T> returnType, Element element) throws JAXBException
- {
- if (returnType == null) {
- throw new IllegalArgumentException("Expected Returntype null");
- }
- if (element == null) {
- throw new IllegalArgumentException("Passed Element null");
- }
- JAXBContext context = getJaxbContext(returnType.getPackage().getName());
- Unmarshaller unmarshaller = context.createUnmarshaller();
- JAXBElement<T> userElement = unmarshaller.unmarshal(element, returnType);
- Object value = userElement.getValue();
-
- if(value != null) {
- if(!returnType.isAssignableFrom(value.getClass()))
- throw new IllegalArgumentException("Returntype does not match the passed Element");
- }
+public final class JaxbUtils
+{
- return returnType.cast(value);
- }
-
-
- /**
- * Unmarshals the specified XML byte array into a bean of the specified returnType.
- *
- * @param returnType class of the expected bean
- * @param xml XML to unmarshal
- *
- * @return <T> instance of the bean
- * @throws JAXBException if there's a problem during unmarshalling
- */
- public static <T> T unmarshal(Class<T> returnType, byte[] xml) throws JAXBException
- {
- return unmarshal(returnType, new ByteArrayInputStream(xml));
- }
-
- /**
- *
- * @param returnType class of the expected bean
- * @param xml XML to unmarshal
- * @return instance of the bean
- * @throws JAXBException if there's a problem during unmarshalling
- */
- public static <T> T unmarshal(Class<T> returnType, InputStream xml)
- throws JAXBException {
- if (returnType == null) {
- throw new IllegalArgumentException("Expected Returntype null");
- }
- if (xml == null) {
- throw new IllegalArgumentException("Passed Byte array null");
- }
- JAXBContext context = getJaxbContext(returnType.getPackage().getName());
- Unmarshaller unmarshaller = context.createUnmarshaller();
- JAXBElement<T> jaxbElement;
- try {
- jaxbElement = unmarshaller.unmarshal(
- XMLInputFactory.newInstance().createXMLStreamReader(xml), returnType);
- }
- catch (XMLStreamException e) {
- throw new RuntimeException("Unable to create XMLStreamReader", e);
- }
- catch (FactoryConfigurationError e) {
- throw new RuntimeException("Unable to create XMLInputFactory", e);
- }
- Object value = jaxbElement.getValue();
-
- if(value != null) {
- if(!returnType.isAssignableFrom(value.getClass()))
- throw new IllegalArgumentException("Returntype does not match the passed Element");
- }
+ private static final Logger LOG = LoggerFactory.getLogger(JaxbUtils.class);
+
+ // JAXBContext is thread-safe, so they can be cached for re-use
+ private static Map<String, JAXBContext> _jaxbContextCache = new HashMap<String, JAXBContext>();
+
+ private JaxbUtils()
+ {}
+
+ /**
+ * Unmarshals the specified Element into a bean of the specified returnType.
+ *
+ * @param returnType
+ * class of the expected bean
+ * @param element
+ * XML to umarshal
+ *
+ * @return <T> instance of the bean
+ * @throws JAXBException
+ * if there's a problem during unmarshalling
+ */
+ public static <T> T unmarshal(Class<T> returnType, Element element) throws JAXBException
+ {
+ if (returnType == null)
+ {
+ throw new IllegalArgumentException("Expected Returntype null");
+ }
+ if (element == null)
+ {
+ throw new IllegalArgumentException("Passed Element null");
+ }
+ JAXBContext context = getJaxbContext(returnType);
+ Unmarshaller unmarshaller = context.createUnmarshaller();
+ JAXBElement<T> userElement = unmarshaller.unmarshal(element, returnType);
+ Object value = userElement.getValue();
+
+ if (value != null)
+ {
+ if (!returnType.isAssignableFrom(value.getClass()))
+ throw new IllegalArgumentException("Returntype does not match the passed Element");
+ }
+
+ return returnType.cast(value);
+ }
+
+ /**
+ * Unmarshals the specified XML byte array into a bean of the specified returnType.
+ *
+ * @param returnType
+ * class of the expected bean
+ * @param xml
+ * XML to unmarshal
+ *
+ * @return <T> instance of the bean
+ * @throws JAXBException
+ * if there's a problem during unmarshalling
+ */
+ public static <T> T unmarshal(Class<T> returnType, byte[] xml) throws JAXBException
+ {
+ return unmarshal(returnType, new ByteArrayInputStream(xml));
+ }
+
+ /**
+ *
+ * @param returnType
+ * class of the expected bean
+ * @param xml
+ * XML to unmarshal
+ * @return instance of the bean
+ * @throws JAXBException
+ * if there's a problem during unmarshalling
+ */
+ public static <T> T unmarshal(Class<T> returnType, InputStream xml) throws JAXBException
+ {
+ if (returnType == null)
+ {
+ throw new IllegalArgumentException("Expected Returntype null");
+ }
+ if (xml == null)
+ {
+ throw new IllegalArgumentException("Passed Byte array null");
+ }
+ JAXBContext context = getJaxbContext(returnType);
+ Unmarshaller unmarshaller = context.createUnmarshaller();
+ JAXBElement<T> jaxbElement;
+ try
+ {
+ jaxbElement = unmarshaller.unmarshal(XMLInputFactory.newInstance().createXMLStreamReader(xml), returnType);
+ }
+ catch (XMLStreamException e)
+ {
+ throw new RuntimeException("Unable to create XMLStreamReader", e);
+ }
+ catch (FactoryConfigurationError e)
+ {
+ throw new RuntimeException("Unable to create XMLInputFactory", e);
+ }
+ Object value = jaxbElement.getValue();
+
+ if (value != null)
+ {
+ if (!returnType.isAssignableFrom(value.getClass()))
+ throw new IllegalArgumentException("Returntype does not match the passed Element");
+ }
- return returnType.cast(value);
- }
-
-
- /**
- * Marshals the specified Object into its XML representation.
- * NOTE: This method uses the simple class name of the specified object to create a QName.
- * If the QName of the object is known, you should use {@link #marshalElement(Class, Object, QName)}
- *
- * @param declareType
- * @param value the object to marshal
- * @return Element the XML as an Element
- * @throws JAXBException if there's a problem during marshalling
- */
- public static <T> Element marshalElement(Class<T> declaredType, Object value) throws JAXBException
- {
- return marshalElement(declaredType, value, new QName(value.getClass().getSimpleName()));
- }
-
- /**
- *
- * @param declaredType
- * @param value
- * @param qname
- * @return
- * @throws JAXBException
- */
- public static <T> Element marshalElement(Class<T> declaredType, Object value, QName qname) throws JAXBException
- {
- try {
- if (value == null) {
- throw new IllegalArgumentException("Passed object is null");
- }
-
- JAXBContext context = getJaxbContext(value.getClass().getPackage().getName());
- Marshaller mashaller = context.createMarshaller();
-
- DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
- dbf.setNamespaceAware(true);
-
- DocumentBuilder db = dbf.newDocumentBuilder();
- Document doc = db.newDocument();
- JAXBElement<T> jaxbElement = new JAXBElement<T>(qname, declaredType, declaredType.cast(value));
- mashaller.marshal(jaxbElement, doc);
-
- return (Element) doc.getFirstChild();
- }
- catch (ParserConfigurationException e) {
- LOG.error(e.getMessage(), e);
- throw new JAXBException(e.getMessage(), e);
- }
- }
-
- /**
- * Marshals the specified Object into its XML representation.
- * NOTE: This method uses the simple class name of the specified object to create a QName.
- * If the QName of the object is known, you should use {@link #marshalByteArray(Class, Object, QName)}
- *
- * @param declareType
- * @param value the object to marshal
- * @return the XML as a byte array
- * @throws JAXBException if there's a problem during marshalling
- */
- public static <T> byte[] marshalByteArray(Class<T> declaredType, Object value) throws JAXBException
- {
- return marshalOutputStream(declaredType, value).toByteArray();
- }
-
- /**
- * Marshals the specified Object into its XML representation.
- *
- * @param declaredType
- * @param value
- * @param qname
- * @return the XML as a byte array
- * @throws JAXBException
- */
- public static <T> byte[] marshalByteArray(Class<T> declaredType, Object value, QName qname) throws JAXBException
- {
- return marshalOutputStream(declaredType, value, qname).toByteArray();
- }
-
- /**
- * Marshals the specified Object into its XML representation
- * NOTE: This method uses the simple class name of the specified object to create a QName.
- * If the QName of the object is known, you should use {@link #marshalByteArray(Class, Object, QName)}
- *
- * @param declaredType
- * @param value the object to marshal
- * @return the XML as an output stream
- * @throws JAXBException if there's a problem during marshalling
- */
- public static <T> ByteArrayOutputStream marshalOutputStream(Class<T> declaredType, Object value) throws JAXBException
- {
- return marshalOutputStream(declaredType, value, new QName(value.getClass().getSimpleName()));
- }
-
- /**
- * Marshals the specified object into its XML representation.
- *
- * @param declaredType
- * @param dataBean
- * @param qname
- * @return the XML as an output stream
- * @throws JAXBException
- */
- public static <T> ByteArrayOutputStream marshalOutputStream(Class<T> declaredType, Object dataBean, QName qname) throws JAXBException
- {
- if (dataBean == null) {
+ return returnType.cast(value);
+ }
+
+ /**
+ * Marshals the specified Object into its XML representation. NOTE: This method uses
+ * the simple class name of the specified object to create a QName. If the QName of the
+ * object is known, you should use {@link #marshalElement(Class, Object, QName)}
+ *
+ * @param declareType
+ * @param value
+ * the object to marshal
+ * @return Element the XML as an Element
+ * @throws JAXBException
+ * if there's a problem during marshalling
+ */
+ public static <T> Element marshalElement(Class<T> declaredType, Object value) throws JAXBException
+ {
+ return marshalElement(declaredType, value, new QName(value.getClass().getSimpleName()));
+ }
+
+ /**
+ *
+ * @param declaredType
+ * @param value
+ * @param qname
+ * @return
+ * @throws JAXBException
+ */
+ public static <T> Element marshalElement(Class<T> declaredType, Object value, QName qname) throws JAXBException
+ {
+ try
+ {
+ if (value == null)
+ {
throw new IllegalArgumentException("Passed object is null");
- }
-
- JAXBContext context = getJaxbContext(dataBean.getClass().getPackage().getName());
- Marshaller marshaller = context.createMarshaller();
-
- ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- JAXBElement<T> jaxbElement = new JAXBElement<T>(qname, declaredType, declaredType.cast(dataBean));
- marshaller.marshal(jaxbElement, outputStream);
-
- return outputStream;
- }
-
- private static JAXBContext getJaxbContext(String contextPath) throws JAXBException {
- JAXBContext context = _jaxbContextCache.get(contextPath);
- if( null == context ) {
- context = JAXBContext.newInstance(contextPath);
- _jaxbContextCache.put(contextPath, context);
- }
- return context;
- }
+ }
+
+ JAXBContext context = getJaxbContext(value.getClass());
+ Marshaller mashaller = context.createMarshaller();
+
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ dbf.setNamespaceAware(true);
+
+ DocumentBuilder db = dbf.newDocumentBuilder();
+ Document doc = db.newDocument();
+ JAXBElement<T> jaxbElement = new JAXBElement<T>(qname, declaredType, declaredType.cast(value));
+ mashaller.marshal(jaxbElement, doc);
+
+ return (Element) doc.getFirstChild();
+ }
+ catch (ParserConfigurationException e)
+ {
+ LOG.error(e.getMessage(), e);
+ throw new JAXBException(e.getMessage(), e);
+ }
+ }
+
+ /**
+ * Marshals the specified Object into its XML representation. NOTE: This method uses
+ * the simple class name of the specified object to create a QName. If the QName of the
+ * object is known, you should use {@link #marshalByteArray(Class, Object, QName)}
+ *
+ * @param declareType
+ * @param value
+ * the object to marshal
+ * @return the XML as a byte array
+ * @throws JAXBException
+ * if there's a problem during marshalling
+ */
+ public static <T> byte[] marshalByteArray(Class<T> declaredType, Object value) throws JAXBException
+ {
+ return marshalOutputStream(declaredType, value).toByteArray();
+ }
+
+ /**
+ * Marshals the specified Object into its XML representation.
+ *
+ * @param declaredType
+ * @param value
+ * @param qname
+ * @return the XML as a byte array
+ * @throws JAXBException
+ */
+ public static <T> byte[] marshalByteArray(Class<T> declaredType, Object value, QName qname) throws JAXBException
+ {
+ return marshalOutputStream(declaredType, value, qname).toByteArray();
+ }
+
+ /**
+ * Marshals the specified Object into its XML representation NOTE: This method uses the
+ * simple class name of the specified object to create a QName. If the QName of the
+ * object is known, you should use {@link #marshalByteArray(Class, Object, QName)}
+ *
+ * @param declaredType
+ * @param value
+ * the object to marshal
+ * @return the XML as an output stream
+ * @throws JAXBException
+ * if there's a problem during marshalling
+ */
+ public static <T> ByteArrayOutputStream marshalOutputStream(Class<T> declaredType, Object value)
+ throws JAXBException
+ {
+ return marshalOutputStream(declaredType, value, new QName(value.getClass().getSimpleName()));
+ }
+
+ /**
+ * Marshals the specified object into its XML representation.
+ *
+ * @param declaredType
+ * @param dataBean
+ * @param qname
+ * @return the XML as an output stream
+ * @throws JAXBException
+ */
+ public static <T> ByteArrayOutputStream marshalOutputStream(Class<T> declaredType, Object dataBean, QName qname)
+ throws JAXBException
+ {
+ if (dataBean == null)
+ {
+ throw new IllegalArgumentException("Passed object is null");
+ }
+
+ JAXBContext context = getJaxbContext(dataBean.getClass());
+ Marshaller marshaller = context.createMarshaller();
+
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ JAXBElement<T> jaxbElement = new JAXBElement<T>(qname, declaredType, declaredType.cast(dataBean));
+ marshaller.marshal(jaxbElement, outputStream);
+
+ return outputStream;
+ }
+
+ private static <T> JAXBContext getJaxbContext(Class<T> type) throws JAXBException
+ {
+ JAXBContext context = _jaxbContextCache.get(type.getName());
+ if (null == context)
+ {
+ context = JAXBContext.newInstance(type);
+ _jaxbContextCache.put(type.getName(), context);
+ }
+ return context;
+ }
} \ No newline at end of file