Updated Branches: refs/heads/camel-2.12.x a504a9198 -> 37372a8af refs/heads/master 8f89d1274 -> a185e7d76
CAMEL-6916: Properties component/placeholder should ignore non existing jvm/env variables if ignoreMissingLocation is enabled. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a185e7d7 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a185e7d7 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a185e7d7 Branch: refs/heads/master Commit: a185e7d769f6de8597d03b07f919c2898f14c0a6 Parents: 8f89d12 Author: Claus Ibsen <[email protected]> Authored: Sun Nov 10 12:49:28 2013 +0100 Committer: Claus Ibsen <[email protected]> Committed: Sun Nov 10 12:49:28 2013 +0100 ---------------------------------------------------------------------- .../properties/DefaultPropertiesResolver.java | 2 +- .../properties/PropertiesComponent.java | 30 ++++++++++++++------ .../PropertiesComponentDefaultTest.java | 29 +++++++++++++++++++ 3 files changed, 51 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/a185e7d7/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java b/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java index 8b3a202..dd730d4 100644 --- a/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java +++ b/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesResolver.java @@ -110,7 +110,7 @@ public class DefaultPropertiesResolver implements PropertiesResolver { if (path.startsWith("ref:")) { path = ObjectHelper.after(path, "ref:"); } - Properties answer = null; + Properties answer; try { answer = context.getRegistry().lookupByNameAndType(path, Properties.class); } catch (Exception ex) { http://git-wip-us.apache.org/repos/asf/camel/blob/a185e7d7/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java b/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java index 48e000b..7d0e75c 100644 --- a/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java +++ b/camel-core/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java @@ -17,7 +17,9 @@ package org.apache.camel.component.properties; import java.io.Serializable; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import java.util.Map; import java.util.Properties; @@ -25,6 +27,7 @@ import org.apache.camel.Endpoint; import org.apache.camel.impl.DefaultComponent; import org.apache.camel.util.FilePathResolver; import org.apache.camel.util.LRUSoftCache; +import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -120,7 +123,7 @@ public class PropertiesComponent extends DefaultComponent { // location may contain JVM system property or OS environment variables // so we need to parse those String[] locations = parseLocations(paths); - + // check cache first CacheKey key = new CacheKey(locations); prop = cache ? cacheMap.get(key) : null; @@ -280,19 +283,28 @@ public class PropertiesComponent extends DefaultComponent { } private String[] parseLocations(String[] locations) { - String[] answer = new String[locations.length]; + List<String> answer = new ArrayList<String>(); - for (int i = 0; i < locations.length; i++) { - String location = locations[i]; + for (String location : locations) { LOG.trace("Parsing location: {} ", location); - location = FilePathResolver.resolvePath(location); - - LOG.debug("Parsed location: {} ", location); - answer[i] = location; + try { + location = FilePathResolver.resolvePath(location); + LOG.debug("Parsed location: {} ", location); + if (ObjectHelper.isNotEmpty(location)) { + answer.add(location); + } + } catch (IllegalArgumentException e) { + if (!ignoreMissingLocation) { + throw e; + } else { + LOG.debug("Ignored missing location: {}", location); + } + } } - return answer; + // must return a not-null answer + return answer.toArray(new String[answer.size()]); } /** http://git-wip-us.apache.org/repos/asf/camel/blob/a185e7d7/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultTest.java b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultTest.java index b1c2ca0..5cdc53d 100644 --- a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentDefaultTest.java @@ -103,6 +103,35 @@ public class PropertiesComponentDefaultTest extends ContextTestSupport { assertMockEndpointsSatisfied(); } + public void testIgnoreMissingPropertySystemPropertyOnClasspath() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").to("properties:bar.end?locations=${my.home}/unknown.properties,org/apache/camel/component/properties/bar.properties" + + "&ignoreMissingLocation=true"); + } + }); + context.start(); + getMockEndpoint("mock:bar").expectedMessageCount(1); + template.sendBody("direct:start", "Hello World"); + assertMockEndpointsSatisfied(); + } + + public void testNotIgnoreMissingPropertySystemPropertyOnClasspath() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").to("properties:bar.end?locations=${my.home}/unknown.properties,org/apache/camel/component/properties/bar.properties" + + "&ignoreMissingLocation=false"); + } + }); + try { + context.start(); + fail("Should have thrown exception"); + } catch (FailedToCreateRouteException e) { + assertEquals("Cannot find JVM system property with key: my.home", e.getCause().getCause().getMessage()); + } + } @Override public boolean isUseRouteBuilder() {
