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 2020/10/12 14:54:12 UTC

[camel] branch master updated (a5f207a -> d23efa9)

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

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


    from a5f207a  Regen
     new c913916  CAMEL-15673: endpoint-dsl - Reference bean by #type prefix
     new d23efa9  Regen

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../org/apache/camel/util/EndpointHelperTest.java  | 74 ++++++++++++++++++++++
 .../org/apache/camel/builder/endpoint/SqlTest.java | 46 ++++++++++++++
 .../org/apache/camel/support/EndpointHelper.java   | 50 +++++++++++----
 3 files changed, 157 insertions(+), 13 deletions(-)


[camel] 02/02: Regen

Posted by da...@apache.org.
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

commit d23efa9cf87f6f5dab886206a3c00cc5e0e2472c
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Oct 12 16:14:46 2020 +0200

    Regen
---
 .../src/test/java/org/apache/camel/builder/endpoint/SqlTest.java         | 1 -
 1 file changed, 1 deletion(-)

diff --git a/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/SqlTest.java b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/SqlTest.java
index d5e7da5..8587834 100644
--- a/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/SqlTest.java
+++ b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/SqlTest.java
@@ -126,5 +126,4 @@ public class SqlTest extends ContextTestSupport {
         context.stop();
     }
 
-
 }


[camel] 01/02: CAMEL-15673: endpoint-dsl - Reference bean by #type prefix

Posted by da...@apache.org.
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

commit c9139167182b7f135236e34426bd6e962ee324a8
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Oct 12 13:58:42 2020 +0200

    CAMEL-15673: endpoint-dsl - Reference bean by #type prefix
---
 .../org/apache/camel/util/EndpointHelperTest.java  | 74 ++++++++++++++++++++++
 .../org/apache/camel/builder/endpoint/SqlTest.java | 47 ++++++++++++++
 .../org/apache/camel/support/EndpointHelper.java   | 50 +++++++++++----
 3 files changed, 158 insertions(+), 13 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/util/EndpointHelperTest.java b/core/camel-core/src/test/java/org/apache/camel/util/EndpointHelperTest.java
index 27231c3..9ba7b5d 100644
--- a/core/camel-core/src/test/java/org/apache/camel/util/EndpointHelperTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/util/EndpointHelperTest.java
@@ -23,8 +23,11 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
+import org.apache.camel.NamedNode;
 import org.apache.camel.NoSuchBeanException;
 import org.apache.camel.Processor;
+import org.apache.camel.Route;
+import org.apache.camel.spi.AuthorizationPolicy;
 import org.apache.camel.support.EndpointHelper;
 import org.hamcrest.MatcherAssert;
 import org.junit.jupiter.api.Test;
@@ -180,4 +183,75 @@ public class EndpointHelperTest extends ContextTestSupport {
         MatcherAssert.assertThat(EndpointHelper.matchEndpoint(null, notMatchingEndpointUri, pattern), is(false));
     }
 
+    @Test
+    public void testResolveByType() throws Exception {
+        AuthorizationPolicy myPolicy = new AuthorizationPolicy() {
+            @Override
+            public void beforeWrap(Route route, NamedNode definition) {
+                // noop
+            }
+
+            @Override
+            public Processor wrap(Route route, Processor processor) {
+                return processor;
+            }
+        };
+
+        context.getRegistry().bind("foobar", myPolicy);
+
+        AuthorizationPolicy policy = EndpointHelper.resolveReferenceParameter(context,
+                "#type:org.apache.camel.spi.AuthorizationPolicy", AuthorizationPolicy.class);
+        assertNotNull(policy);
+        assertSame(myPolicy, policy);
+    }
+
+    @Test
+    public void testResolveByTypeNoBean() throws Exception {
+        try {
+            EndpointHelper.resolveReferenceParameter(context, "#type:org.apache.camel.spi.AuthorizationPolicy",
+                    AuthorizationPolicy.class);
+            fail("Should throw exception");
+        } catch (NoSuchBeanException e) {
+            // expected
+        }
+    }
+
+    @Test
+    public void testResolveByTypeTwo() throws Exception {
+        AuthorizationPolicy myPolicy = new AuthorizationPolicy() {
+            @Override
+            public void beforeWrap(Route route, NamedNode definition) {
+                // noop
+            }
+
+            @Override
+            public Processor wrap(Route route, Processor processor) {
+                return processor;
+            }
+        };
+        context.getRegistry().bind("foobar", myPolicy);
+
+        AuthorizationPolicy myPolicy2 = new AuthorizationPolicy() {
+            @Override
+            public void beforeWrap(Route route, NamedNode definition) {
+                // noop
+            }
+
+            @Override
+            public Processor wrap(Route route, Processor processor) {
+                return processor;
+            }
+        };
+        context.getRegistry().bind("foobar2", myPolicy2);
+
+        // when there are 2 instances of the same time, then we cannot decide
+        try {
+            EndpointHelper.resolveReferenceParameter(context, "#type:org.apache.camel.spi.AuthorizationPolicy",
+                    AuthorizationPolicy.class);
+            fail("Should throw exception");
+        } catch (NoSuchBeanException e) {
+            assertTrue(e.getMessage().contains("Found 2 beans"));
+        }
+    }
+
 }
