This is an automated email from the ASF dual-hosted git repository. valdar pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git
The following commit(s) were added to refs/heads/master by this push: new 96e4997 rest: support configuring rest's configuration component/endpoint properties 96e4997 is described below commit 96e49972c5d45803c57c9f45243dc14dc8f06095 Author: lburgazzoli <lburgazz...@gmail.com> AuthorDate: Mon May 20 13:15:34 2019 +0200 rest: support configuring rest's configuration component/endpoint properties --- .../main/java/org/apache/camel/k/Constants.java | 2 + .../apache/camel/k/listener/ContextConfigurer.java | 3 +- .../k/listener/ContextLifecycleConfigurer.java | 4 +- .../apache/camel/k/support/PropertiesSupport.java | 153 +++++++++++++++++++++ .../org/apache/camel/k/support/RuntimeSupport.java | 136 +++--------------- .../apache/camel/k/support/RuntimeSupportTest.java | 28 ++++ .../java/org/apache/camel/k/jvm/Application.java | 4 +- .../org/apache/camel/k/jvm/ApplicationRuntime.java | 4 +- .../org/apache/camel/k/jvm/ApplicationSupport.java | 4 +- .../org/apache/camel/k/jvm/PropertiesTest.java | 4 +- 10 files changed, 213 insertions(+), 129 deletions(-) diff --git a/camel-k-runtime-core/src/main/java/org/apache/camel/k/Constants.java b/camel-k-runtime-core/src/main/java/org/apache/camel/k/Constants.java index 083e1c4..7091e4b 100644 --- a/camel-k-runtime-core/src/main/java/org/apache/camel/k/Constants.java +++ b/camel-k-runtime-core/src/main/java/org/apache/camel/k/Constants.java @@ -29,6 +29,8 @@ public final class Constants { public static final String CONTEXT_CUSTOMIZER_RESOURCE_PATH = "META-INF/services/org/apache/camel/k/customizer/"; public static final String PROPERTY_CAMEL_K_CUSTOMIZER = "camel.k.customizer"; public static final String ENABLE_CUSTOMIZER_PATTERN = "customizer.([\\w][\\w-]*).enabled"; + public static final String PROPERTY_PREFIX_REST_COMPONENT_PROPERTY = "camel.rest.componentProperty."; + public static final String PROPERTY_PREFIX_REST_ENDPOINT_PROPERTY = "camel.rest.endpointProperty."; private Constants() { } diff --git a/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/ContextConfigurer.java b/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/ContextConfigurer.java index ef02736..af806c7 100644 --- a/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/ContextConfigurer.java +++ b/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/ContextConfigurer.java @@ -17,6 +17,7 @@ package org.apache.camel.k.listener; import org.apache.camel.k.Runtime; +import org.apache.camel.k.support.PropertiesSupport; import org.apache.camel.k.support.RuntimeSupport; public class ContextConfigurer extends AbstractPhaseListener { @@ -31,7 +32,7 @@ public class ContextConfigurer extends AbstractPhaseListener { // // camel.context.${name} = ${value} // - RuntimeSupport.bindProperties(runtime.getContext(), runtime.getContext(), "camel.context."); + PropertiesSupport.bindProperties(runtime.getContext(), runtime.getContext(), "camel.context."); // // Configure the camel rest definition using properties in the form: diff --git a/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/ContextLifecycleConfigurer.java b/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/ContextLifecycleConfigurer.java index 85383c7..a01b886 100644 --- a/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/ContextLifecycleConfigurer.java +++ b/camel-k-runtime-core/src/main/java/org/apache/camel/k/listener/ContextLifecycleConfigurer.java @@ -18,7 +18,7 @@ package org.apache.camel.k.listener; import org.apache.camel.Component; import org.apache.camel.k.Runtime; -import org.apache.camel.k.support.RuntimeSupport; +import org.apache.camel.k.support.PropertiesSupport; import org.apache.camel.support.LifecycleStrategySupport; public class ContextLifecycleConfigurer extends AbstractPhaseListener { @@ -41,7 +41,7 @@ public class ContextLifecycleConfigurer extends AbstractPhaseListener { // // camel.component.${scheme}.${name} = ${value} // - RuntimeSupport.bindProperties(runtime.getContext(), component, "camel.component." + name + "."); + PropertiesSupport.bindProperties(runtime.getContext(), component, "camel.component." + name + "."); } }); } diff --git a/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/PropertiesSupport.java b/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/PropertiesSupport.java new file mode 100644 index 0000000..6ce2360 --- /dev/null +++ b/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/PropertiesSupport.java @@ -0,0 +1,153 @@ +/** + * 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.k.support; + +import java.io.IOException; +import java.io.Reader; +import java.nio.file.FileVisitResult; +import java.nio.file.FileVisitor; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.Objects; +import java.util.Properties; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiConsumer; +import java.util.function.Predicate; + +import org.apache.camel.CamelContext; +import org.apache.camel.component.properties.PropertiesComponent; +import org.apache.camel.k.Constants; +import org.apache.camel.k.adapter.Introspection; +import org.apache.camel.util.ObjectHelper; +import org.apache.commons.io.FilenameUtils; + +public final class PropertiesSupport { + private PropertiesSupport() { + } + + public static void forEachProperty(CamelContext context, Predicate<String> nameFilter, BiConsumer<String, Object> consumer) { + final PropertiesComponent component = context.getComponent("properties", PropertiesComponent.class); + final Properties properties = component.getInitialProperties(); + + if (properties != null) { + for (String name: properties.stringPropertyNames()) { + if (nameFilter.test(name)) { + consumer.accept(name, properties.get(name)); + } + } + } + } + + public static int bindProperties(CamelContext context, Object target, String prefix) { + final PropertiesComponent component = context.getComponent("properties", PropertiesComponent.class); + final Properties properties = component.getInitialProperties(); + + if (properties == null) { + return 0; + } + + return bindProperties(properties, target, prefix); + } + + public static int bindProperties(Properties properties, Object target, String prefix) { + final AtomicInteger count = new AtomicInteger(); + + properties.entrySet().stream() + .filter(entry -> entry.getKey() instanceof String) + .filter(entry -> entry.getValue() != null) + .filter(entry -> ((String)entry.getKey()).startsWith(prefix)) + .forEach(entry -> { + final String key = ((String)entry.getKey()).substring(prefix.length()); + final Object val = entry.getValue(); + + try { + if (Introspection.setProperty(target, key, val)) { + count.incrementAndGet(); + } + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + ); + + return count.get(); + } + + public static Properties loadProperties() { + final String conf = System.getenv(Constants.ENV_CAMEL_K_CONF); + final String confd = System.getenv(Constants.ENV_CAMEL_K_CONF_D); + + return loadProperties(conf, confd); + } + + public static Properties loadProperties(String conf, String confd) { + final Properties properties = new Properties(); + + // Main location + if (ObjectHelper.isNotEmpty(conf)) { + if (conf.startsWith(Constants.SCHEME_ENV)) { + try (Reader reader = URIResolver.resolveEnv(conf)) { + properties.load(reader); + } catch (IOException e) { + throw new RuntimeException(e); + } + } else { + try (Reader reader = Files.newBufferedReader(Paths.get(conf))) { + properties.load(reader); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + // Additional locations + if (ObjectHelper.isNotEmpty(confd)) { + Path root = Paths.get(confd); + FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Objects.requireNonNull(file); + Objects.requireNonNull(attrs); + + String path = file.toFile().getAbsolutePath(); + String ext = FilenameUtils.getExtension(path); + + if (Objects.equals("properties", ext)) { + try (Reader reader = Files.newBufferedReader(Paths.get(path))) { + properties.load(reader); + } + } + + return FileVisitResult.CONTINUE; + } + }; + + if (Files.exists(root)) { + try { + Files.walkFileTree(root, visitor); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + return properties; + } +} diff --git a/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/RuntimeSupport.java b/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/RuntimeSupport.java index a5cfff7..d74be38 100644 --- a/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/RuntimeSupport.java +++ b/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/RuntimeSupport.java @@ -16,24 +16,14 @@ */ package org.apache.camel.k.support; -import java.io.IOException; -import java.io.Reader; -import java.nio.file.FileVisitResult; -import java.nio.file.FileVisitor; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Objects; import java.util.Properties; import java.util.Set; import java.util.TreeSet; import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -45,11 +35,9 @@ import org.apache.camel.k.ContextCustomizer; import org.apache.camel.k.RoutesLoader; import org.apache.camel.k.Runtime; import org.apache.camel.k.Source; -import org.apache.camel.k.adapter.Introspection; import org.apache.camel.spi.FactoryFinder; import org.apache.camel.spi.RestConfiguration; import org.apache.camel.util.ObjectHelper; -import org.apache.commons.io.FilenameUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -69,7 +57,7 @@ public final class RuntimeSupport { .forEach(e -> { LOGGER.info("Apply ContextCustomizer with id={} and type={}", e.getKey(), e.getValue().getClass().getName()); - bindProperties(context, e.getValue(), "customizer." + e.getKey() + "."); + PropertiesSupport.bindProperties(context, e.getValue(), "customizer." + e.getKey() + "."); e.getValue().apply(context, registry); appliedCustomizers.add(e.getValue()); @@ -78,10 +66,24 @@ public final class RuntimeSupport { return appliedCustomizers; } + @SuppressWarnings("unchecked") public static void configureRest(CamelContext context) { RestConfiguration configuration = new RestConfiguration(); - - if (RuntimeSupport.bindProperties(context, configuration, "camel.rest.") > 0) { + configuration.setComponentProperties(new HashMap<>()); + configuration.setEndpointProperties(new HashMap<>()); + + PropertiesSupport.forEachProperty( + context, + name -> name.startsWith(Constants.PROPERTY_PREFIX_REST_COMPONENT_PROPERTY), + (k, v) -> configuration.getComponentProperties().put(k.substring(Constants.PROPERTY_PREFIX_REST_COMPONENT_PROPERTY.length()), v) + ); + PropertiesSupport.forEachProperty( + context, + name -> name.startsWith(Constants.PROPERTY_PREFIX_REST_ENDPOINT_PROPERTY), + (k, v) -> configuration.getEndpointProperties().put(k.substring(Constants.PROPERTY_PREFIX_REST_ENDPOINT_PROPERTY.length()), v) + ); + + if (PropertiesSupport.bindProperties(context, configuration, "camel.rest.") > 0) { // // Set the rest configuration if only if at least one // rest parameter has been set. @@ -174,108 +176,6 @@ public final class RuntimeSupport { // ********************************* // - // Helpers - Properties - // - // ********************************* - - public static int bindProperties(CamelContext context, Object target, String prefix) { - final PropertiesComponent component = context.getComponent("properties", PropertiesComponent.class); - final Properties properties = component.getInitialProperties(); - - if (properties == null) { - return 0; - } - - return bindProperties(properties, target, prefix); - } - - public static int bindProperties(Properties properties, Object target, String prefix) { - final AtomicInteger count = new AtomicInteger(); - - properties.entrySet().stream() - .filter(entry -> entry.getKey() instanceof String) - .filter(entry -> entry.getValue() != null) - .filter(entry -> ((String)entry.getKey()).startsWith(prefix)) - .forEach(entry -> { - final String key = ((String)entry.getKey()).substring(prefix.length()); - final Object val = entry.getValue(); - - try { - if (Introspection.setProperty(target, key, val)) { - count.incrementAndGet(); - } - } catch (Exception ex) { - throw new RuntimeException(ex); - } - } - ); - - return count.get(); - } - - public static Properties loadProperties() { - final String conf = System.getenv(Constants.ENV_CAMEL_K_CONF); - final String confd = System.getenv(Constants.ENV_CAMEL_K_CONF_D); - - return loadProperties(conf, confd); - } - - public static Properties loadProperties(String conf, String confd) { - final Properties properties = new Properties(); - - // Main location - if (ObjectHelper.isNotEmpty(conf)) { - if (conf.startsWith(Constants.SCHEME_ENV)) { - try (Reader reader = URIResolver.resolveEnv(conf)) { - properties.load(reader); - } catch (IOException e) { - throw new RuntimeException(e); - } - } else { - try (Reader reader = Files.newBufferedReader(Paths.get(conf))) { - properties.load(reader); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - } - - // Additional locations - if (ObjectHelper.isNotEmpty(confd)) { - Path root = Paths.get(confd); - FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Objects.requireNonNull(file); - Objects.requireNonNull(attrs); - - String path = file.toFile().getAbsolutePath(); - String ext = FilenameUtils.getExtension(path); - - if (Objects.equals("properties", ext)) { - try (Reader reader = Files.newBufferedReader(Paths.get(path))) { - properties.load(reader); - } - } - - return FileVisitResult.CONTINUE; - } - }; - - if (Files.exists(root)) { - try { - Files.walkFileTree(root, visitor); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - } - - return properties; - } - - // ********************************* - // // Helpers - Loaders // // ********************************* diff --git a/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/RuntimeSupportTest.java b/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/RuntimeSupportTest.java index 11a1210..0ce719b 100644 --- a/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/RuntimeSupportTest.java +++ b/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/RuntimeSupportTest.java @@ -28,6 +28,7 @@ import org.apache.camel.k.Constants; import org.apache.camel.k.ContextCustomizer; import org.apache.camel.k.InMemoryRegistry; import org.apache.camel.k.Runtime; +import org.apache.camel.spi.RestConfiguration; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -127,6 +128,7 @@ public class RuntimeSupportTest { public int getOrder() { return Ordered.LOWEST; } + @Override public void apply(CamelContext camelContext, Runtime.Registry registry) { camelContext.setNameStrategy(new ExplicitCamelContextNameStrategy(camelContext.getName() + "-c1")); @@ -137,6 +139,7 @@ public class RuntimeSupportTest { public int getOrder() { return Ordered.HIGHEST; } + @Override public void apply(CamelContext camelContext, Runtime.Registry registry) { camelContext.setNameStrategy(new ExplicitCamelContextNameStrategy(camelContext.getName() + "-c2")); @@ -159,4 +162,29 @@ public class RuntimeSupportTest { assertThat(customizers).hasSize(3); assertThat(context.getName()).isEqualTo("camel-c2-c3-c1"); } + + @Test + public void testCustomizeRestConfiguration() { + Properties properties = new Properties(); + properties.setProperty("camel.rest.component", "servlet"); + properties.setProperty("camel.rest.contextPath", "/mypath"); + properties.setProperty(Constants.PROPERTY_PREFIX_REST_COMPONENT_PROPERTY + "servletName", "MyCamelServlet"); + properties.setProperty(Constants.PROPERTY_PREFIX_REST_ENDPOINT_PROPERTY + "headerFilterStrategy", "myHeaderStrategy"); + + PropertiesComponent pc = new PropertiesComponent(); + pc.setInitialProperties(properties); + + Runtime.Registry registry = new InMemoryRegistry(); + CamelContext context = new DefaultCamelContext(registry); + context.addComponent("properties", pc); + + + RuntimeSupport.configureRest(context); + + RestConfiguration configuration = context.getRestConfiguration(); + assertThat(configuration).hasFieldOrPropertyWithValue("component", "servlet"); + assertThat(configuration).hasFieldOrPropertyWithValue("contextPath", "/mypath"); + assertThat(configuration.getComponentProperties()).containsEntry("servletName", "MyCamelServlet"); + assertThat(configuration.getEndpointProperties()).containsEntry("headerFilterStrategy", "myHeaderStrategy"); + } } diff --git a/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/Application.java b/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/Application.java index 4cf65a1..51745cc 100644 --- a/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/Application.java +++ b/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/Application.java @@ -19,7 +19,7 @@ package org.apache.camel.k.jvm; import java.util.ServiceLoader; import org.apache.camel.k.Runtime; -import org.apache.camel.k.support.RuntimeSupport; +import org.apache.camel.k.support.PropertiesSupport; public class Application { static { @@ -36,7 +36,7 @@ public class Application { public static void main(String[] args) throws Exception { ApplicationRuntime runtime = new ApplicationRuntime(); - runtime.setProperties(RuntimeSupport.loadProperties()); + runtime.setProperties(PropertiesSupport.loadProperties()); runtime.addListeners(ServiceLoader.load(Runtime.Listener.class)); runtime.run(); } diff --git a/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/ApplicationRuntime.java b/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/ApplicationRuntime.java index 806608b..e02a07e 100644 --- a/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/ApplicationRuntime.java +++ b/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/ApplicationRuntime.java @@ -25,7 +25,7 @@ import org.apache.camel.k.InMemoryRegistry; import org.apache.camel.k.Runtime; import org.apache.camel.k.adapter.Exceptions; import org.apache.camel.k.adapter.Main; -import org.apache.camel.k.support.RuntimeSupport; +import org.apache.camel.k.support.PropertiesSupport; import org.apache.camel.main.MainSupport; import org.apache.camel.spi.HasId; import org.apache.camel.util.function.ThrowingConsumer; @@ -81,7 +81,7 @@ public final class ApplicationRuntime implements Runtime { id = id + "."; } - RuntimeSupport.bindProperties(getContext(), listener, id); + PropertiesSupport.bindProperties(getContext(), listener, id); } LOGGER.info("Add listener: {}", listener); diff --git a/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/ApplicationSupport.java b/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/ApplicationSupport.java index 3524dd1..497815a 100644 --- a/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/ApplicationSupport.java +++ b/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/ApplicationSupport.java @@ -19,7 +19,7 @@ package org.apache.camel.k.jvm; import java.util.Properties; import org.apache.camel.k.Constants; -import org.apache.camel.k.support.RuntimeSupport; +import org.apache.camel.k.support.PropertiesSupport; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.core.LoggerContext; @@ -32,7 +32,7 @@ public final class ApplicationSupport { public static void configureLogging() { final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); - final Properties properties = RuntimeSupport.loadProperties(); + final Properties properties = PropertiesSupport.loadProperties(); properties.entrySet().stream() .filter(entry -> entry.getKey() instanceof String) diff --git a/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java b/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java index 65916a7..0374a2c 100644 --- a/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java +++ b/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java @@ -25,7 +25,7 @@ import org.apache.camel.k.ContextCustomizer; import org.apache.camel.k.Runtime; import org.apache.camel.k.listener.ContextConfigurer; import org.apache.camel.k.listener.ContextLifecycleConfigurer; -import org.apache.camel.k.support.RuntimeSupport; +import org.apache.camel.k.support.PropertiesSupport; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; @@ -34,7 +34,7 @@ public class PropertiesTest { @Test public void testLoadProperties() throws Exception { - Properties properties = RuntimeSupport.loadProperties("src/test/resources/conf.properties", "src/test/resources/conf.d"); + Properties properties = PropertiesSupport.loadProperties("src/test/resources/conf.properties", "src/test/resources/conf.d"); ApplicationRuntime runtime = new ApplicationRuntime(); runtime.setProperties(properties);