You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2014/01/28 15:26:28 UTC

[1/2] git commit: CAMEL-5940: Allow to configure ehcache xml file on cache component.

Updated Branches:
  refs/heads/camel-2.12.x eb2596ef4 -> af575cb54
  refs/heads/master ca124fd29 -> 1bfb73d27


CAMEL-5940: Allow to configure ehcache xml file on cache component.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1bfb73d2
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1bfb73d2
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1bfb73d2

Branch: refs/heads/master
Commit: 1bfb73d275ef656056ee0279272a1501cdd34536
Parents: ca124fd
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Jan 28 15:22:20 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Jan 28 15:22:20 2014 +0100

----------------------------------------------------------------------
 .../camel/component/cache/CacheComponent.java   | 25 +++++++-
 .../cache/DefaultCacheManagerFactory.java       | 25 +++++++-
 .../cache/CacheConfigurationFileTest.java       | 64 ++++++++++++++++++++
 .../src/test/resources/test-ehcache.xml         |  2 +-
 4 files changed, 113 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1bfb73d2/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java b/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java
index 86eab82..80958ef 100755
--- a/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java
+++ b/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.cache;
 
+import java.io.InputStream;
 import java.net.URI;
 import java.util.Map;
 
@@ -23,11 +24,13 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.impl.DefaultComponent;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ResourceHelper;
 import org.apache.camel.util.ServiceHelper;
 
 public class CacheComponent extends DefaultComponent {
     private CacheConfiguration configuration;
-    private CacheManagerFactory cacheManagerFactory = new DefaultCacheManagerFactory();
+    private CacheManagerFactory cacheManagerFactory;
+    private String configurationFile = "classpath:ehcache.xml";
     
     public CacheComponent() {
         configuration = new CacheConfiguration();
@@ -75,9 +78,29 @@ public class CacheComponent extends DefaultComponent {
         this.configuration = configuration;
     }
 
+    public String getConfigurationFile() {
+        return configurationFile;
+    }
+
+    /**
+     * Sets the location of the <tt>ehcache.xml</tt> file to load from classpath or file system.
+     * <p/>
+     * By default the file is loaded from <tt>classpath:ehcache.xml</tt>
+     */
+    public void setConfigurationFile(String configurationFile) {
+        this.configurationFile = configurationFile;
+    }
+
     @Override
     protected void doStart() throws Exception {
         super.doStart();
+        if (cacheManagerFactory == null) {
+            InputStream is = null;
+            if (configurationFile != null) {
+                is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(), configurationFile);
+            }
+            cacheManagerFactory = new DefaultCacheManagerFactory(is);
+        }
         ServiceHelper.startService(cacheManagerFactory);
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/1bfb73d2/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java b/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java
index 1c8d539..f0cec7c 100644
--- a/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java
+++ b/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java
@@ -16,13 +16,36 @@
  */
 package org.apache.camel.component.cache;
 
+import java.io.InputStream;
+
 import net.sf.ehcache.CacheManager;
+import org.apache.camel.util.IOHelper;
 
 public class DefaultCacheManagerFactory extends CacheManagerFactory {
 
+    private InputStream is;
+
+    public DefaultCacheManagerFactory() {
+        this(null);
+    }
+
+    public DefaultCacheManagerFactory(InputStream is) {
+        this.is = is;
+    }
+
     @Override
     protected CacheManager createCacheManagerInstance() {
-        return EHCacheUtil.createCacheManager(getClass().getResourceAsStream("/ehcache.xml"));
+        if (is == null) {
+            is = getClass().getResourceAsStream("/ehcache.xml");
+        }
+        return EHCacheUtil.createCacheManager(is);
     }
 
+    @Override
+    protected void doStop() throws Exception {
+        if (is != null) {
+            IOHelper.close(is);
+        }
+        super.doStop();
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/1bfb73d2/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheConfigurationFileTest.java
----------------------------------------------------------------------
diff --git a/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheConfigurationFileTest.java b/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheConfigurationFileTest.java
new file mode 100644
index 0000000..498092d
--- /dev/null
+++ b/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheConfigurationFileTest.java
@@ -0,0 +1,64 @@
+/**
+ * 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.cache;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import net.sf.ehcache.CacheManager;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class CacheConfigurationFileTest extends CamelTestSupport {
+
+    private CacheComponent cache;
+
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testConfigurationFile() throws Exception {
+        getMockEndpoint("mock:foo").expectedMessageCount(1);
+
+        Map map = new HashMap();
+        map.put(CacheConstants.CACHE_KEY, "myKey");
+        map.put(CacheConstants.CACHE_OPERATION, "ADD");
+        template.sendBodyAndHeaders("direct:start", "Hello World", map);
+
+        assertMockEndpointsSatisfied();
+
+        CacheManager cacheManager = cache.getCacheManagerFactory().getInstance();
+        assertNotNull(cacheManager);
+
+        assertEquals("target/mytemp", cacheManager.getConfiguration().getDiskStoreConfiguration().getPath());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                cache = context.getComponent("cache", CacheComponent.class);
+                cache.setConfigurationFile("classpath:test-ehcache.xml");
+
+                from("direct:start")
+                    .to("cache:foo");
+
+                from("cache:foo").to("mock:foo");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/1bfb73d2/components/camel-cache/src/test/resources/test-ehcache.xml
----------------------------------------------------------------------
diff --git a/components/camel-cache/src/test/resources/test-ehcache.xml b/components/camel-cache/src/test/resources/test-ehcache.xml
index 7360578..3f3bc71 100755
--- a/components/camel-cache/src/test/resources/test-ehcache.xml
+++ b/components/camel-cache/src/test/resources/test-ehcache.xml
@@ -19,7 +19,7 @@
          xsi:noNamespaceSchemaLocation="ehcache.xsd" >
 
   <!-- a very simple ehachace configuration only used for unit testing -->
-    <diskStore path="java.io.tmpdir"/>
+    <diskStore path="target/mytemp"/>
     <defaultCache
             maxElementsInMemory="10000"
             eternal="false"


[2/2] git commit: CAMEL-5940: Allow to configure ehcache xml file on cache component.

Posted by da...@apache.org.
CAMEL-5940: Allow to configure ehcache xml file on cache component.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/af575cb5
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/af575cb5
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/af575cb5

Branch: refs/heads/camel-2.12.x
Commit: af575cb54a09bd4e03cd33a069a6c7e2027ea73e
Parents: eb2596e
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Jan 28 15:22:20 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Jan 28 15:22:34 2014 +0100

----------------------------------------------------------------------
 .../camel/component/cache/CacheComponent.java   | 25 +++++++-
 .../cache/DefaultCacheManagerFactory.java       | 25 +++++++-
 .../cache/CacheConfigurationFileTest.java       | 64 ++++++++++++++++++++
 .../src/test/resources/test-ehcache.xml         |  2 +-
 4 files changed, 113 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/af575cb5/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java b/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java
index 86eab82..80958ef 100755
--- a/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java
+++ b/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.component.cache;
 
+import java.io.InputStream;
 import java.net.URI;
 import java.util.Map;
 
@@ -23,11 +24,13 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.impl.DefaultComponent;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.ResourceHelper;
 import org.apache.camel.util.ServiceHelper;
 
 public class CacheComponent extends DefaultComponent {
     private CacheConfiguration configuration;
-    private CacheManagerFactory cacheManagerFactory = new DefaultCacheManagerFactory();
+    private CacheManagerFactory cacheManagerFactory;
+    private String configurationFile = "classpath:ehcache.xml";
     
     public CacheComponent() {
         configuration = new CacheConfiguration();
@@ -75,9 +78,29 @@ public class CacheComponent extends DefaultComponent {
         this.configuration = configuration;
     }
 
+    public String getConfigurationFile() {
+        return configurationFile;
+    }
+
+    /**
+     * Sets the location of the <tt>ehcache.xml</tt> file to load from classpath or file system.
+     * <p/>
+     * By default the file is loaded from <tt>classpath:ehcache.xml</tt>
+     */
+    public void setConfigurationFile(String configurationFile) {
+        this.configurationFile = configurationFile;
+    }
+
     @Override
     protected void doStart() throws Exception {
         super.doStart();
+        if (cacheManagerFactory == null) {
+            InputStream is = null;
+            if (configurationFile != null) {
+                is = ResourceHelper.resolveMandatoryResourceAsInputStream(getCamelContext().getClassResolver(), configurationFile);
+            }
+            cacheManagerFactory = new DefaultCacheManagerFactory(is);
+        }
         ServiceHelper.startService(cacheManagerFactory);
     }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/af575cb5/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java
----------------------------------------------------------------------
diff --git a/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java b/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java
index 1c8d539..f0cec7c 100644
--- a/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java
+++ b/components/camel-cache/src/main/java/org/apache/camel/component/cache/DefaultCacheManagerFactory.java
@@ -16,13 +16,36 @@
  */
 package org.apache.camel.component.cache;
 
+import java.io.InputStream;
+
 import net.sf.ehcache.CacheManager;
+import org.apache.camel.util.IOHelper;
 
 public class DefaultCacheManagerFactory extends CacheManagerFactory {
 
+    private InputStream is;
+
+    public DefaultCacheManagerFactory() {
+        this(null);
+    }
+
+    public DefaultCacheManagerFactory(InputStream is) {
+        this.is = is;
+    }
+
     @Override
     protected CacheManager createCacheManagerInstance() {
-        return EHCacheUtil.createCacheManager(getClass().getResourceAsStream("/ehcache.xml"));
+        if (is == null) {
+            is = getClass().getResourceAsStream("/ehcache.xml");
+        }
+        return EHCacheUtil.createCacheManager(is);
     }
 
+    @Override
+    protected void doStop() throws Exception {
+        if (is != null) {
+            IOHelper.close(is);
+        }
+        super.doStop();
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/af575cb5/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheConfigurationFileTest.java
----------------------------------------------------------------------
diff --git a/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheConfigurationFileTest.java b/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheConfigurationFileTest.java
new file mode 100644
index 0000000..498092d
--- /dev/null
+++ b/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheConfigurationFileTest.java
@@ -0,0 +1,64 @@
+/**
+ * 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.cache;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import net.sf.ehcache.CacheManager;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class CacheConfigurationFileTest extends CamelTestSupport {
+
+    private CacheComponent cache;
+
+    @Test
+    @SuppressWarnings("unchecked")
+    public void testConfigurationFile() throws Exception {
+        getMockEndpoint("mock:foo").expectedMessageCount(1);
+
+        Map map = new HashMap();
+        map.put(CacheConstants.CACHE_KEY, "myKey");
+        map.put(CacheConstants.CACHE_OPERATION, "ADD");
+        template.sendBodyAndHeaders("direct:start", "Hello World", map);
+
+        assertMockEndpointsSatisfied();
+
+        CacheManager cacheManager = cache.getCacheManagerFactory().getInstance();
+        assertNotNull(cacheManager);
+
+        assertEquals("target/mytemp", cacheManager.getConfiguration().getDiskStoreConfiguration().getPath());
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                cache = context.getComponent("cache", CacheComponent.class);
+                cache.setConfigurationFile("classpath:test-ehcache.xml");
+
+                from("direct:start")
+                    .to("cache:foo");
+
+                from("cache:foo").to("mock:foo");
+            }
+        };
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/af575cb5/components/camel-cache/src/test/resources/test-ehcache.xml
----------------------------------------------------------------------
diff --git a/components/camel-cache/src/test/resources/test-ehcache.xml b/components/camel-cache/src/test/resources/test-ehcache.xml
index 7360578..3f3bc71 100755
--- a/components/camel-cache/src/test/resources/test-ehcache.xml
+++ b/components/camel-cache/src/test/resources/test-ehcache.xml
@@ -19,7 +19,7 @@
          xsi:noNamespaceSchemaLocation="ehcache.xsd" >
 
   <!-- a very simple ehachace configuration only used for unit testing -->
-    <diskStore path="java.io.tmpdir"/>
+    <diskStore path="target/mytemp"/>
     <defaultCache
             maxElementsInMemory="10000"
             eternal="false"