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 70029d888204 CAMEL-27103: camel-main - the early lookup honors 
autoConfigurationEnvironmentVariablesEnabled and 
autoConfigurationSystemPropertiesEnabled (#31195)
70029d888204 is described below

commit 70029d8882044f4561838dd4354a00d36c36141c
Author: Loris Boiteux <[email protected]>
AuthorDate: Tue Dec 1 20:40:40 2026 +0100

    CAMEL-27103: camel-main - the early lookup honors 
autoConfigurationEnvironmentVariablesEnabled and 
autoConfigurationSystemPropertiesEnabled (#31195)
    
    The options read before the CamelContext is built (virtual threads and type 
converter statistics) were taken from the OS environment variables and the JVM 
system properties even when these sources were disabled in the properties, as 
these two options are only applied later by the auto configuration. They are 
now read from the loaded properties first, as in autoConfigurationFailFast. A 
value with a property placeholder cannot be resolved before the context is 
built, so it is ignored wi [...]
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../org/apache/camel/main/BaseMainSupport.java     | 39 ++++++++++++++++++++--
 .../MainTypeConverterStatisticsEnabledTest.java    | 25 ++++++++++++++
 2 files changed, 61 insertions(+), 3 deletions(-)

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 076d954d5119..a4ad532598b1 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
@@ -973,6 +973,11 @@ public abstract class BaseMainSupport extends BaseService {
      * is before the auto configuration applies the configuration. The sources 
and their priority are the same as in
      * {@link #autoConfigurationSingleOption}: JVM system properties over OS 
environment variables over the properties
      * component, with the key in any case. They are loaded once, for all 
these options.
+     * <p>
+     * As in {@link #autoConfigurationFailFast}, the OS environment variables 
and the JVM system properties are only
+     * used when {@code 
camel.main.autoConfigurationEnvironmentVariablesEnabled} and
+     * {@code camel.main.autoConfigurationSystemPropertiesEnabled} are not set 
to false in the properties component. A
+     * value with a property placeholder cannot be resolved yet, so it is 
ignored, with a WARN.
      *
      * @return the properties, or <code>null</code> if the auto configuration 
is disabled
      */
@@ -982,10 +987,31 @@ public abstract class BaseMainSupport extends BaseService 
{
         }
         OrderedLocationProperties prop = (OrderedLocationProperties) 
camelContext.getPropertiesComponent()
                 .loadProperties(name -> name.startsWith("camel."), 
MainHelper::optionKey);
-        overrideWithEnvironmentAndSystemProperties(prop, "camel.main.");
+        // the auto configuration has not yet applied these two options, so 
they are read here
+        boolean envEnabled = isEarlyEnabled(prop, 
"camel.main.autoConfigurationEnvironmentVariablesEnabled",
+                
mainConfigurationProperties.isAutoConfigurationEnvironmentVariablesEnabled());
+        boolean jvmEnabled = isEarlyEnabled(prop, 
"camel.main.autoConfigurationSystemPropertiesEnabled",
+                
mainConfigurationProperties.isAutoConfigurationSystemPropertiesEnabled());
+        overrideWithEnvironmentAndSystemProperties(prop, envEnabled, 
jvmEnabled, "camel.main.");
         return prop;
     }
 
+    private static boolean isEarlyEnabled(OrderedLocationProperties prop, 
String optionName, boolean defaultValue) {
+        Object value = prop.get(optionName);
+        if (ObjectHelper.isEmpty(value)) {
+            return defaultValue;
+        }
+        String str = value.toString().trim();
+        if (str.contains(PropertiesComponent.PREFIX_TOKEN)) {
+            LOG.warn("The option {}={} is ignored by the options that must be 
known before the CamelContext is built,"
+                     + " which is before property placeholders can be 
resolved, so it is {} for these options."
+                     + " Use true or false instead.",
+                    optionName, str, defaultValue);
+            return defaultValue;
+        }
+        return Boolean.parseBoolean(str);
+    }
+
     /**
      * Looks up a boolean option of Camel Main that must be known before the 
CamelContext is built, in the properties
      * from {@link #loadEarlyProperties(CamelContext)}, with the initial 
properties as fallback.
@@ -1265,7 +1291,14 @@ public abstract class BaseMainSupport extends 
BaseService {
      * the highest priority.
      */
     private void 
overrideWithEnvironmentAndSystemProperties(OrderedLocationProperties prop, 
String... prefixes) {
-        if 
(mainConfigurationProperties.isAutoConfigurationEnvironmentVariablesEnabled()) {
+        overrideWithEnvironmentAndSystemProperties(prop,
+                
mainConfigurationProperties.isAutoConfigurationEnvironmentVariablesEnabled(),
+                
mainConfigurationProperties.isAutoConfigurationSystemPropertiesEnabled(), 
prefixes);
+    }
+
+    private static void overrideWithEnvironmentAndSystemProperties(
+            OrderedLocationProperties prop, boolean envEnabled, boolean 
jvmEnabled, String... prefixes) {
+        if (envEnabled) {
             Properties propENV = 
MainHelper.loadEnvironmentVariablesAsProperties(prefixes);
             if (!propENV.isEmpty()) {
                 putAllOverride(prop, "ENV", propENV);
@@ -1275,7 +1308,7 @@ public abstract class BaseMainSupport extends BaseService 
{
                 }
             }
         }
-        if 
(mainConfigurationProperties.isAutoConfigurationSystemPropertiesEnabled()) {
+        if (jvmEnabled) {
             Properties propJVM = 
MainHelper.loadJvmSystemPropertiesAsProperties(prefixes);
             if (!propJVM.isEmpty()) {
                 putAllOverride(prop, "SYS", propJVM);
diff --git 
a/core/camel-main/src/test/java/org/apache/camel/main/MainTypeConverterStatisticsEnabledTest.java
 
b/core/camel-main/src/test/java/org/apache/camel/main/MainTypeConverterStatisticsEnabledTest.java
index b2aa25b9e863..e02adec718e4 100644
--- 
a/core/camel-main/src/test/java/org/apache/camel/main/MainTypeConverterStatisticsEnabledTest.java
+++ 
b/core/camel-main/src/test/java/org/apache/camel/main/MainTypeConverterStatisticsEnabledTest.java
@@ -105,6 +105,31 @@ public class MainTypeConverterStatisticsEnabledTest {
         }
     }
 
+    @Test
+    public void testSystemPropertyIgnoredWhenSystemPropertiesDisabled() throws 
Exception {
+        // the JVM system properties are disabled in the properties, so the 
early lookup must not use them either
+        System.setProperty("camel.main.typeConverterStatisticsEnabled", 
"true");
+        try {
+            Main main = new Main();
+            
main.addInitialProperty("camel.main.autoConfigurationSystemPropertiesEnabled", 
"false");
+            main.start();
+            try {
+                CamelContext context = main.getCamelContext();
+                TypeConverterRegistry.Statistics statistics = 
context.getTypeConverterRegistry().getStatistics();
+                long before = statistics.getAttemptCounter();
+
+                assertEquals(123, 
context.getTypeConverter().convertTo(int.class, "123"));
+
+                assertFalse(context.isTypeConverterStatisticsEnabled());
+                assertEquals(before, statistics.getAttemptCounter());
+            } finally {
+                main.stop();
+            }
+        } finally {
+            System.clearProperty("camel.main.typeConverterStatisticsEnabled");
+        }
+    }
+
     private static void assertStatisticsEnabled(Main main) throws Exception {
         main.start();
         try {

Reply via email to