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 2012/03/12 11:07:36 UTC

svn commit: r1299606 - in /camel/trunk/components/camel-cache/src: main/java/org/apache/camel/component/cache/ test/java/org/apache/camel/component/cache/

Author: davsclaus
Date: Mon Mar 12 10:07:35 2012
New Revision: 1299606

URL: http://svn.apache.org/viewvc?rev=1299606&view=rev
Log:
CAMEL-4300: You can now configure a default key/operation in cache endpoints. Thanks to Piotr Klimczak for the patch.

Modified:
    camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheConstants.java
    camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheEndpoint.java
    camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheProducer.java
    camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheProducerTest.java

Modified: camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheConstants.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheConstants.java?rev=1299606&r1=1299605&r2=1299606&view=diff
==============================================================================
--- camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheConstants.java (original)
+++ camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheConstants.java Mon Mar 12 10:07:35 2012
@@ -20,15 +20,22 @@ package org.apache.camel.component.cache
  * Constants used in this module
  */
 public interface CacheConstants {
+    String CACHE_HEADER_PREFIX = "CamelCache";
+    String CACHE_OPERATION = CACHE_HEADER_PREFIX + "Operation";
+    String CACHE_KEY = CACHE_HEADER_PREFIX + "Key";
+    String CACHE_ELEMENT_WAS_FOUND = CACHE_HEADER_PREFIX + "ElementWasFound";
 
-    String CACHE_OPERATION = "CamelCacheOperation";
-    String CACHE_KEY = "CamelCacheKey";
-    String CACHE_ELEMENT_WAS_FOUND = "CamelCacheElementWasFound";
+    String CACHE_OPERATION_URL_ADD = "Add";
+    String CACHE_OPERATION_URL_UPDATE = "Update";
+    String CACHE_OPERATION_URL_DELETE = "Delete";
+    String CACHE_OPERATION_URL_DELETEALL = "DeleteAll";
+    String CACHE_OPERATION_URL_GET = "Get";
+    String CACHE_OPERATION_URL_CHECK = "Check";
 
-    String CACHE_OPERATION_ADD = "CamelCacheAdd";
-    String CACHE_OPERATION_UPDATE = "CamelCacheUpdate";
-    String CACHE_OPERATION_DELETE = "CamelCacheDelete";
-    String CACHE_OPERATION_DELETEALL = "CamelCacheDeleteAll";
-    String CACHE_OPERATION_GET = "CamelCacheGet";
-    String CACHE_OPERATION_CHECK = "CamelCacheCheck";
-}
+    String CACHE_OPERATION_ADD = CACHE_HEADER_PREFIX + CACHE_OPERATION_URL_ADD;
+    String CACHE_OPERATION_UPDATE = CACHE_HEADER_PREFIX + CACHE_OPERATION_URL_UPDATE;
+    String CACHE_OPERATION_DELETE = CACHE_HEADER_PREFIX + CACHE_OPERATION_URL_DELETE;
+    String CACHE_OPERATION_DELETEALL = CACHE_HEADER_PREFIX + CACHE_OPERATION_URL_DELETEALL;
+    String CACHE_OPERATION_GET = CACHE_HEADER_PREFIX + CACHE_OPERATION_URL_GET;
+    String CACHE_OPERATION_CHECK = CACHE_HEADER_PREFIX + CACHE_OPERATION_URL_CHECK;
+}
\ No newline at end of file

Modified: camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheEndpoint.java?rev=1299606&r1=1299605&r2=1299606&view=diff
==============================================================================
--- camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheEndpoint.java (original)
+++ camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheEndpoint.java Mon Mar 12 10:07:35 2012
@@ -39,6 +39,9 @@ public class CacheEndpoint extends Defau
     private CacheConfiguration config;
     private CacheManagerFactory cacheManagerFactory;
 
+    private String operation;
+    private String key;
+
     public CacheEndpoint() {
     }
 
