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 d6a94f5  CAMEL-15059: endpointdsl - Allow to inject endpoint builders 
into pojos and use them easily in your routes and fluent templates.
d6a94f5 is described below

commit d6a94f56a27dbd24318780c835be28c67ebe7ac2
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri May 15 07:36:56 2020 +0200

    CAMEL-15059: endpointdsl - Allow to inject endpoint builders into pojos and 
use them easily in your routes and fluent templates.
---
 .../org/apache/camel/EndpointConsumerResolver.java | 13 ++++-
 .../org/apache/camel/EndpointProducerResolver.java | 12 ++++-
 .../builder/endpoint/AbstractEndpointBuilder.java  | 12 +++++
 .../camel/builder/endpoint/EndpointInjectTest.java | 59 ++++++++++++++++++++++
 4 files changed, 94 insertions(+), 2 deletions(-)

diff --git 
a/core/camel-api/src/main/java/org/apache/camel/EndpointConsumerResolver.java 
b/core/camel-api/src/main/java/org/apache/camel/EndpointConsumerResolver.java
index 9ce2c22..eba24cf 100644
--- 
a/core/camel-api/src/main/java/org/apache/camel/EndpointConsumerResolver.java
+++ 
b/core/camel-api/src/main/java/org/apache/camel/EndpointConsumerResolver.java
@@ -19,8 +19,8 @@ package org.apache.camel;
 /**
  * An interface to represent an object that can be resolved as a consumer 
{@link Endpoint}
  */
-@FunctionalInterface
 public interface EndpointConsumerResolver {
+
     /**
      * Resolves this object as an endpoint.
      *
@@ -29,4 +29,15 @@ public interface EndpointConsumerResolver {
      * @throws NoSuchEndpointException is thrown if the endpoint
      */
     Endpoint resolve(CamelContext context) throws NoSuchEndpointException;
+
+    /**
+     * Resolves this object as an endpoint.
+     *
+     * @param context the camel context
+     * @param endpointType the expected type
+     * @return a built {@link Endpoint}
+     * @throws NoSuchEndpointException is thrown if the endpoint
+     */
+    <T extends Endpoint> T resolve(CamelContext context, Class<T> 
endpointType) throws NoSuchEndpointException;
+
 }
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/EndpointProducerResolver.java 
b/core/camel-api/src/main/java/org/apache/camel/EndpointProducerResolver.java
index 7e55401..f623933 100644
--- 
a/core/camel-api/src/main/java/org/apache/camel/EndpointProducerResolver.java
+++ 
b/core/camel-api/src/main/java/org/apache/camel/EndpointProducerResolver.java
@@ -19,8 +19,8 @@ package org.apache.camel;
 /**
  * An interface to represent an object that can be resolved as a producer 
{@link Endpoint}
  */
-@FunctionalInterface
 public interface EndpointProducerResolver {
+
     /**
      * Resolves this object as an endpoint.
      *
@@ -29,4 +29,14 @@ public interface EndpointProducerResolver {
      * @throws NoSuchEndpointException is thrown if the endpoint
      */
     Endpoint resolve(CamelContext context) throws NoSuchEndpointException;
+
+    /**
+     * Resolves this object as an endpoint.
+     *
+     * @param context the camel context
+     * @param endpointType the expected type
+     * @return a built {@link Endpoint}
+     * @throws NoSuchEndpointException is thrown if the endpoint
+     */
+    <T extends Endpoint> T resolve(CamelContext context, Class<T> 
endpointType) throws NoSuchEndpointException;
 }
diff --git 
a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/AbstractEndpointBuilder.java
 
b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/AbstractEndpointBuilder.java
index 483b256..6405836 100644
--- 
a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/AbstractEndpointBuilder.java
+++ 
b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/AbstractEndpointBuilder.java
@@ -37,6 +37,7 @@ public class AbstractEndpointBuilder {
     protected final String scheme;
     protected final String path;
     protected final Map<String, Object> properties = new LinkedHashMap<>();
+    private volatile Endpoint resolvedEndpoint;
 
     public AbstractEndpointBuilder(String scheme, String path) {
         this.scheme = scheme;
@@ -44,6 +45,10 @@ public class AbstractEndpointBuilder {
     }
 
     public Endpoint resolve(CamelContext context) throws 
NoSuchEndpointException {
+        if (resolvedEndpoint != null) {
+            return resolvedEndpoint;
+        }
+
         Map<String, Object> remaining = new LinkedHashMap<>();
         // we should not bind complex objects to registry as we create the 
endpoint via the properties as-is
         NormalizedEndpointUri uri = computeUri(remaining, context, false);
@@ -52,9 +57,16 @@ public class AbstractEndpointBuilder {
         if (endpoint == null) {
             throw new NoSuchEndpointException(uri.getUri());
         }
+
+        resolvedEndpoint = endpoint;
         return endpoint;
     }
 
+    public <T extends Endpoint> T resolve(CamelContext context, Class<T> 
endpointType) throws NoSuchEndpointException {
+        Endpoint answer = resolve(context);
+        return endpointType.cast(answer);
+    }
+
     public String getUri() {
         return computeUri(new LinkedHashMap<>(), null, false).getUri();
     }
diff --git 
a/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/EndpointInjectTest.java
 
b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/EndpointInjectTest.java
new file mode 100644
index 0000000..df33bd7
--- /dev/null
+++ 
b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/EndpointInjectTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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.builder.endpoint;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.FluentProducerTemplate;
+import org.apache.camel.builder.EndpointProducerBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.junit.Test;
+
+
+import static org.apache.camel.builder.endpoint.StaticEndpointBuilders.mock;
+
+/**
+ * Its not exactly @EndpointInject but we can simulate it via endpoint builders
+ */
+public class EndpointInjectTest extends ContextTestSupport {
+
+    private final EndpointProducerBuilder foo = 
mock("result").expectedCount(3);
+
+    @Test
+    public void testEndpointInject() throws Exception {
+        FluentProducerTemplate ft = context.createFluentProducerTemplate();
+        ft.to(foo).withBody("World").send();
+
+        template.sendBody("seda:foo", "Hello");
+        template.sendBody("direct:foo", "Camel");
+
+        foo.resolve(context, MockEndpoint.class).assertIsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new EndpointRouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from(direct("foo"))
+                    .to(seda("foo"));
+                from(seda("foo"))
+                    .to(foo);
+            }
+        };
+    }
+}

Reply via email to