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 2014/12/03 08:50:01 UTC
[2/5] camel git commit: CAMEL-8107: Allow to use property placeholder
with default values without having to setup the properties component
CAMEL-8107: Allow to use property placeholder with default values without having to setup the properties component
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d00b08d8
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d00b08d8
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d00b08d8
Branch: refs/heads/master
Commit: d00b08d8221b3811f8275659ceb3bf8cb1578683
Parents: 4e636bd
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Dec 2 20:17:56 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Wed Dec 3 07:35:45 2014 +0100
----------------------------------------------------------------------
.../properties/DefaultPropertiesParser.java | 25 +++++-
.../properties/PropertiesComponent.java | 9 ++-
.../apache/camel/impl/DefaultCamelContext.java | 20 ++---
.../PropertiesComponentEndpointTest.java | 3 +-
...ertiesComponentOnlyUseDefaultValuesTest.java | 85 ++++++++++++++++++++
5 files changed, 126 insertions(+), 16 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/d00b08d8/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 0f52985..eb490c2 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,14 +26,31 @@ 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 {
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);
@@ -214,6 +231,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/d00b08d8/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/d00b08d8/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 4ee7115..93dfbfc 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
@@ -37,7 +37,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;
@@ -148,6 +147,7 @@ import org.apache.camel.util.TimeUtils;
import org.apache.camel.util.URISupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+
import static org.apache.camel.util.StringQuoteHelper.doubleQuote;
/**
@@ -1467,18 +1467,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/d00b08d8/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/d00b08d8/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
+ }
+ }
+
+}