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 2011/01/04 15:17:14 UTC

svn commit: r1055051 - in /camel/trunk/components/camel-cache/src: main/java/org/apache/camel/component/cache/ test/java/org/apache/camel/component/cache/

Author: davsclaus
Date: Tue Jan  4 14:17:14 2011
New Revision: 1055051

URL: http://svn.apache.org/viewvc?rev=1055051&view=rev
Log:
CAMEL-3473: Fixed camel-cache configuration not defensivly copied when creating endpoint. Thanks to Tracy Snell for patch.

Added:
    camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheConfigurationTest.java   (with props)
Modified:
    camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java
    camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheConfiguration.java

Modified: camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java?rev=1055051&r1=1055050&r2=1055051&view=diff
==============================================================================
--- camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java (original)
+++ camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheComponent.java Tue Jan  4 14:17:14 2011
@@ -22,24 +22,29 @@ import java.util.Map;
 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.ServiceHelper;
 
 public class CacheComponent extends DefaultComponent {
-    private CacheConfiguration config;
+    private CacheConfiguration configuration;
     private CacheManagerFactory cacheManagerFactory = new CacheManagerFactory();
     
     public CacheComponent() {
-        config = new CacheConfiguration();
+        configuration = new CacheConfiguration();
     }
 
     public CacheComponent(CamelContext context) {
         super(context);
-        config = new CacheConfiguration();
+        configuration = new CacheConfiguration();
     }
 
     @Override
     @SuppressWarnings("unchecked")
     protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception {
+        // must use copy as each endpoint can have different options
+        ObjectHelper.notNull(configuration, "configuration");
+        CacheConfiguration config = configuration.copy();
+
         config.parseURI(new URI(uri));
         
         CacheEndpoint cacheEndpoint = new CacheEndpoint(uri, this, config, cacheManagerFactory);
@@ -55,6 +60,19 @@ public class CacheComponent extends Defa
         this.cacheManagerFactory = cacheManagerFactory;
     }
 
+    public CacheConfiguration getConfiguration() {
+        return configuration;
+    }
+
+    /**
+     * Sets the Cache configuration
+     *
+     * @param configuration the configuration to use by default for endpoints
+     */
+    public void setConfiguration(CacheConfiguration configuration) {
+        this.configuration = configuration;
+    }
+
     @Override
     protected void doStart() throws Exception {
         super.doStart();

Modified: camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheConfiguration.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheConfiguration.java?rev=1055051&r1=1055050&r2=1055051&view=diff
==============================================================================
--- camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheConfiguration.java (original)
+++ camel/trunk/components/camel-cache/src/main/java/org/apache/camel/component/cache/CacheConfiguration.java Tue Jan  4 14:17:14 2011
@@ -20,9 +20,10 @@ import java.net.URI;
 import java.util.Map;
 
 import net.sf.ehcache.store.MemoryStoreEvictionPolicy;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.util.URISupport;
 
-public class CacheConfiguration {
+public class CacheConfiguration implements Cloneable {
     private String cacheName;
     private int maxElementsInMemory = 1000;
     private MemoryStoreEvictionPolicy memoryStoreEvictionPolicy = MemoryStoreEvictionPolicy.LFU;
@@ -40,7 +41,17 @@ public class CacheConfiguration {
     public CacheConfiguration(URI uri) throws Exception {
         parseURI(uri);
     }
-    
+
+    public CacheConfiguration copy() {
+        try {
+            CacheConfiguration copy = (CacheConfiguration) clone();
+            // override any properties where a reference copy isn't what we want
+            return copy;
+        } catch (CloneNotSupportedException e) {
+            throw new RuntimeCamelException(e);
+        }
+    }
+
     public void parseURI(URI uri) throws Exception {
         String protocol = uri.getScheme();
         

Added: camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheConfigurationTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheConfigurationTest.java?rev=1055051&view=auto
==============================================================================
--- camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheConfigurationTest.java (added)
+++ camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheConfigurationTest.java Tue Jan  4 14:17:14 2011
@@ -0,0 +1,43 @@
+/**
+ * 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 org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class CacheConfigurationTest {
+
+    @Test
+    public void doURICheck() throws Exception {
+        CamelContext context = new DefaultCamelContext();
+
+        CacheComponent component = new CacheComponent(context);
+
+        CacheEndpoint endpoint1 = (CacheEndpoint) component.createEndpoint("cache://myname1?diskPersistent=true");
+
+        CacheEndpoint endpoint2 = (CacheEndpoint) component.createEndpoint("cache://myname2?diskPersistent=false");
+
+        Assert.assertTrue("Endpoint1 cache name is myname1", "myname1".equals(endpoint1.getConfig().getCacheName()));
+        Assert.assertTrue("Endpoint2 cache name is myname2", "myname2".equals(endpoint2.getConfig().getCacheName()));
+
+        Assert.assertTrue("Endpoint1 is true", endpoint1.getConfig().isDiskPersistent());
+        Assert.assertTrue("Endpoint2 is false", !endpoint2.getConfig().isDiskPersistent());
+    }
+}

Propchange: camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheConfigurationTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-cache/src/test/java/org/apache/camel/component/cache/CacheConfigurationTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date