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 2009/02/12 09:45:39 UTC

svn commit: r743658 - in /camel/trunk/components: camel-freemarker/src/main/java/org/apache/camel/component/freemarker/ camel-freemarker/src/test/java/org/apache/camel/component/freemarker/ camel-spring/src/main/java/org/apache/camel/component/ camel-v...

Author: davsclaus
Date: Thu Feb 12 08:45:39 2009
New Revision: 743658

URL: http://svn.apache.org/viewvc?rev=743658&view=rev
Log:
CAMEL-505: freemarker and velocity endpoints can now be created using spring bean style

Added:
    camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerEndpointTest.java   (with props)
    camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityEndpointTest.java   (contents, props changed)
      - copied, changed from r743633, camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityTest.java
Modified:
    camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerComponent.java
    camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java
    camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedComponent.java
    camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedEndpoint.java
    camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityComponent.java
    camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java

Modified: camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerComponent.java?rev=743658&r1=743657&r2=743658&view=diff
==============================================================================
--- camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerComponent.java (original)
+++ camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerComponent.java Thu Feb 12 08:45:39 2009
@@ -33,7 +33,7 @@
     private Configuration noCacheConfiguration;
 
     protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
-        FreemarkerEndpoint endpoint = new FreemarkerEndpoint(uri, this, remaining, parameters);
+        FreemarkerEndpoint endpoint = new FreemarkerEndpoint(uri, this, remaining);
 
         // should we use regular configuration or no cache (content cache is default true)
         Configuration config;

Modified: camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java?rev=743658&r1=743657&r2=743658&view=diff
==============================================================================
--- camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java (original)
+++ camel/trunk/components/camel-freemarker/src/main/java/org/apache/camel/component/freemarker/FreemarkerEndpoint.java Thu Feb 12 08:45:39 2009
@@ -37,7 +37,10 @@
     private String encoding;
     private Configuration configuration;
 
-    public FreemarkerEndpoint(String uri, Component component, String resourceUri, Map parameters) {
+    public FreemarkerEndpoint() {
+    }
+
+    public FreemarkerEndpoint(String uri, Component component, String resourceUri) {
         super(uri, component, resourceUri, null);
     }
 
@@ -51,6 +54,11 @@
         return ExchangePattern.InOut;
     }
 
+    @Override
+    protected String createEndpointUri() {
+        return "freemarker:" + getResourceUri();
+    }
+
     /**
      * Sets the encoding to be used for loading the template file.
      */

Added: camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerEndpointTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerEndpointTest.java?rev=743658&view=auto
==============================================================================
--- camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerEndpointTest.java (added)
+++ camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerEndpointTest.java Thu Feb 12 08:45:39 2009
@@ -0,0 +1,46 @@
+/**
+ * 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.freemarker;
+
+import freemarker.cache.ClassTemplateLoader;
+import freemarker.template.Configuration;
+import org.apache.camel.builder.RouteBuilder;
+import org.springframework.core.io.Resource;
+
+/**
+ * Freemarker unit test
+ */
+public class FreemarkerEndpointTest extends FreemarkerTest {
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                FreemarkerEndpoint endpoint = new FreemarkerEndpoint();
+                endpoint.setCamelContext(context);
+                endpoint.setResourceUri("org/apache/camel/component/freemarker/example.ftl");
+
+                Configuration configuraiton = new Configuration();
+                configuraiton.setTemplateLoader(new ClassTemplateLoader(Resource.class, "/"));
+                endpoint.setConfiguration(configuraiton);
+
+                context.addEndpoint("free", endpoint);
+
+                from("direct:a").to("free");
+            }
+        };
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerEndpointTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-freemarker/src/test/java/org/apache/camel/component/freemarker/FreemarkerEndpointTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedComponent.java?rev=743658&r1=743657&r2=743658&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedComponent.java (original)
+++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedComponent.java Thu Feb 12 08:45:39 2009
@@ -16,7 +16,6 @@
  */
 package org.apache.camel.component;
 
-import org.apache.camel.Exchange;
 import org.apache.camel.impl.DefaultComponent;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;

Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedEndpoint.java?rev=743658&r1=743657&r2=743658&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedEndpoint.java (original)
+++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/ResourceBasedEndpoint.java Thu Feb 12 08:45:39 2009
@@ -38,12 +38,15 @@
  */
 public abstract class ResourceBasedEndpoint extends ProcessorEndpoint {
     protected final transient Log log = LogFactory.getLog(getClass());
-    private final String resourceUri;
+    private String resourceUri;
     private ResourceLoader resourceLoader = new DefaultResourceLoader();
     private Resource resource;
     private boolean contentCache;
     private byte[] buffer;
 
+    protected ResourceBasedEndpoint() {
+    }
+
     public ResourceBasedEndpoint(String endpointUri, Component component, String resourceUri, Processor processor) {
         super(endpointUri, component, processor);
         this.resourceUri = resourceUri;
@@ -67,19 +70,6 @@
         return resource;
     }
 
-    public boolean isContentCache() {
-        return contentCache;
-    }
-
-    /**
-     * Sets wether to use resource content cache or not - default is <tt>false</tt>.
-     *
-     * @see #getResourceAsInputStream()
-     */
-    public void setContentCache(boolean contentCache) {
-        this.contentCache = contentCache;
-    }
-
     /**
      * Gets the resource as an input stream considering the cache flag as well.
      * <p/>
@@ -111,6 +101,19 @@
         return resource.getInputStream();
     }
 
+    public boolean isContentCache() {
+        return contentCache;
+    }
+
+    /**
+     * Sets wether to use resource content cache or not - default is <tt>false</tt>.
+     *
+     * @see #getResourceAsInputStream()
+     */
+    public void setContentCache(boolean contentCache) {
+        this.contentCache = contentCache;
+    }
+
     public ResourceLoader getResourceLoader() {
         return resourceLoader;
     }
@@ -122,4 +125,9 @@
     public String getResourceUri() {
         return resourceUri;
     }
+
+    public void setResourceUri(String resourceUri) {
+        this.resourceUri = resourceUri;
+    }
+
 }
