Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 3F730200C7D for ; Tue, 16 May 2017 17:49:40 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 3DE52160BAC; Tue, 16 May 2017 15:49:40 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 331EA160B9D for ; Tue, 16 May 2017 17:49:39 +0200 (CEST) Received: (qmail 74099 invoked by uid 500); 16 May 2017 15:49:38 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 74090 invoked by uid 99); 16 May 2017 15:49:38 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 16 May 2017 15:49:38 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 4B5BDDFBC8; Tue, 16 May 2017 15:49:38 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: lburgazzoli@apache.org To: commits@camel.apache.org Message-Id: <3743efd5053d4ecda21cd9c6ff2b347b@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: camel git commit: camel-ehcache: improve cache manager Date: Tue, 16 May 2017 15:49:38 +0000 (UTC) archived-at: Tue, 16 May 2017 15:49:40 -0000 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 Authored: Tue May 16 17:49:30 2017 +0200 Committer: lburgazzoli 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 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> 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")