Repository: camel Updated Branches: refs/heads/camel-2.14.x 279b7f3d0 -> 48a75fa38
CAMEL-8107: Allow to use property placeholder with default values without having to setup the properties component Conflicts: camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3beb8e2d Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3beb8e2d Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3beb8e2d Branch: refs/heads/camel-2.14.x Commit: 3beb8e2dbbabae82eac78894723c469dd0e1e223 Parents: 279b7f3 Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Dec 2 20:17:56 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Wed Dec 3 08:53:34 2014 +0100 ---------------------------------------------------------------------- .../properties/DefaultPropertiesParser.java | 27 ++++++- .../properties/PropertiesComponent.java | 9 ++- .../apache/camel/impl/DefaultCamelContext.java | 22 ++--- .../PropertiesComponentEndpointTest.java | 3 +- ...ertiesComponentOnlyUseDefaultValuesTest.java | 85 ++++++++++++++++++++ 5 files changed, 130 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/3beb8e2d/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java b/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java index 6a5b80f..137c6c1 100644 --- a/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java +++ b/camel-core/src/main/java/org/apache/camel/component/properties/DefaultPropertiesParser.java @@ -26,15 +26,32 @@ import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - /** - * A parser to parse a string which contains property placeholders + * A parser to parse a string which contains property placeholders. */ public class DefaultPropertiesParser implements AugmentedPropertyNameAwarePropertiesParser { - protected final Logger log = LoggerFactory.getLogger(getClass()); private static final String GET_OR_ELSE_TOKEN = ":"; + protected final Logger log = LoggerFactory.getLogger(getClass()); + + private PropertiesComponent propertiesComponent; + + public DefaultPropertiesParser() { + } + + public DefaultPropertiesParser(PropertiesComponent propertiesComponent) { + this.propertiesComponent = propertiesComponent; + } + + public PropertiesComponent getPropertiesComponent() { + return propertiesComponent; + } + + public void setPropertiesComponent(PropertiesComponent propertiesComponent) { + this.propertiesComponent = propertiesComponent; + } + @Override public String parseUri(String text, Properties properties, String prefixToken, String suffixToken) throws IllegalArgumentException { return parseUri(text, properties, prefixToken, suffixToken, null, null, false); @@ -215,6 +232,10 @@ public class DefaultPropertiesParser implements AugmentedPropertyNameAwareProper if (value == null) { StringBuilder esb = new StringBuilder(); + if (propertiesComponent.isDefaultCreated()) { + // if the component was auto created then include more information that the end user should define it + esb.append("PropertiesComponent with name properties must be defined in CamelContext to support property placeholders. "); + } esb.append("Property with key [").append(augmentedKey).append("] "); if (shouldFallback) { esb.append("(and original key [").append(key).append("]) "); http://git-wip-us.apache.org/repos/asf/camel/blob/3beb8e2d/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 31162fc..fec14a7 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 @@ -71,7 +71,7 @@ public class PropertiesComponent extends DefaultComponent { private static final Logger LOG = LoggerFactory.getLogger(PropertiesComponent.class); private final Map<CacheKey, Properties> cacheMap = new LRUSoftCache<CacheKey, Properties>(1000); private PropertiesResolver propertiesResolver = new DefaultPropertiesResolver(); - private PropertiesParser propertiesParser = new DefaultPropertiesParser(); + private PropertiesParser propertiesParser = new DefaultPropertiesParser(this); private String[] locations; private boolean ignoreMissingLocation; private boolean cache = true; @@ -173,6 +173,13 @@ public class PropertiesComponent extends DefaultComponent { } } + /** + * Is this component created as a default by {@link org.apache.camel.CamelContext} during starting up Camel. + */ + public boolean isDefaultCreated() { + return locations == null; + } + public String[] getLocations() { return locations; } http://git-wip-us.apache.org/repos/asf/camel/blob/3beb8e2d/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java index 1645fd8..bdc3bd4 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java +++ b/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java @@ -36,7 +36,6 @@ import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; - import javax.naming.Context; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; @@ -145,6 +144,11 @@ import org.apache.camel.util.TimeUtils; import org.apache.camel.util.URISupport; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +<<<<<<< HEAD +======= + +import static org.apache.camel.util.StringQuoteHelper.doubleQuote; +>>>>>>> d00b08d... CAMEL-8107: Allow to use property placeholder with default values without having to setup the properties component /** * Represents the context used to configure routes and the policies to use. @@ -1269,18 +1273,14 @@ public class DefaultCamelContext extends ServiceSupport implements ModelCamelCon } } - if (pc != null) { - // the parser will throw exception if property key was not found - String answer = pc.parseUri(text); - log.debug("Resolved text: {} -> {}", text, answer); - return answer; - } else { - throw new IllegalArgumentException("PropertiesComponent with name properties must be defined" - + " in CamelContext to support property placeholders."); + if (pc == null) { + // create a default properties component to be used as there may be default values we can use + log.info("No existing PropertiesComponent has been configured, creating a new default PropertiesComponent with name: properties"); + pc = getComponent("properties", PropertiesComponent.class); } + } - // Component available, use actual tokens - } else if (pc != null && text.contains(pc.getPrefixToken())) { + if (pc != null && text.contains(pc.getPrefixToken())) { // the parser will throw exception if property key was not found String answer = pc.parseUri(text); log.debug("Resolved text: {} -> {}", text, answer); http://git-wip-us.apache.org/repos/asf/camel/blob/3beb8e2d/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEndpointTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEndpointTest.java b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEndpointTest.java index 47a39c3..557008f 100644 --- a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEndpointTest.java +++ b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEndpointTest.java @@ -82,7 +82,8 @@ public class PropertiesComponentEndpointTest extends ContextTestSupport { } catch (FailedToCreateRouteException e) { ResolveEndpointFailedException cause = assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause()); IllegalArgumentException iae = assertIsInstanceOf(IllegalArgumentException.class, cause.getCause()); - assertEquals("PropertiesComponent with name properties must be defined in CamelContext to support property placeholders.", iae.getMessage()); + String msg = "PropertiesComponent with name properties must be defined in CamelContext to support property placeholders."; + assertTrue(iae.getMessage().startsWith(msg)); } } http://git-wip-us.apache.org/repos/asf/camel/blob/3beb8e2d/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOnlyUseDefaultValuesTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOnlyUseDefaultValuesTest.java b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOnlyUseDefaultValuesTest.java new file mode 100644 index 0000000..2bb8e51 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentOnlyUseDefaultValuesTest.java @@ -0,0 +1,85 @@ +/** + * 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.properties; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.FailedToCreateRouteException; +import org.apache.camel.builder.RouteBuilder; + +public class PropertiesComponentOnlyUseDefaultValuesTest extends ContextTestSupport { + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + public void testOnlyDefaults() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .to("{{foo:mock:foo}}") + .to("{{bar:mock:bar}}"); + } + }); + context.start(); + + getMockEndpoint("mock:foo").expectedMessageCount(1); + getMockEndpoint("mock:bar").expectedMessageCount(1); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + public void testOneMissing() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .to("{{foo:mock:foo}}") + .to("{{bar}}"); + } + }); + + try { + context.start(); + fail("Should have thrown exception"); + } catch (FailedToCreateRouteException e) { + // expected + } + } + + public void testAllMissing() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .to("{{foo:mock:foo}}") + .to("{{bar}}"); + } + }); + + try { + context.start(); + fail("Should have thrown exception"); + } catch (FailedToCreateRouteException e) { + // expected + } + } + +}