@@ -146,4 +149,20 @@ public class CacheEndpoint extends Defau
         CacheManager cacheManager = getCacheManagerFactory().getInstance();
         cacheManager.removeCache(config.getCacheName());
     }
+
+    public String getOperation() {
+        return operation;
+    }
+
+    public void setOperation(String operation) {
+        this.operation = operation;
+    }
+
+    public String getKey() {
+        return key;
+    }
+
+    public void setKey(String key) {
+        this.key = key;
+    }
 }

Modified: camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheProducer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheProducer.java?rev=1299606&r1=1299605&r2=1299606&view=diff
==============================================================================
--- camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheProducer.java (original)
+++ camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheProducer.java Mon Mar 12 10:07:35 2012
@@ -18,12 +18,12 @@ package org.apache.camel.component.cache
 
 import java.io.InputStream;
 import java.io.Serializable;
+import java.util.Map;
 
 import net.sf.ehcache.CacheException;
 import net.sf.ehcache.Ehcache;
 import net.sf.ehcache.Element;
 
-import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.NoTypeConversionAvailableException;
 import org.apache.camel.impl.DefaultProducer;
@@ -35,7 +35,7 @@ public class CacheProducer extends Defau
     private CacheConfiguration config;
     private Ehcache cache;
 
-    public CacheProducer(Endpoint endpoint, CacheConfiguration config) throws Exception {
+    public CacheProducer(CacheEndpoint endpoint, CacheConfiguration config) throws Exception {
         super(endpoint);
         this.config = config;
     }
@@ -55,18 +55,22 @@ public class CacheProducer extends Defau
 
         cache = getEndpoint().initializeCache();
 
-        String key = exchange.getIn().getHeader(CacheConstants.CACHE_KEY, String.class);
-        String operation = exchange.getIn().getHeader(CacheConstants.CACHE_OPERATION, String.class);
+        Map<String, Object> headers = exchange.getIn().getHeaders();
+        String key = (headers.containsKey(CacheConstants.CACHE_KEY)) ? (String) headers
+                .get(CacheConstants.CACHE_KEY) : getEndpoint().getKey();
+
+        String operation = (headers.containsKey(CacheConstants.CACHE_OPERATION)) ? (String) headers
+                .get(CacheConstants.CACHE_OPERATION) : getEndpoint().getOperation();
 
         if (operation == null) {
             throw new CacheException(CacheConstants.CACHE_OPERATION + " header not specified in message");
         }
-        if ((key == null) && (!operation.equalsIgnoreCase(CacheConstants.CACHE_OPERATION_DELETEALL))) {
+        if ((key == null) && (!checkIsEqual(operation, CacheConstants.CACHE_OPERATION_DELETEALL))) {
             throw new CacheException(CacheConstants.CACHE_KEY + " is not specified in message header or endpoint URL.");
         }
 
         performCacheOperation(exchange, operation, key);
-        
+
         //cleanup the cache headers
         exchange.getIn().removeHeader(CacheConstants.CACHE_KEY);
         exchange.getIn().removeHeader(CacheConstants.CACHE_OPERATION);
@@ -75,21 +79,21 @@ public class CacheProducer extends Defau
     private void performCacheOperation(Exchange exchange, String operation, String key) throws Exception {
         Object element;
 
-        if (operation.equalsIgnoreCase(CacheConstants.CACHE_OPERATION_ADD)) {
+        if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_ADD)) {
             LOG.debug("Adding an element with key {} into the Cache", key);
             element = createElementFromBody(exchange, CacheConstants.CACHE_OPERATION_ADD);
             cache.put(new Element(key, element));
-        } else if (operation.equalsIgnoreCase(CacheConstants.CACHE_OPERATION_UPDATE)) {
+        } else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_UPDATE)) {
             LOG.debug("Updating an element with key {} into the Cache", key);
             element = createElementFromBody(exchange, CacheConstants.CACHE_OPERATION_UPDATE);
             cache.put(new Element(key, element));
-        } else if (operation.equalsIgnoreCase(CacheConstants.CACHE_OPERATION_DELETEALL)) {
+        } else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_DELETEALL)) {
             LOG.debug("Deleting All elements from the Cache");
             cache.removeAll();
-        } else if (operation.equalsIgnoreCase(CacheConstants.CACHE_OPERATION_DELETE)) {
+        } else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_DELETE)) {
             LOG.debug("Deleting an element with key {} into the Cache", key);
             cache.remove(key);
-        } else if (operation.equalsIgnoreCase(CacheConstants.CACHE_OPERATION_GET)) {
+        } else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_GET)) {
             LOG.debug("Quering an element with key {} from the Cache", key);
             if (cache.isKeyInCache(key) && cache.get(key) != null) {
                 exchange.getIn().setHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND, true);
@@ -97,7 +101,7 @@ public class CacheProducer extends Defau
             } else {
                 exchange.getIn().removeHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND);
             }