diff --git a/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/SqlTest.java b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/SqlTest.java
index 769e958..d5e7da5 100644
--- a/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/SqlTest.java
+++ b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/SqlTest.java
@@ -80,4 +80,51 @@ public class SqlTest extends ContextTestSupport {
         context.stop();
     }
 
+    @Test
+    public void testSqlDataSourceRefBeanSyntax() throws Exception {
+        context.start();
+
+        final DataSource ds = new SimpleDriverDataSource();
+        context.getRegistry().bind("myDS", ds);
+
+        context.addRoutes(new EndpointRouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                SqlEndpointBuilderFactory.SqlEndpointBuilder builder
+                        = sql("SELECT * FROM FOO").dataSource("#bean:myDS");
+                Endpoint endpoint = builder.resolve(context);
+                assertNotNull(endpoint);
+                SqlEndpoint se = assertIsInstanceOf(SqlEndpoint.class, endpoint);
+                assertEquals("SELECT * FROM FOO", se.getQuery());
+                assertSame(ds, se.getDataSource());
+            }
+        });
+
+        context.stop();
+    }
+
+    @Test
+    public void testSqlDataSourceType() throws Exception {
+        context.start();
+
+        final DataSource ds = new SimpleDriverDataSource();
+        context.getRegistry().bind("myDS", ds);
+
+        context.addRoutes(new EndpointRouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                SqlEndpointBuilderFactory.SqlEndpointBuilder builder
+                        = sql("SELECT * FROM FOO").dataSource("#type:javax.sql.DataSource");
+                Endpoint endpoint = builder.resolve(context);
+                assertNotNull(endpoint);
+                SqlEndpoint se = assertIsInstanceOf(SqlEndpoint.class, endpoint);
+                assertEquals("SELECT * FROM FOO", se.getQuery());
+                assertSame(ds, se.getDataSource());
+            }
+        });
+
+        context.stop();
+    }
+
+
 }
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/EndpointHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/EndpointHelper.java
index e9b0aad..0a6de99 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/EndpointHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/EndpointHelper.java
@@ -22,6 +22,7 @@ import java.util.Arrays;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.atomic.AtomicLong;
 
 import org.apache.camel.CamelContext;
@@ -30,6 +31,7 @@ import org.apache.camel.Endpoint;
 import org.apache.camel.Exchange;
 import org.apache.camel.ExchangePattern;
 import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.NoSuchBeanException;
 import org.apache.camel.PollingConsumer;
 import org.apache.camel.Processor;
 import org.apache.camel.ResolveEndpointFailedException;
@@ -251,22 +253,44 @@ public final class EndpointHelper {
     /**
      * Resolves a reference parameter by making a lookup in the registry.
      *
-     * @param  <T>                      type of object to lookup.
-     * @param  context                  Camel context to use for lookup.
-     * @param  value                    reference parameter value.
-     * @param  type                     type of object to lookup.
-     * @return                          lookup result (or <code>null</code> only if <code>mandatory</code> is
-     *                                  <code>false</code>).
-     * @throws IllegalArgumentException if object was not found in registry and <code>mandatory</code> is
-     *                                  <code>true</code>.
+     * @param  <T>                 type of object to lookup.
+     * @param  context             Camel context to use for lookup.
+     * @param  value               reference parameter value.
+     * @param  type                type of object to lookup.
+     * @return                     lookup result (or <code>null</code> only if <code>mandatory</code> is
+     *                             <code>false</code>).
+     * @throws NoSuchBeanException if object was not found in registry and <code>mandatory</code> is <code>true</code>.
      */
     public static <T> T resolveReferenceParameter(CamelContext context, String value, Class<T> type, boolean mandatory) {
-        String valueNoHash = StringHelper.replaceAll(value, "#bean:", "");
-        valueNoHash = StringHelper.replaceAll(valueNoHash, "#", "");
-        if (mandatory) {
-            return CamelContextHelper.mandatoryLookupAndConvert(context, valueNoHash, type);
+        // it may refer to a type
+        if (value.startsWith("#type:")) {
+            try {
+                Object answer = null;
+
+                String valueNoHash = value.substring(6);
+                Class<?> clazz = context.getClassResolver().resolveMandatoryClass(valueNoHash);
+                Set<T> set = context.getRegistry().findByType(type);
+                if (set.size() == 1) {
+                    answer = set.iterator().next();
+                } else if (set.size() > 1) {
+                    throw new NoSuchBeanException(
+                            value, "Found " + set.size() + " beans of type: " + clazz + ". Only 1 bean instance is supported.");
+                }
+                if (mandatory && answer == null) {
+                    throw new NoSuchBeanException(value);
+                }
+                return type.cast(answer);
+            } catch (ClassNotFoundException e) {
+                throw new NoSuchBeanException(value, e);
+            }
         } else {
-            return CamelContextHelper.lookupAndConvert(context, valueNoHash, type);
+            String valueNoHash = StringHelper.replaceAll(value, "#bean:", "");
+            valueNoHash = StringHelper.replaceAll(valueNoHash, "#", "");
+            if (mandatory) {
+                return CamelContextHelper.mandatoryLookupAndConvert(context, valueNoHash, type);
+            } else {
+                return CamelContextHelper.lookupAndConvert(context, valueNoHash, type);
+            }
         }
     }