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 2019/02/28 11:46:15 UTC
[camel] branch master updated: CAMEL-13243: Camel Main should
optionally load properties from application.properties
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/master by this push:
new ccd0c79 CAMEL-13243: Camel Main should optionally load properties from application.properties
ccd0c79 is described below
commit ccd0c79cad979d9afc9041d85856f393eff92300
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Thu Feb 28 12:40:20 2019 +0100
CAMEL-13243: Camel Main should optionally load properties from application.properties
---
.../component/properties/PropertiesComponent.java | 19 ++++++++++++++++++-
.../org/apache/camel/spi/PropertiesComponent.java | 13 +++++++++++++
.../main/java/org/apache/camel/main/MainSupport.java | 20 +++++++++++++++++---
.../test/java/org/apache/camel/main/MainTest.java | 18 +++++++++++++++---
.../src/test/resources/application.properties | 17 +++++++++++++++++
5 files changed, 80 insertions(+), 7 deletions(-)
diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
index 15e6d95..a6c0f8b 100644
--- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
+++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java
@@ -90,13 +90,16 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
private PropertiesParser propertiesParser = new DefaultPropertiesParser(this);
private List<PropertiesLocation> locations = Collections.emptyList();
+ private transient String propertyPrefixResolved;
+
+ @Metadata
private boolean ignoreMissingLocation;
+ @Metadata
private String encoding;
@Metadata(defaultValue = "true")
private boolean cache = true;
@Metadata(label = "advanced")
private String propertyPrefix;
- private transient String propertyPrefixResolved;
@Metadata(label = "advanced")
private String propertySuffix;
private transient String propertySuffixResolved;
@@ -277,6 +280,20 @@ public class PropertiesComponent extends DefaultComponent implements org.apache.
setLocations(locations);
}
+ public void addLocation(String location) {
+ if (location != null) {
+ List<PropertiesLocation> newLocations = new ArrayList<>();
+ for (String loc : location.split(",")) {
+ newLocations.add(new PropertiesLocation(loc));
+ }
+ List<PropertiesLocation> current = getLocations();
+ if (!current.isEmpty()) {
+ newLocations.addAll(0, current);
+ }
+ setLocations(newLocations);
+ }
+ }
+
/**
* A list of locations to load properties. You can use comma to separate multiple locations.
* This option will override any default locations and only use the locations from this option.
diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java b/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java
index 9a8e6c6..1dddb79 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/PropertiesComponent.java
@@ -49,4 +49,17 @@ public interface PropertiesComponent extends Component {
*/
void setLocation(String location);
+ /**
+ * Adds the list of locations to the current locations, where to load properties.
+ * You can use comma to separate multiple locations.
+ * This option will override any default locations and only use the locations from this option.
+ */
+ void addLocation(String location);
+
+ /**
+ * Whether to silently ignore if a location cannot be located, such as a properties file not found.
+ */
+ void setIgnoreMissingLocation(boolean ignoreMissingLocation);
+
+
}
diff --git a/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java b/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
index 0ff50ac..63c8be1 100644
--- a/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
+++ b/core/camel-core/src/main/java/org/apache/camel/main/MainSupport.java
@@ -157,6 +157,13 @@ public abstract class MainSupport extends ServiceSupport {
setFileWatchDirectory(parameter);
}
});
+ addOption(new ParameterOption("pl", "propertiesLocation",
+ "Sets location(s) to load properties, such as from classpath or file system.",
+ "propertiesLocation") {
+ protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
+ setPropertyPlaceholderLocations(parameter);
+ }
+ });
}
/**
@@ -450,8 +457,8 @@ public abstract class MainSupport extends ServiceSupport {
}
/**
- * A list of locations to load properties. You can use comma to separate multiple locations.
- * This option will override any default locations and only use the locations from this option.
+ * A list of locations to add for loading properties.
+ * You can use comma to separate multiple locations.
*/
public void setPropertyPlaceholderLocations(String location) {
this.propertyPlaceholderLocations = location;
@@ -574,7 +581,14 @@ public abstract class MainSupport extends ServiceSupport {
protected void postProcessCamelContext(CamelContext camelContext) throws Exception {
if (propertyPlaceholderLocations != null) {
PropertiesComponent pc = camelContext.getPropertiesComponent();
- pc.setLocation(propertyPlaceholderLocations);
+ pc.addLocation(propertyPlaceholderLocations);
+ LOG.info("Using properties from: {}", propertyPlaceholderLocations);
+ } else {
+ // lets default to application.properties and ignore if its missing
+ PropertiesComponent pc = camelContext.getPropertiesComponent();
+ pc.addLocation("classpath:application.properties");
+ pc.setIgnoreMissingLocation(true);
+ LOG.info("Using optional properties from classpath:application.properties");
}
if (trace) {
camelContext.setTracing(true);
diff --git a/core/camel-core/src/test/java/org/apache/camel/main/MainTest.java b/core/camel-core/src/test/java/org/apache/camel/main/MainTest.java
index 799914a..4b86b8f 100644
--- a/core/camel-core/src/test/java/org/apache/camel/main/MainTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/main/MainTest.java
@@ -16,8 +16,6 @@
*/
package org.apache.camel.main;
-import java.util.List;
-
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
@@ -91,7 +89,21 @@ public class MainTest extends Assert {
endpoint.assertIsSatisfied();
main.stop();
}
-
+
+ @Test
+ public void testOptionalProperties() throws Exception {
+ // lets make a simple route
+ Main main = new Main();
+ main.addRouteBuilder(new MyRouteBuilder());
+ main.start();
+
+ CamelContext camelContext = main.getCamelContext();
+ // should load application.properties from classpath
+ assertEquals("World", camelContext.resolvePropertyPlaceholders("{{hello}}"));
+
+ main.stop();
+ }
+
public static class MyRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
diff --git a/core/camel-core/src/test/resources/application.properties b/core/camel-core/src/test/resources/application.properties
new file mode 100644
index 0000000..51f6541
--- /dev/null
+++ b/core/camel-core/src/test/resources/application.properties
@@ -0,0 +1,17 @@
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
+hello=World