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/01/11 14:35:51 UTC

[camel] branch camel-3.x updated: CAMEL-18878 Autowiring on endpoint works even if is disabled on component (#9042)

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

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


The following commit(s) were added to refs/heads/camel-3.x by this push:
     new c018e30a39b CAMEL-18878 Autowiring on endpoint works even if is disabled on component (#9042)
c018e30a39b is described below

commit c018e30a39b6d4898b4c6452561fde9eb300be2c
Author: JiriOndrusek <on...@gmail.com>
AuthorDate: Wed Jan 11 15:35:13 2023 +0100

    CAMEL-18878 Autowiring on endpoint works even if is disabled on component (#9042)
---
 .../impl/DefaultComponentAutowiredFalseTest.java   | 68 +++++++++++++++++++++-
 .../org/apache/camel/support/DefaultEndpoint.java  |  2 +-
 2 files changed, 66 insertions(+), 4 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentAutowiredFalseTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentAutowiredFalseTest.java
index ee927126c3e..d1bf0161a73 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentAutowiredFalseTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentAutowiredFalseTest.java
@@ -22,12 +22,17 @@ import java.net.CookiePolicy;
 import java.util.Map;
 
 import org.apache.camel.CamelContext;
+import org.apache.camel.Consumer;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Endpoint;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
 import org.apache.camel.spi.GeneratedPropertyConfigurer;
 import org.apache.camel.spi.PropertyConfigurerGetter;
 import org.apache.camel.spi.Registry;
+import org.apache.camel.spi.annotations.Component;
 import org.apache.camel.support.DefaultComponent;
+import org.apache.camel.support.DefaultEndpoint;
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
@@ -37,6 +42,7 @@ public class DefaultComponentAutowiredFalseTest extends ContextTestSupport {
     protected Registry createRegistry() throws Exception {
         Registry reg = super.createRegistry();
         reg.bind("mycomponent-component", new MyComponentConfigurer());
+        reg.bind("mycomponent-endpoint-configurer", new MyComponentConfigurer());
         reg.bind("chf", new MyContentHandlerFactory());
         return reg;
     }
@@ -58,6 +64,27 @@ public class DefaultComponentAutowiredFalseTest extends ContextTestSupport {
         Assertions.assertNull(my.getCookiePolicy());
     }
 
+    @Test
+    public void testAutowiredFalseWithEndpointTrue() throws Exception {
+        MyComponent my = new MyComponent(context);
+        my.setAutowiredEnabled(false);
+        context.addComponent("mycomponent", my);
+
+        my = context.getComponent("mycomponent", MyComponent.class);
+        Assertions.assertNotNull(my);
+
+        ContentHandlerFactory chf = (ContentHandlerFactory) context.getRegistry().lookupByName("chf");
+        Assertions.assertNotNull(chf);
+
+        // should not be autowired
+        Assertions.assertNull(my.getContentHandlerFactory());
+        Assertions.assertNull(my.getCookiePolicy());
+
+        // endpoint
+        MyEndpoint me = context.getEndpoint("mycomponent://test", MyEndpoint.class);
+        Assertions.assertNull(me.getContentHandlerFactory());
+    }
+
     @Test
     public void testAutowiredTrue() throws Exception {
         MyComponent my = new MyComponent(context);
@@ -75,6 +102,7 @@ public class DefaultComponentAutowiredFalseTest extends ContextTestSupport {
         Assertions.assertNull(my.getCookiePolicy());
     }
 
+    @Component("mycomponent")
     private static final class MyComponent extends DefaultComponent {
 
         private ContentHandlerFactory contentHandlerFactory;
@@ -86,7 +114,9 @@ public class DefaultComponentAutowiredFalseTest extends ContextTestSupport {
 
         @Override
         protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
-            return null;
+            MyEndpoint me = new MyEndpoint();
+            me.setComponent(this);
+            return me;
         }
 
         public ContentHandlerFactory getContentHandlerFactory() {
@@ -117,8 +147,13 @@ public class DefaultComponentAutowiredFalseTest extends ContextTestSupport {
         @Override
         public boolean configure(CamelContext camelContext, Object target, String name, Object value, boolean ignoreCase) {
             if ("contentHandlerFactory".equals(name)) {
-                MyComponent comp = (MyComponent) target;
-                comp.setContentHandlerFactory((ContentHandlerFactory) value);
+                if (target instanceof MyComponent) {
+                    MyComponent comp = (MyComponent) target;
+                    comp.setContentHandlerFactory((ContentHandlerFactory) value);
+                } else {
+                    MyEndpoint endp = (MyEndpoint) target;
+                    endp.setContentHandlerFactory((ContentHandlerFactory) value);
+                }
                 return true;
             } else {
                 return false;
@@ -148,4 +183,31 @@ public class DefaultComponentAutowiredFalseTest extends ContextTestSupport {
         }
     }
 
+    private static final class MyEndpoint extends DefaultEndpoint {
+        private ContentHandlerFactory contentHandlerFactory;
+
+        @Override
+        public Producer createProducer() throws Exception {
+            return null;
+        }
+
+        @Override
+        public Consumer createConsumer(Processor processor) throws Exception {
+            return null;
+        }
+
+        @Override
+        public boolean isSingleton() {
+            return false;
+        }
+
+        public ContentHandlerFactory getContentHandlerFactory() {
+            return contentHandlerFactory;
+        }
+
+        public void setContentHandlerFactory(ContentHandlerFactory contentHandlerFactory) {
+            this.contentHandlerFactory = contentHandlerFactory;
+        }
+    }
+
 }
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java b/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java
index 39334ef3fab..d56aebc35ba 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java
@@ -493,7 +493,7 @@ public abstract class DefaultEndpoint extends ServiceSupport implements Endpoint
     protected void doInit() throws Exception {
         ObjectHelper.notNull(getCamelContext(), "camelContext");
 
-        if (autowiredEnabled && getComponent() != null) {
+        if (autowiredEnabled && getComponent() != null && getComponent().isAutowiredEnabled()) {
             PropertyConfigurer configurer = getComponent().getEndpointPropertyConfigurer();
             if (configurer instanceof PropertyConfigurerGetter) {
                 PropertyConfigurerGetter getter = (PropertyConfigurerGetter) configurer;