CAMEL-7349 cach the schema in JaxbDataFormate

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

Branch: refs/heads/camel-2.12.x
Commit: eab42aad98d8bc77bf6efeeceb740181611a76ca
Parents: 0892953
Author: Willem Jiang <willem.ji...@gmail.com>
Authored: Tue Apr 8 22:06:45 2014 +0800
Committer: Willem Jiang <willem.ji...@gmail.com>
Committed: Tue Apr 8 22:22:35 2014 +0800

----------------------------------------------------------------------
 .../camel/converter/jaxb/JaxbDataFormat.java    | 70 ++++++++++++--------
 1 file changed, 42 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/eab42aad/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 c29ab2d..cf2d33b 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
@@ -92,6 +92,7 @@ public class JaxbDataFormat extends ServiceSupport implements 
DataFormat, CamelC
     private JaxbNamespacePrefixMapper namespacePrefixMapper;
     private JaxbXmlStreamWriterWrapper xmlStreamWriterWrapper;
     private TypeConverter typeConverter;
+    private volatile Schema cachedSchema;
 
     public JaxbDataFormat() {
     }
@@ -368,47 +369,60 @@ public class JaxbDataFormat extends ServiceSupport 
implements DataFormat, CamelC
         }
     }
     
-    protected Unmarshaller createUnmarshaller() throws JAXBException, 
SAXException, FileNotFoundException, MalformedURLException {
+    protected Unmarshaller createUnmarshaller() throws JAXBException, 
SAXException, FileNotFoundException,
+        MalformedURLException {
         Unmarshaller unmarshaller = getContext().createUnmarshaller();
         if (schema != null) {
-            SchemaFactory factory = getOrCreateSchemaFactory();
-            try {
-                Schema newSchema = factory.newSchema(getSources());
-                unmarshaller.setSchema(newSchema);
-                unmarshaller.setEventHandler(new ValidationEventHandler() {
-                    public boolean handleEvent(ValidationEvent event) {
-                        // stop unmarshalling if the event is an ERROR or 
FATAL ERROR
-                        return event.getSeverity() == ValidationEvent.WARNING;
-                    }
-                });
-            } finally {
-                returnSchemaFactory(factory);
-            }
+            unmarshaller.setSchema(getCachedSchema());
+            unmarshaller.setEventHandler(new ValidationEventHandler() {
+                public boolean handleEvent(ValidationEvent event) {
+                    // stop unmarshalling if the event is an ERROR or FATAL
+                    // ERROR
+                    return event.getSeverity() == ValidationEvent.WARNING;
+                }
+            });
+
         }
 
         return unmarshaller;
     }
 
-    protected Marshaller createMarshaller() throws JAXBException, 
SAXException, FileNotFoundException, MalformedURLException {
+    protected Marshaller createMarshaller() throws JAXBException, 
SAXException, FileNotFoundException,
+        MalformedURLException {
         Marshaller marshaller = getContext().createMarshaller();
         if (schema != null) {
-            SchemaFactory factory = getOrCreateSchemaFactory();
-            try {
-                Schema newSchema = factory.newSchema(getSources());
-                marshaller.setSchema(newSchema);
-                marshaller.setEventHandler(new ValidationEventHandler() {
-                    public boolean handleEvent(ValidationEvent event) {
-                        // stop marshalling if the event is an ERROR or FATAL 
ERROR
-                        return event.getSeverity() == ValidationEvent.WARNING;
-                    }
-                });
-            } finally {
-                returnSchemaFactory(factory);
-            }
+            marshaller.setSchema(getCachedSchema());
+            marshaller.setEventHandler(new ValidationEventHandler() {
+                public boolean handleEvent(ValidationEvent event) {
+                    // stop marshalling if the event is an ERROR or FATAL ERROR
+                    return event.getSeverity() == ValidationEvent.WARNING;
+                }
+            });
+
         }
 
         return marshaller;
     }
+    
+    private Schema getCachedSchema() throws FileNotFoundException, 
MalformedURLException, SAXException {
+        if (cachedSchema == null) {
+            synchronized (this) {
+                if (cachedSchema == null) {
+                    cachedSchema = createSchema(getSources());
+                }
+            }
+        }
+        return cachedSchema;
+    }
+    
+    private Schema createSchema(Source[] sources) throws SAXException {
+        SchemaFactory factory = getOrCreateSchemaFactory();
+        try {
+            return factory.newSchema(sources);
+        } finally {
+            returnSchemaFactory(factory);
+        }
+    }
 
     private Source[] getSources() throws FileNotFoundException, 
MalformedURLException {
         // we support multiple schema by delimiting they by ','

Reply via email to