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/02 19:44:35 UTC
[2/4] camel git commit: CAMEL-8099: Add support for using default
values in Camel property placeholder.
CAMEL-8099: Add support for using default values in Camel property placeholder.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/e8abd636
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/e8abd636
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/e8abd636
Branch: refs/heads/camel-2.14.x
Commit: e8abd636f12c423e9f6f1ad319084aca0e254570
Parents: 0c55f56
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Dec 2 17:03:19 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Dec 2 19:44:16 2014 +0100
----------------------------------------------------------------------
.../properties/DefaultPropertiesParser.java | 15 ++++
.../PropertiesComponentGetOrElseTest.java | 72 ++++++++++++++++++++
2 files changed, 87 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/e8abd636/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 16ef52d..6a5b80f 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
@@ -22,6 +22,7 @@ import java.util.Set;
import static java.lang.String.format;
+import org.apache.camel.util.ObjectHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -32,6 +33,8 @@ import org.slf4j.LoggerFactory;
public class DefaultPropertiesParser implements AugmentedPropertyNameAwarePropertiesParser {
protected final Logger log = LoggerFactory.getLogger(getClass());
+ private static final String GET_OR_ELSE_TOKEN = ":";
+
@Override
public String parseUri(String text, Properties properties, String prefixToken, String suffixToken) throws IllegalArgumentException {
return parseUri(text, properties, prefixToken, suffixToken, null, null, false);
@@ -189,6 +192,13 @@ public class DefaultPropertiesParser implements AugmentedPropertyNameAwareProper
* @return Value of the property with the given key
*/
private String getPropertyValue(String key, String input) {
+ // they key may have a get or else expression
+ String defaultValue = null;
+ if (key.contains(GET_OR_ELSE_TOKEN)) {
+ defaultValue = ObjectHelper.after(key, GET_OR_ELSE_TOKEN);
+ key = ObjectHelper.before(key, GET_OR_ELSE_TOKEN);
+ }
+
String augmentedKey = getAugmentedKey(key);
boolean shouldFallback = fallbackToUnaugmentedProperty && !key.equals(augmentedKey);
@@ -198,6 +208,11 @@ public class DefaultPropertiesParser implements AugmentedPropertyNameAwareProper
value = doGetPropertyValue(key);
}
+ if (value == null && defaultValue != null) {
+ log.debug("Property with key [{}] not found, using default value: {}", augmentedKey, defaultValue);
+ value = defaultValue;
+ }
+
if (value == null) {
StringBuilder esb = new StringBuilder();
esb.append("Property with key [").append(augmentedKey).append("] ");
http://git-wip-us.apache.org/repos/asf/camel/blob/e8abd636/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentGetOrElseTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentGetOrElseTest.java b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentGetOrElseTest.java
new file mode 100644
index 0000000..e5e5d12
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentGetOrElseTest.java
@@ -0,0 +1,72 @@
+/**
+ * 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.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * @version
+ */
+public class PropertiesComponentGetOrElseTest extends ContextTestSupport {
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ public void testPropertiesComponentFoundKey() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start").to("properties:{{cool.end:mock:wrong}}");
+ }
+ });
+ context.start();
+
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ public void testPropertiesComponentUseDefaultValue() throws Exception {
+ context.addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() throws Exception {
+ from("direct:start").to("properties:{{unknown:mock:result}}");
+ }
+ });
+ context.start();
+
+ getMockEndpoint("mock:result").expectedMessageCount(1);
+
+ template.sendBody("direct:start", "Hello World");
+
+ assertMockEndpointsSatisfied();
+ }
+
+ @Override
+ protected CamelContext createCamelContext() throws Exception {
+ CamelContext context = super.createCamelContext();
+ context.addComponent("properties", new PropertiesComponent("classpath:org/apache/camel/component/properties/myproperties.properties"));
+ return context;
+ }
+
+}