This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch CAMEL-17571
in repository https://gitbox.apache.org/repos/asf/camel.git

commit edcbd22a65371e753dc13710598287cc90439832
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Mar 11 10:51:31 2022 +0100

    CAMEL-17571: camel-jbang - Support for spring @Component/@Service 
annotations in custom beans
---
 dsl/camel-kamelet-main/pom.xml                     | 13 +++++
 .../java/org/apache/camel/main/KameletMain.java    |  2 +
 .../apache/camel/main/SpringAnnotationSupport.java | 65 +++++++++++++++++++++-
 3 files changed, 79 insertions(+), 1 deletion(-)

diff --git a/dsl/camel-kamelet-main/pom.xml b/dsl/camel-kamelet-main/pom.xml
index c1b4f9f..0236a5f 100644
--- a/dsl/camel-kamelet-main/pom.xml
+++ b/dsl/camel-kamelet-main/pom.xml
@@ -105,6 +105,19 @@
             <artifactId>camel-catalog-console</artifactId>
         </dependency>
 
+        <!-- optional spring annotation support -->
+        <dependency>
+            <groupId>org.springframework</groupId>
+            <artifactId>spring-context</artifactId>
+            <version>${spring-version}</version>
+            <exclusions>
+                <exclusion>
+                    <groupId>org.springframework</groupId>
+                    <artifactId>*</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+
         <dependency>
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-timer</artifactId>
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 665c872..8279f1b 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
@@ -163,6 +163,8 @@ public class KameletMain extends MainCommandLineSupport {
         answer.setRegistry(registry);
         // load camel component and custom health-checks
         answer.setLoadHealthChecks(true);
+        // optional spring annotation support
+        SpringAnnotationSupport.registerSpringSupport(answer);
 
         // embed HTTP server if port is specified
         Object port = 
getInitialProperties().get("camel.jbang.platform-http.port");
diff --git 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/SpringAnnotationSupport.java
 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/SpringAnnotationSupport.java
index 1682402..22a185b 100644
--- 
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/SpringAnnotationSupport.java
+++ 
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/SpringAnnotationSupport.java
@@ -1,2 +1,65 @@
-package org.apache.camel.main;public class SpringAnnotationSupport {
+/*
+ * 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;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.dsl.support.AnnotationPreProcessor;
+import org.apache.camel.spi.CamelBeanPostProcessor;
+import org.apache.camel.util.ObjectHelper;
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+
+public final class SpringAnnotationSupport {
+
+    private SpringAnnotationSupport() {
+    }
+
+    public static void registerSpringSupport(CamelContext context) {
+        context.getRegistry().bind("SpringAnnotationPreProcessor", new 
SpringAnnotationPreProcessor());
+    }
+
+    private static class SpringAnnotationPreProcessor implements 
AnnotationPreProcessor {
+
+        @Override
+        public void handleAnnotation(CamelContext camelContext, String name, 
Class<?> clazz, Object instance) throws Exception {
+            // @Component and @Service are the same
+            Component comp = clazz.getAnnotation(Component.class);
+            Service service = clazz.getAnnotation(Service.class);
+            if (comp != null || service != null) {
+                CamelBeanPostProcessor bpp = 
camelContext.adapt(ExtendedCamelContext.class).getBeanPostProcessor();
+                if (comp != null && ObjectHelper.isNotEmpty(comp.value())) {
+                    name = comp.value();
+                } else if (service != null && 
ObjectHelper.isNotEmpty(service.value())) {
+                    name = service.value();
+                }
+                // to support hot reloading of beans then we need to enable 
unbind mode in bean post processor
+                bpp.setUnbindEnabled(true);
+                try {
+                    // re-bind the bean to the registry
+                    camelContext.getRegistry().unbind(name);
+                    camelContext.getRegistry().bind(name, instance);
+                    // this class is a bean service which needs to be post 
processed
+                    bpp.postProcessBeforeInitialization(instance, name);
+                    bpp.postProcessAfterInitialization(instance, name);
+                } finally {
+                    bpp.setUnbindEnabled(false);
+                }
+            }
+        }
+    }
 }

Reply via email to