-        } else if (operation.equalsIgnoreCase(CacheConstants.CACHE_OPERATION_CHECK)) {
+        } else if (checkIsEqual(operation, CacheConstants.CACHE_OPERATION_URL_CHECK)) {
             LOG.debug("Querying an element with key {} from the Cache", key);
             if (cache.isKeyInCache(key)) {
                 exchange.getIn().setHeader(CacheConstants.CACHE_ELEMENT_WAS_FOUND, true);
@@ -109,6 +113,12 @@ public class CacheProducer extends Defau
         }
     }
 
+    private boolean checkIsEqual(String operation, String constant) {
+        return operation.equalsIgnoreCase(constant)
+                || operation.equalsIgnoreCase(CacheConstants.CACHE_HEADER_PREFIX + constant);
+    }
+
+
     private Object createElementFromBody(Exchange exchange, String cacheOperation) throws NoTypeConversionAvailableException {
         Object element;
         Object body = exchange.getIn().getBody();

Modified: camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheProducerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheProducerTest.java?rev=1299606&r1=1299605&r2=1299606&view=diff
==============================================================================
--- camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheProducerTest.java (original)
+++ camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheProducerTest.java Mon Mar 12 10:07:35 2012
@@ -37,6 +37,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class CacheProducerTest extends CamelTestSupport {
+    private static final Poetry POETRY;
 
     private static final String FILEPATH_UPDATEDTEST_TXT = "./src/test/resources/updatedtest.txt";
 
@@ -50,6 +51,12 @@ public class CacheProducerTest extends C
     @EndpointInject(uri = "mock:CacheProducerTest.cacheException")
     protected MockEndpoint cacheExceptionEndpoint;
 
+    static {
+        POETRY = new Poetry();
+        POETRY.setPoet("Ralph Waldo Emerson");
+        POETRY.setPoem("Brahma");
+    }
+
     @Override
     public boolean isUseRouteBuilder() {
         return false;
@@ -101,14 +108,11 @@ public class CacheProducerTest extends C
     private void sendSerializedData() throws Exception {
         template.send("direct:a", new Processor() {
             public void process(Exchange exchange) throws Exception {
-                Poetry p = new Poetry();
-                p.setPoet("Ralph Waldo Emerson");
-                p.setPoem("Brahma");
 
                 // Set the property of the charset encoding
                 exchange.setProperty(Exchange.CHARSET_NAME, "UTF-8");
                 Message in = exchange.getIn();
-                in.setBody(p);
+                in.setBody(POETRY);
             }
         });
     }
@@ -143,10 +147,12 @@ public class CacheProducerTest extends C
                         to("cache://TestCache1");
             }
         });
+        resultEndpoint.expectedMessageCount(0);
         cacheExceptionEndpoint.expectedMessageCount(1);
         context.start();
         LOG.debug("------------Beginning CacheProducer Add Does Fail On Empty Body Test---------------");
         sendEmptyBody();
+        resultEndpoint.assertIsSatisfied();
         cacheExceptionEndpoint.assertIsSatisfied();
     }
 
