Author: davsclaus
Date: Fri Jan 13 16:01:40 2012
New Revision: 1231142

URL: http://svn.apache.org/viewvc?rev=1231142&view=rev
Log:
CAMEL-4849: Added option to ignore missing property location. Thanks to Bilgin 
for the patch.

Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesResolver.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultTest.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesResolverTest.java
    
camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesResolver.java
    
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
    
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java?rev=1231142&r1=1231141&r2=1231142&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java
 Fri Jan 13 16:01:40 2012
@@ -38,21 +38,21 @@ import org.apache.camel.util.ObjectHelpe
  */
 public class DefaultPropertiesResolver implements PropertiesResolver {
 
-    public Properties resolveProperties(CamelContext context, String... uri) 
throws Exception {
+    public Properties resolveProperties(CamelContext context, boolean 
ignoreMissingLocation, String... uri) throws Exception {
         Properties answer = new Properties();
 
         for (String path : uri) {
             if (path.startsWith("ref:")) {
-                Properties prop = loadPropertiesFromRegistry(context, path);
+                Properties prop = loadPropertiesFromRegistry(context, 
ignoreMissingLocation, path);
                 prop = prepareLoadedProperties(prop);
                 answer.putAll(prop);
             } else if (path.startsWith("file:")) {
-                Properties prop = loadPropertiesFromFilePath(context, path);
+                Properties prop = loadPropertiesFromFilePath(context, 
ignoreMissingLocation, path);
                 prop = prepareLoadedProperties(prop);
                 answer.putAll(prop);
             } else {
                 // default to classpath
-                Properties prop = loadPropertiesFromClasspath(context, path);
+                Properties prop = loadPropertiesFromClasspath(context, 
ignoreMissingLocation, path);
                 prop = prepareLoadedProperties(prop);
                 answer.putAll(prop);
             }
@@ -61,17 +61,21 @@ public class DefaultPropertiesResolver i
         return answer;
     }
 
-    protected Properties loadPropertiesFromFilePath(CamelContext context, 
String path) throws IOException {
-        Properties answer = null;
+    protected Properties loadPropertiesFromFilePath(CamelContext context, 
boolean ignoreMissingLocation, String path) throws IOException {
+        Properties answer = new Properties();
 
         if (path.startsWith("file:")) {
             path = ObjectHelper.after(path, "file:");
         }
 
-        InputStream is = new FileInputStream(path);
+        InputStream is = null;
         try {
-            answer = new Properties();
+            is = new FileInputStream(path);
             answer.load(is);
+        } catch (FileNotFoundException e) {
+            if (!ignoreMissingLocation) {
+                throw e;
+            }
         } finally {
             IOHelper.close(is);
         }
@@ -79,8 +83,8 @@ public class DefaultPropertiesResolver i
         return answer;
     }
 
-    protected Properties loadPropertiesFromClasspath(CamelContext context, 
String path) throws IOException {
-        Properties answer = null;
+    protected Properties loadPropertiesFromClasspath(CamelContext context, 
boolean ignoreMissingLocation, String path) throws IOException {
+        Properties answer = new Properties();
 
         if (path.startsWith("classpath:")) {
             path = ObjectHelper.after(path, "classpath:");
@@ -88,28 +92,28 @@ public class DefaultPropertiesResolver i
 
         InputStream is = context.getClassResolver().loadResourceAsStream(path);
         if (is == null) {
-            throw new FileNotFoundException("Properties file " + path + " not 
found in classpath");
-        }
-
-        try {
-            answer = new Properties();
-            answer.load(is);
-        } finally {
-            IOHelper.close(is);
+            if (!ignoreMissingLocation) {
+                throw new FileNotFoundException("Properties file " + path + " 
not found in classpath");
+            }
+        } else {
+            try {
+                answer.load(is);
+            } finally {
+                IOHelper.close(is);
+            }
         }
-
         return answer;
     }
 
-    protected Properties loadPropertiesFromRegistry(CamelContext context, 
String path) throws IOException {
+    protected Properties loadPropertiesFromRegistry(CamelContext context, 
boolean ignoreMissingLocation, String path) throws IOException {
         if (path.startsWith("ref:")) {
             path = ObjectHelper.after(path, "ref:");
         }
         Properties answer = context.getRegistry().lookup(path, 
Properties.class);
-        if (answer == null) {
+        if (answer == null && (!ignoreMissingLocation)) {
             throw new FileNotFoundException("Properties " + path + " not found 
in registry");
         }
-        return answer;
+        return answer != null ? answer : new Properties();
     }
 
     /**

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=1231142&r1=1231141&r2=1231142&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 Jan 13 16:01:40 2012
@@ -69,6 +69,7 @@ public class PropertiesComponent extends
     private PropertiesResolver propertiesResolver = new 
DefaultPropertiesResolver();
     private PropertiesParser propertiesParser = new DefaultPropertiesParser();
     private String[] locations;
+    private boolean ignoreMissingLocation;
     private boolean cache = true;
     private String propertyPrefix;
     private String propertySuffix;
@@ -93,10 +94,15 @@ public class PropertiesComponent extends
 
         // override default locations
         String locations = getAndRemoveParameter(parameters, "locations", 
String.class);
+        Boolean ignoreMissingLocationLoc = getAndRemoveParameter(parameters, 
"ignoreMissingLocation", Boolean.class);
         if (locations != null) {
             LOG.trace("Overriding default locations with location: {}", 
locations);
             paths = locations.split(",");
         }
+        if (ignoreMissingLocationLoc != null) {
+            ignoreMissingLocation = ignoreMissingLocationLoc;
+        }
+
         String endpointUri = parseUri(remaining, paths);
         LOG.debug("Endpoint uri parsed as: {}", endpointUri);
         return getCamelContext().getEndpoint(endpointUri);
@@ -117,7 +123,7 @@ public class PropertiesComponent extends
             CacheKey key = new CacheKey(locations);
             prop = cache ? cacheMap.get(key) : null;
             if (prop == null) {
-                prop = propertiesResolver.resolveProperties(getCamelContext(), 
locations);
+                prop = propertiesResolver.resolveProperties(getCamelContext(), 
ignoreMissingLocation, locations);
                 if (cache) {
                     cacheMap.put(key, prop);
                 }
@@ -201,7 +207,15 @@ public class PropertiesComponent extends
     public void setFallbackToUnaugmentedProperty(boolean 
fallbackToUnaugmentedProperty) {
         this.fallbackToUnaugmentedProperty = fallbackToUnaugmentedProperty;
     }
-    
+
+    public boolean isIgnoreMissingLocation() {
+        return ignoreMissingLocation;
+    }
+
+    public void setIgnoreMissingLocation(boolean ignoreMissingLocation) {
+        this.ignoreMissingLocation = ignoreMissingLocation;
+    }
+
     public String getPrefixToken() {
         return prefixToken;
     }

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesResolver.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesResolver.java?rev=1231142&r1=1231141&r2=1231142&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesResolver.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesResolver.java
 Fri Jan 13 16:01:40 2012
@@ -32,11 +32,12 @@ public interface PropertiesResolver {
     /**
      * Resolve properties from the given uri
      *
+     *
      * @param context the camel context
-     * @param uri uri(s) defining the source(s)
-     * @return the properties
+     * @param ignoreMissingLocation ignore silently if the property file is 
missing
+     * @param uri uri(s) defining the source(s)  @return the properties
      * @throws Exception is thrown if resolving the properties failed
      */
-    Properties resolveProperties(CamelContext context, String... uri) throws 
Exception;
+    Properties resolveProperties(CamelContext context, boolean 
ignoreMissingLocation, String... uri) throws Exception;
     
 }

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultTest.java?rev=1231142&r1=1231141&r2=1231142&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultTest.java
 Fri Jan 13 16:01:40 2012
@@ -61,6 +61,49 @@ public class PropertiesComponentDefaultT
         }
     }
 
+    public void testIgnoreMissingPropertyFilesOnClasspath() throws Exception {
+        System.setProperty("bar.end", "mock:bar");
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                
from("direct:start").to("properties:bar.end?locations=org/apache/camel/component/properties/unknown.properties&ignoreMissingLocation=true");
+            }
+        });
+        context.start();
+        getMockEndpoint("mock:bar").expectedMessageCount(1);
+        template.sendBody("direct:start", "Hello World");
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testIgnoreMissingPropertyFilesFromRegistry() throws Exception {
+        System.setProperty("bar.end", "mock:bar");
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                
from("direct:start").to("properties:bar.end?locations=ref:unknown.properties&ignoreMissingLocation=true");
+            }
+        });
+        context.start();
+        getMockEndpoint("mock:bar").expectedMessageCount(1);
+        template.sendBody("direct:start", "Hello World");
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testIgnoreMissingPropertyFilesFromFilePath() throws Exception {
+        System.setProperty("bar.end", "mock:bar");
+        context.addRoutes(new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                
from("direct:start").to("properties:bar.end?locations=file:unknown.properties&ignoreMissingLocation=true");
+            }
+        });
+        context.start();
+        getMockEndpoint("mock:bar").expectedMessageCount(1);
+        template.sendBody("direct:start", "Hello World");
+        assertMockEndpointsSatisfied();
+    }
+
+
     @Override
     public boolean isUseRouteBuilder() {
         return false;

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesResolverTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesResolverTest.java?rev=1231142&r1=1231141&r2=1231142&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesResolverTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesResolverTest.java
 Fri Jan 13 16:01:40 2012
@@ -64,7 +64,7 @@ public class PropertiesResolverTest exte
 
     public static class MyCustomResolver implements PropertiesResolver {
 
-        public Properties resolveProperties(CamelContext context, String... 
uri) throws Exception {
+        public Properties resolveProperties(CamelContext context, boolean 
ignoreMissingLocation, String... uri) throws Exception {
             Properties answer = new Properties();
             answer.put("foo", "mock:result");
             return answer;

Modified: 
camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesResolver.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesResolver.java?rev=1231142&r1=1231141&r2=1231142&view=diff
==============================================================================
--- 
camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesResolver.java
 (original)
+++ 
camel/trunk/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/BlueprintPropertiesResolver.java
 Fri Jan 13 16:01:40 2012
@@ -40,7 +40,7 @@ public class BlueprintPropertiesResolver
     }
 
     @Override
-    public Properties resolveProperties(CamelContext context, String... urls) 
throws Exception {
+    public Properties resolveProperties(CamelContext context, boolean 
ignoreMissingLocation, String... urls) throws Exception {
         Properties answer = new Properties();
 
         boolean explicit = false;
@@ -53,7 +53,7 @@ public class BlueprintPropertiesResolver
                 explicit = true;
             } else {
                 // delegate the url
-                answer.putAll(delegate.resolveProperties(context, url));
+                answer.putAll(delegate.resolveProperties(context, 
ignoreMissingLocation, url));
             }
         }
 

Modified: 
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java?rev=1231142&r1=1231141&r2=1231142&view=diff
==============================================================================
--- 
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
 (original)
+++ 
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
 Fri Jan 13 16:01:40 2012
@@ -395,6 +395,10 @@ public abstract class AbstractCamelConte
             PropertiesComponent pc = new PropertiesComponent();
             pc.setLocation(def.getLocation());
 
+            if (def.isIgnoreMissingLocation() != null) {
+                pc.setIgnoreMissingLocation(def.isIgnoreMissingLocation());
+            }
+
             // if using a custom resolver
             if (ObjectHelper.isNotEmpty(def.getPropertiesResolverRef())) {
                 PropertiesResolver resolver = 
CamelContextHelper.mandatoryLookup(getContext(), def.getPropertiesResolverRef(),

Modified: 
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java?rev=1231142&r1=1231141&r2=1231142&view=diff
==============================================================================
--- 
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java
 (original)
+++ 
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/CamelPropertyPlaceholderDefinition.java
 Fri Jan 13 16:01:40 2012
@@ -36,6 +36,9 @@ public class CamelPropertyPlaceholderDef
     private String location;
 
     @XmlAttribute
+    private Boolean ignoreMissingLocation;
+
+    @XmlAttribute
     private String propertiesResolverRef;
 
     @XmlAttribute
@@ -104,6 +107,14 @@ public class CamelPropertyPlaceholderDef
         this.fallbackToUnaugmentedProperty = fallbackToUnaugmentedProperty;
     }
 
+    public Boolean isIgnoreMissingLocation() {
+        return ignoreMissingLocation;
+    }
+
+    public void setIgnoreMissingLocation(Boolean ignoreMissingLocation) {
+        this.ignoreMissingLocation = ignoreMissingLocation;
+    }
+
     public String getPrefixToken() {
         return prefixToken;
     }


Reply via email to