camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject [camel] 03/08: Cache the JAXBContext in ModelJAXBContextFactory
Date Thu, 08 Nov 2018 15:00:17 GMT
This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch sandbox/camel-3.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 121fe1dfc88f64dde4c5b7ced88f76e8004db169
Author: Guillaume Nodet <gnodet@gmail.com>
AuthorDate: Wed Nov 7 16:43:14 2018 +0100

    Cache the JAXBContext in ModelJAXBContextFactory
---
 .../camel/impl/DefaultModelJAXBContextFactory.java | 11 ++++++++++-
 .../java/org/apache/camel/model/ModelHelper.java   | 22 ++++++++++++----------
 .../BlueprintModelJAXBContextFactory.java          | 19 +++++++++----------
 .../spring/SpringModelJAXBContextFactory.java      |  4 ----
 .../commands/AbstractLocalCamelController.java     |  4 ++--
 5 files changed, 33 insertions(+), 27 deletions(-)

diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultModelJAXBContextFactory.java
b/camel-core/src/main/java/org/apache/camel/impl/DefaultModelJAXBContextFactory.java
index c830241..348bda2 100644
--- a/camel-core/src/main/java/org/apache/camel/impl/DefaultModelJAXBContextFactory.java
+++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultModelJAXBContextFactory.java
@@ -27,8 +27,17 @@ import org.apache.camel.spi.ModelJAXBContextFactory;
  */
 public class DefaultModelJAXBContextFactory implements ModelJAXBContextFactory {
 
+    private volatile JAXBContext context;
+
     public JAXBContext newJAXBContext() throws JAXBException {
-        return JAXBContext.newInstance(getPackages(), getClassLoader());
+        if (context == null) {
+            synchronized (this) {
+                if (context == null) {
+                    context = JAXBContext.newInstance(getPackages(), getClassLoader());
+                }
+            }
+        }
+        return context;
     }
 
     protected String getPackages() {
diff --git a/camel-core/src/main/java/org/apache/camel/model/ModelHelper.java b/camel-core/src/main/java/org/apache/camel/model/ModelHelper.java
index 476f15f..dbc95f6 100644
--- a/camel-core/src/main/java/org/apache/camel/model/ModelHelper.java
+++ b/camel-core/src/main/java/org/apache/camel/model/ModelHelper.java
@@ -41,7 +41,9 @@ import org.apache.camel.Expression;
 import org.apache.camel.NamedNode;
 import org.apache.camel.TypeConversionException;
 import org.apache.camel.converter.jaxp.XmlConverter;
+import org.apache.camel.impl.DefaultModelJAXBContextFactory;
 import org.apache.camel.model.language.ExpressionDefinition;
+import org.apache.camel.spi.ModelJAXBContextFactory;
 import org.apache.camel.spi.NamespaceAware;
 import org.apache.camel.spi.TypeConverterRegistry;
 import org.apache.camel.util.ObjectHelper;
@@ -237,13 +239,10 @@ public final class ModelHelper {
     }
 
     private static JAXBContext getJAXBContext(CamelContext context) throws JAXBException
{
-        JAXBContext jaxbContext;
-        if (context == null) {
-            jaxbContext = createJAXBContext();
-        } else {
-            jaxbContext = context.getModelJAXBContextFactory().newJAXBContext();
-        }
-        return jaxbContext;
+        ModelJAXBContextFactory factory = context != null
+                ? context.getModelJAXBContextFactory()
+                : getDefaultModelJAXBContextFactory();
+        return factory.newJAXBContext();
     }
 
     private static void applyNamespaces(RouteDefinition route, Map<String, String>
namespaces) {
@@ -270,9 +269,12 @@ public final class ModelHelper {
         return na;
     }
 
-    private static JAXBContext createJAXBContext() throws JAXBException {
-        // must use classloader from CamelContext to have JAXB working
-        return JAXBContext.newInstance(Constants.JAXB_CONTEXT_PACKAGES, CamelContext.class.getClassLoader());
+    private static class ModelJAXBContextFactoryHolder {
+        private static ModelJAXBContextFactory INSTANCE = new DefaultModelJAXBContextFactory();
+    }
+
+    private static ModelJAXBContextFactory getDefaultModelJAXBContextFactory() {
+        return ModelJAXBContextFactoryHolder.INSTANCE;
     }
 
     /**
diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintModelJAXBContextFactory.java
b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintModelJAXBContextFactory.java
index 1ea5153f..0fed087 100644
--- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintModelJAXBContextFactory.java
+++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintModelJAXBContextFactory.java
@@ -18,14 +18,12 @@ package org.apache.camel.blueprint;
 
 import java.util.LinkedHashSet;
 import java.util.Set;
-import javax.xml.bind.JAXBContext;
-import javax.xml.bind.JAXBException;
 
 import org.apache.camel.core.xml.AbstractCamelContextFactoryBean;
-import org.apache.camel.spi.ModelJAXBContextFactory;
+import org.apache.camel.impl.DefaultModelJAXBContextFactory;
 import org.apache.camel.util.blueprint.SSLContextParametersFactoryBean;
 
-public class BlueprintModelJAXBContextFactory implements ModelJAXBContextFactory {
+public class BlueprintModelJAXBContextFactory extends DefaultModelJAXBContextFactory {
 
     private final ClassLoader classLoader;
 
@@ -33,7 +31,12 @@ public class BlueprintModelJAXBContextFactory implements ModelJAXBContextFactory
         this.classLoader = classLoader;
     }
 
-    private String getPackages() {
+    @Override
+    protected ClassLoader getClassLoader() {
+        return classLoader;
+    }
+
+    protected String getPackages() {
         // we nedd to have a class from each different package with jaxb models
         // and we must use the .class for the classloader to work in OSGi
         Set<Class<?>> classes = new LinkedHashSet<>();
@@ -54,13 +57,9 @@ public class BlueprintModelJAXBContextFactory implements ModelJAXBContextFactory
             if (packages.length() > 0) {
                 packages.append(":");
             }
-            packages.append(cl.getName().substring(0, cl.getName().lastIndexOf('.')));
+            packages.append(cl.getName(), 0, cl.getName().lastIndexOf('.'));
         }
         return packages.toString();
     }
 
-    @Override
-    public JAXBContext newJAXBContext() throws JAXBException {
-        return JAXBContext.newInstance(getPackages(), classLoader);
-    }
 }
\ No newline at end of file
diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/SpringModelJAXBContextFactory.java
b/components/camel-spring/src/main/java/org/apache/camel/spring/SpringModelJAXBContextFactory.java
index 37b04ba..2fc3097 100644
--- a/components/camel-spring/src/main/java/org/apache/camel/spring/SpringModelJAXBContextFactory.java
+++ b/components/camel-spring/src/main/java/org/apache/camel/spring/SpringModelJAXBContextFactory.java
@@ -26,10 +26,6 @@ public class SpringModelJAXBContextFactory extends DefaultModelJAXBContextFactor
             + "org.apache.camel.spring:"
             + "org.apache.camel.util.spring:";
 
-    protected ClassLoader getClassLoader() {
-        return getClass().getClassLoader();
-    }
-
     protected String getPackages() {
         return super.getPackages() + ADDITIONAL_JAXB_CONTEXT_PACKAGES;
     }
diff --git a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractLocalCamelController.java
b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractLocalCamelController.java
index 7144478..a07cf12 100644
--- a/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractLocalCamelController.java
+++ b/platforms/commands/commands-core/src/main/java/org/apache/camel/commands/AbstractLocalCamelController.java
@@ -379,7 +379,7 @@ public abstract class AbstractLocalCamelController extends AbstractCamelControll
             return null;
         }
 
-        return ModelHelper.dumpModelAsXml(null, route);
+        return ModelHelper.dumpModelAsXml(context, route);
     }
 
     @Override
@@ -421,7 +421,7 @@ public abstract class AbstractLocalCamelController extends AbstractCamelControll
         // use a rests definition to dump the rests
         RestsDefinition def = new RestsDefinition();
         def.setRests(rests);
-        return ModelHelper.dumpModelAsXml(null, def);
+        return ModelHelper.dumpModelAsXml(context, def);
     }
 
     public String getRestApiDocAsJson(String camelContextName) throws Exception {


Mime
View raw message