Repository: camel Updated Branches: refs/heads/master 87affd2b7 -> dcf340002
CAMEL-9649: Optimized the logic a bit Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a3d22b3e Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a3d22b3e Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a3d22b3e Branch: refs/heads/master Commit: a3d22b3e8922e7ae8fd4a0d835468f72bdf1ef56 Parents: 87affd2 Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Mar 8 08:25:03 2016 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Mar 8 08:25:03 2016 +0100 ---------------------------------------------------------------------- .../converter/jaxb/FallbackTypeConverter.java | 54 ++++++++++++-------- .../camel/converter/jaxb/JaxbDataFormat.java | 40 ++++++++------- .../apache/camel/converter/jaxb/JaxbHelper.java | 35 +++++-------- 3 files changed, 67 insertions(+), 62 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a3d22b3e/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java ---------------------------------------------------------------------- diff --git a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java index 3155f1e..92571c5 100644 --- a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java +++ b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/FallbackTypeConverter.java @@ -61,7 +61,12 @@ import org.slf4j.LoggerFactory; * @version */ public class FallbackTypeConverter extends ServiceSupport implements TypeConverter, TypeConverterAware, CamelContextAware { - public static final String PRETTY_PRINT = "CamelJaxbPrettyPrint"; + + /** + * Whether the JAXB converter should use pretty print or not (default is true) + */ + public static final String PRETTY_PRINT = "CamelJaxbPrettyPrint"; + private static final Logger LOG = LoggerFactory.getLogger(FallbackTypeConverter.class); private final Map<AnnotatedElement, JAXBContext> contexts = new HashMap<>(); private final StaxConverter staxConverter = new StaxConverter(); @@ -91,6 +96,16 @@ public class FallbackTypeConverter extends ServiceSupport implements TypeConvert public void setCamelContext(CamelContext camelContext) { this.camelContext = camelContext; + + // configure pretty print + String property = camelContext.getProperty(PRETTY_PRINT); + if (property != null) { + if (property.equalsIgnoreCase("false")) { + setPrettyPrint(false); + } else { + setPrettyPrint(true); + } + } } public <T> T convertTo(Class<T> type, Object value) { @@ -108,9 +123,13 @@ public class FallbackTypeConverter extends ServiceSupport implements TypeConvert if (isJaxbType(type)) { return unmarshall(type, exchange, value); } - if (value != null) { - if (isJaxbType(value.getClass()) && isNotStreamCacheType(type)) { - return marshall(type, exchange, value); + if (value != null && isNotStreamCacheType(type)) { + if (hasXmlRootElement(value.getClass())) { + return marshall(type, exchange, value, null); + } + Method objectFactoryMethod = JaxbHelper.getJaxbElementFactoryMethod(camelContext, value.getClass()); + if (objectFactoryMethod != null) { + return marshall(type, exchange, value, objectFactoryMethod); } } } catch (Exception e) { @@ -229,7 +248,7 @@ public class FallbackTypeConverter extends ServiceSupport implements TypeConvert return null; } - protected <T> T marshall(Class<T> type, Exchange exchange, Object value) + protected <T> T marshall(Class<T> type, Exchange exchange, Object value, Method objectFactoryMethod) throws JAXBException, XMLStreamException, FactoryConfigurationError, TypeConversionException { LOG.trace("Marshal from value {} to type {}", value, type); @@ -241,31 +260,22 @@ public class FallbackTypeConverter extends ServiceSupport implements TypeConvert // must create a new instance of marshaller as its not thread safe Marshaller marshaller = context.createMarshaller(); Writer buffer = new StringWriter(); - boolean prettyPrint = isPrettyPrint(); - // check the camel context property to decide the value of PrettyPrint - if (exchange != null) { - String property = exchange.getContext().getProperty(PRETTY_PRINT); - if (property != null) { - if (property.equalsIgnoreCase("false")) { - prettyPrint = false; - } else { - prettyPrint = true; - } - } - } - if (prettyPrint) { + + if (isPrettyPrint()) { marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); } if (exchange != null && exchange.getProperty(Exchange.CHARSET_NAME, String.class) != null) { marshaller.setProperty(Marshaller.JAXB_ENCODING, exchange.getProperty(Exchange.CHARSET_NAME, String.class)); } Object toMarshall = value; - if (!hasXmlRootElement(value.getClass())) { - Method m = JaxbHelper.getJaxbElementFactoryMethod(camelContext, value.getClass()); + if (objectFactoryMethod != null) { try { - toMarshall = m.invoke(JaxbHelper.getObjectFactory(camelContext, value.getClass()).newInstance(), value); + Object instance = objectFactoryMethod.getDeclaringClass().newInstance(); + if (instance != null) { + toMarshall = objectFactoryMethod.invoke(instance, value); + } } catch (Exception e) { - LOG.error("Unable to create JAXBElement object for type {} due to {}", value.getClass().getName(), e); + LOG.debug("Unable to create JAXBElement object for type " + value.getClass() + " due to " + e.getMessage(), e); } } if (needFiltering(exchange)) { http://git-wip-us.apache.org/repos/asf/camel/blob/a3d22b3e/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java ---------------------------------------------------------------------- diff --git a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java index 990fdf5..911d010 100644 --- a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java +++ b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbDataFormat.java @@ -46,8 +46,6 @@ import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; -import org.xml.sax.SAXException; - import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; import org.apache.camel.Exchange; @@ -63,6 +61,7 @@ import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.ResourceHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.xml.sax.SAXException; /** * A <a href="http://camel.apache.org/data-format.html">data format</a> ({@link DataFormat}) @@ -177,24 +176,29 @@ public class JaxbDataFormat extends ServiceSupport implements DataFormat, DataFo } return; } else if (element != null) { - Method m = JaxbHelper.getJaxbElementFactoryMethod(camelContext, element.getClass()); - try { - Object toMarshall = m.invoke(JaxbHelper.getObjectFactory(camelContext, element.getClass()).newInstance(), element); - if (asXmlStreamWriter(exchange)) { - XMLStreamWriter writer = typeConverter.convertTo(XMLStreamWriter.class, stream); - if (needFiltering(exchange)) { - writer = new FilteringXmlStreamWriter(writer); - } - if (xmlStreamWriterWrapper != null) { - writer = xmlStreamWriterWrapper.wrapWriter(writer); + Method objectFactoryMethod = JaxbHelper.getJaxbElementFactoryMethod(camelContext, element.getClass()); + if (objectFactoryMethod != null) { + try { + Object instance = objectFactoryMethod.getDeclaringClass().newInstance(); + if (instance != null) { + Object toMarshall = objectFactoryMethod.invoke(instance, element); + if (asXmlStreamWriter(exchange)) { + XMLStreamWriter writer = typeConverter.convertTo(XMLStreamWriter.class, stream); + if (needFiltering(exchange)) { + writer = new FilteringXmlStreamWriter(writer); + } + if (xmlStreamWriterWrapper != null) { + writer = xmlStreamWriterWrapper.wrapWriter(writer); + } + marshaller.marshal(toMarshall, writer); + } else { + marshaller.marshal(toMarshall, stream); + } + return; } - marshaller.marshal(toMarshall, writer); - } else { - marshaller.marshal(toMarshall, stream); + } catch (Exception e) { + LOG.debug("Unable to create JAXBElement object for type " + element.getClass() + " due to " + e.getMessage(), e); } - return; - } catch (Exception e) { - LOG.debug("Unable to create JAXBElement object for type {} due to {}", element.getClass().getName(), e); } } http://git-wip-us.apache.org/repos/asf/camel/blob/a3d22b3e/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbHelper.java ---------------------------------------------------------------------- diff --git a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbHelper.java b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbHelper.java index 9fe2ad6..8fdbc78 100644 --- a/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbHelper.java +++ b/components/camel-jaxb/src/main/java/org/apache/camel/converter/jaxb/JaxbHelper.java @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * + * <p/> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p/> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -31,39 +31,30 @@ public final class JaxbHelper { } public static <T> Method getJaxbElementFactoryMethod(CamelContext camelContext, Class<T> type) { - Method factoryMethod = null; - try { - for (Method m : getObjectFactory(camelContext, type).getMethods()) { + // find the first method that has @XmlElementDecl with one parameter that matches the type + Class factory = getObjectFactory(camelContext, type); + if (factory != null) { + for (Method m : factory.getMethods()) { final XmlElementDecl a = m.getAnnotation(XmlElementDecl.class); if (a == null) { continue; } final Class<?>[] parameters = m.getParameterTypes(); if (parameters.length == 1 && parameters[0].isAssignableFrom(type)) { - if (factoryMethod != null) { - throw new IllegalStateException("There are several possible XML schema mappings for class " + type.getName()); - } else { - factoryMethod = m; - } + return m; } } - } catch (ClassNotFoundException e) { - LOG.debug(e.getMessage(), e); } - return factoryMethod; + + return null; } - public static <T> Class getObjectFactory(CamelContext camelContext, Class<T> type) throws ClassNotFoundException { - Class<?> c = null; + public static <T> Class getObjectFactory(CamelContext camelContext, Class<T> type) { if (type.getPackage() != null) { String objectFactoryClassName = type.getPackage().getName() + ".ObjectFactory"; - c = camelContext.getClassResolver().resolveClass(objectFactoryClassName); - } - if (c == null) { - throw new ClassNotFoundException(String.format("ObjectFactory for type %s was not found", type.getName())); - } else { - return c; + return camelContext.getClassResolver().resolveClass(objectFactoryClassName); } + return null; } }