You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/08/23 13:48:02 UTC

[camel] 01/03: CAMEL-19772: camel-core - Dump routes to include custom beans

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 7585e9d5bd9dd459e790dbb72ccea23a644c6c09
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Aug 23 15:01:19 2023 +0200

    CAMEL-19772: camel-core - Dump routes to include custom beans
---
 .../org/apache/camel/yaml/LwModelToYAMLDumper.java |  2 +-
 .../java/org/apache/camel/main/KameletMain.java    | 35 ++++++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/LwModelToYAMLDumper.java b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/LwModelToYAMLDumper.java
index c3081ca2d68..ed49ca2fceb 100644
--- a/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/LwModelToYAMLDumper.java
+++ b/core/camel-yaml-io/src/main/java/org/apache/camel/yaml/LwModelToYAMLDumper.java
@@ -309,7 +309,7 @@ public class LwModelToYAMLDumper implements ModelToYAMLDumper {
 
         private void doWriteRegistryBeanDefinition(RegistryBeanDefinition b) {
             buffer.write(String.format("    - name: %s%n", b.getName()));
-            buffer.write(String.format("      type: %s%n", b.getType()));
+            buffer.write(String.format("      type: \"%s\"%n", b.getType()));
             if (b.getProperties() != null && !b.getProperties().isEmpty()) {
                 buffer.write(String.format("      properties:%n"));
                 for (Map.Entry<String, Object> entry : b.getProperties().entrySet()) {
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 3c434a76cad..3df25dd3d73 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
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
@@ -65,6 +66,8 @@ import org.apache.camel.main.download.PackageNameSourceLoader;
 import org.apache.camel.main.download.TypeConverterLoaderDownloadListener;
 import org.apache.camel.main.injection.AnnotationDependencyInjection;
 import org.apache.camel.main.util.ExtraFilesClassLoader;
+import org.apache.camel.model.Model;
+import org.apache.camel.model.app.RegistryBeanDefinition;
 import org.apache.camel.spi.ClassResolver;
 import org.apache.camel.spi.CliConnector;
 import org.apache.camel.spi.CliConnectorFactory;
@@ -84,11 +87,15 @@ import org.apache.camel.support.PluginHelper;
 import org.apache.camel.support.RouteOnDemandReloadStrategy;
 import org.apache.camel.support.service.ServiceHelper;
 import org.apache.camel.tooling.maven.MavenGav;
+import org.springframework.beans.MutablePropertyValues;
+import org.springframework.beans.PropertyValue;
 import org.springframework.beans.factory.BeanFactory;
 import org.springframework.beans.factory.CannotLoadBeanClassException;
 import org.springframework.beans.factory.SmartFactoryBean;
 import org.springframework.beans.factory.SmartInitializingSingleton;
 import org.springframework.beans.factory.config.BeanDefinition;
+import org.springframework.beans.factory.config.BeanReference;
+import org.springframework.beans.factory.config.TypedStringValue;
 import org.springframework.beans.factory.support.AbstractBeanDefinition;
 import org.springframework.beans.factory.support.DefaultListableBeanFactory;
 import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
@@ -798,6 +805,34 @@ public class KameletMain extends MainCommandLineSupport {
                 // rely on the bean factory to implement prototype scope
                 registry.bind(name, (Supplier<Object>) () -> beanFactory.getBean(name));
             }
+
+            // register bean into model (as a BeanRegistry that allows Camel DSL to know about these beans)
+            Model model = camelContext.getCamelContextExtension().getContextPlugin(Model.class);
+            if (model != null) {
+                RegistryBeanDefinition rrd = new RegistryBeanDefinition();
+                // TODO: Resource
+                // rrd.setResource();
+                rrd.setType(def.getBeanClassName());
+                rrd.setName(name);
+                model.addRegistryBean(rrd);
+                if (def.hasPropertyValues()) {
+                    Map<String, Object> properties = new LinkedHashMap<>();
+                    rrd.setProperties(properties);
+
+                    MutablePropertyValues values = def.getPropertyValues();
+                    for (PropertyValue v : values) {
+                        String key = v.getName();
+                        PropertyValue src = v.getOriginalPropertyValue();
+                        Object val = src.getValue();
+                        // ref was string value
+                        if (val instanceof TypedStringValue tsv) {
+                            properties.put(key, tsv.getValue());
+                        } else if (val instanceof BeanReference br) {
+                            properties.put(key, "#bean:" + br.getBeanName());
+                        }
+                    }
+                }
+            }
         }
     }