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/03/06 14:02:24 UTC

[camel] 02/02: CAMEL-14668: camel-core - Optimize endpoint-dsl for getEndpoint

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 74812d763572a0d1964940c80a78172e3205a6a2
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Mar 6 14:46:23 2020 +0100

    CAMEL-14668: camel-core - Optimize endpoint-dsl for getEndpoint
---
 .../org/apache/camel/ExtendedCamelContext.java     | 13 ++++++++++
 .../camel/impl/engine/AbstractCamelContext.java    | 23 ++++++++++++++----
 .../builder/endpoint/AbstractEndpointBuilder.java  | 28 ++++++++++++++--------
 .../camel/builder/endpoint/TimerAdvancedTest.java  |  2 +-
 4 files changed, 50 insertions(+), 16 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java b/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java
index 2fde5db..d1c96b7 100644
--- a/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java
+++ b/core/camel-api/src/main/java/org/apache/camel/ExtendedCamelContext.java
@@ -168,6 +168,19 @@ public interface ExtendedCamelContext extends CamelContext {
     Endpoint getEndpoint(NormalizedEndpointUri uri);
 
     /**
+     * Resolves the given name to an {@link Endpoint} of the specified type.
+     * If the name has a singleton endpoint registered, then the singleton is returned.
+     * Otherwise, a new {@link Endpoint} is created and registered in the {@link org.apache.camel.spi.EndpointRegistry}.
+     *
+     * @param uri the URI of the endpoint
+     * @param parameters the parameters to customize the endpoint
+     * @return the endpoint
+     *
+     * @see #getPrototypeEndpoint(String)
+     */
+    Endpoint getEndpoint(NormalizedEndpointUri uri, Map<String, Object> parameters);
+
+    /**
      * Normalizes the given uri.
      *
      * @param uri  the uri
diff --git a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index 459b6a9..45f4da9 100644
--- a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -890,6 +890,15 @@ public abstract class AbstractCamelContext extends ServiceSupport
 
     @Override
     public Endpoint getEndpoint(String uri, Map<String, Object> parameters) {
+        return doGetEndpoint(uri, parameters, false);
+    }
+
+    @Override
+    public Endpoint getEndpoint(NormalizedEndpointUri uri, Map<String, Object> parameters) {
+        return doGetEndpoint(uri.getUri(), parameters, true);
+    }
+
+    protected Endpoint doGetEndpoint(String uri, Map<String, Object> parameters, boolean normalized) {
         // ensure CamelContext are initialized before we can get an endpoint
         init();
 
@@ -899,17 +908,21 @@ public abstract class AbstractCamelContext extends ServiceSupport
 
         // in case path has property placeholders then try to let property
         // component resolve those
-        try {
-            uri = resolvePropertyPlaceholders(uri);
-        } catch (Exception e) {
-            throw new ResolveEndpointFailedException(uri, e);
+        if (!normalized) {
+            try {
+                uri = resolvePropertyPlaceholders(uri);
+            } catch (Exception e) {
+                throw new ResolveEndpointFailedException(uri, e);
+            }
         }
 
         final String rawUri = uri;
 
         // normalize uri so we can do endpoint hits with minor mistakes and
         // parameters is not in the same order
-        uri = normalizeEndpointUri(uri);
+        if (!normalized) {
+            uri = normalizeEndpointUri(uri);
+        }
 
         LOG.trace("Getting endpoint with raw uri: {}, normalized uri: {}", rawUri, uri);
 
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 ea27785..f935a0a 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
@@ -27,9 +27,12 @@ import javax.xml.bind.annotation.XmlTransient;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.Expression;
+import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.NoSuchEndpointException;
 import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.builder.SimpleBuilder;
+import org.apache.camel.spi.NormalizedEndpointUri;
+import org.apache.camel.support.NormalizedUri;
 import org.apache.camel.util.URISupport;
 
 @XmlTransient
@@ -47,19 +50,23 @@ public class AbstractEndpointBuilder {
     public Endpoint resolve(CamelContext context) throws NoSuchEndpointException {
         Map<String, Object> remaining = new HashMap<>();
         // we should not bind complex objects to registry as we create the endpoint via the properties as-is
-        String uri = computeUri(remaining, context, false);
-        Endpoint endpoint = context.getEndpoint(uri, properties);
+        NormalizedEndpointUri uri = computeUri(remaining, context, false);
+        ExtendedCamelContext ecc = (ExtendedCamelContext) context;
+        Endpoint endpoint = ecc.getEndpoint(uri, properties);
         if (endpoint == null) {
-            throw new NoSuchEndpointException(uri);
+            throw new NoSuchEndpointException(uri.getUri());
         }
         return endpoint;
     }
 
     public String getUri() {
-        return computeUri(new HashMap<>(), null, false);
+        return computeUri(new HashMap<>(), null, false).getUri();
     }
 
-    protected String computeUri(Map<String, Object> remaining, CamelContext camelContext, boolean bindToRegistry) {
+    protected NormalizedUri computeUri(Map<String, Object> remaining, CamelContext camelContext, boolean bindToRegistry) {
+        NormalizedUri answer;
+
+        // sort parameters so it can be regarded as normalized
         Map<String, Object> params = new TreeMap<>();
         for (Map.Entry<String, Object> entry : properties.entrySet()) {
             String key = entry.getKey();
@@ -78,15 +85,17 @@ public class AbstractEndpointBuilder {
             params.put("hash", Integer.toHexString(remaining.hashCode()));
         }
         if (params.isEmpty()) {
-            return scheme + ":" + path;
+            answer = new NormalizedUri(scheme + ":" + path);
         } else {
             try {
                 String query = URISupport.createQueryString(params);
-                return scheme + ":" + path + "?" + query;
+                answer = new NormalizedUri(scheme + ":" + path + "?" + query);
             } catch (URISyntaxException e) {
                 throw RuntimeCamelException.wrapRuntimeCamelException(e);
             }
         }
+
+        return answer;
     }
 
     @Override
@@ -104,9 +113,8 @@ public class AbstractEndpointBuilder {
 
     public Expression expr(CamelContext camelContext) {
         // need to bind complex properties so we can return an uri that includes these parameters too
-        String uri = computeUri(new HashMap<>(), camelContext, true);
-        return SimpleBuilder.simple(uri);
+        NormalizedEndpointUri uri = computeUri(new HashMap<>(), camelContext, true);
+        return SimpleBuilder.simple(uri.getUri());
     }
 
-
 }
diff --git a/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/TimerAdvancedTest.java b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/TimerAdvancedTest.java
index 5b0250a..b257905 100644
--- a/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/TimerAdvancedTest.java
+++ b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/TimerAdvancedTest.java
@@ -65,7 +65,7 @@ public class TimerAdvancedTest extends ContextTestSupport {
             public void configure() throws Exception {
                 errorHandler(noErrorHandler());
 
-                from(timer("foo").delay(-1).period(0).repeatCount(10).advanced().exceptionHandler(myErrorHandler))
+                from(timer("foo").period(0).delay(-1).repeatCount(10).advanced().exceptionHandler(myErrorHandler))
                     .noAutoStartup()
                         .to("mock:result")
                         .throwException(new IllegalArgumentException("Forced"));