You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2007/09/15 08:51:51 UTC

svn commit: r575883 - in /activemq/camel/trunk/components/camel-stringtemplate: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/camel/ src/main/java/org/apache/camel/component/ src/main/java/org/ap...

Author: jstrachan
Date: Fri Sep 14 23:51:49 2007
New Revision: 575883

URL: http://svn.apache.org/viewvc?rev=575883&view=rev
Log:
initial spike of a string-template component for CAMEL-148

Added:
    activemq/camel/trunk/components/camel-stringtemplate/
    activemq/camel/trunk/components/camel-stringtemplate/pom.xml   (with props)
    activemq/camel/trunk/components/camel-stringtemplate/src/
    activemq/camel/trunk/components/camel-stringtemplate/src/main/
    activemq/camel/trunk/components/camel-stringtemplate/src/main/java/
    activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/
    activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/
    activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/
    activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/
    activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/
    activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateComponent.java   (with props)
    activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java   (with props)
    activemq/camel/trunk/components/camel-stringtemplate/src/main/resources/
    activemq/camel/trunk/components/camel-stringtemplate/src/main/resources/META-INF/
    activemq/camel/trunk/components/camel-stringtemplate/src/main/resources/META-INF/services/
    activemq/camel/trunk/components/camel-stringtemplate/src/main/resources/META-INF/services/org/
    activemq/camel/trunk/components/camel-stringtemplate/src/main/resources/META-INF/services/org/apache/
    activemq/camel/trunk/components/camel-stringtemplate/src/main/resources/META-INF/services/org/apache/camel/
    activemq/camel/trunk/components/camel-stringtemplate/src/main/resources/META-INF/services/org/apache/camel/component/
    activemq/camel/trunk/components/camel-stringtemplate/src/main/resources/META-INF/services/org/apache/camel/component/string-template
    activemq/camel/trunk/components/camel-stringtemplate/src/test/
    activemq/camel/trunk/components/camel-stringtemplate/src/test/java/
    activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/
    activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/
    activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/
    activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/
    activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/
    activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateTest.java   (with props)
    activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/
    activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/log4j.properties   (with props)
    activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/
    activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/apache/
    activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/apache/camel/
    activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/
    activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/
    activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/template.tm

Added: activemq/camel/trunk/components/camel-stringtemplate/pom.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-stringtemplate/pom.xml?rev=575883&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-stringtemplate/pom.xml (added)
+++ activemq/camel/trunk/components/camel-stringtemplate/pom.xml Fri Sep 14 23:51:49 2007
@@ -0,0 +1,82 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+  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.
+-->
+
+<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <groupId>org.apache.camel</groupId>
+    <artifactId>camel-parent</artifactId>
+    <version>1.1-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>camel-stringtemplate</artifactId>
+  <name>Camel :: StringTemplate</name>
+  <description>Camel StringTemplate support</description>
+
+  <dependencies>
+
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-core</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-spring</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>antlr</groupId>
+      <artifactId>stringtemplate</artifactId>
+      <version>2.3b6</version>
+    </dependency>
+
+    <!-- testing -->
+    <dependency>
+      <groupId>org.apache.camel</groupId>
+      <artifactId>camel-core</artifactId>
+      <type>test-jar</type>
+      <scope>test</scope>
+    </dependency>
+
+    <dependency>
+      <groupId>commons-logging</groupId>
+      <artifactId>commons-logging</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>log4j</groupId>
+      <artifactId>log4j</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+
+</project>
+
+

