Repository: camel Updated Branches: refs/heads/master d313c9e9a -> 5be15dc14
CAMEL-11284: camel-ehcache: allow to configure some options like a global cache manager on component level Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5be15dc1 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5be15dc1 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5be15dc1 Branch: refs/heads/master Commit: 5be15dc14916d51efeea71b4a4ccabd3aa08ec5e Parents: d313c9e Author: lburgazzoli <lburgazz...@gmail.com> Authored: Tue May 16 13:35:50 2017 +0200 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Tue May 16 13:36:19 2017 +0200 ---------------------------------------------------------------------- .../src/main/docs/ehcache-component.adoc | 20 +- .../component/ehcache/EhcacheComponent.java | 73 ++++++- .../component/ehcache/EhcacheConfiguration.java | 126 +++-------- .../component/ehcache/EhcacheConsumer.java | 4 +- .../component/ehcache/EhcacheEndpoint.java | 15 +- .../camel/component/ehcache/EhcacheManager.java | 58 +++-- .../component/ehcache/EhcacheProducer.java | 92 +++----- .../EhcacheComponentConfigurationTest.java | 101 +++++++++ .../ehcache/EhcacheConfigurationTest.java | 42 +++- .../ehcache/EhcacheSpringConfigurationTest.xml | 2 +- .../EhcacheComponentAutoConfiguration.java | 35 +++- .../EhcacheComponentConfiguration.java | 210 +++++++++++++++++++ 12 files changed, 582 insertions(+), 196 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/5be15dc1/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 09f7b82..75c576a 100644 --- a/components/camel-ehcache/src/main/docs/ehcache-component.adoc +++ b/components/camel-ehcache/src/main/docs/ehcache-component.adoc @@ -37,7 +37,19 @@ format, `?option=value&option=#beanRef&...` // component options: START -The Ehcache component has no options. +The Ehcache component supports 5 options which are listed below. + + + +[width="100%",cols="2,5,^1,2",options="header"] +|======================================================================= +| Name | Description | Default | Type +| **configuration** (advanced) | Sets the global component configuration | | EhcacheConfiguration +| **cacheManager** (common) | The cache manager | | CacheManager +| **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 +|======================================================================= // component options: END @@ -63,7 +75,7 @@ with the following path and query parameters: |======================================================================= | Name | Description | Default | Type | **cacheManager** (common) | The cache manager | | CacheManager -| **configUri** (common) | URI pointing to the Ehcache XML configuration file's location | | String +| **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 | **eventFiring** (consumer) | Set the the delivery mode (synchronous asynchronous) | ASYNCHRONOUS | EventFiring @@ -74,9 +86,9 @@ with the following path and query parameters: | **action** (producer) | To configure the default cache action. If an action is set in the message header then the operation from the header takes precedence. | | String | **key** (producer) | To configure the default action key. If a key is set in the message header then the key from the header takes precedence. | | Object | **configuration** (advanced) | The default cache configuration to be used to create caches. | | CacheConfiguration<?,?> -| **keyType** (advanced) | The cache key type default Object.class | java.lang.Object | String +| **keyType** (advanced) | The cache key type default java.lang.Object | java.lang.Object | String | **synchronous** (advanced) | Sets whether synchronous processing should be strictly used or Camel is allowed to use asynchronous processing (if supported). | false | boolean -| **valueType** (advanced) | The cache value type default Object.class | java.lang.Object | String +| **valueType** (advanced) | The cache value type default java.lang.Object | java.lang.Object | String |======================================================================= // endpoint options: END http://git-wip-us.apache.org/repos/asf/camel/blob/5be15dc1/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 bccc39e..0a32465 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 @@ -21,12 +21,17 @@ 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.ehcache.CacheManager; +import org.ehcache.config.CacheConfiguration; /** * Represents the component that manages {@link DefaultComponent}. */ public class EhcacheComponent extends DefaultComponent { - + @Metadata(label = "advanced") + private EhcacheConfiguration configuration = new EhcacheConfiguration(); + public EhcacheComponent() { } @@ -36,13 +41,63 @@ public class EhcacheComponent extends DefaultComponent { @Override protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - return new EhcacheEndpoint( - uri, - this, - EhcacheConfiguration.create( - getCamelContext(), - remaining, - parameters) - ); + EhcacheConfiguration configuration; + if (this.configuration != null) { + configuration = this.configuration.copy(); + } else { + configuration = new EhcacheConfiguration(); + } + + setProperties(configuration, parameters); + + return new EhcacheEndpoint(uri, this, remaining, configuration); + } + + // **************************** + // Properties + // **************************** + + public EhcacheConfiguration getConfiguration() { + return configuration; + } + + /** + * Sets the global component configuration + */ + public void setConfiguration(EhcacheConfiguration configuration) { + this.configuration = configuration; + } + + public CacheManager getCacheManager() { + return configuration.getCacheManager(); + } + + /** + * The cache manager + */ + public void setCacheManager(CacheManager cacheManager) { + this.configuration.setCacheManager(cacheManager); + } + + /** + * The default cache configuration to be used to create caches. + */ + public <K, V> void setCacheConfiguration(CacheConfiguration<K, V> cacheConfiguration) { + this.configuration.setConfiguration(cacheConfiguration); + } + + public <K, V> CacheConfiguration<K, V> getCacheConfiguration() { + return this.configuration.getConfiguration(); + } + + public String getCacheConfigurationUri() { + return this.configuration.getConfigUri(); + } + + /** + * URI pointing to the Ehcache XML configuration file's location + */ + public void setCacheConfigurationUri(String configurationUri) { + this.configuration.setConfigUri(configurationUri); } } http://git-wip-us.apache.org/repos/asf/camel/blob/5be15dc1/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 798f443..c2ed23d 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 @@ -16,110 +16,75 @@ */ package org.apache.camel.component.ehcache; -import java.io.IOException; -import java.net.URL; 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.RuntimeCamelException; import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriParams; -import org.apache.camel.util.EndpointHelper; 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.builders.CacheManagerBuilder; import org.ehcache.event.EventFiring; import org.ehcache.event.EventOrdering; import org.ehcache.event.EventType; -import org.ehcache.xml.XmlConfiguration; @UriParams -public class EhcacheConfiguration { - public static final String PREFIX_CONF = "conf."; - public static final String PREFIX_POOL = "pool."; - - private final CamelContext context; - private final String cacheName; - - @UriParam - private String configUri; - +public class EhcacheConfiguration implements Cloneable { @UriParam(defaultValue = "true") private boolean createCacheIfNotExist = true; - @UriParam(label = "producer") private String action; @UriParam(label = "producer") private Object key; - @UriParam private CacheManager cacheManager; + @UriParam + private String configurationUri; @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", - defaultValue = "ORDERED") + @UriParam(label = "consumer", defaultValue = "ORDERED") private EventOrdering eventOrdering = EventOrdering.ORDERED; - - @UriParam( - label = "consumer", - enums = "ASYNCHRONOUS,SYNCHRONOUS", - defaultValue = "ASYNCHRONOUS") + @UriParam(label = "consumer", defaultValue = "ASYNCHRONOUS") private EventFiring eventFiring = EventFiring.ASYNCHRONOUS; - - @UriParam( - label = "consumer", - enums = "EVICTED,EXPIRED,REMOVED,CREATED,UPDATED", - defaultValue = "EVICTED,EXPIRED,REMOVED,CREATED,UPDATED") + @UriParam(label = "consumer", enums = "EVICTED,EXPIRED,REMOVED,CREATED,UPDATED", defaultValue = "EVICTED,EXPIRED,REMOVED,CREATED,UPDATED") private Set<EventType> eventTypes = EnumSet.of(EventType.values()[0], EventType.values()); - EhcacheConfiguration(String cacheName) { - this(null, cacheName); - } - - EhcacheConfiguration(CamelContext context, String cacheName) { - this.context = context; - this.cacheName = cacheName; - - Stream.of(EventType.values()).map(EventType::name).collect(Collectors.joining(", ")); + public EhcacheConfiguration() { } - public CamelContext getContext() { - return context; + /** + * URI pointing to the Ehcache XML configuration file's location + */ + public void setConfigurationUri(String configurationUri) { + this.configurationUri = configurationUri; } - public String getCacheName() { - return cacheName; + public String getConfigurationUri() { + return configurationUri; } + /** + * @deprecated use {@link #getConfigurationUri()} instead + */ + @Deprecated public String getConfigUri() { - return configUri; - } - - public URL getConfigUriAsUrl() throws IOException { - return context != null - ? ResourceHelper.resolveMandatoryResourceAsUrl(context.getClassResolver(), configUri) - : new URL(configUri); + return getConfigurationUri(); } /** * URI pointing to the Ehcache XML configuration file's location + * + * @deprecated use {@link #setConfigurationUri(String)} instead */ + @Deprecated public void setConfigUri(String configUri) { - this.configUri = configUri; + setConfigurationUri(configUri); } public boolean isCreateCacheIfNotExist() { @@ -248,59 +213,32 @@ public class EhcacheConfiguration { } /** - * The cache key type, default Object.class + * The cache key type, default "java.lang.Object" */ 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 + * The cache value type, default "java.lang.Object" */ public void setValueType(Class<?> valueType) { this.valueType = valueType; } - public void setValueType(String valueType) throws ClassNotFoundException { - setValueType(context.getClassResolver().resolveMandatoryClass(valueType)); - } - // **************************** - // Helpers + // Cloneable // **************************** - static EhcacheConfiguration create(CamelContext context, String remaining, Map<String, Object> parameters) throws Exception { - EhcacheConfiguration configuration = new EhcacheConfiguration(context, remaining); - EndpointHelper.setReferenceProperties(context, configuration, parameters); - EndpointHelper.setProperties(context, configuration, parameters); - - return configuration; - } - - CacheManager createCacheManager() throws IOException { - CacheManager manager; - - if (cacheManager != null) { - manager = cacheManager; - } else if (configUri != null) { - manager = CacheManagerBuilder.newCacheManager(new XmlConfiguration(getConfigUriAsUrl())); - } else { - CacheManagerBuilder builder = CacheManagerBuilder.newCacheManagerBuilder(); - if (configuration != null) { - builder.withCache(cacheName, configuration); - } - - manager = builder.build(); + public EhcacheConfiguration copy() { + try { + return (EhcacheConfiguration)super.clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeCamelException(e); } - - return manager; } } http://git-wip-us.apache.org/repos/asf/camel/blob/5be15dc1/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 4e1f0ab..b85bf19 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 @@ -29,12 +29,12 @@ public class EhcacheConsumer extends DefaultConsumer implements CacheEventListen private final EhcacheManager manager; private final Cache cache; - public EhcacheConsumer(EhcacheEndpoint endpoint, EhcacheConfiguration configuration, Processor processor) throws Exception { + public EhcacheConsumer(EhcacheEndpoint endpoint, String cacheName, EhcacheConfiguration configuration, Processor processor) throws Exception { super(endpoint, processor); this.configuration = configuration; this.manager = endpoint.getManager(); - this.cache = manager.getCache(); + this.cache = manager.getCache(cacheName, configuration.getKeyType(), configuration.getValueType()); } @Override http://git-wip-us.apache.org/repos/asf/camel/blob/5be15dc1/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 ac7bd7f..91fec1d 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,22 +37,22 @@ public class EhcacheEndpoint extends DefaultEndpoint { private final EhcacheConfiguration configuration; private final EhcacheManager cacheManager; - EhcacheEndpoint(String uri, EhcacheComponent component, EhcacheConfiguration configuration) throws Exception { + EhcacheEndpoint(String uri, EhcacheComponent component, String cacheName, EhcacheConfiguration configuration) throws Exception { super(uri, component); - this.cacheName = configuration.getCacheName(); + this.cacheName = cacheName; this.configuration = configuration; - this.cacheManager = new EhcacheManager(configuration); + this.cacheManager = new EhcacheManager(cacheName, configuration, component.getCamelContext()); } @Override public Producer createProducer() throws Exception { - return new EhcacheProducer(this, configuration); + return new EhcacheProducer(this, this.cacheName, configuration); } @Override public Consumer createConsumer(Processor processor) throws Exception { - return new EhcacheConsumer(this, configuration, processor); + return new EhcacheConsumer(this, this.cacheName, configuration, processor); } @Override @@ -72,11 +72,6 @@ public class EhcacheEndpoint extends DefaultEndpoint { cacheManager.stop(); } - @Override - public EhcacheComponent getComponent() { - return (EhcacheComponent) super.getComponent(); - } - EhcacheManager getManager() { return cacheManager; } http://git-wip-us.apache.org/repos/asf/camel/blob/5be15dc1/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 be56d06..81bdb38 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 @@ -17,16 +17,21 @@ 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.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; @@ -39,8 +44,12 @@ public class EhcacheManager implements Service { private final boolean managed; - public EhcacheManager(EhcacheConfiguration configuration) throws IOException { - this(configuration.createCacheManager(), !configuration.hasCacheManager(), configuration); + 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) { @@ -98,19 +107,42 @@ public class EhcacheManager implements Service { return cache; } - public Cache<?, ?> getCache(String name) throws Exception { - return getCache( - name, - configuration.getKeyType(), - configuration.getValueType()); + CacheManager getCacheManager() { + return this.cacheManager; + } + + // ************************************************* + // + // ************************************************* + + private static CacheManager createCacheManager(String cacheName, EhcacheConfiguration configuration) throws IOException { + return createCacheManager(cacheName, configuration, null); } - public Cache<?, ?> getCache() throws Exception { - ObjectHelper.notNull(configuration, "Ehcache configuration"); + private static CacheManager createCacheManager(String cacheName, EhcacheConfiguration configuration, CamelContext camelContext) throws IOException { + ObjectHelper.notNull(cacheName, "Ehcache cacheName"); + ObjectHelper.notNull(configuration, "Camel Ehcache configuration"); + + CacheManager manager = configuration.getCacheManager(); + + if (manager == null) { + String configurationUri = configuration.getConfigurationUri(); + if (configurationUri != null) { + 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()); + } + + manager = builder.build(); + } + } - return getCache( - configuration.getCacheName(), - configuration.getKeyType(), - configuration.getValueType()); + return manager; } } http://git-wip-us.apache.org/repos/asf/camel/blob/5be15dc1/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 e5d9fd5..7a45b05 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 @@ -16,112 +16,79 @@ */ package org.apache.camel.component.ehcache; +import java.util.Collections; import java.util.Map; import java.util.Set; import org.apache.camel.CamelExchangeException; -import org.apache.camel.Exchange; +import org.apache.camel.InvokeOnHeader; import org.apache.camel.Message; -import org.apache.camel.NoSuchHeaderException; -import org.apache.camel.impl.DefaultProducer; +import org.apache.camel.impl.HeaderSelectorProducer; import org.ehcache.Cache; -public class EhcacheProducer extends DefaultProducer { +public class EhcacheProducer extends HeaderSelectorProducer { private final EhcacheConfiguration configuration; private final EhcacheManager manager; private final Cache cache; - public EhcacheProducer(EhcacheEndpoint endpoint, EhcacheConfiguration configuration) throws Exception { - super(endpoint); + public EhcacheProducer(EhcacheEndpoint endpoint, String cacheName, EhcacheConfiguration configuration) throws Exception { + super(endpoint, EhcacheConstants.ACTION, () -> configuration.getAction()); this.configuration = configuration; this.manager = endpoint.getManager(); - this.cache = manager.getCache(); - } - - @Override - public void process(Exchange exchange) throws Exception { - final Message message = exchange.getIn(); - final String action = exchange.getIn().getHeader(EhcacheConstants.ACTION, configuration.getAction(), String.class); - - if (action == null) { - throw new NoSuchHeaderException(exchange, EhcacheConstants.ACTION, String.class); - } - - switch (action) { - case EhcacheConstants.ACTION_CLEAR: - onClear(message); - break; - case EhcacheConstants.ACTION_PUT: - onPut(message); - break; - case EhcacheConstants.ACTION_PUT_ALL: - onPutAll(message); - break; - case EhcacheConstants.ACTION_PUT_IF_ABSENT: - onPutIfAbsent(message); - break; - case EhcacheConstants.ACTION_GET: - onGet(message); - break; - case EhcacheConstants.ACTION_GET_ALL: - onGetAll(message); - break; - case EhcacheConstants.ACTION_REMOVE: - onRemove(message); - break; - case EhcacheConstants.ACTION_REMOVE_ALL: - onRemoveAll(message); - break; - case EhcacheConstants.ACTION_REPLACE: - onReplace(message); - break; - default: - throw new IllegalStateException("Unsupported operation " + action); - } + this.cache = manager.getCache(cacheName, configuration.getKeyType(), configuration.getValueType()); } // **************************** // Handlers // **************************** - private void onClear(Message message) throws Exception { + @InvokeOnHeader(EhcacheConstants.ACTION_CLEAR) + public void onClear(Message message) throws Exception { cache.clear(); setResult(message, true, null, null); } - private void onPut(Message message) throws Exception { + @InvokeOnHeader(EhcacheConstants.ACTION_PUT) + public void onPut(Message message) throws Exception { cache.put(getKey(message), getValue(message, configuration.getValueType())); setResult(message, true, null, null); } - private void onPutAll(Message message) throws Exception { + @InvokeOnHeader(EhcacheConstants.ACTION_PUT_ALL) + public void onPutAll(Message message) throws Exception { cache.putAll((Map)getValue(message, Map.class)); setResult(message, true, null, null); } - private void onPutIfAbsent(Message message) throws Exception { + @InvokeOnHeader(EhcacheConstants.ACTION_PUT_IF_ABSENT) + public void onPutIfAbsent(Message message) throws Exception { Object oldValue = cache.putIfAbsent(getKey(message), getValue(message, configuration.getValueType())); setResult(message, true, null, oldValue); } - private void onGet(Message message) throws Exception { + @InvokeOnHeader(EhcacheConstants.ACTION_GET) + public void onGet(Message message) throws Exception { Object result = cache.get(getKey(message)); setResult(message, true, result, null); } - private void onGetAll(Message message) throws Exception { - Object result = cache.getAll(message.getHeader(EhcacheConstants.KEYS, Set.class)); + @InvokeOnHeader(EhcacheConstants.ACTION_GET_ALL) + public void onGetAll(Message message) throws Exception { + Object result = cache.getAll( + message.getHeader(EhcacheConstants.KEYS, Collections::emptySet, Set.class) + ); setResult(message, true, result, null); } - private void onRemove(Message message) throws Exception { + @InvokeOnHeader(EhcacheConstants.ACTION_REMOVE) + public void onRemove(Message message) throws Exception { boolean success = true; Object valueToReplace = message.getHeader(EhcacheConstants.OLD_VALUE); if (valueToReplace == null) { @@ -133,17 +100,22 @@ public class EhcacheProducer extends DefaultProducer { setResult(message, success, null, null); } - private void onRemoveAll(Message message) throws Exception { - cache.removeAll(message.getHeader(EhcacheConstants.KEYS, Set.class)); + @InvokeOnHeader(EhcacheConstants.ACTION_REMOVE_ALL) + public void onRemoveAll(Message message) throws Exception { + cache.removeAll( + message.getHeader(EhcacheConstants.KEYS, Collections::emptySet, Set.class) + ); setResult(message, true, null, null); } - private void onReplace(Message message) throws Exception { + @InvokeOnHeader(EhcacheConstants.ACTION_REPLACE) + public void onReplace(Message message) throws Exception { boolean success = true; Object oldValue = null; Object value = getValue(message, configuration.getValueType()); Object valueToReplace = message.getHeader(EhcacheConstants.OLD_VALUE); + if (valueToReplace == null) { oldValue = cache.replace(getKey(message), value); } else { http://git-wip-us.apache.org/repos/asf/camel/blob/5be15dc1/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheComponentConfigurationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheComponentConfigurationTest.java b/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheComponentConfigurationTest.java new file mode 100644 index 0000000..2688223 --- /dev/null +++ b/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheComponentConfigurationTest.java @@ -0,0 +1,101 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.camel.component.ehcache; + +import org.apache.camel.EndpointInject; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; +import org.apache.camel.test.junit4.CamelTestSupport; +import org.ehcache.Cache; +import org.ehcache.config.ResourcePools; +import org.ehcache.config.ResourceType; +import org.ehcache.config.SizedResourcePool; +import org.ehcache.config.builders.CacheConfigurationBuilder; +import org.ehcache.config.builders.CacheManagerBuilder; +import org.ehcache.config.builders.ResourcePoolsBuilder; +import org.ehcache.config.units.EntryUnit; +import org.ehcache.config.units.MemoryUnit; +import org.junit.Test; + +public class EhcacheComponentConfigurationTest extends CamelTestSupport { + @EndpointInject(uri = "ehcache:myCache") + private EhcacheEndpoint endpoint; + + @Override + protected JndiRegistry createRegistry() throws Exception { + EhcacheComponent component = new EhcacheComponent(); + component.getConfiguration().setKeyType(String.class); + component.getConfiguration().setValueType(String.class); + component.getConfiguration().setCacheManager( + CacheManagerBuilder.newCacheManagerBuilder() + .withCache( + "myCache", + CacheConfigurationBuilder.newCacheConfigurationBuilder( + String.class, + String.class, + ResourcePoolsBuilder.newResourcePoolsBuilder() + .heap(100, EntryUnit.ENTRIES) + .offheap(1, MemoryUnit.MB)) + ).build(true) + ); + + JndiRegistry registry = super.createRegistry(); + registry.bind("ehcache", component); + + return registry; + } + + // ***************************** + // Test + // ***************************** + + @Test + public void testCacheManager() throws Exception { + assertEquals( + context().getRegistry().lookupByNameAndType("ehcache", EhcacheComponent.class).getCacheManager(), + endpoint.getManager().getCacheManager() + ); + + Cache<String, String> cache = endpoint.getManager().getCache("myCache", String.class, String.class); + ResourcePools pools = cache.getRuntimeConfiguration().getResourcePools(); + + SizedResourcePool h = pools.getPoolForResource(ResourceType.Core.HEAP); + assertNotNull(h); + assertEquals(100, h.getSize()); + assertEquals(EntryUnit.ENTRIES, h.getUnit()); + + SizedResourcePool o = pools.getPoolForResource(ResourceType.Core.OFFHEAP); + assertNotNull(o); + assertEquals(1, o.getSize()); + assertEquals(MemoryUnit.MB, o.getUnit()); + } + + // **************************** + // Route + // **************************** + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + public void configure() { + from("direct:ehcache") + .to(endpoint); + } + }; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/5be15dc1/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java ---------------------------------------------------------------------- diff --git a/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java b/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java index b275869..6eabab0 100644 --- a/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java +++ b/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java @@ -22,11 +22,13 @@ import org.apache.camel.builder.RouteBuilder; import org.apache.camel.impl.JndiRegistry; import org.apache.camel.test.junit4.CamelTestSupport; import org.ehcache.Cache; +import org.ehcache.CacheManager; import org.ehcache.UserManagedCache; import org.ehcache.config.ResourcePools; import org.ehcache.config.ResourceType; import org.ehcache.config.SizedResourcePool; import org.ehcache.config.builders.CacheConfigurationBuilder; +import org.ehcache.config.builders.CacheManagerBuilder; import org.ehcache.config.builders.ResourcePoolsBuilder; import org.ehcache.config.units.EntryUnit; import org.ehcache.config.units.MemoryUnit; @@ -35,10 +37,12 @@ import org.junit.Test; public class EhcacheConfigurationTest extends CamelTestSupport { @EndpointInject(uri = "ehcache:myProgrammaticCacheConf?configuration=#myProgrammaticConfiguration") private EhcacheEndpoint ehcacheProgrammaticConf; - @EndpointInject(uri = "ehcache:myFileCacheConf?keyType=java.lang.String&valueType=java.lang.String&configUri=classpath:ehcache/ehcache-file-config.xml") + @EndpointInject(uri = "ehcache:myFileCacheConf?keyType=java.lang.String&valueType=java.lang.String&configurationUri=classpath:ehcache/ehcache-file-config.xml") private EhcacheEndpoint ehcacheFileConf; @EndpointInject(uri = "ehcache:myUserCacheConf") private EhcacheEndpoint ehcacheUserConf; + @EndpointInject(uri = "ehcache:myCache?cacheManager=#myCacheManager&keyType=java.lang.String&valueType=java.lang.String") + private EhcacheEndpoint ehcacheCacheManager; @Override protected JndiRegistry createRegistry() throws Exception { @@ -53,6 +57,19 @@ public class EhcacheConfigurationTest extends CamelTestSupport { .offheap(1, MemoryUnit.MB)) .build() ); + registry.bind( + "myCacheManager", + CacheManagerBuilder.newCacheManagerBuilder() + .withCache( + "myCache", + CacheConfigurationBuilder.newCacheConfigurationBuilder( + String.class, + String.class, + ResourcePoolsBuilder.newResourcePoolsBuilder() + .heap(100, EntryUnit.ENTRIES) + .offheap(1, MemoryUnit.MB)) + ).build(true) + ); return registry; } @@ -103,6 +120,27 @@ public class EhcacheConfigurationTest extends CamelTestSupport { assertEquals("user-val", cache.get("user-key")); } + @Test + public void testCacheManager() throws Exception { + assertEquals( + context().getRegistry().lookupByNameAndType("myCacheManager", CacheManager.class), + ehcacheCacheManager.getManager().getCacheManager() + ); + + Cache<String, String> cache = getCache(ehcacheCacheManager, "myCache"); + ResourcePools pools = cache.getRuntimeConfiguration().getResourcePools(); + + SizedResourcePool h = pools.getPoolForResource(ResourceType.Core.HEAP); + assertNotNull(h); + assertEquals(100, h.getSize()); + assertEquals(EntryUnit.ENTRIES, h.getUnit()); + + SizedResourcePool o = pools.getPoolForResource(ResourceType.Core.OFFHEAP); + assertNotNull(o); + assertEquals(1, o.getSize()); + assertEquals(MemoryUnit.MB, o.getUnit()); + } + protected Cache<String, String> getCache(EhcacheEndpoint endpoint, String cacheName) throws Exception { return endpoint.getManager().getCache(cacheName, String.class, String.class); } @@ -121,6 +159,8 @@ public class EhcacheConfigurationTest extends CamelTestSupport { .to(ehcacheFileConf); from("direct:ehcacheUserConf") .to(ehcacheUserConf); + from("direct:ehcacheCacheManager") + .to(ehcacheCacheManager); } }; } http://git-wip-us.apache.org/repos/asf/camel/blob/5be15dc1/components/camel-ehcache/src/test/resources/org/apache/camel/component/ehcache/EhcacheSpringConfigurationTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-ehcache/src/test/resources/org/apache/camel/component/ehcache/EhcacheSpringConfigurationTest.xml b/components/camel-ehcache/src/test/resources/org/apache/camel/component/ehcache/EhcacheSpringConfigurationTest.xml index 4a76584..4e0eb8f 100644 --- a/components/camel-ehcache/src/test/resources/org/apache/camel/component/ehcache/EhcacheSpringConfigurationTest.xml +++ b/components/camel-ehcache/src/test/resources/org/apache/camel/component/ehcache/EhcacheSpringConfigurationTest.xml @@ -37,7 +37,7 @@ </route> <route> <from uri="direct:file"/> - <to uri="ehcache://myFileCacheConf?keyType=java.lang.String&valueType=java.lang.String&configUri=classpath:ehcache/ehcache-file-config.xml"/> + <to uri="ehcache://myFileCacheConf?keyType=java.lang.String&valueType=java.lang.String&configurationUri=classpath:ehcache/ehcache-file-config.xml"/> </route> </camelContext> </beans> http://git-wip-us.apache.org/repos/asf/camel/blob/5be15dc1/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/EhcacheComponentAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/EhcacheComponentAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/EhcacheComponentAutoConfiguration.java index ece5cf2..4d42fdc 100644 --- a/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/EhcacheComponentAutoConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-ehcache-starter/src/main/java/org/apache/camel/component/ehcache/springboot/EhcacheComponentAutoConfiguration.java @@ -16,9 +16,12 @@ */ package org.apache.camel.component.ehcache.springboot; +import java.util.HashMap; +import java.util.Map; import javax.annotation.Generated; import org.apache.camel.CamelContext; import org.apache.camel.component.ehcache.EhcacheComponent; +import org.apache.camel.util.IntrospectionSupport; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.condition.ConditionMessage; import org.springframework.boot.autoconfigure.condition.ConditionOutcome; @@ -27,6 +30,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.SpringBootCondition; import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; @@ -42,16 +46,43 @@ import org.springframework.core.type.AnnotatedTypeMetadata; @ConditionalOnBean(type = "org.apache.camel.spring.boot.CamelAutoConfiguration") @Conditional(EhcacheComponentAutoConfiguration.Condition.class) @AutoConfigureAfter(name = "org.apache.camel.spring.boot.CamelAutoConfiguration") +@EnableConfigurationProperties(EhcacheComponentConfiguration.class) public class EhcacheComponentAutoConfiguration { @Lazy @Bean(name = "ehcache-component") @ConditionalOnClass(CamelContext.class) @ConditionalOnMissingBean(EhcacheComponent.class) - public EhcacheComponent configureEhcacheComponent(CamelContext camelContext) - throws Exception { + public EhcacheComponent configureEhcacheComponent( + CamelContext camelContext, + EhcacheComponentConfiguration configuration) throws Exception { EhcacheComponent component = new EhcacheComponent(); component.setCamelContext(camelContext); + Map<String, Object> parameters = new HashMap<>(); + IntrospectionSupport.getProperties(configuration, parameters, null, + false); + for (Map.Entry<String, Object> entry : parameters.entrySet()) { + Object value = entry.getValue(); + Class<?> paramClass = value.getClass(); + if (paramClass.getName().endsWith("NestedConfiguration")) { + Class nestedClass = null; + try { + nestedClass = (Class) paramClass.getDeclaredField( + "CAMEL_NESTED_CLASS").get(null); + HashMap<String, Object> nestedParameters = new HashMap<>(); + IntrospectionSupport.getProperties(value, nestedParameters, + null, false); + Object nestedProperty = nestedClass.newInstance(); + IntrospectionSupport.setProperties(camelContext, + camelContext.getTypeConverter(), nestedProperty, + nestedParameters); + entry.setValue(nestedProperty); + } catch (NoSuchFieldException e) { + } + } + } + IntrospectionSupport.setProperties(camelContext, + camelContext.getTypeConverter(), component, parameters); return component; } http://git-wip-us.apache.org/repos/asf/camel/blob/5be15dc1/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 4593d88..0b6768c 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 @@ -16,7 +16,16 @@ */ package org.apache.camel.component.ehcache.springboot; +import java.util.Set; +import javax.annotation.Generated; +import org.apache.camel.component.ehcache.EhcacheComponent; +import org.ehcache.CacheManager; +import org.ehcache.config.CacheConfiguration; +import org.ehcache.event.EventFiring; +import org.ehcache.event.EventOrdering; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.DeprecatedConfigurationProperty; +import org.springframework.boot.context.properties.NestedConfigurationProperty; /** * The ehcache component enables you to perform caching operations using Ehcache @@ -24,16 +33,69 @@ import org.springframework.boot.context.properties.ConfigurationProperties; * * Generated by camel-package-maven-plugin - do not edit this file! */ +@Generated("org.apache.camel.maven.packaging.SpringBootAutoConfigurationMojo") @ConfigurationProperties(prefix = "camel.component.ehcache") public class EhcacheComponentConfiguration { /** + * Sets the global component configuration + */ + private EhcacheConfigurationNestedConfiguration configuration; + /** + * The cache manager + */ + @NestedConfigurationProperty + private CacheManager cacheManager; + /** + * The default cache configuration to be used to create caches. + */ + @NestedConfigurationProperty + private CacheConfiguration<K, V> cacheConfiguration; + /** + * URI pointing to the Ehcache XML configuration file's location + */ + private String cacheConfigurationUri; + /** * Whether the component should resolve property placeholders on itself when * starting. Only properties which are of String type can use property * placeholders. */ private Boolean resolvePropertyPlaceholders = true; + public EhcacheConfigurationNestedConfiguration getConfiguration() { + return configuration; + } + + public void setConfiguration( + EhcacheConfigurationNestedConfiguration configuration) { + this.configuration = configuration; + } + + public CacheManager getCacheManager() { + return cacheManager; + } + + public void setCacheManager(CacheManager cacheManager) { + this.cacheManager = cacheManager; + } + + public CacheConfiguration<K, V> getCacheConfiguration() { + return cacheConfiguration; + } + + public void setCacheConfiguration( + CacheConfiguration<K, V> cacheConfiguration) { + this.cacheConfiguration = cacheConfiguration; + } + + public String getCacheConfigurationUri() { + return cacheConfigurationUri; + } + + public void setCacheConfigurationUri(String cacheConfigurationUri) { + this.cacheConfigurationUri = cacheConfigurationUri; + } + public Boolean getResolvePropertyPlaceholders() { return resolvePropertyPlaceholders; } @@ -42,4 +104,152 @@ public class EhcacheComponentConfiguration { Boolean resolvePropertyPlaceholders) { this.resolvePropertyPlaceholders = resolvePropertyPlaceholders; } + + public static class EhcacheConfigurationNestedConfiguration { + public static final Class CAMEL_NESTED_CLASS = org.apache.camel.component.ehcache.EhcacheConfiguration.class; + /** + * URI pointing to the Ehcache XML configuration file's location + */ + private String configurationUri; + /** + * URI pointing to the Ehcache XML configuration file's location + * + * @deprecated use {@link #setConfigurationUri(String)} instead + */ + @Deprecated + private String configUri; + /** + * Configure if a cache need to be created if it does exist or can't be + * pre-configured. + */ + private Boolean createCacheIfNotExist = true; + /** + * To configure the default cache action. If an action is set in the + * message header, then the operation from the header takes precedence. + */ + private String action; + /** + * To configure the default action key. If a key is set in the message + * header, then the key from the header takes precedence. + */ + private Object key; + /** + * The cache manager + */ + private CacheManager cacheManager; + private EventOrdering eventOrdering = EventOrdering.ORDERED; + private EventFiring eventFiring = EventFiring.ASYNCHRONOUS; + private Set eventTypes; + /** + * The default cache configuration to be used to create caches. + */ + private CacheConfiguration configuration; + /** + * The cache key type, default "java.lang.Object" + */ + private String keyType = "java.lang.Object"; + /** + * The cache value type, default "java.lang.Object" + */ + private String valueType = "java.lang.Object"; + + public String getConfigurationUri() { + return configurationUri; + } + + public void setConfigurationUri(String configurationUri) { + this.configurationUri = configurationUri; + } + + @Deprecated + @DeprecatedConfigurationProperty + public String getConfigUri() { + return configUri; + } + + @Deprecated + public void setConfigUri(String configUri) { + this.configUri = configUri; + } + + public Boolean getCreateCacheIfNotExist() { + return createCacheIfNotExist; + } + + public void setCreateCacheIfNotExist(Boolean createCacheIfNotExist) { + this.createCacheIfNotExist = createCacheIfNotExist; + } + + public String getAction() { + return action; + } + + public void setAction(String action) { + this.action = action; + } + + public Object getKey() { + return key; + } + + public void setKey(Object key) { + this.key = key; + } + + public CacheManager getCacheManager() { + return cacheManager; + } + + public void setCacheManager(CacheManager cacheManager) { + this.cacheManager = cacheManager; + } + + public EventOrdering getEventOrdering() { + return eventOrdering; + } + + public void setEventOrdering(EventOrdering eventOrdering) { + this.eventOrdering = eventOrdering; + } + + public EventFiring getEventFiring() { + return eventFiring; + } + + public void setEventFiring(EventFiring eventFiring) { + this.eventFiring = eventFiring; + } + + public Set getEventTypes() { + return eventTypes; + } + + public void setEventTypes(Set eventTypes) { + this.eventTypes = eventTypes; + } + + public CacheConfiguration getConfiguration() { + return configuration; + } + + public void setConfiguration(CacheConfiguration configuration) { + this.configuration = configuration; + } + + public String getKeyType() { + return keyType; + } + + public void setKeyType(String keyType) { + this.keyType = keyType; + } + + public String getValueType() { + return valueType; + } + + public void setValueType(String valueType) { + this.valueType = valueType; + } + } } \ No newline at end of file