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/05/14 04:29:35 UTC

[camel] 04/07: CAMEL-13503: Camel main - Allow to configure global and common options ala camel-spring-boot have.

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

commit 6cc24e5dc3f24d4aec25b4e1d263674a08010dfa
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon May 13 11:59:06 2019 +0200

    CAMEL-13503: Camel main - Allow to configure global and common options ala camel-spring-boot have.
---
 .../camel/main/MainConfigurationProperties.java    |  5 ++--
 .../java/org/apache/camel/main/MainSupport.java    | 34 +++++++++++++++++++++-
 .../java/org/apache/camel/util/StringHelper.java   | 14 ++++-----
 .../application.properties => data/foo.properties} | 19 +-----------
 .../main/java/org/apache/camel/example/MyBean.java |  8 ++++-
 .../org/apache/camel/example/MyConfiguration.java  |  4 +--
 .../org/apache/camel/example/MyRouteBuilder.java   |  4 ++-
 .../org/apache/camel/example/StandaloneCamel.java  |  2 +-
 .../src/main/resources/application.properties      |  3 ++
 9 files changed, 59 insertions(+), 34 deletions(-)

diff --git a/core/camel-core/src/main/java/org/apache/camel/main/MainConfigurationProperties.java b/core/camel-core/src/main/java/org/apache/camel/main/MainConfigurationProperties.java
index 2ce8ac3..f542a58 100644
--- a/core/camel-core/src/main/java/org/apache/camel/main/MainConfigurationProperties.java
+++ b/core/camel-core/src/main/java/org/apache/camel/main/MainConfigurationProperties.java
@@ -184,9 +184,8 @@ public class MainConfigurationProperties {
      * This can be used to refer to files that may have secret configuration that
      * has been mounted on the file system for containers.
      *
-     * You must use either file: or classpath: as prefix to load
-     * from file system or classpath. Then you can specify a pattern to load
-     * from sub directories and a name pattern such as file:/var/app/secret/*.properties
+     * You can specify a pattern to load from sub directories and a name pattern such as /var/app/secret/*.properties,
+     * multiple directories can be separated by comma.
      */
     public void setFileConfigurations(String fileConfigurations) {
         this.fileConfigurations = fileConfigurations;
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 44aa577..819ad65 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
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.main;
 
+import java.io.File;
+import java.io.FileInputStream;
 import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -85,6 +87,7 @@ import org.apache.camel.support.LifecycleStrategySupport;
 import org.apache.camel.support.jsse.GlobalSSLContextParametersSupplier;
 import org.apache.camel.support.service.ServiceHelper;
 import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.concurrent.ThreadHelper;
 import org.slf4j.Logger;
@@ -92,6 +95,7 @@ import org.slf4j.LoggerFactory;
 
 import static org.apache.camel.support.ObjectHelper.invokeMethod;
 import static org.apache.camel.util.ReflectionHelper.findMethod;
+import static org.apache.camel.util.StringHelper.matches;
 
 /**
  * Base class for main implementations to allow starting up a JVM with Camel embedded.
@@ -107,7 +111,6 @@ public abstract class MainSupport extends ServiceSupport {
     protected final AtomicInteger exitCode = new AtomicInteger(UNINITIALIZED_EXIT_CODE);
 
     // TODO: Fluent builder on Main configuration properties
-    // TODO: Make it possible to configure MainConfigurationProperties from application.properties via camel.main.xxx
     // TODO: Move these to mainConfigurationProperties (delegate)
     protected long duration = -1;
     protected String fileWatchDirectory;
@@ -846,6 +849,35 @@ public abstract class MainSupport extends ServiceSupport {
      * Configures CamelContext from the {@link MainConfigurationProperties} properties.
      */
     protected void doConfigureCamelContextFromMainConfiguration(CamelContext camelContext, MainConfigurationProperties config) throws Exception {
+        if (config.getFileConfigurations() != null) {
+            String[] locs = config.getFileConfigurations().split(",");
+            for (String loc : locs) {
+                String path = FileUtil.onlyPath(loc);
+                if (path != null) {
+                    String pattern = loc.length() > path.length() ? loc.substring(path.length() + 1) : null;
+                    File[] files = new File(path).listFiles(f -> matches(pattern, f.getName()));
+                    if (files != null) {
+                        for (File file : files) {
+                            Properties props = new Properties();
+                            try (FileInputStream is = new FileInputStream(file)) {
+                                props.load(is);
+                            }
+                            if (!props.isEmpty()) {
+                                if (overrideProperties == null) {
+                                    // setup override properties on properties component
+                                    overrideProperties = new Properties();
+                                    PropertiesComponent pc = camelContext.getPropertiesComponent();
+                                    pc.setOverrideProperties(overrideProperties);
+                                }
+                                LOG.info("Loaded additional {} properties from file: {}", props.size(), file);
+                                overrideProperties.putAll(props);
+                            }
+                        }
+                    }
+                }
+            }
+        }
+
         if (!config.isJmxEnabled()) {
             camelContext.disableJMX();
         }
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java b/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
index ce622ac..ccddc61 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
@@ -795,28 +795,28 @@ public final class StringHelper {
      * - Ant style matching
      * - Regexp
      *
-     * @param patter the pattern
+     * @param pattern the pattern
      * @param target the string to test
      * @return true if target matches the pattern
      */
-    public static boolean matches(String patter, String target) {
-        if (Objects.equals(patter, target)) {
+    public static boolean matches(String pattern, String target) {
+        if (Objects.equals(pattern, target)) {
             return true;
         }
 
-        if (Objects.isNull(patter)) {
+        if (Objects.isNull(pattern)) {
             return true;
         }
 
-        if (Objects.equals("*", patter)) {
+        if (Objects.equals("*", pattern)) {
             return true;
         }
 
-        if (AntPathMatcher.INSTANCE.match(patter, target)) {
+        if (AntPathMatcher.INSTANCE.match(pattern, target)) {
             return true;
         }
 
-        Pattern p = Pattern.compile(patter);
+        Pattern p = Pattern.compile(pattern);
         Matcher m = p.matcher(target);
 
         return m.matches();
diff --git a/examples/camel-example-main/src/main/resources/application.properties b/examples/camel-example-main/src/main/data/foo.properties
similarity index 60%
copy from examples/camel-example-main/src/main/resources/application.properties
copy to examples/camel-example-main/src/main/data/foo.properties
index f07d91c..b43e6bc 100644
--- a/examples/camel-example-main/src/main/resources/application.properties
+++ b/examples/camel-example-main/src/main/data/foo.properties
@@ -15,21 +15,4 @@
 ## limitations under the License.
 ## ---------------------------------------------------------------------------
 
-# to configure camel main
-# here you can configure options on camel main (see MainConfigurationProperties class)
-camel.main.name = MyCoolCamel
-camel.main.jmx-enabled = false
-
-# to configure the camel quartz component
-# here we can configure the options on the component level (and we can use dash-naming-style)
-camel.component.quartz2.start-delayed-seconds = 3
-
-# you can configure whether OS environment should override (=2 which is default) or as fallback (=1)
-### camel.component.properties.environment-variable-mode=1
-
-# properties used in the route
-myCron = 0/2 * * * * ?
-
-# application properties
-hi = Hello
-
+bye = Bye
\ No newline at end of file
diff --git a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyBean.java b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyBean.java
index 9df9291..1e1bdb8 100644
--- a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyBean.java
+++ b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyBean.java
@@ -19,12 +19,18 @@ package org.apache.camel.example;
 public class MyBean {
 
     private String hi;
+    private String bye;
 
-    public MyBean(String hi) {
+    public MyBean(String hi, String bye) {
         this.hi = hi;
+        this.bye = bye;
     }
 
     public String hello() {
         return hi + " how are you?";
     }
+
+    public String bye() {
+        return bye + " World";
+    }
 }
diff --git a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyConfiguration.java b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyConfiguration.java
index 9ee36dc..9c26fdd 100644
--- a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyConfiguration.java
+++ b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyConfiguration.java
@@ -25,9 +25,9 @@ import org.apache.camel.PropertyInject;
 public class MyConfiguration {
 
     @BindToRegistry
-    public MyBean myBean(@PropertyInject("hi") String hi) {
+    public MyBean myBean(@PropertyInject("hi") String hi, @PropertyInject("bye") String bye) {
         // this will create an instance of this bean with the name of the method (eg myBean)
-        return new MyBean(hi);
+        return new MyBean(hi, bye);
     }
 
     public void configure() {
diff --git a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java
index 77eb95d..d98ae5f 100644
--- a/examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java
+++ b/examples/camel-example-main/src/main/java/org/apache/camel/example/MyRouteBuilder.java
@@ -23,7 +23,9 @@ public class MyRouteBuilder extends RouteBuilder {
     @Override
     public void configure() throws Exception {
         from("quartz2:foo?cron={{myCron}}")
-            .bean("myBean")
+            .bean("myBean", "hello")
+            .log("${body}")
+            .bean("myBean", "bye")
             .log("${body}");
     }
 }
diff --git a/examples/camel-example-main/src/main/java/org/apache/camel/example/StandaloneCamel.java b/examples/camel-example-main/src/main/java/org/apache/camel/example/StandaloneCamel.java
index 9411a3a..f01c5b6 100644
--- a/examples/camel-example-main/src/main/java/org/apache/camel/example/StandaloneCamel.java
+++ b/examples/camel-example-main/src/main/java/org/apache/camel/example/StandaloneCamel.java
@@ -42,7 +42,7 @@ public final class StandaloneCamel {
         String hello = camelContext.resolvePropertyPlaceholders("{{hi}}");
 
         // and create bean with the placeholder
-        MyBean myBean = new MyBean(hello);
+        MyBean myBean = new MyBean(hello, "Bye");
         // register bean to Camel
         camelContext.getRegistry().bind("myBean", myBean);
 
diff --git a/examples/camel-example-main/src/main/resources/application.properties b/examples/camel-example-main/src/main/resources/application.properties
index f07d91c..ddbb5a6 100644
--- a/examples/camel-example-main/src/main/resources/application.properties
+++ b/examples/camel-example-main/src/main/resources/application.properties
@@ -20,6 +20,9 @@
 camel.main.name = MyCoolCamel
 camel.main.jmx-enabled = false
 
+# load additional property placeholders from this folder
+camel.main.file-configurations=src/main/data/*.properties
+
 # to configure the camel quartz component
 # here we can configure the options on the component level (and we can use dash-naming-style)
 camel.component.quartz2.start-delayed-seconds = 3