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 419bc3eb5cd7 CAMEL-27103: camel-main - share the parsing of the early 
options and name them in the WARN (#31286)
419bc3eb5cd7 is described below

commit 419bc3eb5cd7434e3d94f986a566f3c289ea1cd5
Author: Loris Boiteux <[email protected]>
AuthorDate: Thu Dec 3 14:26:16 2026 +0100

    CAMEL-27103: camel-main - share the parsing of the early options and name 
them in the WARN (#31286)
    
    * CAMEL-27103: camel-main - share the parsing of the early options and name 
them in the WARN
      Follow-up of the review of #31195:
      - isEarlyEnabled and lookupEarlyOption share parseEarlyBoolean for the 
empty,
        placeholder and parse steps, so they cannot drift apart.
      - The WARN for a property placeholder in
        autoConfigurationEnvironmentVariablesEnabled or
        autoConfigurationSystemPropertiesEnabled names the options that still 
read
        the source, and has the same wording as the WARN of the early options.
      - The 4.25 upgrade guide tells that virtualThreadsEnabled and
        typeConverterStatisticsEnabled now ignore a disabled ENV or SYS source.
    * CAMEL-27103: camel-main - say which methods EARLY_OPTIONS must stay in 
sync with
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../org/apache/camel/main/BaseMainSupport.java     |  52 ++++----
 .../MainEarlyOptionsPlaceholderWarningTest.java    | 135 +++++++++++++++++++++
 .../ROOT/pages/camel-4x-upgrade-guide-4_25.adoc    |   9 ++
 3 files changed, 174 insertions(+), 22 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 4197c9860f9f..efa112328241 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
@@ -151,6 +151,11 @@ public abstract class BaseMainSupport extends BaseService {
 
     private static final Logger LOG = 
LoggerFactory.getLogger(BaseMainSupport.class);
 
+    // the options that must be known before the CamelContext is built (see 
loadEarlyProperties)
+    // keep in sync with configureVirtualThreadsEarly and 
configureTypeConverterStatisticsEarly
+    private static final String EARLY_OPTIONS
+            = "camel.main.virtualThreadsEnabled and 
camel.main.typeConverterStatisticsEnabled";
+
     private static final String PREFIX_SERVER = "camel.server.";
     private static final String PREFIX_SSL = "camel.ssl.";
     private static final String PREFIX_SECURITY = "camel.security.";
@@ -990,25 +995,39 @@ public abstract class BaseMainSupport extends BaseService 
{
                 .loadProperties(name -> name.startsWith("camel."), 
MainHelper::optionKey);
         // the auto configuration has not yet applied these two options, so 
they are read here
         boolean envEnabled = isEarlyEnabled(prop, 
"camel.main.autoConfigurationEnvironmentVariablesEnabled",
-                
mainConfigurationProperties.isAutoConfigurationEnvironmentVariablesEnabled());
+                
mainConfigurationProperties.isAutoConfigurationEnvironmentVariablesEnabled(),
+                "the OS environment variables");
         boolean jvmEnabled = isEarlyEnabled(prop, 
"camel.main.autoConfigurationSystemPropertiesEnabled",
-                
mainConfigurationProperties.isAutoConfigurationSystemPropertiesEnabled());
+                
mainConfigurationProperties.isAutoConfigurationSystemPropertiesEnabled(),
+                "the JVM system properties");
         overrideWithEnvironmentAndSystemProperties(prop, envEnabled, 
jvmEnabled, "camel.main.");
         return prop;
     }
 
