Author: davsclaus
Date: Thu Jun 5 21:24:20 2008
New Revision: 663817
URL: http://svn.apache.org/viewvc?rev=663817&view=rev
Log:
CAMEL-580: Added content cache for resource based endpoints such as
camel-velocity and camel-stringtemplate.
Added:
activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateContentCacheTest.java
(with props)
activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateLetterTest.java
(with props)
activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/letter.tm
(with props)
activemq/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityContentCacheTest.java
activemq/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityLetterTest.java
activemq/camel/trunk/components/camel-velocity/src/test/resources/org/apache/camel/component/velocity/letter.vm
Modified:
activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateComponent.java
activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
activemq/camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
Modified:
activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateComponent.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateComponent.java?rev=663817&r1=663816&r2=663817&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateComponent.java
(original)
+++
activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateComponent.java
Thu Jun 5 21:24:20 2008
@@ -25,7 +25,9 @@
* @version $Revision$
*/
public class StringTemplateComponent extends DefaultComponent {
+
protected Endpoint createEndpoint(String uri, String remaining, Map
parameters) throws Exception {
return new StringTemplateEndpoint(uri, this, remaining, parameters);
}
+
}
\ No newline at end of file
Modified:
activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java?rev=663817&r1=663816&r2=663817&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
(original)
+++
activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
Thu Jun 5 21:24:20 2008
@@ -42,6 +42,7 @@
super(endpointUri, processor, resourceUri);
}
+ @Override
public boolean isSingleton() {
return true;
}
@@ -56,8 +57,8 @@
StringWriter buffer = new StringWriter();
Map variableMap = ExchangeHelper.createVariableMap(exchange);
- // TODO we might wanna add some kinda resource caching of the template
- String text = IOConverter.toString(getResource().getInputStream());
+ // getResourceAsInputStream also considers the content cache
+ String text = IOConverter.toString(getResourceAsInputStream());
StringTemplate template = new StringTemplate(text);
template.setAttributes(variableMap);
template.write(new AutoIndentWriter(buffer));
@@ -67,4 +68,5 @@
out.setBody(buffer.toString());
out.setHeader("org.apache.camel.stringtemplate.resource",
getResource());
}
+
}
\ No newline at end of file
Added:
activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateContentCacheTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateContentCacheTest.java?rev=663817&view=auto
==============================================================================
---
activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateContentCacheTest.java
(added)
+++
activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateContentCacheTest.java
Thu Jun 5 21:24:20 2008
@@ -0,0 +1,80 @@
+/**
+ * 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.velocity;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.component.file.FileComponent;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * Unit test the cahce when reloading .tm files in the classpath
+ */
+public class StringTemplateContentCacheTest extends ContextTestSupport {
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ // create a tm file in the classpath as this is the tricky reloading
stuff
+
template.sendBodyAndHeader("file://target/test-classes/org/apache/camel/component/stringtemplate?append=false",
"Hello $headers.name$", FileComponent.HEADER_FILE_NAME, "hello.tm");
+ }
+
+ public void testNotCached() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("Hello London");
+
+ template.sendBodyAndHeader("direct:a", "Body", "name", "London");
+ mock.assertIsSatisfied();
+
+ // now change content in the file in the classpath and try again
+
template.sendBodyAndHeader("file://target/test-classes/org/apache/camel/component/stringtemplate?append=false",
"Bye $headers.name$", FileComponent.HEADER_FILE_NAME, "hello.tm");
+
+ mock.reset();
+ mock.expectedBodiesReceived("Bye Paris");
+
+ template.sendBodyAndHeader("direct:a", "Body", "name", "Paris");
+ mock.assertIsSatisfied();
+ }
+
+ public void testCached() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("Hello London");
+
+ template.sendBodyAndHeader("direct:b", "Body", "name", "London");
+ mock.assertIsSatisfied();
+
+ // now change content in the file in the classpath and try again
+
template.sendBodyAndHeader("file://target/test-classes/org/apache/camel/component/stringtemplate?append=false",
"Bye $headers.name$", FileComponent.HEADER_FILE_NAME, "hello.tm");
+
+ mock.reset();
+ // we must expected the original filecontent as the cache is enabled,
so its Hello and not Bye
+ mock.expectedBodiesReceived("Hello Paris");
+
+ template.sendBodyAndHeader("direct:b", "Body", "name", "Paris");
+ mock.assertIsSatisfied();
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+
from("direct:a").to("string-template://org/apache/camel/component/stringtemplate/hello.tm?contentCache=false").to("mock:result");
+
+
from("direct:b").to("string-template://org/apache/camel/component/stringtemplate/hello.tm?contentCache=true").to("mock:result");
+ }
+ };
+ }
+}
Propchange:
activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateContentCacheTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateContentCacheTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added:
activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateLetterTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateLetterTest.java?rev=663817&view=auto
==============================================================================
---
activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateLetterTest.java
(added)
+++
activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateLetterTest.java
Thu Jun 5 21:24:20 2008
@@ -0,0 +1,42 @@
+package org.apache.camel.component.velocity;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Message;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * Unit test for wiki documentation
+ */
+public class StringTemplateLetterTest extends ContextTestSupport {
+
+ // START SNIPPET: e1
+ private Exchange createLetter() {
+ Exchange exchange = context.getEndpoint("direct:a").createExchange();
+ Message msg = exchange.getIn();
+ msg.setHeader("firstName", "Claus");
+ msg.setHeader("lastName", "Ibsen");
+ msg.setHeader("item", "Camel in Action");
+ return exchange;
+ }
+
+ public void testVelocityLetter() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+ mock.expectedBodiesReceived("Dear Ibsen, Claus\n\nThanks for the order
of Camel in Action.\n\nRegards Camel Riders Bookstore");
+
+ template.send("direct:a", createLetter());
+
+ mock.assertIsSatisfied();
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+
from("direct:a").to("string-template:org/apache/camel/component/stringtemplate/letter.tm").to("mock:result");
+ }
+ };
+ }
+ // END SNIPPET: e1
+}
Propchange:
activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateLetterTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateLetterTest.java
------------------------------------------------------------------------------
svn:keywords = Rev Date
Added:
activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/letter.tm
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/letter.tm?rev=663817&view=auto
==============================================================================
---
activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/letter.tm
(added)
+++
activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/letter.tm
Thu Jun 5 21:24:20 2008
@@ -0,0 +1,21 @@
+$! ------------------------------------------------------------------------
+## 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.
+## ------------------------------------------------------------------------ !$
+Dear $headers.lastName$, $headers.firstName$
+
+Thanks for the order of $headers.item$.
+
+Regards Camel Riders Bookstore
\ No newline at end of file
Propchange:
activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/letter.tm
------------------------------------------------------------------------------
svn:eol-style = native
Modified:
activemq/camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java?rev=663817&r1=663816&r2=663817&view=diff
==============================================================================
---
activemq/camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
(original)
+++
activemq/camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
Thu Jun 5 21:24:20 2008
@@ -30,6 +30,8 @@
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
+import org.apache.velocity.runtime.log.SimpleLog4JLogSystem;
+import org.springframework.core.io.Resource;
/**
* @version $Revision$
@@ -44,6 +46,7 @@
this.component = component;
}
+ @Override
public boolean isSingleton() {
return true;
}
@@ -53,13 +56,13 @@
return ExchangePattern.InOut;
}
- public VelocityEngine getVelocityEngine() throws Exception {
+ private VelocityEngine getVelocityEngine() throws Exception {
if (velocityEngine == null) {
velocityEngine = component.getVelocityEngine();
+ velocityEngine.setProperty(Velocity.FILE_RESOURCE_LOADER_CACHE,
isLoaderCache() ? Boolean.TRUE : Boolean.FALSE);
+ velocityEngine.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
SimpleLog4JLogSystem.class.getName());
+ velocityEngine.setProperty("runtime.log.logsystem.log4j.category",
VelocityEndpoint.class.getName());
velocityEngine.init();
- if (isLoaderCache()) {
- Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_CACHE,
Boolean.TRUE);
- }
}
return velocityEngine;
}
@@ -83,18 +86,23 @@
@Override
protected void onExchange(Exchange exchange) throws Exception {
- // TODO we might wanna add some kinda resource caching of the template
- Reader reader = new InputStreamReader(getResource().getInputStream());
+ Resource resource = getResource();
+
+ // getResourceAsInputStream also considers the content cache
+ Reader reader = new InputStreamReader(getResourceAsInputStream());
StringWriter buffer = new StringWriter();
String logTag = getClass().getName();
Map variableMap = ExchangeHelper.createVariableMap(exchange);
Context velocityContext = new VelocityContext(variableMap);
+
+ // let velocity parse and generate the result in buffer
VelocityEngine engine = getVelocityEngine();
engine.evaluate(velocityContext, buffer, logTag, reader);
// now lets output the results to the exchange
Message out = exchange.getOut(true);
out.setBody(buffer.toString());
- out.setHeader("org.apache.camel.velocity.resource", getResource());
+ out.setHeader("org.apache.camel.velocity.resource", resource);
}
+
}
Added:
activemq/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityContentCacheTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityContentCacheTest.java?rev=663817&view=auto
==============================================================================
---
activemq/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityContentCacheTest.java
(added)
+++
activemq/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityContentCacheTest.java
Thu Jun 5 21:24:20 2008
@@ -0,0 +1,80 @@
+/**
+ * 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.velocity;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.component.file.FileComponent;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * Unit test the cahce when reloading .vm files in the classpath
+ */
+public class VelocityContentCacheTest extends ContextTestSupport {
+
+ protected void setUp() throws Exception {
+ super.setUp();
+
+ // create a vm file in the classpath as this is the tricky reloading
stuff
+
template.sendBodyAndHeader("file://target/test-classes/org/apache/camel/component/velocity?append=false",
"Hello $headers.name", FileComponent.HEADER_FILE_NAME, "hello.vm");
+ }
+
+ public void testNotCached() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("Hello London");
+
+ template.sendBodyAndHeader("direct:a", "Body", "name", "London");
+ mock.assertIsSatisfied();
+
+ // now change content in the file in the classpath and try again
+
template.sendBodyAndHeader("file://target/test-classes/org/apache/camel/component/velocity?append=false",
"Bye $headers.name", FileComponent.HEADER_FILE_NAME, "hello.vm");
+
+ mock.reset();
+ mock.expectedBodiesReceived("Bye Paris");
+
+ template.sendBodyAndHeader("direct:a", "Body", "name", "Paris");
+ mock.assertIsSatisfied();
+ }
+
+ public void testCached() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedBodiesReceived("Hello London");
+
+ template.sendBodyAndHeader("direct:b", "Body", "name", "London");
+ mock.assertIsSatisfied();
+
+ // now change content in the file in the classpath and try again
+
template.sendBodyAndHeader("file://target/test-classes/org/apache/camel/component/velocity?append=false",
"Bye $headers.name", FileComponent.HEADER_FILE_NAME, "hello.vm");
+
+ mock.reset();
+ // we must expected the original filecontent as the cache is enabled,
so its Hello and not Bye
+ mock.expectedBodiesReceived("Hello Paris");
+
+ template.sendBodyAndHeader("direct:b", "Body", "name", "Paris");
+ mock.assertIsSatisfied();
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+
from("direct:a").to("velocity://org/apache/camel/component/velocity/hello.vm?contentCache=false").to("mock:result");
+
+
from("direct:b").to("velocity://org/apache/camel/component/velocity/hello.vm?contentCache=true").to("mock:result");
+ }
+ };
+ }
+}
Added:
activemq/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityLetterTest.java
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityLetterTest.java?rev=663817&view=auto
==============================================================================
---
activemq/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityLetterTest.java
(added)
+++
activemq/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityLetterTest.java
Thu Jun 5 21:24:20 2008
@@ -0,0 +1,42 @@
+package org.apache.camel.component.velocity;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Message;
+import org.apache.camel.Exchange;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * Unit test for wiki documentation
+ */
+public class VelocityLetterTest extends ContextTestSupport {
+
+ // START SNIPPET: e1
+ private Exchange createLetter() {
+ Exchange exchange = context.getEndpoint("direct:a").createExchange();
+ Message msg = exchange.getIn();
+ msg.setHeader("firstName", "Claus");
+ msg.setHeader("lastName", "Ibsen");
+ msg.setHeader("item", "Camel in Action");
+ return exchange;
+ }
+
+ public void testVelocityLetter() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.expectedMessageCount(1);
+ mock.expectedBodiesReceived("Dear Ibsen, Claus\n\nThanks for the order
of Camel in Action.\n\nRegards Camel Riders Bookstore");
+
+ template.send("direct:a", createLetter());
+
+ mock.assertIsSatisfied();
+ }
+
+ protected RouteBuilder createRouteBuilder() throws Exception {
+ return new RouteBuilder() {
+ public void configure() throws Exception {
+
from("direct:a").to("velocity:org/apache/camel/component/velocity/letter.vm").to("mock:result");
+ }
+ };
+ }
+ // END SNIPPET: e1
+}
Added:
activemq/camel/trunk/components/camel-velocity/src/test/resources/org/apache/camel/component/velocity/letter.vm
URL:
http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-velocity/src/test/resources/org/apache/camel/component/velocity/letter.vm?rev=663817&view=auto
==============================================================================
---
activemq/camel/trunk/components/camel-velocity/src/test/resources/org/apache/camel/component/velocity/letter.vm
(added)
+++
activemq/camel/trunk/components/camel-velocity/src/test/resources/org/apache/camel/component/velocity/letter.vm
Thu Jun 5 21:24:20 2008
@@ -0,0 +1,21 @@
+## ------------------------------------------------------------------------
+## 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.
+## ------------------------------------------------------------------------
+Dear ${headers.lastName}, ${headers.firstName}
+
+Thanks for the order of ${headers.item}.
+
+Regards Camel Riders Bookstore
\ No newline at end of file