You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2016/05/12 09:52:46 UTC
camel git commit: CAMEL-9884 : improve configuration
Repository: camel
Updated Branches:
refs/heads/master d18912325 -> e904bf498
CAMEL-9884 : improve configuration
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e904bf49
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e904bf49
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e904bf49
Branch: refs/heads/master
Commit: e904bf4982fb3b388c98a4db95da24193b6af0bd
Parents: d189123
Author: lburgazzoli <lb...@gmail.com>
Authored: Thu May 12 11:51:37 2016 +0200
Committer: lburgazzoli <lb...@gmail.com>
Committed: Thu May 12 11:52:13 2016 +0200
----------------------------------------------------------------------
.../component/ehcache/EhcacheConfiguration.java | 111 ++-----------------
.../camel/component/ehcache/EhcacheManager.java | 18 +--
.../ehcache/EhcacheConfigurationTest.java | 89 +++++++++++++++
3 files changed, 101 insertions(+), 117 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/e904bf49/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 a0bae81..087148a 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
@@ -19,7 +19,6 @@ package org.apache.camel.component.ehcache;
import java.io.IOException;
import java.net.URL;
import java.util.EnumSet;
-import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
@@ -28,11 +27,10 @@ import org.apache.camel.CamelContext;
import org.apache.camel.spi.UriParam;
import org.apache.camel.spi.UriParams;
import org.apache.camel.util.EndpointHelper;
-import org.apache.camel.util.IntrospectionSupport;
+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.ResourcePools;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.event.EventFiring;
import org.ehcache.event.EventOrdering;
@@ -41,7 +39,7 @@ import org.ehcache.xml.XmlConfiguration;
@UriParams
public class EhcacheConfiguration {
- public static final String PREFIX_CACHE = "cache.";
+ public static final String PREFIX_CONF = "conf.";
public static final String PREFIX_POOL = "pool.";
private final CamelContext context;
@@ -61,14 +59,7 @@ public class EhcacheConfiguration {
@UriParam
private CacheManager cacheManager;
@UriParam(label = "advanced")
- private CacheConfiguration<?, ?> defaultCacheConfiguration;
- @UriParam(label = "advanced")
- private ResourcePools defaultCacheResourcePools;
-
- @UriParam(label = "advanced", prefix = PREFIX_CACHE, multiValue = true, javaType = "java.lang.String")
- private Map<String, CacheConfiguration> cacheConfigurations;
- @UriParam(label = "advanced", prefix = PREFIX_POOL, multiValue = true, javaType = "java.lang.String")
- private Map<String, ResourcePools> cacheResourcePools;
+ private CacheConfiguration<?, ?> configuration;
@UriParam(
label = "consumer",
@@ -231,93 +222,16 @@ public class EhcacheConfiguration {
/**
* The default cache configuration to be used to create caches.
*/
- public void setDefaultCacheConfiguration(CacheConfiguration<?, ?> defaultCacheConfiguration) {
- this.defaultCacheConfiguration = defaultCacheConfiguration;
- }
-
- public CacheConfiguration<?, ?> getDefaultCacheConfiguration() {
- return defaultCacheConfiguration;
- }
-
- /**
- * The cache configuration to be used for cache cacheName.
- */
- public void addCacheConfiguration(String cacheName, CacheConfiguration cacheConfiguration) {
- if (cacheConfigurations == null) {
- cacheConfigurations = new HashMap<>();
- }
-
- cacheConfigurations.put(cacheName, cacheConfiguration);
- }
-
- EhcacheConfiguration addCacheConfigurationFromParameters(Map<String, Object> parameters) {
- Map<String, Object> models = IntrospectionSupport.extractProperties(parameters, PREFIX_CACHE);
- for (Map.Entry<String, Object> entry : models.entrySet()) {
- addCacheConfiguration(
- entry.getKey(),
- EndpointHelper.resolveParameter(
- context,
- (String)entry.getValue(),
- CacheConfiguration.class
- )
- );
- }
-
- return this;
- }
-
- public CacheConfiguration getCacheConfiguration(String cacheName) {
- return cacheConfigurations != null
- ? cacheConfigurations.getOrDefault(cacheName, defaultCacheConfiguration)
- : defaultCacheConfiguration;
- }
-
- // ****************************
- // Cache Resource Pools
- // ****************************
-
- public ResourcePools getDefaultCacheResourcePools() {
- return defaultCacheResourcePools;
- }
-
- /**
- * The default resource pools to be used to create caches.
- */
- public void setDefaultCacheResourcePools(ResourcePools defaultCacheResourcePools) {
- this.defaultCacheResourcePools = defaultCacheResourcePools;
+ public <K, V> void setConfiguration(CacheConfiguration<K, V> configuration) {
+ this.configuration = configuration;
}
- /**
- * The resource pools to be used for cache cacheName.
- */
- public void addResourcePools(String cacheName, ResourcePools resourcePools) {
- if (cacheResourcePools == null) {
- cacheResourcePools = new HashMap<>();
- }
-
- cacheResourcePools.put(cacheName, resourcePools);
- }
-
- EhcacheConfiguration addResourcePoolsFromParameters(Map<String, Object> parameters) {
- Map<String, Object> models = IntrospectionSupport.extractProperties(parameters, PREFIX_POOL);
- for (Map.Entry<String, Object> entry : models.entrySet()) {
- addResourcePools(
- entry.getKey(),
- EndpointHelper.resolveParameter(
- context,
- (String)entry.getValue(),
- ResourcePools.class
- )
- );
- }
-
- return this;
+ public <K, V> CacheConfiguration<K, V> getConfiguration() {
+ return (CacheConfiguration<K, V>)configuration;
}
- public ResourcePools getResourcePools(String cacheName) {
- return cacheResourcePools != null
- ? cacheResourcePools.getOrDefault(cacheName, defaultCacheResourcePools)
- : defaultCacheResourcePools;
+ public <K, V> CacheConfiguration<K, V> getMandatoryConfiguration() {
+ return ObjectHelper.notNull(getConfiguration(), "CacheConfiguration");
}
// ****************************
@@ -326,9 +240,6 @@ public class EhcacheConfiguration {
static EhcacheConfiguration create(CamelContext context, String remaining, Map<String, Object> parameters) throws Exception {
EhcacheConfiguration configuration = new EhcacheConfiguration(context, remaining);
- configuration.addCacheConfigurationFromParameters(parameters);
- configuration.addResourcePoolsFromParameters(parameters);
-
EndpointHelper.setReferenceProperties(context, configuration, parameters);
EndpointHelper.setProperties(context, configuration, parameters);
@@ -344,8 +255,8 @@ public class EhcacheConfiguration {
manager = CacheManagerBuilder.newCacheManager(new XmlConfiguration(getConfigUriAsUrl()));
} else {
CacheManagerBuilder builder = CacheManagerBuilder.newCacheManagerBuilder();
- if (cacheConfigurations != null) {
- cacheConfigurations.forEach(builder::withCache);
+ if (configuration != null) {
+ builder.withCache(cacheName, configuration);
}
manager = builder.build();
http://git-wip-us.apache.org/repos/asf/camel/blob/e904bf49/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 7cfb7d9..9fa24ee 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,13 +22,9 @@ import org.apache.camel.Service;
import org.apache.camel.util.ObjectHelper;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
-import org.ehcache.config.CacheConfiguration;
-import org.ehcache.config.ResourcePools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import static org.ehcache.config.builders.CacheConfigurationBuilder.newCacheConfigurationBuilder;
-
public class EhcacheManager implements Service {
private static final Logger LOGGER = LoggerFactory.getLogger(EhcacheManager.class);
@@ -73,19 +69,7 @@ public class EhcacheManager implements Service {
public <K, V> Cache<K, V> getCache(String name, Class<K> keyType, Class<V> valueType) throws Exception {
Cache<K, V> cache = cacheManager.getCache(name, keyType, valueType);
if (cache == null && configuration != null && configuration.isCreateCacheIfNotExist()) {
- final CacheConfiguration config = configuration.getCacheConfiguration(name);
- final ResourcePools pools = configuration.getResourcePools(name);
-
- if (config == null && pools == null) {
- throw new IllegalArgumentException("No cache config and resource pools for cache " + name);
- }
-
- cache = cacheManager.createCache(
- name,
- config != null
- ? config
- : newCacheConfigurationBuilder(keyType, valueType, pools).build()
- );
+ cache = cacheManager.createCache(name, configuration.getMandatoryConfiguration());
}
return cache;
http://git-wip-us.apache.org/repos/asf/camel/blob/e904bf49/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
new file mode 100644
index 0000000..f1e9df2
--- /dev/null
+++ b/components/camel-ehcache/src/test/java/org/apache/camel/component/ehcache/EhcacheConfigurationTest.java
@@ -0,0 +1,89 @@
+/**
+ * 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.ResourcePoolsBuilder;
+import org.ehcache.config.units.EntryUnit;
+import org.ehcache.config.units.MemoryUnit;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class EhcacheConfigurationTest extends CamelTestSupport {
+ public static final Logger LOGGER = LoggerFactory.getLogger(EhcacheConfigurationTest.class);
+
+ @EndpointInject(uri = "ehcache://myCacheConf?configuration=#myConf")
+ private EhcacheEndpoint ehcacheConf;
+
+ @Override
+ protected JndiRegistry createRegistry() throws Exception {
+ JndiRegistry registry = super.createRegistry();
+ registry.bind(
+ "myConf",
+ CacheConfigurationBuilder.newCacheConfigurationBuilder(
+ String.class,
+ String.class,
+ ResourcePoolsBuilder.newResourcePoolsBuilder()
+ .heap(100, EntryUnit.ENTRIES)
+ .offheap(1, MemoryUnit.MB))
+ .build()
+ );
+
+ return registry;
+ }
+
+ @Test
+ public void testProgrammaticConfiguration() throws Exception {
+ EhcacheManager manager = ehcacheConf.getManager();
+ Cache<String, String> cache = manager.getCache("myCacheConf", 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://start").to(ehcacheConf);
+ }
+ };
+ }
+}