camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject camel git commit: CAMEL-8730 Camel-Hazelcast: Add replace operation to map producer
Date Sat, 02 May 2015 09:45:41 GMT
Repository: camel
Updated Branches:
  refs/heads/master e63deab9a -> 94ce9de81


CAMEL-8730 Camel-Hazelcast: Add replace operation to map producer


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/94ce9de8
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/94ce9de8
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/94ce9de8

Branch: refs/heads/master
Commit: 94ce9de81ccea6ddf0e3e346537f547e32bffe3d
Parents: e63deab
Author: Andrea Cosentino <ancosen@gmail.com>
Authored: Sat May 2 11:43:37 2015 +0200
Committer: Andrea Cosentino <ancosen@gmail.com>
Committed: Sat May 2 11:43:37 2015 +0200

----------------------------------------------------------------------
 .../component/hazelcast/HazelcastConstants.java |  4 ++-
 .../hazelcast/map/HazelcastMapProducer.java     | 29 ++++++++++++++++++++
 .../hazelcast/HazelcastMapProducerTest.java     | 17 ++++++++++++
 3 files changed, 49 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/94ce9de8/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
index 3caa167..7757f74 100644
--- a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
+++ b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/HazelcastConstants.java
@@ -35,6 +35,7 @@ public final class HazelcastConstants {
      */
     public static final String OBJECT_ID = "CamelHazelcastObjectId";
     public static final String OBJECT_POS = "CamelHazelcastObjectIndex";
+    public static final String OBJECT_VALUE = "CamelHazelcastObjectValue";
     public static final String QUERY = "CamelHazelcastQuery";
 
     /*
@@ -48,7 +49,7 @@ public final class HazelcastConstants {
     public static final String CACHE_NAME = "CamelHazelcastCacheName";
     public static final String CACHE_TYPE = "CamelHazelcastCacheType";
 
-    // actions (put, delete, get, update)
+    // actions (put, delete, get, getAll, replace, update)
     public static final String OPERATION = "CamelHazelcastOperationType";
     public static final int PUT_OPERATION = 1;
     public static final int DELETE_OPERATION = 2;
@@ -56,6 +57,7 @@ public final class HazelcastConstants {
     public static final int UPDATE_OPERATION = 4;
     public static final int QUERY_OPERATION = 5;
     public static final int GET_ALL_OPERATION = 6;
+    public static final int REPLACE_OPERATION = 7;
 
     // multimap
     public static final int REMOVEVALUE_OPERATION = 10;

http://git-wip-us.apache.org/repos/asf/camel/blob/94ce9de8/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/map/HazelcastMapProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/map/HazelcastMapProducer.java
b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/map/HazelcastMapProducer.java
index ec453a7..8b6cd69 100644
--- a/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/map/HazelcastMapProducer.java
+++ b/components/camel-hazelcast/src/main/java/org/apache/camel/component/hazelcast/map/HazelcastMapProducer.java
@@ -45,11 +45,16 @@ public class HazelcastMapProducer extends HazelcastDefaultProducer {
 
         // get header parameters
         Object oid = null;
+        Object ovalue = null;
         String query = null;
 
         if (headers.containsKey(HazelcastConstants.OBJECT_ID)) {
             oid = headers.get(HazelcastConstants.OBJECT_ID);
         }
+        
+        if (headers.containsKey(HazelcastConstants.OBJECT_VALUE)) {
+            ovalue = headers.get(HazelcastConstants.OBJECT_VALUE);
+        }
 
         if (headers.containsKey(HazelcastConstants.QUERY)) {
             query = (String) headers.get(HazelcastConstants.QUERY);
@@ -82,6 +87,14 @@ public class HazelcastMapProducer extends HazelcastDefaultProducer {
             this.query(query, exchange);
             break;
 
+        case HazelcastConstants.REPLACE_OPERATION:
+            if (ObjectHelper.isEmpty(ovalue)) {
+                this.replace(oid, exchange);
+            } else {
+                this.replace(oid, ovalue, exchange);
+            }
+            break;
+            
         default:
             throw new IllegalArgumentException(String.format("The value '%s' is not allowed
for parameter '%s' on the MAP cache.", operation, HazelcastConstants.OPERATION));
         }
@@ -143,4 +156,20 @@ public class HazelcastMapProducer extends HazelcastDefaultProducer {
         Object body = exchange.getIn().getBody();
         this.cache.put(oid, body);
     }
+    
+    /**
+     * replace a value related to a specific key
+     */
+    private void replace(Object oid, Exchange exchange) {
+        Object body = exchange.getIn().getBody();
+        this.cache.replace(oid, body);
+    }
+    
+    /**
+     * Replaces the entry for given id with a specific value in the body, only if currently
mapped to a given value
+     */
+    private void replace(Object oid, Object ovalue, Exchange exchange) {
+        Object body = exchange.getIn().getBody();
+        this.cache.replace(oid, ovalue, body);
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/94ce9de8/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastMapProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastMapProducerTest.java
b/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastMapProducerTest.java
index 02889af..132c3c0 100644
--- a/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastMapProducerTest.java
+++ b/components/camel-hazelcast/src/test/java/org/apache/camel/component/hazelcast/HazelcastMapProducerTest.java
@@ -157,6 +157,21 @@ public class HazelcastMapProducerTest extends HazelcastCamelTestSupport
implemen
         assertNotNull(b1);
         assertEquals(3, b1.size());
     }
+    
+    @Test
+    public void testReplace() throws InterruptedException {
+        template.sendBodyAndHeader("direct:replace", "replaced", HazelcastConstants.OBJECT_ID,
"4711");
+        verify(map).replace("4711", "replaced");
+    }
+    
+    @Test
+    public void testReplaceOldValue() throws InterruptedException {
+        Map<String, Object> headers = new HashMap<String, Object>();
+        headers.put(HazelcastConstants.OBJECT_ID, "4711");
+        headers.put(HazelcastConstants.OBJECT_VALUE, "my-foo");
+        template.sendBodyAndHeaders("direct:replace", "replaced", headers);
+        verify(map).replace("4711", "my-foo", "replaced");
+    }
 
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
@@ -180,6 +195,8 @@ public class HazelcastMapProducerTest extends HazelcastCamelTestSupport
implemen
 
                 from("direct:query").setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.QUERY_OPERATION)).to(String.format("hazelcast:%sfoo",
HazelcastConstants.MAP_PREFIX))
                         .to("seda:out");
+                
+                from("direct:replace").setHeader(HazelcastConstants.OPERATION, constant(HazelcastConstants.REPLACE_OPERATION)).to(String.format("hazelcast:%sfoo",
HazelcastConstants.MAP_PREFIX));
 
                 from("direct:putWithOperationNumber").toF("hazelcast:%sfoo?operation=%s",
HazelcastConstants.MAP_PREFIX, HazelcastConstants.PUT_OPERATION);
                 from("direct:putWithOperationName").toF("hazelcast:%sfoo?operation=put",
HazelcastConstants.MAP_PREFIX);


Mime
View raw message