This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new d23fc91  CAMEL-16700: main: allow to configure global options through 
properties
d23fc91 is described below

commit d23fc91394f6c2dc0b1f6b0f96397ecb53753e52
Author: Claus Ibsen <[email protected]>
AuthorDate: Thu Jun 10 13:21:24 2021 +0200

    CAMEL-16700: main: allow to configure global options through properties
---
 .../MainConfigurationPropertiesConfigurer.java     |  8 ++++
 .../camel-main-configuration-metadata.json         |  1 +
 core/camel-main/src/main/docs/main.adoc            |  1 +
 .../org/apache/camel/main/BaseMainSupport.java     | 14 +++++-
 .../camel/main/DefaultConfigurationConfigurer.java | 11 +++++
 .../camel/main/DefaultConfigurationProperties.java | 46 ++++++++++++++++++
 .../apache/camel/main/MainGlobalOptionsTest.java   | 54 ++++++++++++++++++++++
 7 files changed, 133 insertions(+), 2 deletions(-)

diff --git 
a/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
 
b/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
index f148bb5..3d2d077 100644
--- 
a/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
+++ 
b/core/camel-main/src/generated/java/org/apache/camel/main/MainConfigurationPropertiesConfigurer.java
@@ -79,6 +79,8 @@ public class MainConfigurationPropertiesConfigurer extends 
org.apache.camel.supp
         case "ExchangeFactoryStatisticsEnabled": 
target.setExchangeFactoryStatisticsEnabled(property(camelContext, 
boolean.class, value)); return true;
         case "fileconfigurations":
         case "FileConfigurations": 
target.setFileConfigurations(property(camelContext, java.lang.String.class, 
value)); return true;
+        case "globaloptions":
+        case "GlobalOptions": target.setGlobalOptions(property(camelContext, 
java.util.Map.class, value)); return true;
         case "inflightrepositorybrowseenabled":
         case "InflightRepositoryBrowseEnabled": 
target.setInflightRepositoryBrowseEnabled(property(camelContext, boolean.class, 
value)); return true;
         case "javaroutesexcludepattern":
@@ -272,6 +274,8 @@ public class MainConfigurationPropertiesConfigurer extends 
org.apache.camel.supp
         case "ExchangeFactoryStatisticsEnabled": return boolean.class;
         case "fileconfigurations":
         case "FileConfigurations": return java.lang.String.class;
+        case "globaloptions":
+        case "GlobalOptions": return java.util.Map.class;
         case "inflightrepositorybrowseenabled":
         case "InflightRepositoryBrowseEnabled": return boolean.class;
         case "javaroutesexcludepattern":
@@ -466,6 +470,8 @@ public class MainConfigurationPropertiesConfigurer extends 
org.apache.camel.supp
         case "ExchangeFactoryStatisticsEnabled": return 
target.isExchangeFactoryStatisticsEnabled();
         case "fileconfigurations":
         case "FileConfigurations": return target.getFileConfigurations();
+        case "globaloptions":
+        case "GlobalOptions": return target.getGlobalOptions();
         case "inflightrepositorybrowseenabled":
         case "InflightRepositoryBrowseEnabled": return 
target.isInflightRepositoryBrowseEnabled();
         case "javaroutesexcludepattern":
