Croway commented on PR #30303:
URL: https://github.com/apache/camel/pull/30303#issuecomment-6961751668
Thanks, and sorry: my comment crossed with your rework. I re-reviewed
31f90f1f4: the registry is back as on main and the camel-main tests pass. One
case where the early lookup and the auto configuration still disagree, and one
limitation that could be documented.
**1. A higher-priority `false` is ignored.** The auto configuration resolves
an option by priority (JVM system property, then environment variable, then the
properties component, then the initial properties), while
`isEnabledInConfiguration` returns `true` as soon as any source says `true`.
With `main.addInitialProperty("camel.main.typeConverterStatisticsEnabled",
"true")` and `-Dcamel.main.typeConverterStatisticsEnabled=false`, the context
ends with `isTypeConverterStatisticsEnabled() == false`, but the registry has
`TypeConverterStatistics` and counts. On main, statistics are off in that case.
`camel.main.virtualThreadsEnabled` has the same problem, as its early lookup on
main already stopped at the first `true`.
Resolving it the way `autoConfigurationSingleOption` does (ENV over the
properties component, SYS over ENV, key in any case, initial properties as
fallback) fixes it:
```java
private boolean isEnabledInConfiguration(CamelContext camelContext,
String key) {
if (!mainConfigurationProperties.isAutoConfigurationEnabled()) {
return false;
}
// same sources, priority and case as autoConfigurationSingleOption,
but the value is not resolved: resolving
// a placeholder needs the type converter, which does not exist
before the context is built
OrderedLocationProperties prop = (OrderedLocationProperties)
camelContext.getPropertiesComponent()
.loadProperties(name -> name.startsWith("camel."),
MainHelper::optionKey);
if
(mainConfigurationProperties.isAutoConfigurationEnvironmentVariablesEnabled()) {
putAllOverride(prop, "ENV",
MainHelper.loadEnvironmentVariablesAsProperties(new String[] { "camel.main."
}));
}
if
(mainConfigurationProperties.isAutoConfigurationSystemPropertiesEnabled()) {
putAllOverride(prop, "SYS",
MainHelper.loadJvmSystemPropertiesAsProperties(new String[] { "camel.main." }));
}
String name = findKeyIgnoreCase(prop, key);
Object value = name != null ? prop.get(name) : null;
if (ObjectHelper.isEmpty(value)) {
value = getInitialProperties().getProperty(key);
}
return value != null && Boolean.parseBoolean(value.toString());
}
```
`isTrue` then goes away. The lookup part of `autoConfigurationSingleOption`
could also be extracted and shared, rather than repeated. With this change,
this test passes, and so do the camel-main tests (including the 4 in
`MainTypeConverterStatisticsEnabledTest` and `MainVirtualThreadsTest`). It
fails on 31f90f1f4 and passes on main:
```java
@Test
public void testSystemPropertyFalseOverridesInitialPropertyTrue() throws
Exception {
System.setProperty("camel.main.typeConverterStatisticsEnabled",
"false");
try {
Main main = new Main();
main.addInitialProperty("camel.main.typeConverterStatisticsEnabled", "true");
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");
}
}
```
**2. Placeholder values (limitation).** `loadProperties` returns raw values,
so `camel.main.typeConverterStatisticsEnabled = {{stats}}` is read as `false`
before the build. The auto configuration resolves it later, so the context says
`true` while the registry stays `NoopTypeConverterStatistics`: the CAMEL-27103
symptom for that case (same on main). It cannot be resolved early: I tried
reusing `autoConfigurationSingleOption` (which calls
`CamelContextHelper.parseText`) for the early lookup, and startup fails with an
NPE in `DefaultPropertiesLookup` because `getTypeConverter()` is still null. A
sentence in the option's description (the value must be a literal `true`), or a
WARN when the early value contains `{{`, would avoid a silent no-op.
_Claude Code on behalf of Croway_
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]