Repository: camel Updated Branches: refs/heads/master 556201f26 -> ab6f0335d
CAMEL-10897: camel-jcache - Allow to configure cache provider on component Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ab6f0335 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ab6f0335 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ab6f0335 Branch: refs/heads/master Commit: ab6f0335d8846ee611be7ac1d02860e09c07bc68 Parents: 556201f Author: Claus Ibsen <[email protected]> Authored: Sat Feb 25 20:25:58 2017 +0100 Committer: Claus Ibsen <[email protected]> Committed: Sat Feb 25 20:25:58 2017 +0100 ---------------------------------------------------------------------- .../src/main/docs/jcache-component.adoc | 27 +++++-- .../camel/component/jcache/JCacheComponent.java | 65 +++++++++++++++- .../component/jcache/JCacheConfiguration.java | 9 +-- .../camel/component/jcache/JCacheEndpoint.java | 17 ++-- .../JCacheComponentAutoConfiguration.java | 34 +++++++- .../JCacheComponentConfiguration.java | 82 ++++++++++++++++++++ 6 files changed, 214 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/ab6f0335/components/camel-jcache/src/main/docs/jcache-component.adoc ---------------------------------------------------------------------- diff --git a/components/camel-jcache/src/main/docs/jcache-component.adoc b/components/camel-jcache/src/main/docs/jcache-component.adoc index a119afb..3a5b0cc 100644 --- a/components/camel-jcache/src/main/docs/jcache-component.adoc +++ b/components/camel-jcache/src/main/docs/jcache-component.adoc @@ -14,7 +14,11 @@ The JCache component supports 23 endpoint options which are listed below: [width="100%",cols="2,1,1m,1m,5",options="header"] |======================================================================= | Name | Group | Default | Java Type | Description -| cacheName | common | | String | *Required* the cache name +| cacheName | common | | String | *Required* The name of the cache +| cacheConfiguration | common | | Configuration | A Configuration for the Cache +| cacheConfigurationProperties | common | | Properties | The Properties for the javax.cache.spi.CachingProvider to create the CacheManager +| cachingProvider | common | | String | The fully qualified class name of the javax.cache.spi.CachingProvider +| configurationUri | common | | String | An implementation specific URI for the CacheManager | managementEnabled | common | false | boolean | Whether management gathering is enabled | readThrough | common | false | boolean | If read-through caching should be used | statisticsEnabled | common | false | boolean | Whether statistics gathering is enabled @@ -28,12 +32,8 @@ The JCache component supports 23 endpoint options which are listed below: | exceptionHandler | consumer (advanced) | | ExceptionHandler | To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this options is not in use. By default the consumer will deal with exceptions that will be logged at WARN or ERROR level and ignored. | exchangePattern | consumer (advanced) | | ExchangePattern | Sets the exchange pattern when the consumer creates an exchange. | action | producer | | String | To configure using a cache operation by default. If an operation in the message header then the operation from the header takes precedence. -| cacheConfiguration | advanced | | Configuration | A Configuration for the Cache -| cacheConfigurationProperties | advanced | | Properties | The Properties for the javax.cache.spi.CachingProvider to create the CacheManager | cacheLoaderFactory | advanced | | CacheLoader> | The CacheLoader factory | cacheWriterFactory | advanced | | CacheWriter> | The CacheWriter factory -| cachingProvider | advanced | | String | *Required* The fully qualified class name of the javax.cache.spi.CachingProvider -| configurationUri | advanced | | String | An implementation specific URI for the CacheManager | createCacheIfNotExists | advanced | true | boolean | Configure if a cache need to be created if it does exist or can't be pre-configured. | expiryPolicyFactory | advanced | | ExpiryPolicy> | The ExpiryPolicy factory | lookupProviders | advanced | false | boolean | Configure if a camel-cache should try to find implementations of jcache api in runtimes like OSGi. @@ -49,5 +49,18 @@ The JCache component supports 23 endpoint options which are listed below: // component options: START -The JCache component has no options. -// component options: END \ No newline at end of file +The JCache component supports 4 options which are listed below. + + + +{% raw %} +[width="100%",cols="2,1,1m,1m,5",options="header"] +|======================================================================= +| Name | Group | Default | Java Type | Description +| cachingProvider | common | | String | The fully qualified class name of the javax.cache.spi.CachingProvider +| cacheConfiguration | common | | Configuration | A Configuration for the Cache +| cacheConfigurationProperties | common | | Properties | The Properties for the javax.cache.spi.CachingProvider to create the CacheManager +| configurationUri | common | | String | An implementation specific URI for the CacheManager +|======================================================================= +{% endraw %} +// component options: END http://git-wip-us.apache.org/repos/asf/camel/blob/ab6f0335/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheComponent.java ---------------------------------------------------------------------- diff --git a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheComponent.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheComponent.java index 4e76170..e310553 100644 --- a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheComponent.java +++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheComponent.java @@ -17,6 +17,11 @@ package org.apache.camel.component.jcache; import java.util.Map; +import java.util.Properties; + +import javax.cache.Cache; +import javax.cache.CacheManager; +import javax.cache.configuration.Configuration; import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; @@ -27,6 +32,11 @@ import org.apache.camel.impl.UriEndpointComponent; */ public class JCacheComponent extends UriEndpointComponent { + private String cachingProvider; + private Configuration cacheConfiguration; + private Properties cacheConfigurationProperties; + private String configurationUri; + public JCacheComponent() { super(JCacheEndpoint.class); } @@ -37,8 +47,61 @@ public class JCacheComponent extends UriEndpointComponent { @Override protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception { - JCacheConfiguration configuration = new JCacheConfiguration(getCamelContext(), remaining); + String cacheName = remaining; + JCacheConfiguration configuration = new JCacheConfiguration(getCamelContext(), cacheName); + + configuration.setCachingProvider(cachingProvider); + configuration.setCacheConfiguration(cacheConfiguration); + configuration.setCacheConfigurationProperties(cacheConfigurationProperties); + configuration.setConfigurationUri(configurationUri); + setProperties(configuration, parameters); return new JCacheEndpoint(uri, this, configuration); } + + /** + * The fully qualified class name of the {@link javax.cache.spi.CachingProvider} + */ + public String getCachingProvider() { + return cachingProvider; + } + + public void setCachingProvider(String cachingProvider) { + this.cachingProvider = cachingProvider; + } + + /** + * A {@link Configuration} for the {@link Cache} + */ + public Configuration getCacheConfiguration() { + return cacheConfiguration; + } + + public void setCacheConfiguration(Configuration cacheConfiguration) { + this.cacheConfiguration = cacheConfiguration; + } + + /** + * The {@link Properties} for the {@link javax.cache.spi.CachingProvider} to + * create the {@link CacheManager} + */ + public Properties getCacheConfigurationProperties() { + return cacheConfigurationProperties; + } + + public void setCacheConfigurationProperties(Properties cacheConfigurationProperties) { + this.cacheConfigurationProperties = cacheConfigurationProperties; + } + + /** + * An implementation specific URI for the {@link CacheManager} + */ + public String getConfigurationUri() { + return configurationUri; + } + + public void setConfigurationUri(String configurationUri) { + this.configurationUri = configurationUri; + } + } http://git-wip-us.apache.org/repos/asf/camel/blob/ab6f0335/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheConfiguration.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheConfiguration.java index 05dd5a6..43b7e4d 100644 --- a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheConfiguration.java +++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheConfiguration.java @@ -38,17 +38,16 @@ import org.apache.camel.util.EndpointHelper; @UriParams public class JCacheConfiguration { - @UriParam(label = "advanced") - @Metadata(required = "true") + @UriParam(label = "common") private String cachingProvider; - @UriParam(label = "advanced") + @UriParam private Configuration cacheConfiguration; - @UriParam(label = "advanced") + @UriParam private Properties cacheConfigurationProperties; - @UriParam(label = "advanced") + @UriParam private String configurationUri; @UriParam(label = "advanced") http://git-wip-us.apache.org/repos/asf/camel/blob/ab6f0335/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheEndpoint.java ---------------------------------------------------------------------- diff --git a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheEndpoint.java b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheEndpoint.java index c1c1b72..7b66c99 100644 --- a/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheEndpoint.java +++ b/components/camel-jcache/src/main/java/org/apache/camel/component/jcache/JCacheEndpoint.java @@ -30,20 +30,20 @@ import org.apache.camel.spi.UriPath; */ @UriEndpoint(firstVersion = "2.17.0", scheme = "jcache", title = "JCache", syntax = "jcache:cacheName", consumerClass = JCacheConsumer.class, label = "cache,datagrid,clustering") public class JCacheEndpoint extends DefaultEndpoint { - @UriPath(description = "the cache name") + + @UriPath(description = "The name of the cache") @Metadata(required = "true") private final String cacheName; - @UriParam private final JCacheConfiguration cacheConfiguration; - private final JCacheManager<Object, Object> cacheManager; + + private volatile JCacheManager<Object, Object> cacheManager; public JCacheEndpoint(String uri, JCacheComponent component, JCacheConfiguration configuration) { super(uri, component); this.cacheName = configuration.getCacheName(); this.cacheConfiguration = configuration; - this.cacheManager = JCacheHelper.createManager(configuration); } @Override @@ -62,8 +62,15 @@ public class JCacheEndpoint extends DefaultEndpoint { } @Override + protected void doStart() throws Exception { + cacheManager = JCacheHelper.createManager(cacheConfiguration); + } + + @Override protected void doStop() throws Exception { - cacheManager.close(); + if (cacheManager != null) { + cacheManager.close(); + } } JCacheManager getManager() { http://git-wip-us.apache.org/repos/asf/camel/blob/ab6f0335/platforms/spring-boot/components-starter/camel-jcache-starter/src/main/java/org/apache/camel/component/jcache/springboot/JCacheComponentAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-jcache-starter/src/main/java/org/apache/camel/component/jcache/springboot/JCacheComponentAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-jcache-starter/src/main/java/org/apache/camel/component/jcache/springboot/JCacheComponentAutoConfiguration.java index 9a7857e..d552b5f 100644 --- a/platforms/spring-boot/components-starter/camel-jcache-starter/src/main/java/org/apache/camel/component/jcache/springboot/JCacheComponentAutoConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-jcache-starter/src/main/java/org/apache/camel/component/jcache/springboot/JCacheComponentAutoConfiguration.java @@ -16,8 +16,11 @@ */ package org.apache.camel.component.jcache.springboot; +import java.util.HashMap; +import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.component.jcache.JCacheComponent; +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; @@ -26,6 +29,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; @@ -40,16 +44,42 @@ import org.springframework.core.type.AnnotatedTypeMetadata; @ConditionalOnBean(type = "org.apache.camel.spring.boot.CamelAutoConfiguration") @Conditional(JCacheComponentAutoConfiguration.Condition.class) @AutoConfigureAfter(name = "org.apache.camel.spring.boot.CamelAutoConfiguration") +@EnableConfigurationProperties(JCacheComponentConfiguration.class) public class JCacheComponentAutoConfiguration { @Lazy @Bean(name = "jcache-component") @ConditionalOnClass(CamelContext.class) @ConditionalOnMissingBean(JCacheComponent.class) - public JCacheComponent configureJCacheComponent(CamelContext camelContext) - throws Exception { + public JCacheComponent configureJCacheComponent(CamelContext camelContext, + JCacheComponentConfiguration configuration) throws Exception { JCacheComponent component = new JCacheComponent(); 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/ab6f0335/platforms/spring-boot/components-starter/camel-jcache-starter/src/main/java/org/apache/camel/component/jcache/springboot/JCacheComponentConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-jcache-starter/src/main/java/org/apache/camel/component/jcache/springboot/JCacheComponentConfiguration.java b/platforms/spring-boot/components-starter/camel-jcache-starter/src/main/java/org/apache/camel/component/jcache/springboot/JCacheComponentConfiguration.java new file mode 100644 index 0000000..7c0cef8 --- /dev/null +++ b/platforms/spring-boot/components-starter/camel-jcache-starter/src/main/java/org/apache/camel/component/jcache/springboot/JCacheComponentConfiguration.java @@ -0,0 +1,82 @@ +/** + * 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.jcache.springboot; + +import java.util.Properties; +import javax.cache.configuration.Configuration; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * The jcache component enables you to perform caching operations using + * JSR107/JCache as cache implementation. + * + * Generated by camel-package-maven-plugin - do not edit this file! + */ +@ConfigurationProperties(prefix = "camel.component.jcache") +public class JCacheComponentConfiguration { + + /** + * The fully qualified class name of the javax.cache.spi.CachingProvider + */ + private String cachingProvider; + /** + * A Configuration for the Cache + */ + private Configuration cacheConfiguration; + /** + * The Properties for the javax.cache.spi.CachingProvider to create the + * CacheManager + */ + private Properties cacheConfigurationProperties; + /** + * An implementation specific URI for the CacheManager + */ + private String configurationUri; + + public String getCachingProvider() { + return cachingProvider; + } + + public void setCachingProvider(String cachingProvider) { + this.cachingProvider = cachingProvider; + } + + public Configuration getCacheConfiguration() { + return cacheConfiguration; + } + + public void setCacheConfiguration(Configuration cacheConfiguration) { + this.cacheConfiguration = cacheConfiguration; + } + + public Properties getCacheConfigurationProperties() { + return cacheConfigurationProperties; + } + + public void setCacheConfigurationProperties( + Properties cacheConfigurationProperties) { + this.cacheConfigurationProperties = cacheConfigurationProperties; + } + + public String getConfigurationUri() { + return configurationUri; + } + + public void setConfigurationUri(String configurationUri) { + this.configurationUri = configurationUri; + } +} \ No newline at end of file
