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:22 UTC

[camel] branch master updated (79be5bf -> 74812d7)

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 79be5bf  [CAMEL-14673] Split model / runtime
     new b7f447e  CAMEL-14668: camel-core - Optimize getEndpoint on camel context
     new 74812d7  CAMEL-14668: camel-core - Optimize endpoint-dsl for getEndpoint

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:
 .../camel/spring/bind/ProcessorAsEndpointTest.java |   4 +-
 .../SpringCamelContextStartingFailedEventTest.java |   4 +-
 .../aop/SpringAopClassLevelCamelBeanTest.xml       |   2 +-
 .../apache/camel/spring/bind/beanAsEndpoint.xml    |   2 +-
 .../org/apache/camel/ExtendedCamelContext.java     |  13 ++
 .../camel/impl/engine/AbstractCamelContext.java    | 180 +++++++++------------
 .../impl/RouteWithMistypedComponentNameTest.java   |   9 +-
 .../impl/engine/CamelPostProcessorHelperTest.java  |   5 +-
 .../camel/impl/engine/DefaultCamelContextTest.java |  12 +-
 .../camel/issues/SetHeaderInDoCatchIssueTest.java  |   4 +-
 .../RecipientListIgnoreInvalidEndpointsTest.java   |   4 +-
 .../interceptor/AdviceWithReplaceFromTest.java     |   4 +-
 .../RoutingSlipIgnoreInvalidEndpointsTest.java     |   4 +-
 .../builder/endpoint/AbstractEndpointBuilder.java  |  28 ++--
 .../camel/builder/endpoint/TimerAdvancedTest.java  |   2 +-
 .../org/apache/camel/support/ExchangeHelper.java   |  10 +-
 .../java/org/apache/camel/util/StringHelper.java   |  11 +-
 17 files changed, 136 insertions(+), 162 deletions(-)


[camel] 01/02: CAMEL-14668: camel-core - Optimize getEndpoint on camel context

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 b7f447e3fc524bd3cec74e2cf08447324e6797aa
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Mar 6 13:46:11 2020 +0100

    CAMEL-14668: camel-core - Optimize getEndpoint on camel context
---
 .../camel/spring/bind/ProcessorAsEndpointTest.java |   4 +-
 .../SpringCamelContextStartingFailedEventTest.java |   4 +-
 .../aop/SpringAopClassLevelCamelBeanTest.xml       |   2 +-
 .../apache/camel/spring/bind/beanAsEndpoint.xml    |   2 +-
 .../camel/impl/engine/AbstractCamelContext.java    | 157 ++++++++-------------
 .../impl/RouteWithMistypedComponentNameTest.java   |   9 +-
 .../impl/engine/CamelPostProcessorHelperTest.java  |   5 +-
 .../camel/impl/engine/DefaultCamelContextTest.java |  12 +-
 .../camel/issues/SetHeaderInDoCatchIssueTest.java  |   4 +-
 .../RecipientListIgnoreInvalidEndpointsTest.java   |   4 +-
 .../interceptor/AdviceWithReplaceFromTest.java     |   4 +-
 .../RoutingSlipIgnoreInvalidEndpointsTest.java     |   4 +-
 .../org/apache/camel/support/ExchangeHelper.java   |  10 +-
 .../java/org/apache/camel/util/StringHelper.java   |  11 +-
 14 files changed, 86 insertions(+), 146 deletions(-)

diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/bind/ProcessorAsEndpointTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/bind/ProcessorAsEndpointTest.java
index 6bdf3d6..cd337e3 100644
--- a/components/camel-spring/src/test/java/org/apache/camel/spring/bind/ProcessorAsEndpointTest.java
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/bind/ProcessorAsEndpointTest.java
@@ -33,7 +33,7 @@ public class ProcessorAsEndpointTest extends SpringTestSupport {
     public void testSendingToProcessorEndpoint() throws Exception {
         ProcessorStub processor = getMandatoryBean(ProcessorStub.class, "myProcessor");
 
-        template.sendBody("myProcessor", body);
+        template.sendBody("bean:myProcessor", body);
 
         List<Exchange> list = processor.getExchanges();
         assertEquals("Received exchange list: " + list, 1, list.size());
@@ -44,8 +44,6 @@ public class ProcessorAsEndpointTest extends SpringTestSupport {
     @Test
     public void testSendingToNonExistentEndpoint() throws Exception {
         String uri = "unknownEndpoint";
-        Endpoint endpoint = context.getEndpoint(uri);
-        assertNull("Should not have found an endpoint! Was: " + endpoint, endpoint);
         try {
             template.sendBody(uri, body);
             fail("We should have failed as this is a bad endpoint URI");
diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.java
index 30a3f72..0f75da5 100644
--- a/components/camel-spring/src/test/java/org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.java
+++ b/components/camel-spring/src/test/java/org/apache/camel/spring/management/SpringCamelContextStartingFailedEventTest.java
@@ -17,7 +17,7 @@
 package org.apache.camel.spring.management;
 
 import org.apache.camel.FailedToCreateRouteException;
-import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.NoSuchEndpointException;
 import org.apache.camel.spring.SpringTestSupport;
 import org.junit.Test;
 import org.springframework.context.support.AbstractXmlApplicationContext;
@@ -37,7 +37,7 @@ public class SpringCamelContextStartingFailedEventTest extends SpringTestSupport
             fail("Should thrown an exception");
         } catch (Exception e) {
             FailedToCreateRouteException ftcre = assertIsInstanceOf(FailedToCreateRouteException.class, e);
-            assertIsInstanceOf(ResolveEndpointFailedException.class, ftcre.getCause());
+            assertIsInstanceOf(NoSuchEndpointException.class, ftcre.getCause());
             // expected
         }
 
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.xml
index d29aaf7..5bd31b2 100644
--- a/components/camel-spring/src/test/resources/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.xml
+++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/aop/SpringAopClassLevelCamelBeanTest.xml
@@ -48,7 +48,7 @@
     <camelContext xmlns="http://camel.apache.org/schema/spring">
         <route>
             <from uri="direct:start"/>
-            <to uri="myBean"/>
+            <to uri="bean:myBean"/>
             <to uri="mock:result"/>
         </route>
     </camelContext>
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/bind/beanAsEndpoint.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/bind/beanAsEndpoint.xml
index a12c44d..fff1c05 100644
--- a/components/camel-spring/src/test/resources/org/apache/camel/spring/bind/beanAsEndpoint.xml
+++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/bind/beanAsEndpoint.xml
@@ -28,7 +28,7 @@
   <camelContext xmlns="http://camel.apache.org/schema/spring">
     <route>
       <from uri="direct:start"/>
-      <to uri="myBean"/>
+      <to uri="bean:myBean"/>
       <to uri="mock:results"/>
     </route>
   </camelContext>
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 ed9bba3..459b6a9 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
@@ -813,7 +813,7 @@ public abstract class AbstractCamelContext extends ServiceSupport
 
         LOG.trace("Getting endpoint with raw uri: {}, normalized uri: {}", rawUri, uri);
 
-        String scheme = null;
+        String scheme;
         Endpoint answer = null;
         if (!prototype) {
             // use optimized method to get the endpoint uri
@@ -823,27 +823,32 @@ public abstract class AbstractCamelContext extends ServiceSupport
         }
         if (answer == null) {
             try {
-                // Use the URI prefix to find the component.
-                String[] splitURI = StringHelper.splitOnCharacter(uri, ":", 2);
-                if (splitURI[1] != null) {
-                    scheme = splitURI[0];
-                    LOG.trace("Endpoint uri: {} is from component with name: {}", uri, scheme);
-                    Component component = getComponent(scheme);
-
-                    // Ask the component to resolve the endpoint.
-                    if (component != null) {
-                        LOG.trace("Creating endpoint from uri: {} using component: {}", uri, component);
-
-                        // Have the component create the endpoint if it can.
-                        if (component.useRawUri()) {
-                            answer = component.createEndpoint(rawUri);
-                        } else {
-                            answer = component.createEndpoint(uri);
-                        }
+                scheme = StringHelper.before(uri, ":");
+                if (scheme == null) {
+                    // it may refer to a logical endpoint
+                    answer = getRegistry().lookupByNameAndType(uri, Endpoint.class);
+                    if (answer != null) {
+                        return answer;
+                    } else {
+                        throw new NoSuchEndpointException(uri);
+                    }
+                }
+                LOG.trace("Endpoint uri: {} is from component with name: {}", uri, scheme);
+                Component component = getComponent(scheme);
 
-                        if (answer != null && LOG.isDebugEnabled()) {
-                            LOG.debug("{} converted to endpoint: {} by component: {}", URISupport.sanitizeUri(uri), answer, component);
-                        }
+                // Ask the component to resolve the endpoint.
+                if (component != null) {
+                    LOG.trace("Creating endpoint from uri: {} using component: {}", uri, component);
+
+                    // Have the component create the endpoint if it can.
+                    if (component.useRawUri()) {
+                        answer = component.createEndpoint(rawUri);
+                    } else {
+                        answer = component.createEndpoint(uri);
+                    }
+
+                    if (answer != null && LOG.isDebugEnabled()) {
+                        LOG.debug("{} converted to endpoint: {} by component: {}", URISupport.sanitizeUri(uri), answer, component);
                     }
                 }
 
@@ -853,34 +858,6 @@ public abstract class AbstractCamelContext extends ServiceSupport
                     LOG.trace("No component to create endpoint from uri: {} fallback lookup in registry -> {}", uri, answer);
                 }
 
-                if (answer == null && splitURI[1] == null) {
-                    // the uri has no context-path which is rare and it was not
-                    // referring to an endpoint in the registry
-                    // so try to see if it can be created by a component
-
-                    int pos = uri.indexOf('?');
-                    String componentName = pos > 0 ? uri.substring(0, pos) : uri;
-
-                    Component component = getComponent(componentName);
-
-                    // Ask the component to resolve the endpoint.
-                    if (component != null) {
-                        LOG.trace("Creating endpoint from uri: {} using component: {}", uri, component);
-
-                        // Have the component create the endpoint if it can.
-                        if (component.useRawUri()) {
-                            answer = component.createEndpoint(rawUri);
-                        } else {
-                            answer = component.createEndpoint(uri);
-                        }
-
-                        if (answer != null && LOG.isDebugEnabled()) {
-                            LOG.debug("{} converted to endpoint: {} by component: {}", URISupport.sanitizeUri(uri), answer, component);
-                        }
-                    }
-
-                }
-
                 if (answer != null) {
                     if (!prototype) {
                         addService(answer);
@@ -895,14 +872,17 @@ public abstract class AbstractCamelContext extends ServiceSupport
                         }
                     }
                 }
+            } catch (NoSuchEndpointException e) {
+                // throw as-is
+                throw e;
             } catch (Exception e) {
                 throw new ResolveEndpointFailedException(uri, e);
             }
         }
 
         // unknown scheme
-        if (answer == null && scheme != null) {
-            throw new ResolveEndpointFailedException(uri, "No component found with scheme: " + scheme);
+        if (answer == null) {
+            throw new NoSuchEndpointException(uri);
         }
 
         return answer;
@@ -940,27 +920,32 @@ public abstract class AbstractCamelContext extends ServiceSupport
         answer = endpoints.get(key);
         if (answer == null) {
             try {
-                // Use the URI prefix to find the component.
-                String[] splitURI = StringHelper.splitOnCharacter(uri, ":", 2);
-                if (splitURI[1] != null) {
-                    scheme = splitURI[0];
-                    LOG.trace("Endpoint uri: {} is from component with name: {}", uri, scheme);
-                    Component component = getComponent(scheme);
-
-                    // Ask the component to resolve the endpoint.
-                    if (component != null) {
-                        LOG.trace("Creating endpoint from uri: {} using component: {}", uri, component);
-
-                        // Have the component create the endpoint if it can.
-                        if (component.useRawUri()) {
-                            answer = component.createEndpoint(rawUri, parameters);
-                        } else {
-                            answer = component.createEndpoint(uri, parameters);
-                        }
+                scheme = StringHelper.before(uri, ":");
+                if (scheme == null) {
+                    // it may refer to a logical endpoint
+                    answer = getRegistry().lookupByNameAndType(uri, Endpoint.class);
+                    if (answer != null) {
+                        return answer;
+                    } else {
+                        throw new NoSuchEndpointException(uri);
+                    }
+                }
+                LOG.trace("Endpoint uri: {} is from component with name: {}", uri, scheme);
+                Component component = getComponent(scheme);
 
-                        if (answer != null && LOG.isDebugEnabled()) {
-                            LOG.debug("{} converted to endpoint: {} by component: {}", URISupport.sanitizeUri(uri), answer, component);
-                        }
+                // Ask the component to resolve the endpoint.
+                if (component != null) {
+                    LOG.trace("Creating endpoint from uri: {} using component: {}", uri, component);
+
+                    // Have the component create the endpoint if it can.
+                    if (component.useRawUri()) {
+                        answer = component.createEndpoint(rawUri, parameters);
+                    } else {
+                        answer = component.createEndpoint(uri, parameters);
+                    }
+
+                    if (answer != null && LOG.isDebugEnabled()) {
+                        LOG.debug("{} converted to endpoint: {} by component: {}", URISupport.sanitizeUri(uri), answer, component);
                     }
                 }
 
@@ -970,34 +955,6 @@ public abstract class AbstractCamelContext extends ServiceSupport
                     LOG.trace("No component to create endpoint from uri: {} fallback lookup in registry -> {}", uri, answer);
                 }
 
-                if (answer == null && splitURI[1] == null) {
-                    // the uri has no context-path which is rare and it was not
-                    // referring to an endpoint in the registry
-                    // so try to see if it can be created by a component
-
-                    int pos = uri.indexOf('?');
-                    String componentName = pos > 0 ? uri.substring(0, pos) : uri;
-
-                    Component component = getComponent(componentName);
-
-                    // Ask the component to resolve the endpoint.
-                    if (component != null) {
-                        LOG.trace("Creating endpoint from uri: {} using component: {}", uri, component);
-
-                        // Have the component create the endpoint if it can.
-                        if (component.useRawUri()) {
-                            answer = component.createEndpoint(rawUri, parameters);
-                        } else {
-                            answer = component.createEndpoint(uri, parameters);
-                        }
-
-                        if (answer != null && LOG.isDebugEnabled()) {
-                            LOG.debug("{} converted to endpoint: {} by component: {}", URISupport.sanitizeUri(uri), answer, component);
-                        }
-                    }
-
-                }
-
                 if (answer != null) {
                     addService(answer);
                     answer = addEndpointToRegistry(uri, answer);
@@ -1008,7 +965,7 @@ public abstract class AbstractCamelContext extends ServiceSupport
         }
 
         // unknown scheme
-        if (answer == null && scheme != null) {
+        if (answer == null) {
             throw new ResolveEndpointFailedException(uri, "No component found with scheme: " + scheme);
         }
 
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/RouteWithMistypedComponentNameTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/RouteWithMistypedComponentNameTest.java
index 022b106..5ee0ddb 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/RouteWithMistypedComponentNameTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/RouteWithMistypedComponentNameTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.impl;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
+import org.apache.camel.NoSuchEndpointException;
 import org.apache.camel.ResolveEndpointFailedException;
 import org.apache.camel.TestSupport;
 import org.apache.camel.builder.RouteBuilder;
@@ -41,8 +42,8 @@ public class RouteWithMistypedComponentNameTest extends TestSupport {
                     endpoint("mistyped:hello");
                 }
             });
-            fail("Should have thrown a ResolveEndpointFailedException");
-        } catch (ResolveEndpointFailedException e) {
+            fail("Should have thrown a NoSuchEndpointException");
+        } catch (NoSuchEndpointException e) {
             // expected
         }
     }
@@ -60,8 +61,8 @@ public class RouteWithMistypedComponentNameTest extends TestSupport {
                     endpoint("mistyped:hello", Endpoint.class);
                 }
             });
-            fail("Should have thrown a ResolveEndpointFailedException");
-        } catch (ResolveEndpointFailedException e) {
+            fail("Should have thrown a NoSuchEndpointException");
+        } catch (NoSuchEndpointException e) {
             // expected
         }
     }
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/engine/CamelPostProcessorHelperTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/engine/CamelPostProcessorHelperTest.java
index bda5027..4ee2a62 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/engine/CamelPostProcessorHelperTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/engine/CamelPostProcessorHelperTest.java
@@ -30,6 +30,7 @@ import org.apache.camel.Exchange;
 import org.apache.camel.ExtendedExchange;
 import org.apache.camel.FluentProducerTemplate;
 import org.apache.camel.NoSuchBeanException;
+import org.apache.camel.NoSuchEndpointException;
 import org.apache.camel.PollingConsumer;
 import org.apache.camel.Produce;
 import org.apache.camel.Producer;
@@ -343,8 +344,8 @@ public class CamelPostProcessorHelperTest extends ContextTestSupport {
         try {
             helper.getInjectionValue(type, endpointInject.value(), endpointInject.property(), propertyName, bean, "foo");
             fail("Should throw exception");
-        } catch (ResolveEndpointFailedException e) {
-            assertEquals("Failed to resolve endpoint: xxx://foo due to: No component found with scheme: xxx", e.getMessage());
+        } catch (NoSuchEndpointException e) {
+            // expected
         }
     }
 
diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
index 01a29db..076310a 100644
--- a/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/impl/engine/DefaultCamelContextTest.java
@@ -127,14 +127,6 @@ public class DefaultCamelContextTest extends TestSupport {
     }
 
     @Test
-    public void testGetEndpointNoScheme() throws Exception {
-        DefaultCamelContext ctx = new DefaultCamelContext();
-        ctx.disableJMX();
-        Endpoint endpoint = ctx.getEndpoint("log");
-        assertNotNull(endpoint);
-    }
-
-    @Test
     public void testGetEndPointByTypeUnknown() {
         DefaultCamelContext camelContext = new DefaultCamelContext();
         try {
@@ -181,8 +173,8 @@ public class DefaultCamelContextTest extends TestSupport {
         try {
             ctx.getEndpoint("xxx:foo");
             fail("Should have thrown a ResolveEndpointFailedException");
-        } catch (ResolveEndpointFailedException e) {
-            assertTrue(e.getMessage().contains("No component found with scheme: xxx"));
+        } catch (NoSuchEndpointException e) {
+            assertTrue(e.getMessage().contains("No endpoint could be found for: xxx:"));
         }
     }
 
diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/SetHeaderInDoCatchIssueTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/SetHeaderInDoCatchIssueTest.java
index 3b39521..d55d0fb 100644
--- a/core/camel-core/src/test/java/org/apache/camel/issues/SetHeaderInDoCatchIssueTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/issues/SetHeaderInDoCatchIssueTest.java
@@ -98,9 +98,9 @@ public class SetHeaderInDoCatchIssueTest extends ContextTestSupport {
             public void configure() throws Exception {
                 context.setTracing(true);
 
-                from("direct:start").doTry().to("A").setHeader("CamelJmsDestinationName", constant("queue:outQueue")).inOut("B").setHeader("Status", constant("CamsResponse"))
+                from("direct:start").doTry().to("bean:A").setHeader("CamelJmsDestinationName", constant("queue:outQueue")).inOut("bean:B").setHeader("Status", constant("CamsResponse"))
                     .doCatch(ExchangeTimedOutException.class).setHeader("Status", constant("TimeOut")).doCatch(Exception.class).setHeader("Status", constant("ExceptionGeneral"))
-                    .end().to("C").transform(body());
+                    .end().to("bean:C").transform(body());
             }
         };
     }
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/RecipientListIgnoreInvalidEndpointsTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/RecipientListIgnoreInvalidEndpointsTest.java
index 73cdbe2..2efaedc 100644
--- a/core/camel-core/src/test/java/org/apache/camel/processor/RecipientListIgnoreInvalidEndpointsTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/RecipientListIgnoreInvalidEndpointsTest.java
@@ -17,7 +17,7 @@
 package org.apache.camel.processor;
 
 import org.apache.camel.ContextTestSupport;
-import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.NoSuchEndpointException;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.junit.Test;
@@ -49,7 +49,7 @@ public class RecipientListIgnoreInvalidEndpointsTest extends ContextTestSupport
             template.requestBody("direct:startB", "Hello World", String.class);
             fail("Expect the exception here.");
         } catch (Exception ex) {
-            assertTrue("Get a wrong cause of the exception", ex.getCause() instanceof ResolveEndpointFailedException);
+            assertTrue("Get a wrong cause of the exception", ex.getCause() instanceof NoSuchEndpointException);
         }
 
         assertMockEndpointsSatisfied();
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithReplaceFromTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithReplaceFromTest.java
index 9e44e0c..f707800 100644
--- a/core/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithReplaceFromTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithReplaceFromTest.java
@@ -19,7 +19,7 @@ package org.apache.camel.processor.interceptor;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Endpoint;
 import org.apache.camel.FailedToCreateRouteException;
-import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.NoSuchEndpointException;
 import org.apache.camel.builder.AdviceWithRouteBuilder;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.reifier.RouteReifier;
@@ -79,7 +79,7 @@ public class AdviceWithReplaceFromTest extends ContextTestSupport {
             });
             fail("Should have thrown exception");
         } catch (FailedToCreateRouteException e) {
-            assertIsInstanceOf(ResolveEndpointFailedException.class, e.getCause());
+            assertIsInstanceOf(NoSuchEndpointException.class, e.getCause());
         }
     }
 
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/routingslip/RoutingSlipIgnoreInvalidEndpointsTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/routingslip/RoutingSlipIgnoreInvalidEndpointsTest.java
index f41f7ba..00866b3 100644
--- a/core/camel-core/src/test/java/org/apache/camel/processor/routingslip/RoutingSlipIgnoreInvalidEndpointsTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/routingslip/RoutingSlipIgnoreInvalidEndpointsTest.java
@@ -17,7 +17,7 @@
 package org.apache.camel.processor.routingslip;
 
 import org.apache.camel.ContextTestSupport;
