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
commit 10c8576a56fe0d08053d6c925a45f96a0d262464 Author: Claus Ibsen <[email protected]> AuthorDate: Thu Apr 14 14:49:46 2022 +0200 CAMEL-17969: camel-main - Property-placeholder summary --- .../properties/DefaultPropertiesLookup.java | 8 +++ .../org/apache/camel/main/BaseMainSupport.java | 57 ++++++++++++++++++++++ .../java/org/apache/camel/dsl/modeline/Trait.java | 5 +- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java index e5573f83d6f..c20e9df3909 100644 --- a/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java +++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/DefaultPropertiesLookup.java @@ -21,7 +21,9 @@ import java.util.Properties; import org.apache.camel.NoTypeConversionAvailableException; import org.apache.camel.PropertiesLookupListener; import org.apache.camel.RuntimeCamelException; +import org.apache.camel.spi.LoadablePropertiesSource; import org.apache.camel.spi.PropertiesSource; +import org.apache.camel.util.OrderedLocationProperties; /** * Default {@link PropertiesLookup} which lookup properties from a {@link java.util.Properties} with all existing @@ -81,6 +83,12 @@ public class DefaultPropertiesLookup implements PropertiesLookup { source = "ref:" + ((LocationPropertiesSource) ps).getLocation().getPath(); } else if (ps instanceof LocationPropertiesSource) { source = ((LocationPropertiesSource) ps).getLocation().getPath(); + } else if (ps instanceof LoadablePropertiesSource) { + Properties prop = ((LoadablePropertiesSource) ps).loadProperties(); + if (prop instanceof OrderedLocationProperties) { + OrderedLocationProperties olp = (OrderedLocationProperties) prop; + source = olp.getLocation(name); + } } onLookup(name, answer, source); break; 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 0cf744ed4da..c5c8a16ebc7 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 @@ -39,6 +39,7 @@ import org.apache.camel.Component; import org.apache.camel.Configuration; import org.apache.camel.ExtendedCamelContext; import org.apache.camel.NoSuchLanguageException; +import org.apache.camel.PropertiesLookupListener; import org.apache.camel.RuntimeCamelException; import org.apache.camel.StartupStep; import org.apache.camel.console.DevConsole; @@ -46,9 +47,11 @@ import org.apache.camel.console.DevConsoleRegistry; import org.apache.camel.health.HealthCheck; import org.apache.camel.health.HealthCheckRegistry; import org.apache.camel.health.HealthCheckRepository; +import org.apache.camel.impl.event.CamelContextRoutesStartedEvent; import org.apache.camel.saga.CamelSagaService; import org.apache.camel.spi.AutowiredLifecycleStrategy; import org.apache.camel.spi.CamelBeanPostProcessor; +import org.apache.camel.spi.CamelEvent; import org.apache.camel.spi.DataFormat; import org.apache.camel.spi.Language; import org.apache.camel.spi.PackageScanClassResolver; @@ -56,6 +59,7 @@ import org.apache.camel.spi.PropertiesComponent; import org.apache.camel.spi.RouteTemplateParameterSource; import org.apache.camel.spi.StartupStepRecorder; import org.apache.camel.support.CamelContextHelper; +import org.apache.camel.support.EventNotifierSupport; import org.apache.camel.support.LifecycleStrategySupport; import org.apache.camel.support.PropertyBindingSupport; import org.apache.camel.support.ResourceHelper; @@ -534,12 +538,18 @@ public abstract class BaseMainSupport extends BaseService { } protected void postProcessCamelContext(CamelContext camelContext) throws Exception { + // gathers the properties (key=value) that was used as property placeholders during bootstrap + final OrderedLocationProperties propertyPlaceholders = new OrderedLocationProperties(); + // use the main autowired lifecycle strategy instead of the default camelContext.getLifecycleStrategies().removeIf(s -> s instanceof AutowiredLifecycleStrategy); camelContext.addLifecycleStrategy(new MainAutowiredLifecycleStrategy(camelContext)); // setup properties configurePropertiesService(camelContext); + // register listener on properties component so we can capture them + PropertiesComponent pc = camelContext.getPropertiesComponent(); + pc.addPropertiesLookupListener(new PropertyPlaceholderListener(propertyPlaceholders)); // setup startup recorder before building context configureStartupRecorder(camelContext); // setup package scan @@ -586,6 +596,36 @@ public abstract class BaseMainSupport extends BaseService { listener.afterConfigure(this); listener.configure(camelContext); } + + // we want to log the property placeholder summary after routes has been started, + // but before camel context logs that it has been started, so we need to use an event listener + if (standalone && mainConfigurationProperties.isAutoConfigurationLogSummary()) { + camelContext.getManagementStrategy().addEventNotifier(new EventNotifierSupport() { + @Override + public boolean isEnabled(CamelEvent event) { + return event instanceof CamelContextRoutesStartedEvent; + } + + @Override + public void notify(CamelEvent event) throws Exception { + // log summary of configurations + if (!propertyPlaceholders.isEmpty()) { + LOG.info("Property-placeholders summary"); + for (var entry : propertyPlaceholders.entrySet()) { + String k = entry.getKey().toString(); + Object v = entry.getValue(); + String loc = locationSummary(propertyPlaceholders, k); + + if (SensitiveUtils.containsSensitive(k)) { + LOG.info(" {} {}=xxxxxx", loc, k); + } else { + LOG.info(" {} {}={}", loc, k, v); + } + } + } + } + }); + } } protected void autoConfigurationFailFast(CamelContext camelContext, OrderedLocationProperties autoConfiguredProperties) @@ -1628,4 +1668,21 @@ public abstract class BaseMainSupport extends BaseService { return loc; } + private static final class PropertyPlaceholderListener implements PropertiesLookupListener { + + private final OrderedLocationProperties olp; + + public PropertyPlaceholderListener(OrderedLocationProperties olp) { + this.olp = olp; + } + + @Override + public void onLookup(String name, String value, String source) { + if (source == null) { + source = "unknown"; + } + olp.put(source, name, value); + } + } + } diff --git a/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/Trait.java b/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/Trait.java index 93473fc656e..d91c3b0f48d 100644 --- a/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/Trait.java +++ b/dsl/camel-dsl-modeline/src/main/java/org/apache/camel/dsl/modeline/Trait.java @@ -20,10 +20,13 @@ import org.apache.camel.spi.CamelContextCustomizer; import org.apache.camel.spi.Resource; /** - * modeline trait + * Modeline trait */ public interface Trait { + /** + * Name of trait + */ String getName(); /**
