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 2021/03/22 12:29:30 UTC

[camel] branch master updated: CAMEL-16382: Fixed setting autowried-enabled=false on component level.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 5f3dcf4  CAMEL-16382: Fixed setting autowried-enabled=false on component level.
5f3dcf4 is described below

commit 5f3dcf4087e17ffa9fd8ae08b2bbf50bdcb2c90d
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Mar 22 13:28:45 2021 +0100

    CAMEL-16382: Fixed setting autowried-enabled=false on component level.
---
 .../src/main/java/org/apache/camel/Component.java  |  10 ++
 .../impl/engine/AutowiredLifecycleStrategy.java    |   4 +-
 .../impl/DefaultComponentAutowiredFalseTest.java   | 151 +++++++++++++++++++++
 3 files changed, 163 insertions(+), 2 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/Component.java b/core/camel-api/src/main/java/org/apache/camel/Component.java
index 84a2148..dc4d964 100644
--- a/core/camel-api/src/main/java/org/apache/camel/Component.java
+++ b/core/camel-api/src/main/java/org/apache/camel/Component.java
@@ -120,4 +120,14 @@ public interface Component extends CamelContextAware, Service {
         return null;
     }
 
+    /**
+     * Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as
+     * autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets
+     * configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection
+     * factories, AWS Clients, etc.
+     */
+    default boolean isAutowiredEnabled() {
+        return true;
+    }
+
 }
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/AutowiredLifecycleStrategy.java
index 1a2956a..73da22f 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/AutowiredLifecycleStrategy.java
@@ -41,8 +41,8 @@ class AutowiredLifecycleStrategy extends LifecycleStrategySupport {
 
     @Override
     public void onComponentAdd(String name, Component component) {
-        // autowiring can be turned off on context level
-        boolean enabled = camelContext.isAutowiredEnabled();
+        // autowiring can be turned off on context level and per component
+        boolean enabled = camelContext.isAutowiredEnabled() && component.isAutowiredEnabled();
         if (enabled) {
             autwire(name, "component", component);
         }
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
new file mode 100644
index 0000000..ee92712
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/DefaultComponentAutowiredFalseTest.java
@@ -0,0 +1,151 @@
+/*
+ * 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.impl;
+
+import java.net.ContentHandler;
+import java.net.ContentHandlerFactory;
+import java.net.CookiePolicy;
+import java.util.Map;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.spi.GeneratedPropertyConfigurer;
+import org.apache.camel.spi.PropertyConfigurerGetter;
+import org.apache.camel.spi.Registry;
+import org.apache.camel.support.DefaultComponent;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class DefaultComponentAutowiredFalseTest extends ContextTestSupport {
+
+    @Override
+    protected Registry createRegistry() throws Exception {
+        Registry reg = super.createRegistry();
+        reg.bind("mycomponent-component", new MyComponentConfigurer());
+        reg.bind("chf", new MyContentHandlerFactory());
+        return reg;
+    }
+
+    @Test
+    public void testAutowiredFalse() 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());
+    }
+
+    @Test
+    public void testAutowiredTrue() throws Exception {
+        MyComponent my = new MyComponent(context);
+        my.setAutowiredEnabled(true);
+        context.addComponent("mycomponent", my);
+
+        my = context.getComponent("mycomponent", MyComponent.class);
+        Assertions.assertNotNull(my);
+
+        ContentHandlerFactory chf = (ContentHandlerFactory) context.getRegistry().lookupByName("chf");
+        Assertions.assertNotNull(chf);
+
+        // should be autowired
+        Assertions.assertSame(chf, my.getContentHandlerFactory());
+        Assertions.assertNull(my.getCookiePolicy());
+    }
+
+    private static final class MyComponent extends DefaultComponent {
+
+        private ContentHandlerFactory contentHandlerFactory;
+        private CookiePolicy cookiePolicy;
+
+        private MyComponent(CamelContext context) {
+            super(context);
+        }
+
+        @Override
+        protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
+            return null;
+        }
+
+        public ContentHandlerFactory getContentHandlerFactory() {
+            return contentHandlerFactory;
+        }
+
+        public void setContentHandlerFactory(ContentHandlerFactory contentHandlerFactory) {
+            this.contentHandlerFactory = contentHandlerFactory;
+        }
+
+        public CookiePolicy getCookiePolicy() {
+            return cookiePolicy;
+        }
+
+        public void setCookiePolicy(CookiePolicy cookiePolicy) {
+            this.cookiePolicy = cookiePolicy;
+        }
+    }
+
+    private static class MyComponentConfigurer extends org.apache.camel.support.component.PropertyConfigurerSupport
+            implements GeneratedPropertyConfigurer, PropertyConfigurerGetter {
+
+        @Override
+        public String[] getAutowiredNames() {
+            return new String[] { "contentHandlerFactory" };
+        }
+
+        @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);
+                return true;
+            } else {
+                return false;
+            }
+        }
+
+        @Override
+        public Class<?> getOptionType(String name, boolean ignoreCase) {
+            if ("contentHandlerFactory".equals(name)) {
+                return ContentHandlerFactory.class;
+            } else {
+                return null;
+            }
+        }
+
+        @Override
+        public Object getOptionValue(Object target, String name, boolean ignoreCase) {
+            return null;
+        }
+    }
+
+    private static class MyContentHandlerFactory implements ContentHandlerFactory {
+
+        @Override
+        public ContentHandler createContentHandler(String mimetype) {
+            return null;
+        }
+    }
+
+}