-import org.apache.camel.ResolveEndpointFailedException;
+import org.apache.camel.NoSuchEndpointException;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.junit.Test;
@@ -46,7 +46,7 @@ public class RoutingSlipIgnoreInvalidEndpointsTest extends ContextTestSupport {
             template.sendBodyAndHeader("direct:b", "Hello", "myHeader", "direct:start,fail:endpoint,mock:result");
             fail("Expect the exception here.");
         } catch (Exception ex) {
-            assertTrue("Get a wrong cause of the exception", ex.getCause() instanceof ResolveEndpointFailedException);
+            assertTrue("Get a wrong cause of the exception", ex.getCause() instanceof NoSuchEndpointException);
         }
         assertMockEndpointsSatisfied();
     }
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
index 80a6f08..40a7ea4 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java
@@ -892,15 +892,7 @@ public final class ExchangeHelper {
      * @return     the component scheme (name), or <tt>null</tt> if not possible to resolve
      */
     public static String resolveScheme(String uri) {
-        String scheme = null;
-        if (uri != null) {
-            // Use the URI prefix to find the component.
-            String[] splitURI = StringHelper.splitOnCharacter(uri, ":", 2);
-            if (splitURI[1] != null) {
-                scheme = splitURI[0];
-            }
-        }
-        return scheme;
+        return StringHelper.before(uri, ":");
     }
 
     @SuppressWarnings("unchecked")
diff --git a/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java b/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
index 7bdc36b..fd6fe35 100644
--- a/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
+++ b/core/camel-util/src/main/java/org/apache/camel/util/StringHelper.java
@@ -466,10 +466,11 @@ public final class StringHelper {
      * @return the text after the token, or <tt>null</tt> if text does not contain the token
      */
     public static String after(String text, String after) {
-        if (!text.contains(after)) {
+        int pos = text.indexOf(after);
+        if (pos == -1) {
             return null;
         }
-        return text.substring(text.indexOf(after) + after.length());
+        return text.substring(pos + after.length());
     }
 
     /**
@@ -498,10 +499,8 @@ public final class StringHelper {
      *         contain the token
      */
     public static String before(String text, String before) {
-        if (!text.contains(before)) {
-            return null;
-        }
-        return text.substring(0, text.indexOf(before));
+        int pos = text.indexOf(before);
+        return pos == -1 ? null : text.substring(0, pos);
     }
 
     /**


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

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 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"));