\ No newline at end of file

Modified: camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityComponent.java?rev=743658&r1=743657&r2=743658&view=diff
==============================================================================
--- camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityComponent.java (original)
+++ camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityComponent.java Thu Feb 12 08:45:39 2009
@@ -26,7 +26,7 @@
  * @version $Revision$
  */
 public class VelocityComponent extends DefaultComponent {
-    private VelocityEngine velocityEngine = new VelocityEngine();
+    private VelocityEngine velocityEngine;
 
     public VelocityEngine getVelocityEngine() {
         return velocityEngine;
@@ -38,8 +38,9 @@
 
     protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
         boolean cache = getAndRemoveParameter(parameters, "contentCache", Boolean.class, Boolean.TRUE);
-        VelocityEndpoint answer = new VelocityEndpoint(uri, this, remaining, parameters);
+        VelocityEndpoint answer = new VelocityEndpoint(uri, this, remaining);
         answer.setContentCache(cache);
+        answer.setVelocityEngine(velocityEngine);
         return answer;
     }
 }

Modified: camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java?rev=743658&r1=743657&r2=743658&view=diff
==============================================================================
--- camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java (original)
+++ camel/trunk/components/camel-velocity/src/main/java/org/apache/camel/component/velocity/VelocityEndpoint.java Thu Feb 12 08:45:39 2009
@@ -38,14 +38,15 @@
  * @version $Revision$
  */
 public class VelocityEndpoint extends ResourceBasedEndpoint {
-    private final VelocityComponent component;
     private VelocityEngine velocityEngine;
     private boolean loaderCache = true;
     private String encoding;
 
-    public VelocityEndpoint(String uri, VelocityComponent component, String resourceUri, Map parameters) {
+    public VelocityEndpoint() {
+    }
+
+    public VelocityEndpoint(String uri, VelocityComponent component, String resourceUri) {
         super(uri, component, resourceUri, null);
-        this.component = component;
     }
 
     @Override
@@ -58,9 +59,14 @@
         return ExchangePattern.InOut;
     }
 
-    private VelocityEngine getVelocityEngine() throws Exception {
+    @Override
+    protected String createEndpointUri() {
+        return "velocity:" + getResourceUri();
+    }
+
+    private synchronized VelocityEngine getVelocityEngine() throws Exception {
         if (velocityEngine == null) {
-            velocityEngine = component.getVelocityEngine();
+            velocityEngine = new VelocityEngine();
             velocityEngine.setProperty(Velocity.FILE_RESOURCE_LOADER_CACHE, isLoaderCache() ? Boolean.TRUE : Boolean.FALSE);
             velocityEngine.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, Log4JLogChute.class.getName());
             velocityEngine.setProperty(Log4JLogChute.RUNTIME_LOG_LOG4J_LOGGER, VelocityEndpoint.class.getName());
@@ -71,6 +77,7 @@
 
     public void setVelocityEngine(VelocityEngine velocityEngine) {
         this.velocityEngine = velocityEngine;
+
     }
 
     public boolean isLoaderCache() {

Copied: camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityEndpointTest.java (from r743633, camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityEndpointTest.java?p2=camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityEndpointTest.java&p1=camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityTest.java&r1=743633&r2=743658&rev=743658&view=diff
==============================================================================
--- camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityTest.java (original)
+++ camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityEndpointTest.java Thu Feb 12 08:45:39 2009
@@ -16,43 +16,23 @@
  */
 package org.apache.camel.component.velocity;
 
-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;
 
 /**
  * @version $Revision$
  */
-public class VelocityTest 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);
-    }
+public class VelocityEndpointTest extends VelocityTest {
 
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
-            public void configure() {
-                // START SNIPPET: example
-                from("direct:a").
-                        to("velocity:org/apache/camel/component/velocity/example.vm");
-                // END SNIPPET: example
+            public void configure() throws Exception {
+                VelocityEndpoint endpoint = new VelocityEndpoint();
+                endpoint.setCamelContext(context);
+                endpoint.setResourceUri("org/apache/camel/component/velocity/example.vm");
+
+                context.addEndpoint("velo", endpoint);
+
+                from("direct:a").to("velo");
             }
         };
     }

Propchange: camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityEndpointTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityEndpointTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: camel/trunk/components/camel-velocity/src/test/java/org/apache/camel/component/velocity/VelocityEndpointTest.java
------------------------------------------------------------------------------
    svn:mergeinfo =