From 7ca4e47c60ae02ce2373449b11627f60b6fdbdbe Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Sat, 10 Nov 2007 13:33:03 +0000 Subject: [PATCH] SWS-223 --- oxm-tiger/pom.xml | 11 +- .../oxm/jaxb/Jaxb2Marshaller.java | 174 ++++++++++-------- .../oxm/jaxb/Jaxb1Marshaller.java | 12 +- 3 files changed, 114 insertions(+), 83 deletions(-) diff --git a/oxm-tiger/pom.xml b/oxm-tiger/pom.xml index fcc4e7f8..5de635c3 100644 --- a/oxm-tiger/pom.xml +++ b/oxm-tiger/pom.xml @@ -1,4 +1,5 @@ - + spring-ws org.springframework.ws @@ -26,8 +27,8 @@ - - + + ${project.build.directory}/generated-sources/test/java @@ -99,13 +100,13 @@ com.sun.xml.bind jaxb-impl - 2.1.3 + 2.1.5 test com.sun.xml.bind jaxb-xjc - 2.1.3 + 2.1.5 test diff --git a/oxm-tiger/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java b/oxm-tiger/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java index d825aaf4..8f737853 100644 --- a/oxm-tiger/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java +++ b/oxm-tiger/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java @@ -16,22 +16,34 @@ package org.springframework.oxm.jaxb; -import org.springframework.core.io.Resource; -import org.springframework.oxm.GenericMarshaller; -import org.springframework.oxm.GenericUnmarshaller; -import org.springframework.oxm.XmlMappingException; -import org.springframework.oxm.mime.MimeContainer; -import org.springframework.oxm.mime.MimeMarshaller; -import org.springframework.oxm.mime.MimeUnmarshaller; -import org.springframework.util.*; -import org.springframework.xml.transform.StaxResult; -import org.springframework.xml.transform.StaxSource; -import org.springframework.xml.validation.SchemaLoaderUtils; - +import java.awt.Image; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.lang.reflect.GenericArrayType; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.math.BigDecimal; +import java.math.BigInteger; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.Map; +import java.util.UUID; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.xml.XMLConstants; -import javax.xml.bind.*; +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBElement; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.adapters.XmlAdapter; @@ -43,18 +55,23 @@ import javax.xml.namespace.QName; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.validation.Schema; -import java.awt.*; -import java.io.*; -import java.lang.reflect.GenericArrayType; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.math.BigDecimal; -import java.math.BigInteger; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.URLDecoder; -import java.net.URLEncoder; -import java.util.*; + +import org.springframework.beans.factory.BeanClassLoaderAware; +import org.springframework.core.io.Resource; +import org.springframework.oxm.GenericMarshaller; +import org.springframework.oxm.GenericUnmarshaller; +import org.springframework.oxm.XmlMappingException; +import org.springframework.oxm.mime.MimeContainer; +import org.springframework.oxm.mime.MimeMarshaller; +import org.springframework.oxm.mime.MimeUnmarshaller; +import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; +import org.springframework.util.FileCopyUtils; +import org.springframework.util.ObjectUtils; +import org.springframework.util.StringUtils; +import org.springframework.xml.transform.StaxResult; +import org.springframework.xml.transform.StaxSource; +import org.springframework.xml.validation.SchemaLoaderUtils; /** * Implementation of the Marshaller interface for JAXB 2.0. @@ -77,7 +94,9 @@ import java.util.*; * @since 1.0.0 */ public class Jaxb2Marshaller extends AbstractJaxbMarshaller - implements MimeMarshaller, MimeUnmarshaller, GenericMarshaller, GenericUnmarshaller { + implements MimeMarshaller, MimeUnmarshaller, GenericMarshaller, GenericUnmarshaller, BeanClassLoaderAware { + + private ClassLoader classLoader; private Resource[] schemaResources; @@ -97,6 +116,10 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller private boolean mtomEnabled = false; + public void setBeanClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + } + /** * Sets the XmlAdapters to be registered with the JAXB Marshaller and * Unmarshaller @@ -123,9 +146,7 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller this.jaxbContextProperties = jaxbContextProperties; } - /** - * Sets the Marshaller.Listener to be registered with the JAXB Marshaller. - */ + /** Sets the Marshaller.Listener to be registered with the JAXB Marshaller. */ public void setMarshallerListener(Marshaller.Listener marshallerListener) { this.marshallerListener = marshallerListener; } @@ -148,23 +169,17 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller this.schemaLanguage = schemaLanguage; } - /** - * Sets the schema resource to use for validation. - */ + /** Sets the schema resource to use for validation. */ public void setSchema(Resource schemaResource) { schemaResources = new Resource[]{schemaResource}; } - /** - * Sets the schema resources to use for validation. - */ + /** Sets the schema resources to use for validation. */ public void setSchemas(Resource[] schemaResources) { this.schemaResources = schemaResources; } - /** - * Sets the Unmarshaller.Listener to be registered with the JAXB Unmarshaller. - */ + /** Sets the Unmarshaller.Listener to be registered with the JAXB Unmarshaller. */ public void setUnmarshallerListener(Unmarshaller.Listener unmarshallerListener) { this.unmarshallerListener = unmarshallerListener; } @@ -172,7 +187,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller public boolean supports(Type type) { if (type instanceof Class) { return supportsInternal((Class) type, true); - } else if (type instanceof ParameterizedType) { + } + else if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; if (JAXBElement.class.equals(parameterizedType.getRawType())) { Assert.isTrue(parameterizedType.getActualTypeArguments().length == 1, @@ -187,7 +203,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller else if (typeArgument instanceof GenericArrayType) { GenericArrayType genericArrayType = (GenericArrayType) typeArgument; return genericArrayType.getGenericComponentType().equals(Byte.TYPE); - } else if (!supports(typeArgument)) { + } + else if (!supports(typeArgument)) { return false; } return true; @@ -197,31 +214,18 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller } private boolean isPrimitiveType(Class clazz) { - return (Boolean.class.equals(clazz) || - Byte.class.equals(clazz) || - Short.class.equals(clazz) || - Integer.class.equals(clazz) || - Long.class.equals(clazz) || - Float.class.equals(clazz) || - Double.class.equals(clazz) || - byte[].class.equals(clazz)); + return (Boolean.class.equals(clazz) || Byte.class.equals(clazz) || Short.class.equals(clazz) || + Integer.class.equals(clazz) || Long.class.equals(clazz) || Float.class.equals(clazz) || + Double.class.equals(clazz) || byte[].class.equals(clazz)); } private boolean isStandardType(Class clazz) { - return (String.class.equals(clazz) || - BigInteger.class.equals(clazz) || - BigDecimal.class.equals(clazz) || - Calendar.class.isAssignableFrom(clazz) || - Date.class.isAssignableFrom(clazz) || - QName.class.equals(clazz) || - URI.class.equals(clazz) || - XMLGregorianCalendar.class.isAssignableFrom(clazz) || - Duration.class.isAssignableFrom(clazz) || - Object.class.equals(clazz) || - Image.class.isAssignableFrom(clazz) || - DataHandler.class.equals(clazz) || - Source.class.isAssignableFrom(clazz) || - UUID.class.equals(clazz)); + return (String.class.equals(clazz) || BigInteger.class.equals(clazz) || BigDecimal.class.equals(clazz) || + Calendar.class.isAssignableFrom(clazz) || Date.class.isAssignableFrom(clazz) || + QName.class.equals(clazz) || URI.class.equals(clazz) || + XMLGregorianCalendar.class.isAssignableFrom(clazz) || Duration.class.isAssignableFrom(clazz) || + Object.class.equals(clazz) || Image.class.isAssignableFrom(clazz) || DataHandler.class.equals(clazz) || + Source.class.isAssignableFrom(clazz) || UUID.class.equals(clazz)); } public boolean supports(Class clazz) { @@ -249,7 +253,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller } } return false; - } else if (!ObjectUtils.isEmpty(classesToBeBound)) { + } + else if (!ObjectUtils.isEmpty(classesToBeBound)) { return Arrays.asList(classesToBeBound).contains(clazz); } return false; @@ -276,9 +281,11 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller } if (StringUtils.hasLength(getContextPath())) { return createJaxbContextFromContextPath(); - } else if (!ObjectUtils.isEmpty(classesToBeBound)) { + } + else if (!ObjectUtils.isEmpty(classesToBeBound)) { return createJaxbContextFromClasses(); - } else { + } + else { throw new IllegalArgumentException("setting either contextPath or classesToBeBound is required"); } } @@ -288,10 +295,18 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller logger.info("Creating JAXBContext with context path [" + getContextPath() + "]"); } if (jaxbContextProperties != null) { - return JAXBContext - .newInstance(getContextPath(), ClassUtils.getDefaultClassLoader(), jaxbContextProperties); - } else { - return JAXBContext.newInstance(getContextPath()); + if (classLoader != null) { + return JAXBContext + .newInstance(getContextPath(), classLoader, jaxbContextProperties); + } + else { + return JAXBContext + .newInstance(getContextPath(), ClassUtils.getDefaultClassLoader(), jaxbContextProperties); + } + } + else { + return classLoader != null ? JAXBContext.newInstance(getContextPath(), classLoader) : + JAXBContext.newInstance(getContextPath()); } } @@ -302,7 +317,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller } if (jaxbContextProperties != null) { return JAXBContext.newInstance(classesToBeBound, jaxbContextProperties); - } else { + } + else { return JAXBContext.newInstance(classesToBeBound); } } @@ -355,7 +371,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller } if (result instanceof StaxResult) { marshalStaxResult(marshaller, graph, (StaxResult) result); - } else { + } + else { marshaller.marshal(graph, result); } } @@ -368,9 +385,11 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller throws JAXBException { if (staxResult.getXMLStreamWriter() != null) { jaxbMarshaller.marshal(graph, staxResult.getXMLStreamWriter()); - } else if (staxResult.getXMLEventWriter() != null) { + } + else if (staxResult.getXMLEventWriter() != null) { jaxbMarshaller.marshal(graph, staxResult.getXMLEventWriter()); - } else { + } + else { throw new IllegalArgumentException("StaxResult contains neither XMLStreamWriter nor XMLEventConsumer"); } } @@ -391,7 +410,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller } if (source instanceof StaxSource) { return unmarshalStaxSource(unmarshaller, (StaxSource) source); - } else { + } + else { return unmarshaller.unmarshal(source); } } @@ -403,9 +423,11 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller private Object unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, StaxSource staxSource) throws JAXBException { if (staxSource.getXMLStreamReader() != null) { return jaxbUnmarshaller.unmarshal(staxSource.getXMLStreamReader()); - } else if (staxSource.getXMLEventReader() != null) { + } + else if (staxSource.getXMLEventReader() != null) { return jaxbUnmarshaller.unmarshal(staxSource.getXMLEventReader()); - } else { + } + else { throw new IllegalArgumentException("StaxSource contains neither XMLStreamReader nor XMLEventReader"); } } diff --git a/oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb1Marshaller.java b/oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb1Marshaller.java index f403d0d9..c3c172a8 100644 --- a/oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb1Marshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb1Marshaller.java @@ -22,6 +22,7 @@ import javax.xml.bind.Unmarshaller; import javax.xml.transform.Result; import javax.xml.transform.Source; +import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; @@ -38,10 +39,16 @@ import org.springframework.util.StringUtils; * @see #setValidating(boolean) * @since 1.0.0 */ -public class Jaxb1Marshaller extends AbstractJaxbMarshaller { +public class Jaxb1Marshaller extends AbstractJaxbMarshaller implements BeanClassLoaderAware { private boolean validating = false; + private ClassLoader classLoader; + + public void setBeanClassLoader(ClassLoader classLoader) { + this.classLoader = classLoader; + } + /** Set if the JAXB Unmarshaller should validate the incoming document. Default is false. */ public void setValidating(boolean validating) { this.validating = validating; @@ -77,7 +84,8 @@ public class Jaxb1Marshaller extends AbstractJaxbMarshaller { if (logger.isInfoEnabled()) { logger.info("Creating JAXBContext with context path [" + getContextPath() + "]"); } - return JAXBContext.newInstance(getContextPath()); + return classLoader != null ? JAXBContext.newInstance(getContextPath(), classLoader) : + JAXBContext.newInstance(getContextPath()); } protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {