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 39ca0e7  CAMEL-16776: Autowiring components should be deferred till 
later when using main based Camel which allows to read the configuration and 
know if autowiring is enabled or not before using it. The old behavior should 
be to always autowire which is what classic Camel uses.
39ca0e7 is described below

commit 39ca0e7a8fb0520e10f754de84b5415ac8af8495
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Fri Jul 2 16:14:00 2021 +0200

    CAMEL-16776: Autowiring components should be deferred till later when using 
main based Camel which allows to read the configuration and know if autowiring 
is enabled or not before using it. The old behavior should be to always 
autowire which is what classic Camel uses.
---
 .../camel/spi/AutowiredLifecycleStrategy.java      | 23 +++++++++
 .../camel/impl/engine/AbstractCamelContext.java    |  2 +-
 ...java => DefaultAutowiredLifecycleStrategy.java} | 55 +++-------------------
 .../MainAutowiredLifecycleStrategy.java}           |  9 ++--
 .../org/apache/camel/main/BaseMainSupport.java     |  5 ++
 5 files changed, 41 insertions(+), 53 deletions(-)

diff --git 
a/core/camel-api/src/main/java/org/apache/camel/spi/AutowiredLifecycleStrategy.java
 
b/core/camel-api/src/main/java/org/apache/camel/spi/AutowiredLifecycleStrategy.java
new file mode 100644
index 0000000..899ef26
--- /dev/null
+++ 
b/core/camel-api/src/main/java/org/apache/camel/spi/AutowiredLifecycleStrategy.java
@@ -0,0 +1,23 @@
+/*
+ * 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.spi;
+
+/**
+ * Marker interface for {@link LifecycleStrategy} that are used for 
auto-wiring components, data formats and languages.
+ */
+public interface AutowiredLifecycleStrategy extends LifecycleStrategy {
+}
diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 6d0c86d..cee6d68 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -370,7 +370,7 @@ public abstract class AbstractCamelContext extends 
BaseService
         this.lifecycleStrategies.add(new OnCamelContextLifecycleStrategy());
 
         // add a default autowired strategy
-        this.lifecycleStrategies.add(new AutowiredLifecycleStrategy(this));
+        this.lifecycleStrategies.add(new 
DefaultAutowiredLifecycleStrategy(this));
 
         // add a default LifecycleStrategy to customize services using 
customizers from registry
         this.lifecycleStrategies.add(new CustomizersLifecycleStrategy(this));
diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AutowiredLifecycleStrategy.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultAutowiredLifecycleStrategy.java
similarity index 65%
copy from 
core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AutowiredLifecycleStrategy.java
copy to 
core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultAutowiredLifecycleStrategy.java
index e2b878a..4261554 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AutowiredLifecycleStrategy.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultAutowiredLifecycleStrategy.java
@@ -16,14 +16,12 @@
  */
 package org.apache.camel.impl.engine;
 
-import java.util.HashMap;
-import java.util.Map;
 import java.util.Set;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Component;
 import org.apache.camel.ExtendedCamelContext;
-import org.apache.camel.VetoCamelContextStartException;
+import org.apache.camel.spi.AutowiredLifecycleStrategy;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.spi.Language;
 import org.apache.camel.spi.PropertyConfigurer;
@@ -32,68 +30,29 @@ import org.apache.camel.support.LifecycleStrategySupport;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-class AutowiredLifecycleStrategy extends LifecycleStrategySupport {
+class DefaultAutowiredLifecycleStrategy extends LifecycleStrategySupport 
implements AutowiredLifecycleStrategy {
 
-    private static final Logger LOG = 
LoggerFactory.getLogger(AutowiredLifecycleStrategy.class);
+    private static final Logger LOG = 
LoggerFactory.getLogger(DefaultAutowiredLifecycleStrategy.class);
 
-    // provisional maps to hold components, dataformats, languages that are 
created during
-    // starting camel, but need to defer autowiring until later in case 
additional configuration
-    // would turn off autowired for some components
-    private final Map<String, Component> autowrieComponents = new HashMap<>();
-    private final Map<String, DataFormat> autowrieDataFormats = new 
HashMap<>();
-    private final Map<String, Language> autowrieLanguages = new HashMap<>();
     private final ExtendedCamelContext camelContext;
-    private volatile boolean initializing;
 
-    public AutowiredLifecycleStrategy(CamelContext camelContext) {
+    public DefaultAutowiredLifecycleStrategy(CamelContext camelContext) {
         this.camelContext = (ExtendedCamelContext) camelContext;
     }
 
     @Override
-    public void onContextInitializing(CamelContext context) throws 
VetoCamelContextStartException {
-        // we have parsed configuration (such as via camel-main) and are now 
initializing
-        // so lets do autowiring on what we have collected so far
-        // we also need to eager autowire components as when they create 
endpoints they must be configured with
-        // those autowired options
-        autowrieComponents.forEach(this::autowireComponent);
-        autowrieDataFormats.forEach(this::autowireDataFormat);
-        autowrieLanguages.forEach(this::autowireLanguage);
-        autowrieComponents.clear();
-        autowrieDataFormats.clear();
-        autowrieLanguages.clear();
-        initializing = true;
-    }
-
-    @Override
-    public void onContextStopped(CamelContext context) {
-        initializing = false;
-    }
-
-    @Override
     public void onComponentAdd(String name, Component component) {
-        if (initializing) {
-            autowireComponent(name, component);
-        } else {
-            autowrieComponents.put(name, component);
-        }
+        autowireComponent(name, component);
     }
 
     @Override
     public void onDataFormatCreated(String name, DataFormat dataFormat) {
-        if (initializing) {
-            autowireDataFormat(name, dataFormat);
-        } else {
-            autowrieDataFormats.put(name, dataFormat);
-        }
+        autowireDataFormat(name, dataFormat);
     }
 
     @Override
     public void onLanguageCreated(String name, Language language) {
-        if (initializing) {
-            autowireLanguage(name, language);
-        } else {
-            autowrieLanguages.put(name, language);
-        }
+        autowireLanguage(name, language);
     }
 
     private void autowireComponent(String name, Component component) {
diff --git 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AutowiredLifecycleStrategy.java
 
b/core/camel-base-engine/src/main/java/org/apache/camel/main/MainAutowiredLifecycleStrategy.java
similarity index 94%
rename from 
core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AutowiredLifecycleStrategy.java
rename to 
core/camel-base-engine/src/main/java/org/apache/camel/main/MainAutowiredLifecycleStrategy.java
index e2b878a..c72bacc 100644
--- 
a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/AutowiredLifecycleStrategy.java
+++ 
b/core/camel-base-engine/src/main/java/org/apache/camel/main/MainAutowiredLifecycleStrategy.java
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.camel.impl.engine;
+package org.apache.camel.main;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -24,6 +24,7 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.Component;
 import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.VetoCamelContextStartException;
+import org.apache.camel.spi.AutowiredLifecycleStrategy;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.spi.Language;
 import org.apache.camel.spi.PropertyConfigurer;
@@ -32,9 +33,9 @@ import org.apache.camel.support.LifecycleStrategySupport;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-class AutowiredLifecycleStrategy extends LifecycleStrategySupport {
+public class MainAutowiredLifecycleStrategy extends LifecycleStrategySupport 
implements AutowiredLifecycleStrategy {
 
-    private static final Logger LOG = 
LoggerFactory.getLogger(AutowiredLifecycleStrategy.class);
+    private static final Logger LOG = 
LoggerFactory.getLogger(MainAutowiredLifecycleStrategy.class);
 
     // provisional maps to hold components, dataformats, languages that are 
created during
     // starting camel, but need to defer autowiring until later in case 
additional configuration
@@ -45,7 +46,7 @@ class AutowiredLifecycleStrategy extends 
LifecycleStrategySupport {
     private final ExtendedCamelContext camelContext;
     private volatile boolean initializing;
 
-    public AutowiredLifecycleStrategy(CamelContext camelContext) {
+    public MainAutowiredLifecycleStrategy(CamelContext camelContext) {
         this.camelContext = (ExtendedCamelContext) camelContext;
     }
 
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 26a0648..331224f 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
@@ -46,6 +46,7 @@ import org.apache.camel.health.HealthCheckConfiguration;
 import org.apache.camel.health.HealthCheckRegistry;
 import org.apache.camel.health.HealthCheckRepository;
 import org.apache.camel.saga.CamelSagaService;
+import org.apache.camel.spi.AutowiredLifecycleStrategy;
 import org.apache.camel.spi.CamelBeanPostProcessor;
 import org.apache.camel.spi.DataFormat;
 import org.apache.camel.spi.Language;
@@ -514,6 +515,10 @@ public abstract class BaseMainSupport extends BaseService {
     }
 
     protected void postProcessCamelContext(CamelContext camelContext) throws 
Exception {
+        // 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);
         // setup startup recorder before building context

Reply via email to