This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch uri-assembler in repository https://gitbox.apache.org/repos/asf/camel.git
commit c0585571a31d70209f5408849bb7c015632a2c15 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Sep 24 16:00:08 2020 +0200 CAMEL-15567: components - Generate source code for creating endpoint uri via a map of properties. WIP --- .../org/apache/camel/ExtendedCamelContext.java | 6 ++++ .../apache/camel/catalog/RuntimeCamelCatalog.java | 5 +++ .../org/apache/camel/spi/EndpointUriAssembler.java | 41 +++++++++++++++++++++ .../camel/impl/engine/AbstractCamelContext.java | 13 +++++++ .../runtime-camelcatalog-endpointuriassembler | 2 ++ .../impl/CamelCatalogEndpointUriAssembler.java | 30 ++++++++++++++++ .../camel/impl/lw/LightweightCamelContext.java | 6 ++++ .../impl/lw/LightweightRuntimeCamelContext.java | 7 ++++ ...untimeCamelCatalogEndpointUriAssemblerTest.java | 42 ++++++++++++++++++++++ 9 files changed, 152 insertions(+) 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 e469142..4122a85 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 @@ -36,6 +36,7 @@ import org.apache.camel.spi.ConfigurerResolver; import org.apache.camel.spi.DataFormatResolver; import org.apache.camel.spi.DeferServiceFactory; import org.apache.camel.spi.EndpointStrategy; +import org.apache.camel.spi.EndpointUriAssembler; import org.apache.camel.spi.FactoryFinder; import org.apache.camel.spi.FactoryFinderResolver; import org.apache.camel.spi.HeadersMapFactory; @@ -556,6 +557,11 @@ public interface ExtendedCamelContext extends CamelContext { RouteController getInternalRouteController(); /** + * Gets the {@link EndpointUriAssembler} for the given component name. + */ + EndpointUriAssembler getEndpointUriAssembler(String scheme); + + /** * Internal API for adding routes. Do not use this as end user. */ void addRoute(Route route); diff --git a/core/camel-api/src/main/java/org/apache/camel/catalog/RuntimeCamelCatalog.java b/core/camel-api/src/main/java/org/apache/camel/catalog/RuntimeCamelCatalog.java index ca0e6aa..d7d5555 100644 --- a/core/camel-api/src/main/java/org/apache/camel/catalog/RuntimeCamelCatalog.java +++ b/core/camel-api/src/main/java/org/apache/camel/catalog/RuntimeCamelCatalog.java @@ -36,6 +36,11 @@ public interface RuntimeCamelCatalog extends StaticService, CamelContextAware { String FACTORY = "runtime-camelcatalog"; /** + * Factory key for {@link org.apache.camel.spi.EndpointUriAssembler} + */ + String ENDPOINT_URI_ASSEMBLER_FACTORY = "runtime-camelcatalog-endpointuriassembler"; + + /** * Returns the component information as JSON format. * <p/> * This API is needed by {@link ComponentVerifierExtension}. diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/EndpointUriAssembler.java b/core/camel-api/src/main/java/org/apache/camel/spi/EndpointUriAssembler.java new file mode 100644 index 0000000..27ba83f --- /dev/null +++ b/core/camel-api/src/main/java/org/apache/camel/spi/EndpointUriAssembler.java @@ -0,0 +1,41 @@ +/* + * 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.spi; + +import java.net.URISyntaxException; +import java.util.Map; + +import org.apache.camel.CamelContext; + +/** + * To assemble an endpoint uri String from a map of parameters. + */ +public interface EndpointUriAssembler { + + // TODO: Consider a better name. + + /** + * Assembles an endpoint uri for the given component name with the given parameters. + * + * @param camelContext the Camel context + * @param scheme the component name + * @param parameters endpoint options + * @return the constructed endpoint uri + */ + String buildUri(CamelContext camelContext, String scheme, Map<String, String> parameters) throws URISyntaxException; + +} 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 58fc598..cdab0f2 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 @@ -95,6 +95,7 @@ import org.apache.camel.spi.Debugger; import org.apache.camel.spi.DeferServiceFactory; import org.apache.camel.spi.EndpointRegistry; import org.apache.camel.spi.EndpointStrategy; +import org.apache.camel.spi.EndpointUriAssembler; import org.apache.camel.spi.EventNotifier; import org.apache.camel.spi.ExecutorServiceManager; import org.apache.camel.spi.FactoryFinder; @@ -4234,6 +4235,18 @@ public abstract class AbstractCamelContext extends BaseService return internalRouteController; } + @Override + public EndpointUriAssembler getEndpointUriAssembler(String scheme) { + // TODO: lookup factory specific for a given component + + // no specific factory found so lets fallback to use runtime catalog + return new BaseServiceResolver<>(RuntimeCamelCatalog.ENDPOINT_URI_ASSEMBLER_FACTORY, EndpointUriAssembler.class) + .resolve(getCamelContextReference()) + .orElseThrow(() -> new IllegalArgumentException( + "Cannot find RuntimeCamelCatalog on classpath. " + + "Add camel-core-catalog to classpath.")); + } + public enum Initialization { Eager, Default, diff --git a/core/camel-core-catalog/src/generated/resources/META-INF/services/org/apache/camel/runtime-camelcatalog-endpointuriassembler b/core/camel-core-catalog/src/generated/resources/META-INF/services/org/apache/camel/runtime-camelcatalog-endpointuriassembler new file mode 100644 index 0000000..ec29104 --- /dev/null +++ b/core/camel-core-catalog/src/generated/resources/META-INF/services/org/apache/camel/runtime-camelcatalog-endpointuriassembler @@ -0,0 +1,2 @@ +# Generated by camel build tools - do NOT edit this file! +class=org.apache.camel.catalog.impl.CamelCatalogEndpointUriAssembler diff --git a/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/CamelCatalogEndpointUriAssembler.java b/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/CamelCatalogEndpointUriAssembler.java new file mode 100644 index 0000000..153671e --- /dev/null +++ b/core/camel-core-catalog/src/main/java/org/apache/camel/catalog/impl/CamelCatalogEndpointUriAssembler.java @@ -0,0 +1,30 @@ +package org.apache.camel.catalog.impl; + +import java.net.URISyntaxException; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.ExtendedCamelContext; +import org.apache.camel.RuntimeCamelException; +import org.apache.camel.catalog.RuntimeCamelCatalog; +import org.apache.camel.spi.EndpointUriAssembler; +import org.apache.camel.spi.annotations.JdkService; + +import static org.apache.camel.catalog.RuntimeCamelCatalog.ENDPOINT_URI_ASSEMBLER_FACTORY; + +/** + * Uses {@link RuntimeCamelCatalog} to assemble the endpoint uri. + */ +@JdkService(ENDPOINT_URI_ASSEMBLER_FACTORY) +public class CamelCatalogEndpointUriAssembler implements EndpointUriAssembler { + + @Override + public String buildUri(CamelContext camelContext, String scheme, Map<String, String> parameters) { + try { + return camelContext.adapt(ExtendedCamelContext.class).getRuntimeCamelCatalog().asEndpointUri(scheme, parameters, + false); + } catch (URISyntaxException e) { + throw RuntimeCamelException.wrapRuntimeException(e); + } + } +} diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightCamelContext.java b/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightCamelContext.java index 90ea6a0..235fdfa 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightCamelContext.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightCamelContext.java @@ -89,6 +89,7 @@ import org.apache.camel.spi.Debugger; import org.apache.camel.spi.DeferServiceFactory; import org.apache.camel.spi.EndpointRegistry; import org.apache.camel.spi.EndpointStrategy; +import org.apache.camel.spi.EndpointUriAssembler; import org.apache.camel.spi.ExecutorServiceManager; import org.apache.camel.spi.FactoryFinder; import org.apache.camel.spi.FactoryFinderResolver; @@ -1416,6 +1417,11 @@ public class LightweightCamelContext implements ExtendedCamelContext, CatalogCam } @Override + public EndpointUriAssembler getEndpointUriAssembler(String scheme) { + return getExtendedCamelContext().getEndpointUriAssembler(scheme); + } + + @Override public void addRoute(Route route) { getExtendedCamelContext().addRoute(route); } diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightRuntimeCamelContext.java b/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightRuntimeCamelContext.java index 1e76e83..4c993ae 100644 --- a/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightRuntimeCamelContext.java +++ b/core/camel-core-engine/src/main/java/org/apache/camel/impl/lw/LightweightRuntimeCamelContext.java @@ -84,6 +84,7 @@ import org.apache.camel.spi.Debugger; import org.apache.camel.spi.DeferServiceFactory; import org.apache.camel.spi.EndpointRegistry; import org.apache.camel.spi.EndpointStrategy; +import org.apache.camel.spi.EndpointUriAssembler; import org.apache.camel.spi.ExecutorServiceManager; import org.apache.camel.spi.FactoryFinder; import org.apache.camel.spi.FactoryFinderResolver; @@ -1856,6 +1857,12 @@ public class LightweightRuntimeCamelContext implements ExtendedCamelContext, Cat }; } + @Override + public EndpointUriAssembler getEndpointUriAssembler(String scheme) { + // TODO: If this experiment is continued then this should be implemented + throw new UnsupportedOperationException(); + } + private void startService(Service service) throws Exception { // and register startup aware so they can be notified when // camel context has been started diff --git a/core/camel-core/src/test/java/org/apache/camel/catalog/RuntimeCamelCatalogEndpointUriAssemblerTest.java b/core/camel-core/src/test/java/org/apache/camel/catalog/RuntimeCamelCatalogEndpointUriAssemblerTest.java new file mode 100644 index 0000000..3bdf989 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/catalog/RuntimeCamelCatalogEndpointUriAssemblerTest.java @@ -0,0 +1,42 @@ +/* + * 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.catalog; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.ExtendedCamelContext; +import org.apache.camel.spi.EndpointUriAssembler; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class RuntimeCamelCatalogEndpointUriAssemblerTest extends ContextTestSupport { + + @Test + public void testAssemble() throws Exception { + EndpointUriAssembler assembler = context.adapt(ExtendedCamelContext.class).getEndpointUriAssembler("timer"); + + Map<String, String> params = new HashMap<>(); + params.put("timerName", "foo"); + params.put("period", "123"); + params.put("repeatCount", "5"); + + String uri = assembler.buildUri(context, "timer", params); + Assertions.assertEquals("timer:foo?period=123&repeatCount=5", uri); + } +}