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 3d04902e660 camel-jbang: Should not autowire in stub mode
3d04902e660 is described below

commit 3d04902e6602c2cba4acec10fa74a1e94f3169d5
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Wed Feb 19 11:29:15 2025 +0100

    camel-jbang: Should not autowire in stub mode
---
 .../java/org/apache/camel/main/KameletMain.java    |  7 ++
 .../download/StubComponentAutowireStrategy.java    | 89 ++++++++++++++++++++++
 2 files changed, 96 insertions(+)

diff --git 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
index ff74cec1e4c..c975b3d2c90 100644
--- 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
+++ 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/KameletMain.java
@@ -71,6 +71,7 @@ import org.apache.camel.main.download.PackageNameSourceLoader;
 import org.apache.camel.main.download.PromptPropertyPlaceholderSource;
 import org.apache.camel.main.download.SagaDownloader;
 import org.apache.camel.main.download.StubBeanRepository;
+import org.apache.camel.main.download.StubComponentAutowireStrategy;
 import org.apache.camel.main.download.TransactedDownloader;
 import org.apache.camel.main.download.TypeConverterLoaderDownloadListener;
 import org.apache.camel.main.injection.AnnotationDependencyInjection;
@@ -637,6 +638,12 @@ public class KameletMain extends MainCommandLineSupport {
             
answer.getCamelContextExtension().addContextPlugin(ResourceLoader.class,
                     new DependencyDownloaderResourceLoader(answer, sourceDir));
 
+            if (stubPattern != null) {
+                // need to replace autowire strategy with stub capable
+                answer.getLifecycleStrategies()
+                        .removeIf(s -> 
s.getClass().getSimpleName().equals("DefaultAutowiredLifecycleStrategy"));
+                answer.getLifecycleStrategies().add(new 
StubComponentAutowireStrategy(answer, stubPattern));
+            }
             answer.setInjector(new KameletMainInjector(answer.getInjector(), 
stubPattern, silent));
             Object kameletsVersion = 
getInitialProperties().get(getInstanceType() + ".kameletsVersion");
             if (kameletsVersion != null) {
diff --git 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/StubComponentAutowireStrategy.java
 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/StubComponentAutowireStrategy.java
new file mode 100644
index 00000000000..50ddf0da72e
--- /dev/null
+++ 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/StubComponentAutowireStrategy.java
@@ -0,0 +1,89 @@
+/*
+ * 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.download;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Component;
+import org.apache.camel.Ordered;
+import org.apache.camel.spi.AutowiredLifecycleStrategy;
+import org.apache.camel.spi.DataFormat;
+import org.apache.camel.spi.Language;
+import org.apache.camel.support.LifecycleStrategySupport;
+
+public class StubComponentAutowireStrategy extends LifecycleStrategySupport 
implements AutowiredLifecycleStrategy, Ordered {
+    private final CamelContext camelContext;
+    private final String pattern;
+
+    public StubComponentAutowireStrategy(CamelContext camelContext, String 
pattern) {
+        this.camelContext = camelContext;
+        this.pattern = pattern;
+    }
+
+    @Override
+    public int getOrder() {
+        // we should be last
+        return Ordered.LOWEST;
+    }
+
+    @Override
+    public void onComponentAdd(String name, Component component) {
+        autowireComponent(name, component);
+    }
+
+    @Override
+    public void onDataFormatCreated(String name, DataFormat dataFormat) {
+        autowireDataFormat(name, dataFormat);
+    }
+
+    @Override
+    public void onLanguageCreated(String name, Language language) {
+        autowireLanguage(name, language);
+    }
+
+    private void autowireComponent(String name, Component component) {
+        // autowiring can be turned off on context level and per component
+        boolean enabled = camelContext.isAutowiredEnabled() && 
component.isAutowiredEnabled();
+        if (enabled) {
+            autowire(name, "component", component);
+        }
+    }
+
+    private void autowireDataFormat(String name, DataFormat dataFormat) {
+        // autowiring can be turned off on context level
+        boolean enabled = camelContext.isAutowiredEnabled();
+        if (enabled) {
+            autowire(name, "dataformat", dataFormat);
+        }
+    }
+
+    private void autowireLanguage(String name, Language language) {
+        // autowiring can be turned off on context level
+        boolean enabled = camelContext.isAutowiredEnabled();
+        if (enabled) {
+            autowire(name, "language", language);
+        }
+    }
+
+    private void autowire(String name, String kind, Object target) {
+        if (pattern.contains(name)) {
+            // do not autowire
+        } else {
+            doAutoWire(name, kind, target, camelContext);
+        }
+    }
+
+}

Reply via email to