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 8f30ce4 CAMEL-14306: Endpoint DSL - ToD sets hash parameter when using complex objects as parameters 8f30ce4 is described below commit 8f30ce4cb8534cdd6b5e57a4755261d9b7f58782 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Dec 18 07:41:57 2019 +0100 CAMEL-14306: Endpoint DSL - ToD sets hash parameter when using complex objects as parameters --- .../camel/builder/EndpointProducerBuilder.java | 6 ++ .../org/apache/camel/reifier/ToDynamicReifier.java | 2 +- .../builder/endpoint/AbstractEndpointBuilder.java | 19 +++++- .../camel/builder/endpoint/LogAdvancedTest.java | 65 ++++++++++++++++++ .../camel/builder/endpoint/LogToDAdvancedTest.java | 66 +++++++++++++++++++ .../camel/builder/endpoint/TimerAdvancedTest.java | 76 ++++++++++++++++++++++ 6 files changed, 230 insertions(+), 4 deletions(-) diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/builder/EndpointProducerBuilder.java b/core/camel-core-engine/src/main/java/org/apache/camel/builder/EndpointProducerBuilder.java index 05eb3be..e835574 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/builder/EndpointProducerBuilder.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/builder/EndpointProducerBuilder.java @@ -55,4 +55,10 @@ public interface EndpointProducerBuilder { */ Expression expr(); + /** + * Builds a dynamic expression of this endpoint url. This API is only intended for + * Camel internally. + */ + Expression expr(CamelContext camelContext); + } diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ToDynamicReifier.java b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ToDynamicReifier.java index 687d531..c8aff9e 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ToDynamicReifier.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/ToDynamicReifier.java @@ -45,7 +45,7 @@ public class ToDynamicReifier<T extends ToDynamicDefinition> extends ProcessorRe Expression exp; if (definition.getEndpointProducerBuilder() != null) { uri = definition.getEndpointProducerBuilder().getUri(); - exp = definition.getEndpointProducerBuilder().expr(); + exp = definition.getEndpointProducerBuilder().expr(routeContext.getCamelContext()); } else { uri = StringHelper.notEmpty(definition.getUri(), "uri", this); exp = createExpression(routeContext, 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 040f486..ea27785 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 @@ -46,7 +46,8 @@ public class AbstractEndpointBuilder { public Endpoint resolve(CamelContext context) throws NoSuchEndpointException { Map<String, Object> remaining = new HashMap<>(); - String uri = computeUri(remaining); + // 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); if (endpoint == null) { throw new NoSuchEndpointException(uri); @@ -55,16 +56,20 @@ public class AbstractEndpointBuilder { } public String getUri() { - return computeUri(new HashMap<>()); + return computeUri(new HashMap<>(), null, false); } - protected String computeUri(Map<String, Object> remaining) { + protected String computeUri(Map<String, Object> remaining, CamelContext camelContext, boolean bindToRegistry) { Map<String, Object> params = new TreeMap<>(); for (Map.Entry<String, Object> entry : properties.entrySet()) { String key = entry.getKey(); Object val = entry.getValue(); if (val instanceof String || val instanceof Number || val instanceof Boolean || val instanceof Enum<?>) { params.put(key, val.toString()); + } else if (camelContext != null && bindToRegistry) { + String hash = Integer.toHexString(val.hashCode()); + params.put(key, "#" + hash); + camelContext.getRegistry().bind(hash, val); } else { remaining.put(key, val); } @@ -96,4 +101,12 @@ public class AbstractEndpointBuilder { public Expression expr() { return SimpleBuilder.simple(getUri()); } + + 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); + } + + } diff --git a/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/LogAdvancedTest.java b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/LogAdvancedTest.java new file mode 100644 index 0000000..5cc1e5c --- /dev/null +++ b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/LogAdvancedTest.java @@ -0,0 +1,65 @@ +/* + * 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 java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spi.ExchangeFormatter; +import org.apache.camel.support.processor.DefaultExchangeFormatter; +import org.junit.Test; + +public class LogAdvancedTest extends ContextTestSupport { + + private final AtomicBoolean handled = new AtomicBoolean(); + + private ExchangeFormatter myFormatter = new DefaultExchangeFormatter() { + @Override + public String format(Exchange exchange) { + handled.set(true); + return "Hello"; + } + }; + + @Test + public void testLog() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(10); + + context.getRouteController().startAllRoutes(); + + assertMockEndpointsSatisfied(); + + assertTrue(handled.get()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new EndpointRouteBuilder() { + @Override + public void configure() throws Exception { + from(timer("foo").delay(-1).period(0).repeatCount(10)) + .noAutoStartup() + .to("mock:result") + .to(log("foo").advanced().exchangeFormatter(myFormatter)); + } + }; + } +} diff --git a/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/LogToDAdvancedTest.java b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/LogToDAdvancedTest.java new file mode 100644 index 0000000..862ea62 --- /dev/null +++ b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/LogToDAdvancedTest.java @@ -0,0 +1,66 @@ +/* + * 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 java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spi.ExceptionHandler; +import org.apache.camel.spi.ExchangeFormatter; +import org.apache.camel.support.processor.DefaultExchangeFormatter; +import org.junit.Test; + +public class LogToDAdvancedTest extends ContextTestSupport { + + private final AtomicBoolean handled = new AtomicBoolean(); + + private ExchangeFormatter myFormatter = new DefaultExchangeFormatter() { + @Override + public String format(Exchange exchange) { + handled.set(true); + return "Hello"; + } + }; + + @Test + public void testLog() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(10); + + context.getRouteController().startAllRoutes(); + + assertMockEndpointsSatisfied(); + + assertTrue(handled.get()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new EndpointRouteBuilder() { + @Override + public void configure() throws Exception { + from(timer("foo").delay(-1).period(0).repeatCount(10)) + .noAutoStartup() + .to("mock:result") + .toD(log("foo").advanced().exchangeFormatter(myFormatter)); + } + }; + } +} 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 new file mode 100644 index 0000000..a6dadcc --- /dev/null +++ b/core/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/TimerAdvancedTest.java @@ -0,0 +1,76 @@ +/* + * 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 java.util.concurrent.atomic.AtomicBoolean; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spi.ExceptionHandler; +import org.apache.camel.support.LoggingExceptionHandler; +import org.junit.Test; + +public class TimerAdvancedTest extends ContextTestSupport { + + private final AtomicBoolean handled = new AtomicBoolean(); + + private ExceptionHandler myErrorHandler = new ExceptionHandler() { + @Override + public void handleException(Throwable exception) { + handled.set(true); + } + + @Override + public void handleException(String message, Throwable exception) { + handled.set(true); + } + + @Override + public void handleException(String message, Exchange exchange, Throwable exception) { + handled.set(true); + } + }; + + @Test + public void testTimer() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:result"); + mock.expectedMessageCount(10); + + context.getRouteController().startAllRoutes(); + + assertMockEndpointsSatisfied(); + + assertTrue(handled.get()); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new EndpointRouteBuilder() { + @Override + public void configure() throws Exception { + errorHandler(noErrorHandler()); + + from(timer("foo").delay(-1).period(0).repeatCount(10).advanced().exceptionHandler(myErrorHandler)) + .noAutoStartup() + .to("mock:result") + .throwException(new IllegalArgumentException("Forced")); + } + }; + } +}