Repository: camel Updated Branches: refs/heads/master d2d562f47 -> 8170cbaef
camel-ehcache: add an option to programmatically provide a CacheManager configuration object Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8170cbae Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8170cbae Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8170cbae Branch: refs/heads/master Commit: 8170cbaef802c697c49d9a8cd2ee223c5971922c Parents: d2d562f Author: lburgazzoli <lburgazz...@gmail.com> Authored: Tue May 16 17:24:26 2017 +0200 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Tue May 16 17:24:36 2017 +0200 ---------------------------------------------------------------------- .../src/main/docs/ehcache-component.adoc | 6 ++- .../component/ehcache/EhcacheComponent.java | 16 ++++++- .../component/ehcache/EhcacheConfiguration.java | 22 ++++++++++ .../camel/component/ehcache/EhcacheManager.java | 45 +++++++++++++------- .../EhcacheComponentConfiguration.java | 28 ++++++++++++ 5 files changed, 98 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/8170cbae/components/camel-ehcache/src/main/docs/ehcache-component.adoc ---------------------------------------------------------------------- diff --git a/components/camel-ehcache/src/main/docs/ehcache-component.adoc b/components/camel-ehcache/src/main/docs/ehcache-component.adoc index 75c576a..78b5f9a 100644 --- a/components/camel-ehcache/src/main/docs/ehcache-component.adoc +++ b/components/camel-ehcache/src/main/docs/ehcache-component.adoc @@ -37,7 +37,7 @@ format, `?option=value&option=#beanRef&...` // component options: START -The Ehcache component supports 5 options which are listed below. +The Ehcache component supports 6 options which are listed below. @@ -46,6 +46,7 @@ The Ehcache component supports 5 options which are listed below. | Name | Description | Default | Type | **configuration** (advanced) | Sets the global component configuration | | EhcacheConfiguration | **cacheManager** (common) | The cache manager | | CacheManager +| **cacheManager Configuration** (common) | The cache manager configuration | | Configuration | **cacheConfiguration** (common) | The default cache configuration to be used to create caches. | | CacheConfiguration<K, V> | **cacheConfigurationUri** (common) | URI pointing to the Ehcache XML configuration file's location | | String | **resolveProperty Placeholders** (advanced) | Whether the component should resolve property placeholders on itself when starting. Only properties which are of String type can use property placeholders. | true | boolean @@ -69,12 +70,13 @@ with the following path and query parameters: | **cacheName** | *Required* the cache name | | String |======================================================================= -#### Query Parameters (15 parameters): +#### Query Parameters (16 parameters): [width="100%",cols="2,5,^1,2",options="header"] |======================================================================= | Name | Description | Default | Type | **cacheManager** (common) | The cache manager | | CacheManager +| **cacheManagerConfiguration** (common) | The cache manager configuration | | Configuration | **configurationUri** (common) | URI pointing to the Ehcache XML configuration file's location | | String | **createCacheIfNotExist** (common) | Configure if a cache need to be created if it does exist or can't be pre-configured. | true | boolean | **bridgeErrorHandler** (consumer) | Allows for bridging the consumer to the Camel routing Error Handler which mean any exceptions occurred while the consumer is trying to pickup incoming messages or the likes will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions that will be logged at WARN or ERROR level and ignored. | false | boolean http://git-wip-us.apache.org/repos/asf/camel/blob/8170cbae/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 0a32465..5c6f5ca 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 @@ -24,6 +24,7 @@ import org.apache.camel.impl.DefaultComponent; import org.apache.camel.spi.Metadata; import org.ehcache.CacheManager; import org.ehcache.config.CacheConfiguration; +import org.ehcache.config.Configuration; /** * Represents the component that manages {@link DefaultComponent}. @@ -79,6 +80,17 @@ public class EhcacheComponent extends DefaultComponent { this.configuration.setCacheManager(cacheManager); } + public Configuration getCacheManagerConfiguration() { + return configuration.getCacheManagerConfiguration(); + } + + /** + * The cache manager configuration + */ + public void setCacheManagerConfiguration(Configuration cacheManagerConfiguration) { + this.configuration.setCacheManagerConfiguration(cacheManagerConfiguration); + } + /** * The default cache configuration to be used to create caches. */ @@ -91,13 +103,13 @@ public class EhcacheComponent extends DefaultComponent { } public String getCacheConfigurationUri() { - return this.configuration.getConfigUri(); + return this.configuration.getConfigurationUri(); } /** * URI pointing to the Ehcache XML configuration file's location */ public void setCacheConfigurationUri(String configurationUri) { - this.configuration.setConfigUri(configurationUri); + this.configuration.setConfigurationUri(configurationUri); } } http://git-wip-us.apache.org/repos/asf/camel/blob/8170cbae/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 c2ed23d..4e63c0e 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 @@ -26,6 +26,7 @@ import org.apache.camel.spi.UriParams; import org.apache.camel.util.ObjectHelper; import org.ehcache.CacheManager; import org.ehcache.config.CacheConfiguration; +import org.ehcache.config.Configuration; import org.ehcache.event.EventFiring; import org.ehcache.event.EventOrdering; import org.ehcache.event.EventType; @@ -41,6 +42,8 @@ public class EhcacheConfiguration implements Cloneable { @UriParam private CacheManager cacheManager; @UriParam + private Configuration cacheManagerConfiguration; + @UriParam private String configurationUri; @UriParam(label = "advanced") private CacheConfiguration<?, ?> configuration; @@ -69,6 +72,10 @@ public class EhcacheConfiguration implements Cloneable { return configurationUri; } + public boolean hasConfigurationUri() { + return ObjectHelper.isNotEmpty(configurationUri); + } + /** * @deprecated use {@link #getConfigurationUri()} instead */ @@ -138,6 +145,21 @@ public class EhcacheConfiguration implements Cloneable { return this.cacheManager != null; } + public Configuration getCacheManagerConfiguration() { + return cacheManagerConfiguration; + } + + /** + * The cache manager configuration + */ + public void setCacheManagerConfiguration(Configuration cacheManagerConfiguration) { + this.cacheManagerConfiguration = cacheManagerConfiguration; + } + + public boolean hasCacheManagerConfiguration() { + return this.cacheManagerConfiguration != null; + } + public EventOrdering getEventOrdering() { return eventOrdering; } http://git-wip-us.apache.org/repos/asf/camel/blob/8170cbae/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 81bdb38..926e141 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 @@ -22,6 +22,7 @@ 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; @@ -41,7 +42,6 @@ public class EhcacheManager implements Service { 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 { @@ -104,6 +104,10 @@ public class EhcacheManager implements Service { } } + if (cache == null) { + throw new RuntimeCamelException("Unable to retrieve the cache " + name + " from cache manager " + cacheManager); + } + return cache; } @@ -123,26 +127,37 @@ public class EhcacheManager implements Service { 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; + } - if (manager == null) { + // 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(); - if (configurationUri != null) { - URL url = camelContext != null - ? ResourceHelper.resolveMandatoryResourceAsUrl(camelContext.getClassResolver(), configurationUri) - : new URL(configurationUri); + URL url = camelContext != null + ? ResourceHelper.resolveMandatoryResourceAsUrl(camelContext.getClassResolver(), configurationUri) + : new URL(configurationUri); - manager = CacheManagerBuilder.newCacheManager(new XmlConfiguration(url)); - } else { - CacheManagerBuilder builder = CacheManagerBuilder.newCacheManagerBuilder(); - if (configuration.getConfiguration() != null) { - builder.withCache(cacheName, configuration.getConfiguration()); - } + LOGGER.info("EhcacheManager configured with supplied URI {}", url); + return CacheManagerBuilder.newCacheManager(new XmlConfiguration(url)); + } - manager = builder.build(); - } + // 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 manager; + return builder.build(); } } http://git-wip-us.apache.org/repos/asf/camel/blob/8170cbae/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/EhcacheComponentConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/EhcacheComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/EhcacheComponentConfiguration.java index 0b6768c..66c9742 100644 --- a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/EhcacheComponentConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/EhcacheComponentConfiguration.java @@ -21,6 +21,7 @@ import javax.annotation.Generated; import org.apache.camel.component.ehcache.EhcacheComponent; import org.ehcache.CacheManager; import org.ehcache.config.CacheConfiguration; +import org.ehcache.config.Configuration; import org.ehcache.event.EventFiring; import org.ehcache.event.EventOrdering; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -47,6 +48,11 @@ public class EhcacheComponentConfiguration { @NestedConfigurationProperty private CacheManager cacheManager; /** + * The cache manager configuration + */ + @NestedConfigurationProperty + private Configuration cacheManagerConfiguration; + /** * The default cache configuration to be used to create caches. */ @NestedConfigurationProperty @@ -79,6 +85,15 @@ public class EhcacheComponentConfiguration { this.cacheManager = cacheManager; } + public Configuration getCacheManagerConfiguration() { + return cacheManagerConfiguration; + } + + public void setCacheManagerConfiguration( + Configuration cacheManagerConfiguration) { + this.cacheManagerConfiguration = cacheManagerConfiguration; + } + public CacheConfiguration<K, V> getCacheConfiguration() { return cacheConfiguration; } @@ -137,6 +152,10 @@ public class EhcacheComponentConfiguration { * The cache manager */ private CacheManager cacheManager; + /** + * The cache manager configuration + */ + private Configuration cacheManagerConfiguration; private EventOrdering eventOrdering = EventOrdering.ORDERED; private EventFiring eventFiring = EventFiring.ASYNCHRONOUS; private Set eventTypes; @@ -204,6 +223,15 @@ public class EhcacheComponentConfiguration { this.cacheManager = cacheManager; } + public Configuration getCacheManagerConfiguration() { + return cacheManagerConfiguration; + } + + public void setCacheManagerConfiguration( + Configuration cacheManagerConfiguration) { + this.cacheManagerConfiguration = cacheManagerConfiguration; + } + public EventOrdering getEventOrdering() { return eventOrdering; }