@@ -415,4 +421,95 @@ public class CacheProducerTest extends C
         resultEndpoint.assertIsSatisfied();
         cacheExceptionEndpoint.assertIsSatisfied();
     }
+
+    @Test
+    public void testQueringDataFromCacheUsingUrlParameters() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                onException(CacheException.class).
+                        handled(true).
+                        to("log:LOGGER").
+                        to("mock:CacheProducerTest.cacheException");
+
+                from("direct:a").
+                        to("cache://TestCache1?operation=add&key=foo").
+                        setBody(constant("Don't care. This body will be overridden.")).
+                        to("cache://TestCache1?operation=get&key=foo").
+                        choice().when(header(CacheConstants.CACHE_ELEMENT_WAS_FOUND).isNotNull()).
+                        to("mock:CacheProducerTest.result").end();
+            }
+        });
+
+        resultEndpoint.expectedMessageCount(1);
+        cacheExceptionEndpoint.expectedMessageCount(0);
+        resultEndpoint.expectedBodiesReceived(POETRY);
+        context.start();
+        LOG.debug("------------Beginning CacheProducer Query An Elements Test---------------");
+        sendSerializedData();
+        resultEndpoint.assertIsSatisfied();
+        cacheExceptionEndpoint.assertIsSatisfied();
+    }
+
+    @Test
+    public void testQueringDataFromCacheUsingUrlParametersMixed() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                onException(CacheException.class).
+                        handled(true).
+                        to("log:LOGGER").
+                        to("mock:CacheProducerTest.cacheException");
+
+                from("direct:a").
+                        setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_ADD)).
+                        to("cache://TestCache1?key=foo").
+                        setBody(constant("Don't care. This body will be overridden.")).
+                        setHeader(CacheConstants.CACHE_KEY, constant("foo")).
+                        to("cache://TestCache1?operation=get").
+                        choice().when(header(CacheConstants.CACHE_ELEMENT_WAS_FOUND).isNotNull()).
+                        to("mock:CacheProducerTest.result").end();
+            }
+        });
+
+        resultEndpoint.expectedMessageCount(1);
+        cacheExceptionEndpoint.expectedMessageCount(0);
+        resultEndpoint.expectedBodiesReceived(POETRY);
+        context.start();
+        LOG.debug("------------Beginning CacheProducer Query An Elements Test---------------");
+        sendSerializedData();
+        resultEndpoint.assertIsSatisfied();
+        cacheExceptionEndpoint.assertIsSatisfied();
+    }
+
+    @Test
+    public void testQueringDataFromCacheUsingUrlParametersOverrided() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            public void configure() {
+                onException(CacheException.class).
+                        handled(true).
+                        to("log:LOGGER").
+                        to("mock:CacheProducerTest.cacheException");
+
+                from("direct:a").
+                        setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_ADD)).
+                        setHeader(CacheConstants.CACHE_KEY, constant("foo")).
+                        to("cache://TestCache1?operation=get&key=bar").
+                        setBody(constant("Don't care. This body will be overridden.")).
+                        setHeader(CacheConstants.CACHE_OPERATION, constant(CacheConstants.CACHE_OPERATION_GET)).
+                        setHeader(CacheConstants.CACHE_KEY, constant("foo")).
+                        to("cache://TestCache1?operation=delete&key=Piotr_Klimczak").
+                        choice().when(header(CacheConstants.CACHE_ELEMENT_WAS_FOUND).isNotNull()).
+                        to("mock:CacheProducerTest.result").end();
+            }
+        });
+
+        resultEndpoint.expectedMessageCount(1);
+        cacheExceptionEndpoint.expectedMessageCount(0);
+        resultEndpoint.expectedBodiesReceived(POETRY);
+        context.start();
+        LOG.debug("------------Beginning CacheProducer Query An Elements Test---------------");
+        sendSerializedData();
+        resultEndpoint.assertIsSatisfied();
+        cacheExceptionEndpoint.assertIsSatisfied();
+    }
+
 }