Repository: camel
Updated Branches:
  refs/heads/master 8170cbaef -> 1b164db2f


camel-ehcache: improve cache manager


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

Branch: refs/heads/master
Commit: 1b164db2f951394b8f3af8f99610926f148dfbde
Parents: 8170cba
Author: lburgazzoli <lburgazz...@gmail.com>
Authored: Tue May 16 17:49:30 2017 +0200
Committer: lburgazzoli <lburgazz...@gmail.com>
Committed: Tue May 16 17:49:30 2017 +0200

----------------------------------------------------------------------
 .../component/ehcache/EhcacheComponent.java     | 64 +++++++++++++++--
 .../component/ehcache/EhcacheEndpoint.java      |  4 +-
 .../camel/component/ehcache/EhcacheManager.java | 74 +-------------------
 .../idempotent/EhcacheIdempotentRepository.java |  2 +-
 4 files changed, 61 insertions(+), 83 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1b164db2/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheComponent.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheComponent.java
 
b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheComponent.java
index 5c6f5ca..a92fac5 100644
--- 
a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheComponent.java
+++ 
b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheComponent.java
@@ -16,20 +16,30 @@
  */
 package org.apache.camel.component.ehcache;
 
+import java.io.IOException;
+import java.net.URL;
 import java.util.Map;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.impl.DefaultComponent;
 import org.apache.camel.spi.Metadata;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ResourceHelper;
 import org.ehcache.CacheManager;
 import org.ehcache.config.CacheConfiguration;
 import org.ehcache.config.Configuration;
