You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2010/11/06 16:52:13 UTC

svn commit: r1032099 - in /camel/trunk/components/camel-spring/src: main/java/org/apache/camel/component/xslt/ test/java/org/apache/camel/component/xslt/

Author: davsclaus
Date: Sat Nov  6 15:52:12 2010
New Revision: 1032099

URL: http://svn.apache.org/viewvc?rev=1032099&view=rev
Log:
CAMEL-2737: Added contentCache to xslt component. Thanks to Richard for patch.

Added:
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltContentCacheTest.java
Modified:
    camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/xslt/XsltComponent.java
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java

Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/xslt/XsltComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/xslt/XsltComponent.java?rev=1032099&r1=1032098&r2=1032099&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/xslt/XsltComponent.java (original)
+++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/xslt/XsltComponent.java Sat Nov  6 15:52:12 2010
@@ -22,7 +22,7 @@ import javax.xml.transform.TransformerFa
 import javax.xml.transform.URIResolver;
 
 import org.apache.camel.Endpoint;
-import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.Exchange;
 import org.apache.camel.builder.xml.ResultHandlerFactory;
 import org.apache.camel.builder.xml.XsltBuilder;
 import org.apache.camel.builder.xml.XsltUriResolver;
@@ -41,6 +41,7 @@ import org.springframework.core.io.Resou
 public class XsltComponent extends ResourceBasedComponent {
     private XmlConverter xmlConverter;
     private URIResolver uriResolver;
+    private boolean contentCache = true;
 
     public XmlConverter getXmlConverter() {
         return xmlConverter;
@@ -58,12 +59,20 @@ public class XsltComponent extends Resou
         this.uriResolver = uriResolver;
     }
 
-    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
-        Resource resource = resolveMandatoryResource(remaining);
+    public boolean isContentCache() {
+        return contentCache;
+    }
+
+    public void setContentCache(boolean contentCache) {
+        this.contentCache = contentCache;
+    }
+
+    protected Endpoint createEndpoint(String uri, final String remaining, Map<String, Object> parameters) throws Exception {
+        final Resource resource = resolveMandatoryResource(remaining);
         if (log.isDebugEnabled()) {
             log.debug(this + " using schema resource: " + resource);
         }
-        XsltBuilder xslt = getCamelContext().getInjector().newInstance(XsltBuilder.class);
+        final XsltBuilder xslt = getCamelContext().getInjector().newInstance(XsltBuilder.class);
 
         // lets allow the converter to be configured
         XmlConverter converter = resolveAndRemoveReferenceParameter(parameters, "converter", XmlConverter.class);
@@ -119,6 +128,30 @@ public class XsltComponent extends Resou
         String output = getAndRemoveParameter(parameters, "output", String.class);
         configureOutput(xslt, output);
 
+        configureXslt(xslt, uri, remaining, parameters);
+        loadResource(xslt, resource);
+
+        // default to use the cache option from the component if the endpoint did not have the contentCache parameter
+        boolean cache = getAndRemoveParameter(parameters, "contentCache", Boolean.class, contentCache);
+        if (!cache) {
+            return new ProcessorEndpoint(uri, this, xslt) {
+                @Override
+                protected void onExchange(Exchange exchange) throws Exception {
+                    // force to load the resource on each exchange as we are not cached
+                    loadResource(xslt, resource);
+                    super.onExchange(exchange);
+                }
+            };
+        } else {
+            // we have already loaded xslt so we are cached
+            return new ProcessorEndpoint(uri, this, xslt);
+        }
+    }
+
+    private void loadResource(XsltBuilder xslt, Resource resource) throws TransformerConfigurationException {
+        if (log.isTraceEnabled()) {
+            log.trace(this + " loading schema resource: " + resource);
+        }
         try {
             xslt.setTransformerInputStream(resource.getInputStream());
         } catch (Exception e) {
@@ -126,8 +159,6 @@ public class XsltComponent extends Resou
             // end users to know which resource failed
             throw new TransformerConfigurationException(e.getMessage() + " " + resource.toString(), e);
         }
-        configureXslt(xslt, uri, remaining, parameters);
-        return new ProcessorEndpoint(uri, this, xslt);
     }
 
     protected void configureXslt(XsltBuilder xslt, String uri, String remaining, Map<String, Object> parameters) throws Exception {

Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltContentCacheTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltContentCacheTest.java?rev=1032099&view=auto
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltContentCacheTest.java (added)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltContentCacheTest.java Sat Nov  6 15:52:12 2010
@@ -0,0 +1,120 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.xslt;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * Unit test hot reloading of XSL files (CAMEL-2737).
+ */
+public class XsltContentCacheTest extends ContextTestSupport {
+    
+    private static final String ORIGINAL_XSL = 
+        "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
+            + "<xsl:template match=\"/\"><goodbye><xsl:value-of select=\"/hello\"/></goodbye></xsl:template>"
+            + "</xsl:stylesheet>";
+
+    private static final String NEW_XSL = 
+        "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">"
+            + "<xsl:template match=\"/\"><goodnight><xsl:value-of select=\"/hello\"/></goodnight></xsl:template>"
+            + "</xsl:stylesheet>";
+    
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        
+        // create file with original XSL transformation
+        template.sendBodyAndHeader("file://target/test-classes/org/apache/camel/component/xslt?fileExist=Override", ORIGINAL_XSL, Exchange.FILE_NAME, "hello.xsl");
+        
+        // start the context AFTER we've created the hello.xsl file, otherwise the xslt routes will fail
+        super.startCamelContext();
+    }
+    
+    @Override
+    protected void startCamelContext() throws Exception {
+        // Override so we can start the context ourself in the setUp
+    }
+
+    public void testNotCached() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>");
+
+        template.sendBody("direct:a", "<hello>world!</hello>");
+        mock.assertIsSatisfied();
+
+        // now replace the file with a new XSL transformation
+        template.sendBodyAndHeader("file://target/test-classes/org/apache/camel/component/xslt?fileExist=Override", NEW_XSL, Exchange.FILE_NAME, "hello.xsl");
+
+        mock.reset();
+        // we expect the new output as the cache is not enabled, so it's "goodnight" and not "goodbye"
+        mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodnight>world!</goodnight>");
+
+        template.sendBody("direct:a", "<hello>world!</hello>");
+        mock.assertIsSatisfied();
+    }
+
+    public void testCached() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>");
+
+        template.sendBody("direct:b", "<hello>world!</hello>");
+        mock.assertIsSatisfied();
+
+        // now replace the file with a new XSL transformation
+        template.sendBodyAndHeader("file://target/test-classes/org/apache/camel/component/xslt?fileExist=Override", NEW_XSL, Exchange.FILE_NAME, "hello.xsl");
+
+        mock.reset();
+        // we expect the original output as the cache is enabled, so it's "goodbye" and not "goodnight"
+        mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>");
+
+        template.sendBody("direct:b", "<hello>world!</hello>");
+        mock.assertIsSatisfied();
+    }
+
+    public void testCachedIsDefault() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>");
+
+        template.sendBody("direct:c", "<hello>world!</hello>");
+        mock.assertIsSatisfied();
+
+        // now replace the file with a new XSL transformation
+        template.sendBodyAndHeader("file://target/test-classes/org/apache/camel/component/xslt?fileExist=Override", NEW_XSL, Exchange.FILE_NAME, "hello.xsl");
+
+        mock.reset();
+        // we expect the original output as the cache is enabled, so it's "goodbye" and not "goodnight"
+        mock.expectedBodiesReceived("<?xml version=\"1.0\" encoding=\"UTF-8\"?><goodbye>world!</goodbye>");
+
+        template.sendBody("direct:c", "<hello>world!</hello>");
+        mock.assertIsSatisfied();
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("direct:a").to("xslt://org/apache/camel/component/xslt/hello.xsl?output=string&contentCache=false").to("mock:result");
+
+                from("direct:b").to("xslt://org/apache/camel/component/xslt/hello.xsl?output=string&contentCache=true").to("mock:result");
+
+                from("direct:c").to("xslt://org/apache/camel/component/xslt/hello.xsl?output=string").to("mock:result");
+            }
+        };
+    }
+}

Modified: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java?rev=1032099&r1=1032098&r2=1032099&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java (original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/component/xslt/XsltFileNotFoundTest.java Sat Nov  6 15:52:12 2010
@@ -18,6 +18,8 @@ package org.apache.camel.component.xslt;
 
 import java.io.FileNotFoundException;
 
+import javax.xml.transform.TransformerConfigurationException;
+
 import org.apache.camel.CamelContext;
 import org.apache.camel.FailedToCreateRouteException;
 import org.apache.camel.ResolveEndpointFailedException;
@@ -38,7 +40,8 @@ public class XsltFileNotFoundTest extend
         } catch (FailedToCreateRouteException e) {
             // expected
             assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
-            assertIsInstanceOf(FileNotFoundException.class, e.getCause().getCause());
+            assertIsInstanceOf(TransformerConfigurationException.class, e.getCause().getCause());
+            assertIsInstanceOf(FileNotFoundException.class, e.getCause().getCause().getCause());
         }
     }