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/dcf34000
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/dcf34000
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/dcf34000

Branch: refs/heads/master
Commit: dcf3400023e0819c8ee2ff484c399638910930d1
Parents: cebeb22
Author: Claus Ibsen <davscl...@apache.org>
Authored: Tue Mar 8 08:56:13 2016 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Tue Mar 8 09:07:56 2016 +0100

----------------------------------------------------------------------
 .../converter/jaxb/FallbackTypeConverter.java   | 36 +++++++++++++++++---
 .../apache/camel/converter/jaxb/JaxbHelper.java | 12 ++++---
 2 files changed, 39 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/dcf34000/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 92571c5..16220a2 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
@@ -62,26 +62,41 @@ import org.slf4j.LoggerFactory;
  */
 public class FallbackTypeConverter extends ServiceSupport implements 
TypeConverter, TypeConverterAware, CamelContextAware {
 
-    /**
-     * Whether the JAXB converter should use pretty print or not (default is 
true)
-     */
     public static final String PRETTY_PRINT = "CamelJaxbPrettyPrint";
+    public static final String OBJECT_FACTORY = "CamelJaxbObjectFactory";
 
     private static final Logger LOG = 
LoggerFactory.getLogger(FallbackTypeConverter.class);
     private final Map<AnnotatedElement, JAXBContext> contexts = new 
HashMap<>();
     private final StaxConverter staxConverter = new StaxConverter();
     private TypeConverter parentTypeConverter;
     private boolean prettyPrint = true;
+    private boolean objectFactory = true;
     private CamelContext camelContext;
 
     public boolean isPrettyPrint() {
         return prettyPrint;
     }
 
+    /**
+     * Whether the JAXB converter should use pretty print or not (default is 
true)
+     */
     public void setPrettyPrint(boolean prettyPrint) {
         this.prettyPrint = prettyPrint;
     }
 
+
+    public boolean isObjectFactory() {
+        return objectFactory;
+    }
+
+    /**
+     * Whether the JAXB converter supports using ObjectFactory classes to 
create the POJO classes during conversion.
+     * This only applies to POJO classes that has not been annotated with JAXB 
and providing jaxb.index descriptor files.
+     */
+    public void setObjectFactory(boolean objectFactory) {
+        this.objectFactory = objectFactory;
+    }
+
     public boolean allowNull() {
         return false;
     }
@@ -106,6 +121,16 @@ public class FallbackTypeConverter extends ServiceSupport 
implements TypeConvert
                 setPrettyPrint(true);
             }
         }
+
+        // configure object factory
+        property = camelContext.getProperty(OBJECT_FACTORY);
+        if (property != null) {
+            if (property.equalsIgnoreCase("false")) {
+                setObjectFactory(false);
+            } else {
+                setObjectFactory(true);
+            }
+        }
     }
 
     public <T> T convertTo(Class<T> type, Object value) {
@@ -127,7 +152,8 @@ public class FallbackTypeConverter extends ServiceSupport 
implements TypeConvert
                 if (hasXmlRootElement(value.getClass())) {
                     return marshall(type, exchange, value, null);
                 }
-                Method objectFactoryMethod = 
JaxbHelper.getJaxbElementFactoryMethod(camelContext, value.getClass());
+                CamelContext context = exchange != null ? 
exchange.getContext() : camelContext;
+                Method objectFactoryMethod = 
JaxbHelper.getJaxbElementFactoryMethod(context, value.getClass());
                 if (objectFactoryMethod != null) {
                     return marshall(type, exchange, value, 
objectFactoryMethod);
                 }
@@ -170,7 +196,7 @@ public class FallbackTypeConverter extends ServiceSupport 
implements TypeConvert
 
     @Override
     protected void doStart() throws Exception {
-        // noop
+        LOG.info("Jaxb FallbackTypeConverter[prettyPrint={}, 
objectFactory={}]", prettyPrint, objectFactory);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/dcf34000/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 8fdbc78..137e793 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
@@ -20,17 +20,17 @@ import java.lang.reflect.Method;
 import javax.xml.bind.annotation.XmlElementDecl;
 
 import org.apache.camel.CamelContext;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 public final class JaxbHelper {
 
-    private static final Logger LOG = 
LoggerFactory.getLogger(JaxbHelper.class);
-
     private JaxbHelper() {
     }
 
     public static <T> Method getJaxbElementFactoryMethod(CamelContext 
camelContext, Class<T> type) {
+        if (camelContext == null) {
+            return null;
+        }
+
         // find the first method that has @XmlElementDecl with one parameter 
that matches the type
         Class factory = getObjectFactory(camelContext, type);
         if (factory != null) {
@@ -50,6 +50,10 @@ public final class JaxbHelper {
     }
 
     public static <T> Class getObjectFactory(CamelContext camelContext, 
Class<T> type) {
+        if (camelContext == null) {
+            return null;
+        }
+
         if (type.getPackage() != null) {
             String objectFactoryClassName = type.getPackage().getName() + 
".ObjectFactory";
             return 
camelContext.getClassResolver().resolveClass(objectFactoryClassName);

Reply via email to