Propchange: activemq/camel/trunk/components/camel-stringtemplate/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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=575883&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateComponent.java (added)
+++ activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateComponent.java Fri Sep 14 23:51:49 2007
@@ -0,0 +1,32 @@
+/**
+ *
+ * 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.stringtemplate;
+
+import java.util.Map;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.impl.DefaultComponent;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+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

Propchange: activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateComponent.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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=575883&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java (added)
+++ activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java Fri Sep 14 23:51:49 2007
@@ -0,0 +1,68 @@
+/**
+ *
+ * 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.stringtemplate;
+
+import java.io.StringWriter;
+import java.util.Map;
+
+import org.antlr.stringtemplate.AutoIndentWriter;
+import org.antlr.stringtemplate.StringTemplate;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.Message;
+import org.apache.camel.component.ResourceBasedEndpoint;
+import org.apache.camel.converter.IOConverter;
+import org.apache.camel.util.ExchangeHelper;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class StringTemplateEndpoint extends ResourceBasedEndpoint {
+    private final StringTemplateComponent component;
+
+    public StringTemplateEndpoint(String uri, StringTemplateComponent component, String resourceUri, Map parameters) {
+        super(uri, component, resourceUri, null);
+        this.component = component;
+    }
+
+    public boolean isSingleton() {
+        return true;
+    }
+
+    @Override
+    public ExchangePattern getExchangePattern() {
+        return ExchangePattern.InOut;
+    }
+
+    @Override
+    protected void onExchange(Exchange exchange) throws Exception {
+        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());
+        StringTemplate template = new StringTemplate(text);
+        template.setAttributes(variableMap);
+        template.write(new AutoIndentWriter(buffer));
+
+        // now lets output the results to the exchange
+        Message out = exchange.getOut(true);
+        out.setBody(buffer.toString());
+        out.setHeader("org.apache.camel.stringtemplate.resource", getResource());
+    }
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/components/camel-stringtemplate/src/main/java/org/apache/camel/component/stringtemplate/StringTemplateEndpoint.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/components/camel-stringtemplate/src/main/resources/META-INF/services/org/apache/camel/component/string-template
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-stringtemplate/src/main/resources/META-INF/services/org/apache/camel/component/string-template?rev=575883&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-stringtemplate/src/main/resources/META-INF/services/org/apache/camel/component/string-template (added)
+++ activemq/camel/trunk/components/camel-stringtemplate/src/main/resources/META-INF/services/org/apache/camel/component/string-template Fri Sep 14 23:51:49 2007
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+class=org.apache.camel.component.stringtemplate.StringTemplateComponent

Added: activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateTest.java?rev=575883&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateTest.java (added)
+++ activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateTest.java Fri Sep 14 23:51:49 2007
@@ -0,0 +1,62 @@
+/**
+ * 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.stringtemplate;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.InvalidPayloadException;
+import org.apache.camel.Message;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import static org.apache.camel.language.simple.SimpleLanguage.simple;
+import org.apache.camel.util.ExchangeHelper;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class StringTemplateTest extends ContextTestSupport {
+
+    public void testReceivesFooResponse() throws Exception {
+        assertRespondsWith("foo", "<hello>foo</hello>");
+    }
+
+    public void testReceivesBarResponse() throws Exception {
+        assertRespondsWith("bar", "<hello>bar</hello>");
+    }
+
+    protected void assertRespondsWith(final String value, String expectedBody) throws InvalidPayloadException {
+        Exchange response = template.request("direct:a", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                Message in = exchange.getIn();
+                in.setBody("answer");
+                in.setHeader("cheese", value);
+            }
+        });
+        assertOutMessageBodyEquals(response, expectedBody);
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                // START SNIPPET: example
+                from("direct:a").
+                        to("string-template:org/apache/camel/component/stringtemplate/template.tm");
+                // END SNIPPET: example
+            }
+        };
+    }
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/components/camel-stringtemplate/src/test/java/org/apache/camel/component/stringtemplate/StringTemplateTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/log4j.properties?rev=575883&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/log4j.properties (added)
+++ activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/log4j.properties Fri Sep 14 23:51:49 2007
@@ -0,0 +1,35 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+
+#
+# The logging properties used during tests..
+#
+log4j.rootLogger=INFO, out
+
+log4j.logger.org.apache.activemq.spring=WARN
+
+# CONSOLE appender not used by default
+log4j.appender.stdout=org.apache.log4j.ConsoleAppender
+log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
+log4j.appender.stdout.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
+
+# File appender
+log4j.appender.out=org.apache.log4j.FileAppender
+log4j.appender.out.layout=org.apache.log4j.PatternLayout
+log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
+log4j.appender.out.file=target/camel-test.log
+log4j.appender.out.append=true

Propchange: activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/log4j.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/template.tm
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/template.tm?rev=575883&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/template.tm (added)
+++ activemq/camel/trunk/components/camel-stringtemplate/src/test/resources/org/apache/camel/component/stringtemplate/template.tm Fri Sep 14 23:51:49 2007
@@ -0,0 +1 @@
+<hello>$headers.cheese$</hello>
\ No newline at end of file