camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1149488 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/component/properties/PropertiesComponent.java test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
Date Fri, 22 Jul 2011 07:28:49 GMT
Author: davsclaus
Date: Fri Jul 22 07:28:48 2011
New Revision: 1149488

URL: http://svn.apache.org/viewvc?rev=1149488&view=rev
Log:
CAMEL-4261: Fixed properties component may contain duplicates in its location cache.

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java?rev=1149488&r1=1149487&r2=1149488&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
Fri Jul 22 07:28:48 2011
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.component.properties;
 
+import java.io.Serializable;
+import java.util.Arrays;
 import java.util.Map;
 import java.util.Properties;
 import java.util.regex.Matcher;
@@ -23,7 +25,7 @@ import java.util.regex.Pattern;
 
 import org.apache.camel.Endpoint;
 import org.apache.camel.impl.DefaultComponent;
-import org.apache.camel.util.LRUCache;
+import org.apache.camel.util.LRUSoftCache;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.ServiceHelper;
 import org.slf4j.Logger;
@@ -44,7 +46,7 @@ public class PropertiesComponent extends
     private static final Pattern SYS_PATTERN = Pattern.compile("\\$\\{(.*?)\\}", Pattern.DOTALL);
 
     private static final transient Logger LOG = LoggerFactory.getLogger(PropertiesComponent.class);
-    private final Map<String[], Properties> cacheMap = new LRUCache<String[], Properties>(1000);
+    private final Map<CacheKey, Properties> cacheMap = new LRUSoftCache<CacheKey,
Properties>(1000);
     private PropertiesResolver propertiesResolver = new DefaultPropertiesResolver();
     private PropertiesParser propertiesParser = new DefaultPropertiesParser();
     private String[] locations;
@@ -88,11 +90,12 @@ public class PropertiesComponent extends
         String[] locations = parseLocations(paths);
 
         // check cache first
-        Properties prop = cache ? cacheMap.get(locations) : null;
+        CacheKey key = new CacheKey(locations);
+        Properties prop = cache ? cacheMap.get(key) : null;
         if (prop == null) {
             prop = propertiesResolver.resolveProperties(getCamelContext(), locations);
             if (cache) {
-                cacheMap.put(locations, prop);
+                cacheMap.put(key, prop);
             }
         }
 
@@ -170,7 +173,7 @@ public class PropertiesComponent extends
                 if (ObjectHelper.isEmpty(value)) {
                     throw new IllegalArgumentException("Cannot find system environment with
key: " + key);
                 }
-                // must quoute the replacement to have it work as literal replacement
+                // must quote the replacement to have it work as literal replacement
                 value = Matcher.quoteReplacement(value);
                 location = matcher.replaceFirst(value);
                 // must match again as location is changed
@@ -184,7 +187,7 @@ public class PropertiesComponent extends
                 if (ObjectHelper.isEmpty(value)) {
                     throw new IllegalArgumentException("Cannot find JVM system property with
key: " + key);
                 }
-                // must quoute the replacement to have it work as literal replacement
+                // must quote the replacement to have it work as literal replacement
                 value = Matcher.quoteReplacement(value);
                 location = matcher.replaceFirst(value);
                 // must match again as location is changed
@@ -198,4 +201,48 @@ public class PropertiesComponent extends
         return answer;
     }
 
+    /**
+     * Key used in the locations cache
+     */
+    private final class CacheKey implements Serializable {
+
+        private final String[] locations;
+
+        private CacheKey(String[] locations) {
+            this.locations = locations;
+        }
+
+        public String[] getLocations() {
+            return locations;
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (this == o) {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass()) {
+                return false;
+            }
+
+            CacheKey that = (CacheKey) o;
+
+            if (!Arrays.equals(locations, that.locations)) {
+                return false;
+            }
+
+            return true;
+        }
+
+        @Override
+        public int hashCode() {
+            return locations != null ? Arrays.hashCode(locations) : 0;
+        }
+
+        @Override
+        public String toString() {
+            return "LocationKey[" + Arrays.asList(locations).toString() + "]";
+        }
+    }
+
 }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java?rev=1149488&r1=1149487&r2=1149488&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentTest.java
Fri Jul 22 07:28:48 2011
@@ -48,6 +48,40 @@ public class PropertiesComponentTest ext
         assertMockEndpointsSatisfied();
     }
 
+    public void testPropertiesComponentTwo() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("properties:{{cool.end}}");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:result").expectedMessageCount(2);
+
+        template.sendBody("direct:start", "Hello World");
+        template.sendBody("direct:start", "Bye World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testPropertiesComponentTemplate() throws Exception {
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:cool").to("mock:result");
+            }
+        });
+        context.start();
+
+        getMockEndpoint("mock:result").expectedMessageCount(2);
+
+        template.sendBody("{{cool.start}}", "Hello World");
+        template.sendBody("{{cool.start}}", "Bye World");
+
+        assertMockEndpointsSatisfied();
+    }
+
     public void testPropertiesComponentResult() throws Exception {
         context.addRoutes(new RouteBuilder() {
             @Override



Mime
View raw message