@@ -603,6 +609,8 @@ public class MainConfigurationPropertiesConfigurer extends 
org.apache.camel.supp
         switch (ignoreCase ? name.toLowerCase() : name) {
         case "configurations":
         case "Configurations": return java.lang.Object.class;
+        case "globaloptions":
+        case "GlobalOptions": return java.lang.String.class;
         case "routesbuilders":
         case "RoutesBuilders": return org.apache.camel.RoutesBuilder.class;
         default: return null;
diff --git 
a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
 
b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
index 5ba57b1..2b11917 100644
--- 
a/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
+++ 
b/core/camel-main/src/generated/resources/META-INF/camel-main-configuration-metadata.json
@@ -39,6 +39,7 @@
     { "name": "camel.main.exchangeFactoryCapacity", "description": "The 
capacity the pool (for each consumer) uses for storing exchanges. The default 
capacity is 100.", "sourceType": 
"org.apache.camel.main.DefaultConfigurationProperties", "type": "integer", 
"javaType": "int", "defaultValue": 100 },
     { "name": "camel.main.exchangeFactoryStatisticsEnabled", "description": 
"Configures whether statistics is enabled on exchange factory.", "sourceType": 
"org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", 
"javaType": "boolean", "defaultValue": "false" },
     { "name": "camel.main.fileConfigurations", "description": "Directory to 
load additional configuration files that contains configuration values that 
takes precedence over any other configuration. This can be used to refer to 
files that may have secret configuration that has been mounted on the file 
system for containers. 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.", " [...]
+    { "name": "camel.main.globalOptions", "description": "Sets global options 
that can be referenced in the camel context Important: This has nothing to do 
with property placeholders, and is just a plain set of key\/value pairs which 
are used to configure global options on CamelContext, such as a maximum debug 
logging length etc.", "sourceType": 
"org.apache.camel.main.DefaultConfigurationProperties", "type": "object", 
"javaType": "java.util.Map" },
     { "name": "camel.main.inflightRepositoryBrowseEnabled", "description": 
"Sets whether the inflight repository should allow browsing each inflight 
exchange. This is by default disabled as there is a very slight performance 
overhead when enabled.", "sourceType": 
"org.apache.camel.main.DefaultConfigurationProperties", "type": "boolean", 
"javaType": "boolean", "defaultValue": "false" },
     { "name": "camel.main.javaRoutesExcludePattern", "description": "Used for 
exclusive filtering RouteBuilder classes which are collected from the registry 
or via classpath scanning. The exclusive filtering takes precedence over 
inclusive filtering. The pattern is using Ant-path style pattern. Multiple 
patterns can be specified separated by comma. For example to exclude all 
classes starting with Bar use: &#42;&#42;\/Bar&#42; To exclude all routes form 
a specific package use: com\/mycomp [...]
     { "name": "camel.main.javaRoutesIncludePattern", "description": "Used for 
inclusive filtering RouteBuilder classes which are collected from the registry 
or via classpath scanning. The exclusive filtering takes precedence over 
inclusive filtering. The pattern is using Ant-path style pattern. Multiple 
patterns can be specified separated by comma. Multiple patterns can be 
specified separated by comma. For example to include all classes starting with 
Foo use: &#42;&#42;\/Foo To include a [...]
diff --git a/core/camel-main/src/main/docs/main.adoc 
b/core/camel-main/src/main/docs/main.adoc
index cd2b407..504d9b3 100644
--- a/core/camel-main/src/main/docs/main.adoc
+++ b/core/camel-main/src/main/docs/main.adoc
@@ -51,6 +51,7 @@ The following table lists all the options:
 | *camel.main.exchangeFactory{zwsp}Capacity* | The capacity the pool (for each 
consumer) uses for storing exchanges. The default capacity is 100. | 100 | int
 | *camel.main.exchangeFactory{zwsp}StatisticsEnabled* | Configures whether 
statistics is enabled on exchange factory. | false | boolean
 | *camel.main.fileConfigurations* | Directory to load additional configuration 
files that contains configuration values that takes precedence over any other 
configuration. This can be used to refer to files that may have secret 
configuration that has been mounted on the file system for containers. 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. |  
| String
+| *camel.main.globalOptions* | Sets global options that can be referenced in 
the camel context Important: This has nothing to do with property placeholders, 
and is just a plain set of key/value pairs which are used to configure global 
options on CamelContext, such as a maximum debug logging length etc. |  | Map
 | *camel.main.inflightRepository{zwsp}BrowseEnabled* | Sets whether the 
inflight repository should allow browsing each inflight exchange. This is by 
default disabled as there is a very slight performance overhead when enabled. | 
false | boolean
 | *camel.main.javaRoutesExclude{zwsp}Pattern* | Used for exclusive filtering 
RouteBuilder classes which are collected from the registry or via classpath 
scanning. The exclusive filtering takes precedence over inclusive filtering. 
The pattern is using Ant-path style pattern. Multiple patterns can be specified 
separated by comma. For example to exclude all classes starting with Bar use: 
&#42;&#42;/Bar&#42; To exclude all routes form a specific package use: 
com/mycompany/bar/&#42; To exclud [...]
 | *camel.main.javaRoutesInclude{zwsp}Pattern* | Used for inclusive filtering 
RouteBuilder classes which are collected from the registry or via classpath 
scanning. The exclusive filtering takes precedence over inclusive filtering. 
The pattern is using Ant-path style pattern. Multiple patterns can be specified 
separated by comma. Multiple patterns can be specified separated by comma. For 
example to include all classes starting with Foo use: &#42;&#42;/Foo To include 
all routes form a speci [...]
diff --git 
a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java 
b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
index 5e29e6b..16740f3 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
@@ -114,8 +114,7 @@ public abstract class BaseMainSupport extends BaseService {
         CamelSagaService answer = 
camelContext.adapt(ExtendedCamelContext.class).getBootstrapFactoryFinder()
                 .newInstance("lra-saga-service", CamelSagaService.class)
                 .orElseThrow(() -> new IllegalArgumentException(
-                        "Cannot find LRASagaService on classpath. "
-                                                                + "Add 
camel-lra to classpath."));
+                        "Cannot find LRASagaService on classpath. Add 
camel-lra to classpath."));
 
         // add as service so its discover by saga eip
         camelContext.addService(answer, true, false);
@@ -716,6 +715,7 @@ public abstract class BaseMainSupport extends BaseService {
         Map<String, Object> lraProperties = new LinkedHashMap<>();
         Map<String, Object> routeTemplateProperties = new LinkedHashMap<>();
         Map<String, Object> beansProperties = new LinkedHashMap<>();
+        Map<String, String> globalOptions = new LinkedHashMap<>();
         for (String key : prop.stringPropertyNames()) {
             if (key.startsWith("camel.context.")) {
                 // grab the value
@@ -777,9 +777,19 @@ public abstract class BaseMainSupport extends BaseService {
                 String option = key.substring(12);
                 validateOptionAndValue(key, option, value);
                 beansProperties.put(optionKey(option), value);
+            } else if (key.startsWith("camel.global-options.")) {
+                // grab the value
+                String value = prop.getProperty(key);
+                String option = key.substring(12);
+                validateOptionAndValue(key, option, value);
+                globalOptions.put(optionKey(option), value);
             }
         }
 
+        // global options first
+        if (!globalOptions.isEmpty()) {
+            mainConfigurationProperties.setGlobalOptions(globalOptions);
+        }
         // create beans first as they may be used later
         if (!beansProperties.isEmpty()) {
             LOG.debug("Creating and binding beans to registry from loaded 
properties: {}", beansProperties.size());
diff --git 
a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
 
b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
index 01c9b13..d77f0c60 100644
--- 
a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
+++ 
b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationConfigurer.java
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.main;
 
+import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
@@ -234,6 +235,16 @@ public final class DefaultConfigurationConfigurer {
                     
.setManagementNamePattern(config.getJmxManagementNamePattern());
         }
 
+        // global options
+        if (config.getGlobalOptions() != null) {
+            Map<String, String> map = camelContext.getGlobalOptions();
+            if (map == null) {
+                map = new HashMap<>();
+            }
+            map.putAll(config.getGlobalOptions());
+            camelContext.setGlobalOptions(map);
+        }
+
         // global endpoint configurations
         
camelContext.getGlobalEndpointConfiguration().setAutowiredEnabled(config.isAutowiredEnabled());
         
camelContext.getGlobalEndpointConfiguration().setBridgeErrorHandler(config.isEndpointBridgeErrorHandler());
diff --git 
a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
 
b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
index 0372e61..e50988c 100644
--- 
a/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
+++ 
b/core/camel-main/src/main/java/org/apache/camel/main/DefaultConfigurationProperties.java
@@ -16,6 +16,9 @@
  */
 package org.apache.camel.main;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import org.apache.camel.LoggingLevel;
 import org.apache.camel.ManagementStatisticsLevel;
 import org.apache.camel.StartupSummaryLevel;
@@ -95,6 +98,7 @@ public abstract class DefaultConfigurationProperties<T> {
     private int exchangeFactoryCapacity = 100;
     private boolean exchangeFactoryStatisticsEnabled;
     private boolean dumpRoutes;
+    private Map<String, String> globalOptions;
     // route controller
     @Metadata(defaultValue = "DEBUG")
     @Deprecated
@@ -1015,6 +1019,20 @@ public abstract class DefaultConfigurationProperties<T> {
         this.dumpRoutes = dumpRoutes;
     }
 
+    public Map<String, String> getGlobalOptions() {
+        return globalOptions;
+    }
+
+    /**
+     * Sets global options that can be referenced in the camel context
+     * <p/>
+     * <b>Important:</b> This has nothing to do with property placeholders, 
and is just a plain set of key/value pairs
+     * which are used to configure global options on CamelContext, such as a 
maximum debug logging length etc.
+     */
+    public void setGlobalOptions(Map<String, String> globalOptions) {
+        this.globalOptions = globalOptions;
+    }
+
     @Deprecated
     public LoggingLevel getRouteControllerLoggingLevel() {
         return routeControllerLoggingLevel;
@@ -1918,6 +1936,34 @@ public abstract class DefaultConfigurationProperties<T> {
     }
 
     /**
+     * Sets global options that can be referenced in the camel context
+     * <p/>
+     * <b>Important:</b> This has nothing to do with property placeholders, 
and is just a plain set of key/value pairs
+     * which are used to configure global options on CamelContext, such as a 
maximum debug logging length etc.
+     */
+    public T withGlobalOptions(Map<String, String> globalOptions) {
+        if (this.globalOptions == null) {
+            this.globalOptions = new HashMap<>();
+        }
+        this.globalOptions.putAll(globalOptions);
+        return (T) this;
+    }
+
+    /**
+     * Sets global options that can be referenced in the camel context
+     * <p/>
+     * <b>Important:</b> This has nothing to do with property placeholders, 
and is just a plain set of key/value pairs
+     * which are used to configure global options on CamelContext, such as a 
maximum debug logging length etc.
+     */
+    public T withGlobalOption(String key, String value) {
+        if (this.globalOptions == null) {
+            this.globalOptions = new HashMap<>();
+        }
+        this.globalOptions.put(key, value);
+        return (T) this;
+    }
+
+    /**
      * Sets the logging level used for logging route activity (such as 
starting and stopping routes). The default
      * logging level is DEBUG.
      */
diff --git 
a/core/camel-main/src/test/java/org/apache/camel/main/MainGlobalOptionsTest.java
 
b/core/camel-main/src/test/java/org/apache/camel/main/MainGlobalOptionsTest.java
new file mode 100644
index 0000000..b6de106
--- /dev/null
+++ 
b/core/camel-main/src/test/java/org/apache/camel/main/MainGlobalOptionsTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.main;
+
+import org.apache.camel.CamelContext;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+public class MainGlobalOptionsTest {
+
+    @Test
+    public void testMain() throws Exception {
+        Main main = new Main();
+
+        main.addInitialProperty("camel.main.global-options[foo]", "123");
+        main.addInitialProperty("camel.main.global-options[bar]", "true");
+        main.addInitialProperty("camel.main.globalOptions[baz]", "999");
+
+        main.configure()
+                .withGlobalOption("cheese", "Gauda")
+                .withGlobalOption("drink", "Wine");
+
+        main.start();
+
+        CamelContext context = main.getCamelContext();
+        assertNotNull(context);
+
+        assertEquals(5, context.getGlobalOptions().size());
+        assertEquals("123", context.getGlobalOptions().get("foo"));
+        assertEquals("true", context.getGlobalOptions().get("bar"));
+        assertEquals("Gauda", context.getGlobalOptions().get("cheese"));
+        assertEquals("Wine", context.getGlobalOptions().get("drink"));
+        assertEquals("999", context.getGlobalOptions().get("baz"));
+
+        main.stop();
+    }
+
+}

Reply via email to