camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lburgazz...@apache.org
Subject camel git commit: CAMEL-10576: snakeyaml : as the snakeyaml parser is not thread safe, object like Constructor, Resolver etc should be provided by a factory/supplier
Date Tue, 13 Dec 2016 16:23:46 GMT
Repository: camel
Updated Branches:
  refs/heads/master 97c021e47 -> a48ffc0eb


CAMEL-10576: snakeyaml : as the snakeyaml parser is not thread safe, object like Constructor,
Resolver etc should be provided by a factory/supplier


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a48ffc0e
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a48ffc0e
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a48ffc0e

Branch: refs/heads/master
Commit: a48ffc0eb4f8689b789668179211815fba827c23
Parents: 97c021e
Author: lburgazzoli <lburgazzoli@gmail.com>
Authored: Tue Dec 13 17:23:06 2016 +0100
Committer: lburgazzoli <lburgazzoli@gmail.com>
Committed: Tue Dec 13 17:23:06 2016 +0100

----------------------------------------------------------------------
 .../snakeyaml/SnakeYAMLDataFormat.java          | 161 +++++++++++--------
 1 file changed, 91 insertions(+), 70 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a48ffc0e/components/camel-snakeyaml/src/main/java/org/apache/camel/component/snakeyaml/SnakeYAMLDataFormat.java
----------------------------------------------------------------------
diff --git a/components/camel-snakeyaml/src/main/java/org/apache/camel/component/snakeyaml/SnakeYAMLDataFormat.java
b/components/camel-snakeyaml/src/main/java/org/apache/camel/component/snakeyaml/SnakeYAMLDataFormat.java
index e88994b..e012b7a 100644
--- a/components/camel-snakeyaml/src/main/java/org/apache/camel/component/snakeyaml/SnakeYAMLDataFormat.java
+++ b/components/camel-snakeyaml/src/main/java/org/apache/camel/component/snakeyaml/SnakeYAMLDataFormat.java
@@ -29,6 +29,7 @@ import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.function.Function;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
@@ -51,12 +52,12 @@ import org.yaml.snakeyaml.resolver.Resolver;
  * A <a href="http://camel.apache.org/data-format.html">data format</a> ({@link
DataFormat})
  * using <a href="http://www.snakeyaml.org">SnakeYAML</a> to marshal to and from
YAML.
  */
-public class SnakeYAMLDataFormat extends ServiceSupport implements DataFormat, DataFormatName
{
+public final class SnakeYAMLDataFormat extends ServiceSupport implements DataFormat, DataFormatName
{
     private final ThreadLocal<WeakReference<Yaml>> yamlCache;
-    private BaseConstructor constructor;
-    private Representer representer;
-    private DumperOptions dumperOptions;
-    private Resolver resolver;
+    private Function<CamelContext, BaseConstructor> constructor;
+    private Function<CamelContext, Representer> representer;
+    private Function<CamelContext, DumperOptions> dumperOptions;
+    private Function<CamelContext, Resolver> resolver;
     private ClassLoader classLoader;
     private Class<?> unmarshalType;
     private List<TypeDescription> typeDescriptions;
@@ -75,6 +76,10 @@ public class SnakeYAMLDataFormat extends ServiceSupport implements DataFormat,
D
         this.useApplicationContextClassLoader = true;
         this.prettyFlow = false;
         this.allowAnyType = false;
+        this.constructor = this::defaultConstructor;
+        this.representer = this::defaultRepresenter;
+        this.dumperOptions = this::defaultDumperOptions;
+        this.resolver = this::defaultResolver;
 
         if (type != null) {
             this.unmarshalType = type;
@@ -103,6 +108,16 @@ public class SnakeYAMLDataFormat extends ServiceSupport implements DataFormat,
D
         }
     }
 
+    @Override
+    protected void doStart() throws Exception {
+        // noop
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        // noop
+    }
+
     protected Yaml getYaml(CamelContext context) {
         Yaml yaml = null;
         WeakReference<Yaml> ref = yamlCache.get();
@@ -112,111 +127,60 @@ public class SnakeYAMLDataFormat extends ServiceSupport implements
DataFormat, D
         }
 
         if (yaml == null) {
-            BaseConstructor yamlConstructor = this.constructor;
-            Representer yamlRepresenter = this.representer;
-            DumperOptions yamlDumperOptions = this.dumperOptions;
-            Resolver yamlResolver = this.resolver;
-            ClassLoader yamlClassLoader = this.classLoader;
-            Collection<TypeFilter> yamlTypeFilters = this.typeFilters;
-
-            if (yamlClassLoader == null && useApplicationContextClassLoader) {
-                yamlClassLoader = context.getApplicationContextClassLoader();
-            }
-
-            if (yamlConstructor == null) {
-                if (allowAnyType) {
-                    yamlTypeFilters = Collections.singletonList(TypeFilters.allowAll());
-                }
-
-                if (yamlTypeFilters != null) {
-                    yamlConstructor = yamlClassLoader != null
-                        ? typeFilterConstructor(yamlClassLoader, yamlTypeFilters)
-                        : typeFilterConstructor(yamlTypeFilters);
-                } else {
-                    yamlConstructor = new SafeConstructor();
-                }
+            yaml = new Yaml(
+                this.constructor.apply(context),
+                this.representer.apply(context),
+                this.dumperOptions.apply(context),
+                this.resolver.apply(context)
+            );
 
-                if (typeDescriptions != null && yamlConstructor instanceof Constructor)
{
-                    for (TypeDescription typeDescription : typeDescriptions) {
-                        ((Constructor)yamlConstructor).addTypeDescription(typeDescription);
-                    }
-                }
-            }
-            if (yamlRepresenter == null) {
-                yamlRepresenter = new Representer();
-
-                if (classTags != null) {
-                    for (Map.Entry<Class<?>, Tag> entry : classTags.entrySet())
{
-                        yamlRepresenter.addClassTag(entry.getKey(), entry.getValue());
-                    }
-                }
-            }
-            if (yamlDumperOptions == null) {
-                yamlDumperOptions = new DumperOptions();
-                yamlDumperOptions.setPrettyFlow(prettyFlow);
-            }
-            if (yamlResolver == null) {
-                yamlResolver = new Resolver();
-            }
-
-            yaml = new Yaml(yamlConstructor, yamlRepresenter, yamlDumperOptions, yamlResolver);
             yamlCache.set(new WeakReference<>(yaml));
         }
 
         return yaml;
     }
 