+import org.ehcache.config.builders.CacheManagerBuilder;
+import org.ehcache.xml.XmlConfiguration;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Represents the component that manages {@link DefaultComponent}.
  */
 public class EhcacheComponent extends DefaultComponent {
+    private static final Logger LOGGER = 
LoggerFactory.getLogger(EhcacheComponent.class);
+
     @Metadata(label = "advanced")
     private EhcacheConfiguration configuration = new EhcacheConfiguration();
 
@@ -42,16 +52,53 @@ public class EhcacheComponent extends DefaultComponent {
 
     @Override
     protected Endpoint createEndpoint(String uri, String remaining, 
Map<String, Object> parameters) throws Exception {
-        EhcacheConfiguration configuration;
-        if (this.configuration != null) {
-            configuration = this.configuration.copy();
-        } else {
-            configuration = new EhcacheConfiguration();
+        EhcacheConfiguration configuration = this.configuration.copy();
+        setProperties(configuration, parameters);
+
+        return new EhcacheEndpoint(uri, this, remaining, 
createCacheManager(configuration), configuration);
+    }
+
+    // ****************************
+    // Helpers
+    // ****************************
+
+    private EhcacheManager createCacheManager(EhcacheConfiguration 
configuration) throws IOException {
+        ObjectHelper.notNull(configuration, "Camel Ehcache configuration");
+
+        // Check if a cache manager has been configured
+        CacheManager manager = configuration.getCacheManager();
+        if (manager != null) {
+            LOGGER.info("EhcacheManager configured with supplied 
CacheManager");
+            return new EhcacheManager(manager, false, configuration);
         }
 
-        setProperties(configuration, parameters);
+        // Check if a cache manager configuration has been provided
+        if (configuration.hasCacheManagerConfiguration()) {
+            LOGGER.info("EhcacheManager configured with supplied 
CacheManagerConfiguration");
+
+            return new EhcacheManager(
+                
CacheManagerBuilder.newCacheManager(configuration.getCacheManagerConfiguration()),
+                true,
+                configuration
+            );
+        }
+
+        // Check if a configuration file has been provided
+        if (configuration.hasConfigurationUri()) {
+            String configurationUri = configuration.getConfigurationUri();
+            URL url = 
ResourceHelper.resolveMandatoryResourceAsUrl(getCamelContext().getClassResolver(),
 configurationUri);
 
-        return new EhcacheEndpoint(uri, this, remaining, configuration);
+            LOGGER.info("EhcacheManager configured with supplied URI {}", url);
+
+            return new EhcacheManager(
+                CacheManagerBuilder.newCacheManager(new XmlConfiguration(url)),
+                true,
+                configuration
+            );
+        }
+
+        LOGGER.info("EhcacheManager configured with default builder");
+        return new 
EhcacheManager(CacheManagerBuilder.newCacheManagerBuilder().build(), true, 
configuration);
     }
 
     // ****************************
@@ -66,6 +113,9 @@ public class EhcacheComponent extends DefaultComponent {
      * Sets the global component configuration
      */
     public void setConfiguration(EhcacheConfiguration configuration) {
+        // The component configuration can't be null
+        ObjectHelper.notNull(configuration, "EhcacheConfiguration");
+
         this.configuration = configuration;
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/1b164db2/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheEndpoint.java
----------------------------------------------------------------------
diff --git 
a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheEndpoint.java
 
b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheEndpoint.java
index 91fec1d..878ef16 100644
--- 
a/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheEndpoint.java
+++ 
b/components/camel-ehcache/src/main/java/org/apache/camel/component/ehcache/EhcacheEndpoint.java
@@ -37,12 +37,12 @@ public class EhcacheEndpoint extends DefaultEndpoint {
     private final EhcacheConfiguration configuration;
     private final EhcacheManager cacheManager;
 
-    EhcacheEndpoint(String uri, EhcacheComponent component,  String cacheName, 
EhcacheConfiguration configuration) throws Exception {
+    EhcacheEndpoint(String uri, EhcacheComponent component,  String cacheName, 
EhcacheManager cacheManager, EhcacheConfiguration configuration) throws 
Exception {
         super(uri, component);
 
         this.cacheName = cacheName;
         this.configuration = configuration;
-        this.cacheManager = new EhcacheManager(cacheName, configuration, 
component.getCamelContext());
+        this.cacheManager = cacheManager;
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/camel/blob/1b164db2/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 926e141..90ebd60 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
@@ -16,51 +16,25 @@
  */
 package org.apache.camel.component.ehcache;
 
-import java.io.IOException;
-import java.net.URL;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
-import org.apache.camel.CamelContext;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.Service;
 import org.apache.camel.util.ObjectHelper;
-import org.apache.camel.util.ResourceHelper;
 import org.ehcache.Cache;
 import org.ehcache.CacheManager;
 import org.ehcache.UserManagedCache;
 import org.ehcache.config.CacheConfiguration;
-import org.ehcache.config.builders.CacheManagerBuilder;
 import org.ehcache.config.builders.UserManagedCacheBuilder;
-import org.ehcache.xml.XmlConfiguration;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 public class EhcacheManager implements Service {
-    private static final Logger LOGGER = 
LoggerFactory.getLogger(EhcacheManager.class);
-
     private final EhcacheConfiguration configuration;
     private final CacheManager cacheManager;
     private final ConcurrentMap<String, UserManagedCache<?, ?>> userCaches;
     private final boolean managed;
 
-    public EhcacheManager(String cacheName, EhcacheConfiguration 
configuration) throws IOException {
-        this(cacheName, configuration, null);
-    }
-
-    public EhcacheManager(String cacheName, EhcacheConfiguration 
configuration, CamelContext camelContext) throws IOException {
-        this(createCacheManager(cacheName, configuration, camelContext), 
!configuration.hasCacheManager(), configuration);
-    }
-
-    public EhcacheManager(CacheManager cacheManager) {
-        this(cacheManager, false, null);
-    }
-
-    public EhcacheManager(CacheManager cacheManager, boolean managed) {
-        this(cacheManager, managed, null);
-    }
-
-    private EhcacheManager(CacheManager cacheManager, boolean managed, 
EhcacheConfiguration configuration) {
+    public EhcacheManager(CacheManager cacheManager, boolean managed, 
EhcacheConfiguration configuration) {
         this.cacheManager = cacheManager;
         this.userCaches = new ConcurrentHashMap<>();
         this.managed = managed;
@@ -114,50 +88,4 @@ public class EhcacheManager implements Service {
     CacheManager getCacheManager() {
         return this.cacheManager;
     }
-
-    // *************************************************
-    //
-    // *************************************************
-
-    private static CacheManager createCacheManager(String cacheName, 
EhcacheConfiguration configuration) throws IOException {
-        return createCacheManager(cacheName, configuration, null);
-    }
-
-    private  static CacheManager createCacheManager(String cacheName, 
EhcacheConfiguration configuration, CamelContext camelContext) throws 
IOException {
-        ObjectHelper.notNull(cacheName, "Ehcache cacheName");
-        ObjectHelper.notNull(configuration, "Camel Ehcache configuration");
-
-        // Check if a cache manager has been configured
-        CacheManager manager = configuration.getCacheManager();
-        if (manager != null) {
-            LOGGER.info("EhcacheManager configured with supplied 
CacheManager");
-            return manager;
-        }
-
-        // Check if a cache manager configuration has been provided
-        if (configuration.hasCacheManagerConfiguration()) {
-            LOGGER.info("EhcacheManager configured with supplied 
CacheManagerConfiguration");
-            return 
CacheManagerBuilder.newCacheManager(configuration.getCacheManagerConfiguration());
-        }
-
-        // Check if a configuration file has been provided
-        if (configuration.hasConfigurationUri()) {
-            String configurationUri = configuration.getConfigurationUri();
-            URL url = camelContext != null
-                ? 
ResourceHelper.resolveMandatoryResourceAsUrl(camelContext.getClassResolver(), 
configurationUri)
-                : new URL(configurationUri);
-
-            LOGGER.info("EhcacheManager configured with supplied URI {}", url);
-            return CacheManagerBuilder.newCacheManager(new 
XmlConfiguration(url));
-        }
-
-        // Create a cache manager using a builder
-        CacheManagerBuilder builder = 
CacheManagerBuilder.newCacheManagerBuilder();
-        if (configuration.getConfiguration() != null) {
-            LOGGER.info("Ehcache {} configured with custom 
CacheConfiguration", cacheName);
-            builder.withCache(cacheName, configuration.getConfiguration());
-        }
-
-        return builder.build();
-    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/1b164db2/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 d154b9f..9d5e639 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
@@ -38,7 +38,7 @@ public class EhcacheIdempotentRepository extends 
ServiceSupport implements Idemp
 
     public EhcacheIdempotentRepository(CacheManager cacheManager, String 
repositoryName) {
         this.cacheName = repositoryName;
-        this.cacheManager = new EhcacheManager(cacheManager);
+        this.cacheManager = new EhcacheManager(cacheManager, false, null);
     }
 
     @ManagedAttribute(description = "The processor name")

Reply via email to