-    private static boolean isEarlyEnabled(OrderedLocationProperties prop, 
String optionName, boolean defaultValue) {
-        Object value = prop.get(optionName);
+    private static boolean isEarlyEnabled(
+            OrderedLocationProperties prop, String optionName, boolean 
defaultValue, String source) {
+        Boolean enabled = parseEarlyBoolean(optionName, prop.get(optionName),
+                ", so " + EARLY_OPTIONS + (defaultValue ? " are still read 
from " : " are not read from ") + source);
+        return enabled != null ? enabled : defaultValue;
+    }
+
+    /**
+     * Parses the value of an option that is read before the CamelContext is 
built.
+     *
+     * @param  consequence what happens when the value is ignored, appended to 
the WARN
+     * @return             the value, or <code>null</code> if it is empty or 
has a property placeholder, which cannot be
+     *                     resolved before the CamelContext is built (a WARN 
is logged)
+     */
+    private static Boolean parseEarlyBoolean(String optionName, Object value, 
String consequence) {
         if (ObjectHelper.isEmpty(value)) {
-            return defaultValue;
+            return null;
         }
         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;
+            LOG.warn("The option {}={} is ignored, as it must be known before 
the CamelContext is built,"
+                     + " which is before property placeholders can be 
resolved{}. Use true or false instead.",
+                    optionName, str, consequence);
+            return null;
         }
         return Boolean.parseBoolean(str);
     }
@@ -1033,18 +1052,7 @@ public abstract class BaseMainSupport extends 
BaseService {
             // fallback to initial properties
             value = getInitialProperties().getProperty(optionName);
         }
-        if (ObjectHelper.isEmpty(value)) {
-            return null;
-        }
-
-        String str = value.toString().trim();
-        if (str.contains(PropertiesComponent.PREFIX_TOKEN)) {
-            LOG.warn("The option {}={} is ignored, as it must be known before 
the CamelContext is built,"
-                     + " which is before property placeholders can be 
resolved. Use true or false instead.",
-                    optionName, str);
-            return null;
-        }
-        return Boolean.parseBoolean(str);
+        return parseEarlyBoolean(optionName, value, "");
     }
 
     protected void configureRoutesLoader(CamelContext camelContext) {
diff --git 
a/core/camel-main/src/test/java/org/apache/camel/main/MainEarlyOptionsPlaceholderWarningTest.java
 
b/core/camel-main/src/test/java/org/apache/camel/main/MainEarlyOptionsPlaceholderWarningTest.java
new file mode 100644
index 000000000000..7178d2e62e94
--- /dev/null
+++ 
b/core/camel-main/src/test/java/org/apache/camel/main/MainEarlyOptionsPlaceholderWarningTest.java
@@ -0,0 +1,135 @@
+/*
+ * 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 java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.LoggerConfig;
+import org.apache.logging.log4j.core.config.Property;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.parallel.Isolated;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * The options read before the CamelContext is built cannot resolve a property 
placeholder: the value is ignored with a
+ * WARN that tells what happens instead.
+ */
+@Isolated
+class MainEarlyOptionsPlaceholderWarningTest {
+
+    private static final String LOGGER = BaseMainSupport.class.getName();
+
+    private final List<String> warnings = new CopyOnWriteArrayList<>();
+    private AbstractAppender appender;
+
+    @BeforeEach
+    void addAppender() {
+        appender = new 
AbstractAppender("MainEarlyOptionsPlaceholderWarningTest", null, null, true, 
Property.EMPTY_ARRAY) {
+            @Override
+            public void append(LogEvent event) {
+                if (event.getLevel() == Level.WARN) {
+                    warnings.add(event.getMessage().getFormattedMessage());
+                }
+            }
+        };
+        appender.start();
+        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+        Configuration config = ctx.getConfiguration();
+        LoggerConfig loggerConfig = new LoggerConfig(LOGGER, Level.WARN, true);
+        loggerConfig.addAppender(appender, Level.WARN, null);
+        config.addLogger(LOGGER, loggerConfig);
+        ctx.updateLoggers();
+    }
+
+    @AfterEach
+    void removeAppender() {
+        LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+        ctx.getConfiguration().removeLogger(LOGGER);
+        ctx.updateLoggers();
+        appender.stop();
+    }
+
+    private List<String> placeholderWarnings() {
+        return warnings.stream().filter(w -> w.contains("before property 
placeholders can be resolved")).toList();
+    }
+
+    @Test
+    void testSourceFlagWithPlaceholderNamesTheEarlyOptions() {
+        Main main = new Main();
+        
main.addInitialProperty("camel.main.autoConfigurationSystemPropertiesEnabled", 
"{{sysEnabled}}");
+        main.addInitialProperty("sysEnabled", "false");
+        main.start();
+        try {
+            List<String> list = placeholderWarnings();
+            assertEquals(List.of("The option 
camel.main.autoConfigurationSystemPropertiesEnabled={{sysEnabled}} is ignored,"
+                                 + " as it must be known before the 
CamelContext is built, which is before property"
+                                 + " placeholders can be resolved, so 
camel.main.virtualThreadsEnabled and"
+                                 + " camel.main.typeConverterStatisticsEnabled 
are still read from the JVM system properties."
+                                 + " Use true or false instead."),
+                    list);
+        } finally {
+            main.stop();
+        }
+    }
+
+    @Test
+    void testSourceFlagWithPlaceholderKeepsTheProgrammaticValue() {
+        Main main = new Main();
+        
main.configure().setAutoConfigurationEnvironmentVariablesEnabled(false);
+        
main.addInitialProperty("camel.main.autoConfigurationEnvironmentVariablesEnabled",
 "{{envEnabled}}");
+        main.addInitialProperty("envEnabled", "true");
+        main.start();
+        try {
+            List<String> list = placeholderWarnings();
+            assertEquals(List.of("The option 
camel.main.autoConfigurationEnvironmentVariablesEnabled={{envEnabled}} is 
ignored,"
+                                 + " as it must be known before the 
CamelContext is built, which is before property"
+                                 + " placeholders can be resolved, so 
camel.main.virtualThreadsEnabled and"
+                                 + " camel.main.typeConverterStatisticsEnabled 
are not read from the OS environment variables."
+                                 + " Use true or false instead."),
+                    list);
+        } finally {
+            main.stop();
+        }
+    }
+
+    @Test
+    void testEarlyOptionWithPlaceholder() {
+        Main main = new Main();
+        main.addInitialProperty("camel.main.typeConverterStatisticsEnabled", 
"{{statsEnabled}}");
+        main.addInitialProperty("statsEnabled", "true");
+        main.start();
+        try {
+            List<String> list = placeholderWarnings();
+            assertEquals(List.of("The option 
camel.main.typeConverterStatisticsEnabled={{statsEnabled}} is ignored,"
+                                 + " as it must be known before the 
CamelContext is built, which is before property"
+                                 + " placeholders can be resolved. Use true or 
false instead."),
+                    list);
+        } finally {
+            main.stop();
+        }
+    }
+}
diff --git 
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc 
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
index 5c8f75a17a97..df41cd8ff74c 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc
@@ -566,3 +566,12 @@ The Camel CLI (`camel-kamelet-main`) no longer ships 
Spring Framework (`spring-c
 was only there for the Spring XML support. The Spring annotations for 
dependency injection (`@Component`,
 `@Autowired`, ...) are still downloaded on demand. Routes or beans that use 
other Spring classes must add the
 dependency, such as 
`camel.jbang.dependencies=org.springframework:spring-context:<version>`.
+
+=== camel-main - virtualThreadsEnabled and typeConverterStatisticsEnabled 
honor the disabled ENV and SYS sources
+
+`camel.main.virtualThreadsEnabled` and 
`camel.main.typeConverterStatisticsEnabled` were read from the OS environment
+variables and the JVM system properties even when 
`camel.main.autoConfigurationEnvironmentVariablesEnabled` or
+`camel.main.autoConfigurationSystemPropertiesEnabled` was set to `false`, 
while every other option of Camel Main
+ignored them. Now these two options ignore a disabled source too: for example 
`CAMEL_MAIN_VIRTUALTHREADSENABLED=true`
+no longer turns on virtual threads when the OS environment variables are 
disabled. Set the option in
+`application.properties` instead, or enable the source again.

Reply via email to