Repository: camel
Updated Branches:
  refs/heads/master 8ed65a952 -> a9c129695


CAMEL-9884 : add an option to set the key/value type for a cache (default 
Object)


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

Branch: refs/heads/master
Commit: a9c129695c04e19be85e00e744340a5e6cd53fe3
Parents: 8ed65a9
Author: lburgazzoli <lburgazz...@gmail.com>
Authored: Mon May 16 11:15:17 2016 +0200
Committer: lburgazzoli <lburgazz...@gmail.com>
Committed: Mon May 16 11:15:17 2016 +0200

----------------------------------------------------------------------
 .../component/ehcache/EhcacheConfiguration.java | 47 ++++++++++++++++++--
 .../component/ehcache/EhcacheConsumer.java      |  3 +-
 .../camel/component/ehcache/EhcacheManager.java | 14 +++++-
 .../component/ehcache/EhcacheProducer.java      | 19 ++++----
 .../idempotent/EhcacheIdempotentRepository.java |  8 ++--
 5 files changed, 72 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a9c12969/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConfiguration.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConfiguration.java
 
b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConfiguration.java
index 087148a..798f443 100644
--- 
a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConfiguration.java
+++ 
b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConfiguration.java
@@ -22,6 +22,8 @@ import java.util.EnumSet;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.spi.UriParam;
@@ -54,13 +56,18 @@ public class EhcacheConfiguration {
     @UriParam(label = "producer")
     private String action;
     @UriParam(label = "producer")
-    private String key;
+    private Object key;
 
     @UriParam
     private CacheManager cacheManager;
     @UriParam(label = "advanced")
     private CacheConfiguration<?, ?> configuration;
 
+    @UriParam(label = "advanced", javaType = "java.lang.String", defaultValue 
= "java.lang.Object")
+    private Class<?> keyType = Object.class;
+    @UriParam(label = "advanced", javaType = "java.lang.String", defaultValue 
= "java.lang.Object")
+    private Class<?> valueType = Object.class;
+
     @UriParam(
         label = "consumer",
         enums = "ORDERED,UNORDERED",
@@ -69,7 +76,7 @@ public class EhcacheConfiguration {
 
     @UriParam(
         label = "consumer",
-        enums = "ASYNCHRONOUS, SYNCHRONOUS",
+        enums = "ASYNCHRONOUS,SYNCHRONOUS",
         defaultValue = "ASYNCHRONOUS")
     private EventFiring eventFiring = EventFiring.ASYNCHRONOUS;
 
@@ -86,6 +93,8 @@ public class EhcacheConfiguration {
     EhcacheConfiguration(CamelContext context, String cacheName) {
         this.context = context;
         this.cacheName = cacheName;
+
+        
Stream.of(EventType.values()).map(EventType::name).collect(Collectors.joining(",
 "));
     }
 
     public CamelContext getContext() {
@@ -137,7 +146,7 @@ public class EhcacheConfiguration {
         this.action = action;
     }
 
-    public String getKey() {
+    public Object getKey() {
         return key;
     }
 
@@ -145,7 +154,7 @@ public class EhcacheConfiguration {
      * To configure the default action key. If a key is set in the message
      * header, then the key from the header takes precedence.
      */
-    public void setKey(String key) {
+    public void setKey(Object key) {
         this.key = key;
     }
 
@@ -234,6 +243,36 @@ public class EhcacheConfiguration {
         return ObjectHelper.notNull(getConfiguration(), "CacheConfiguration");
     }
 
+    public Class<?> getKeyType() {
+        return keyType;
+    }
+
+    /**
+     * The cache key type, default Object.class
+     */
+    public void setKeyType(Class<?> keyType) {
+        this.keyType = keyType;
+    }
+
+    public void setKeyType(String keyType) throws ClassNotFoundException {
+        setKeyType(context.getClassResolver().resolveMandatoryClass(keyType));
+    }
+
+    public Class<?> getValueType() {
+        return valueType;
+    }
+
+    /**
+     * The cache value type, default Object.class
+     */
+    public void setValueType(Class<?> valueType) {
+        this.valueType = valueType;
+    }
+
+    public void setValueType(String valueType) throws ClassNotFoundException {
+        
setValueType(context.getClassResolver().resolveMandatoryClass(valueType));
+    }
+
     // ****************************
     // Helpers
     // ****************************

http://git-wip-us.apache.org/repos/asf/camel/blob/a9c12969/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConsumer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConsumer.java
 
b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConsumer.java
index 0098974..18629fd 100644
--- 
a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConsumer.java
+++ 
b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheConsumer.java
@@ -27,8 +27,9 @@ import org.ehcache.event.CacheEventListener;
 public class EhcacheConsumer extends DefaultConsumer implements 
CacheEventListener<Object, Object> {
     private final EhcacheConfiguration configuration;
     private final EhcacheManager manager;
-    private final Cache<Object, Object> cache;
+    private final Cache cache;
 
+    @SuppressWarnings("unchecked")
     public EhcacheConsumer(EhcacheEndpoint endpoint, EhcacheConfiguration 
configuration, Processor processor) throws Exception {
         super(endpoint, processor);
 

http://git-wip-us.apache.org/repos/asf/camel/blob/a9c12969/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheManager.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheManager.java
 
b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheManager.java
index 9fa24ee..091af93 100644
--- 
a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheManager.java
+++ 
b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheManager.java
@@ -75,9 +75,19 @@ public class EhcacheManager implements Service {
         return cache;
     }
 
-    public Cache<Object, Object> getCache() throws Exception  {
+    public Cache<?, ?> getCache(String name) throws Exception {
+        return getCache(
+            name,
+            configuration.getKeyType(),
+            configuration.getValueType());
+    }
+
+    public Cache<?, ?> getCache() throws Exception  {
         ObjectHelper.notNull(configuration, "Ehcache configuration");
 
-        return getCache(configuration.getCacheName(), Object.class, 
Object.class);
+        return getCache(
+            configuration.getCacheName(),
+            configuration.getKeyType(),
+            configuration.getValueType());
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/a9c12969/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheProducer.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheProducer.java
 
b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheProducer.java
index 5967270..fd2b1f3 100644
--- 
a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheProducer.java
+++ 
b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheProducer.java
@@ -29,8 +29,9 @@ import org.ehcache.Cache;
 public class EhcacheProducer extends DefaultProducer {
     private final EhcacheConfiguration configuration;
     private final EhcacheManager manager;
-    private final Cache<Object, Object> cache;
+    private final Cache cache;
 
+    @SuppressWarnings("unchecked")
     public EhcacheProducer(EhcacheEndpoint endpoint, EhcacheConfiguration 
configuration) throws Exception {
         super(endpoint);
 
@@ -92,19 +93,19 @@ public class EhcacheProducer extends DefaultProducer {
     }
 
     private void onPut(Message message) throws Exception {
-        cache.put(getKey(message), getValue(message, Object.class));
+        cache.put(getKey(message), getValue(message, 
configuration.getValueType()));
 
         setResult(message, true, null, null);
     }
 
     private void onPutAll(Message message) throws Exception {
-        cache.putAll(getValue(message, Map.class));
+        cache.putAll((Map)getValue(message, Map.class));
 
         setResult(message, true, null, null);
     }
 
     private void onPutIfAbsent(Message message) throws Exception {
-        Object oldValue = cache.putIfAbsent(getKey(message), getValue(message, 
Object.class));
+        Object oldValue = cache.putIfAbsent(getKey(message), getValue(message, 
configuration.getValueType()));
 
         setResult(message, true, null, oldValue);
     }
@@ -142,7 +143,7 @@ public class EhcacheProducer extends DefaultProducer {
     private void onReplace(Message message) throws Exception {
         boolean success = true;
         Object oldValue = null;
-        Object value = getValue(message, Object.class);
+        Object value = getValue(message, configuration.getValueType());
         Object valueToReplace = message.getHeader(EhcacheConstants.OLD_VALUE);
         if (valueToReplace == null) {
             oldValue = cache.replace(getKey(message), value);
@@ -157,8 +158,8 @@ public class EhcacheProducer extends DefaultProducer {
     // Helpers
     // ****************************
 
-    private String getKey(final Message message) throws Exception {
-        String value = message.getHeader(EhcacheConstants.KEY, String.class);
+    private Object getKey(final Message message) throws Exception {
+        Object value = message.getHeader(EhcacheConstants.KEY, 
configuration.getKeyType());
         if (value == null) {
             value = configuration.getKey();
         }
@@ -173,8 +174,8 @@ public class EhcacheProducer extends DefaultProducer {
         return value;
     }
 
-    private <T> T getValue(final Message message, final Class<T> type)  throws 
Exception {
-        T value = message.getHeader(EhcacheConstants.VALUE, type);
+    private Object getValue(final Message message, final Class<?> type)  
throws Exception {
+        Object value = message.getHeader(EhcacheConstants.VALUE, type);
         if (value == null) {
             value = message.getBody(type);
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/a9c12969/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/processor/idempotent/EhcacheIdempotentRepository.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/processor/idempotent/EhcacheIdempotentRepository.java
 
b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/processor/idempotent/EhcacheIdempotentRepository.java
index 41a5456..d154b9f 100644
--- 
a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/processor/idempotent/EhcacheIdempotentRepository.java
+++ 
b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/processor/idempotent/EhcacheIdempotentRepository.java
@@ -19,6 +19,7 @@ package 
org.apache.camel.component.ehcache.processor.idempotent;
 import org.apache.camel.api.management.ManagedAttribute;
 import org.apache.camel.api.management.ManagedOperation;
 import org.apache.camel.api.management.ManagedResource;
+import org.apache.camel.component.ehcache.EhcacheManager;
 import org.apache.camel.spi.IdempotentRepository;
 import org.apache.camel.support.ServiceSupport;
 import org.ehcache.Cache;
@@ -29,7 +30,7 @@ public class EhcacheIdempotentRepository extends 
ServiceSupport implements Idemp
 
     private String cacheName;
     private Cache<String, Boolean> cache;
-    private CacheManager cacheManager;
+    private EhcacheManager cacheManager;
 
     public EhcacheIdempotentRepository(CacheManager cacheManager) {
         this(cacheManager, EhcacheIdempotentRepository.class.getSimpleName());
@@ -37,7 +38,7 @@ public class EhcacheIdempotentRepository extends 
ServiceSupport implements Idemp
 
     public EhcacheIdempotentRepository(CacheManager cacheManager, String 
repositoryName) {
         this.cacheName = repositoryName;
-        this.cacheManager = cacheManager;
+        this.cacheManager = new EhcacheManager(cacheManager);
     }
 
     @ManagedAttribute(description = "The processor name")
@@ -77,11 +78,12 @@ public class EhcacheIdempotentRepository extends 
ServiceSupport implements Idemp
 
     @Override
     protected void doStart() throws Exception {
+        cacheManager.start();
         cache = cacheManager.getCache(cacheName, String.class, Boolean.class);
     }
 
     @Override
     protected void doStop() throws Exception {
-        // noop
+        cacheManager.stop();
     }
 }
\ No newline at end of file

Reply via email to