camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ningji...@apache.org
Subject [2/3] git commit: CAMEL-7239 Address the SchemaFactory thread safe issue.
Date Mon, 24 Feb 2014 06:25:33 GMT
CAMEL-7239 Address the SchemaFactory thread safe issue.


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

Branch: refs/heads/camel-2.12.x
Commit: 6de2fba644c5dfcc4fdc953e2331cb5ab05ba747
Parents: 87af7cc
Author: Willem Jiang <willem.jiang@gmail.com>
Authored: Mon Feb 24 12:46:32 2014 +0800
Committer: Willem Jiang <willem.jiang@gmail.com>
Committed: Mon Feb 24 14:18:57 2014 +0800

----------------------------------------------------------------------
 .../validation/ValidatingProcessor.java         | 36 +++++++++++++++-----
 .../component/validator/ValidatorRouteTest.java | 30 ++++++++++++++++
 2 files changed, 57 insertions(+), 9 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/6de2fba6/camel-core/src/main/java/org/apache/camel/processor/validation/ValidatingProcessor.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/validation/ValidatingProcessor.java
b/camel-core/src/main/java/org/apache/camel/processor/validation/ValidatingProcessor.java
index 200d0fe..a730915 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/validation/ValidatingProcessor.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/validation/ValidatingProcessor.java
@@ -23,6 +23,7 @@ import java.io.InputStream;
 import java.io.StringWriter;
 import java.net.URL;
 import java.util.Collections;
+
 import javax.xml.XMLConstants;
 import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.transform.Result;
@@ -40,6 +41,7 @@ import javax.xml.validation.Validator;
 
 import org.w3c.dom.Node;
 import org.w3c.dom.ls.LSResourceResolver;
+
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 
@@ -52,10 +54,10 @@ import org.apache.camel.TypeConverter;
 import org.apache.camel.converter.jaxp.XmlConverter;
 import org.apache.camel.util.AsyncProcessorHelper;
 import org.apache.camel.util.IOHelper;
-
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+
 /**
  * A processor which validates the XML version of the inbound message body
  * against some schema either in XSD or RelaxNG
@@ -64,9 +66,9 @@ public class ValidatingProcessor implements AsyncProcessor {
     private static final Logger LOG = LoggerFactory.getLogger(ValidatingProcessor.class);
     private XmlConverter converter = new XmlConverter();
     private String schemaLanguage = XMLConstants.W3C_XML_SCHEMA_NS_URI;
-    private Schema schema;
+    private volatile Schema schema;
     private Source schemaSource;
-    private SchemaFactory schemaFactory;
+    private volatile SchemaFactory schemaFactory;
     private URL schemaUrl;
     private File schemaFile;
     private byte[] schemaAsByteArray;
@@ -192,7 +194,11 @@ public class ValidatingProcessor implements AsyncProcessor {
 
     public Schema getSchema() throws IOException, SAXException {
         if (schema == null) {
-            schema = createSchema();
+            synchronized (this) {
+                if (schema == null) {
+                    schema = createSchema();
+                }
+            }
         }
         return schema;
     }
@@ -246,7 +252,11 @@ public class ValidatingProcessor implements AsyncProcessor {
 
     public SchemaFactory getSchemaFactory() {
         if (schemaFactory == null) {
-            schemaFactory = createSchemaFactory();
+            synchronized (this) {
+                if (schemaFactory == null) {
+                    schemaFactory = createSchemaFactory();
+                }
+            }
         }
         return schemaFactory;
     }
@@ -338,21 +348,29 @@ public class ValidatingProcessor implements AsyncProcessor {
 
         URL url = getSchemaUrl();
         if (url != null) {
-            return factory.newSchema(url);
+            synchronized (this) {
+                return factory.newSchema(url);
+            }
         }
 
         File file = getSchemaFile();
         if (file != null) {
-            return factory.newSchema(file);
+            synchronized (this) {
+                return factory.newSchema(file);
+            }
         }
 
         byte[] bytes = getSchemaAsByteArray();
         if (bytes != null) {
-            return factory.newSchema(new StreamSource(new ByteArrayInputStream(schemaAsByteArray)));
+            synchronized (this) {
+                return factory.newSchema(new StreamSource(new ByteArrayInputStream(schemaAsByteArray)));
+            }
         }
 
         Source source = getSchemaSource();
-        return factory.newSchema(source);
+        synchronized (this) {
+            return factory.newSchema(source);
+        }
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/camel/blob/6de2fba6/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorRouteTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorRouteTest.java
b/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorRouteTest.java
index 036704f..aa78644 100644
--- a/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorRouteTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorRouteTest.java
@@ -16,6 +16,11 @@
  */
 package org.apache.camel.component.validator;
 
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
@@ -129,6 +134,31 @@ public class ValidatorRouteTest extends ContextTestSupport {
 
         MockEndpoint.assertIsSatisfied(validEndpoint, invalidEndpoint, finallyEndpoint);
     }
+    
+    public void testConcurrentUseNotASharedSchema() throws Exception {
+        validEndpoint.expectedMessageCount(10);
+        // latch for the 10 exchanges we expect
+        final CountDownLatch latch = new CountDownLatch(10);
+        // setup a task executor to be able send the messages in parallel
+        ExecutorService executor = Executors.newCachedThreadPool();
+        for (int i = 0; i < 10; i++) {
+            executor.execute(new Runnable() {
+                public void run() {
+                    template.requestBody("direct:useNotASharedSchema", "<mail xmlns='http://foo.com/bar'><subject>Hey</subject><body>Hello
world!</body></mail>");
+                    latch.countDown();
+                }
+            });
+        }
+
+        try {
+            // wait for test completion, timeout after 30 sec to let other unit test run
to not wait forever
+            assertTrue(latch.await(30000L, TimeUnit.MILLISECONDS));
+            assertEquals("Latch should be zero", 0, latch.getCount());
+        } finally {
+            executor.shutdown();
+        }
+        MockEndpoint.assertIsSatisfied(validEndpoint, invalidEndpoint, finallyEndpoint);
+    }
 
     @Override
     protected void setUp() throws Exception {


Mime
View raw message