-    @Override
-    protected void doStart() throws Exception {
-        // noop
-    }
-
-    @Override
-    protected void doStop() throws Exception {
-        // noop
-    }
-
-    public BaseConstructor getConstructor() {
+    public Function<CamelContext, BaseConstructor> getConstructor() {
         return constructor;
     }
 
     /**
      * BaseConstructor to construct incoming documents.
      */
-    public void setConstructor(BaseConstructor constructor) {
+    public void setConstructor(Function<CamelContext, BaseConstructor> constructor)
{
         this.constructor = constructor;
     }
 
-    public Representer getRepresenter() {
+    public Function<CamelContext, Representer> getRepresenter() {
         return representer;
     }
 
     /**
      * Representer to emit outgoing objects.
      */
-    public void setRepresenter(Representer representer) {
+    public void setRepresenter(Function<CamelContext, Representer> representer) {
         this.representer = representer;
     }
 
-    public DumperOptions getDumperOptions() {
+    public Function<CamelContext, DumperOptions> getDumperOptions() {
         return dumperOptions;
     }
 
     /**
      * DumperOptions to configure outgoing objects.
      */
-    public void setDumperOptions(DumperOptions dumperOptions) {
+    public void setDumperOptions(Function<CamelContext, DumperOptions> dumperOptions)
{
         this.dumperOptions = dumperOptions;
     }
 
-    public Resolver getResolver() {
+    public Function<CamelContext, Resolver> getResolver() {
         return resolver;
     }
 
     /**
      * Resolver to detect implicit type
      */
-    public void setResolver(Resolver resolver) {
+    public void setResolver(Function<CamelContext, Resolver> resolver) {
         this.resolver = resolver;
     }
 
@@ -370,6 +334,63 @@ public class SnakeYAMLDataFormat extends ServiceSupport implements DataFormat,
D
     }
 
     // ***************************
+    // Defaults
+    // ***************************
+
+    private BaseConstructor defaultConstructor(CamelContext context) {
+        ClassLoader yamlClassLoader = this.classLoader;
+        Collection<TypeFilter> yamlTypeFilters = this.typeFilters;
+
+        if (yamlClassLoader == null && useApplicationContextClassLoader) {
+            yamlClassLoader = context.getApplicationContextClassLoader();
+        }
+
+        if (allowAnyType) {
+            yamlTypeFilters = Collections.singletonList(TypeFilters.allowAll());
+        }
+
+        BaseConstructor yamlConstructor;
+        if (yamlTypeFilters != null) {
+            yamlConstructor = yamlClassLoader != null
+                ? typeFilterConstructor(yamlClassLoader, yamlTypeFilters)
+                : typeFilterConstructor(yamlTypeFilters);
+        } else {
+            yamlConstructor = new SafeConstructor();
+        }
+
+        if (typeDescriptions != null && yamlConstructor instanceof Constructor) {
+            for (TypeDescription typeDescription : typeDescriptions) {
+                ((Constructor)yamlConstructor).addTypeDescription(typeDescription);
+            }
+        }
+
+        return yamlConstructor;
+    }
+
+    private Representer defaultRepresenter(CamelContext context) {
+        Representer yamlRepresenter = new Representer();
+
+        if (classTags != null) {
+            for (Map.Entry<Class<?>, Tag> entry : classTags.entrySet()) {
+                yamlRepresenter.addClassTag(entry.getKey(), entry.getValue());
+            }
+        }
+
+        return yamlRepresenter;
+    }
+
+    private DumperOptions defaultDumperOptions(CamelContext context) {
+        DumperOptions yamlDumperOptions = new DumperOptions();
+        yamlDumperOptions.setPrettyFlow(prettyFlow);
+
+        return yamlDumperOptions;
+    }
+
+    private Resolver defaultResolver(CamelContext context) {
+        return new Resolver();
+    }
+
+    // ***************************
     // Constructors
     // ***************************
 


Mime
View raw message