From e07ef27c54ecf4e86fccb0dc04a8eaa9ae3d0a7a Mon Sep 17 00:00:00 2001 From: lburgazzoli Date: Thu, 18 Apr 2019 11:22:19 +0200 Subject: [PATCH] customizer: honour cutomizer list property/env --- .../camel/k/support/RuntimeSupport.java | 79 +++++++++++++----- .../camel/k/support/NameCustomizer.java | 39 +++++++++ .../camel/k/support/RuntimeSupportTest.java | 82 +++++++++++++++++++ .../org/apache/camel/k/customizer/name | 18 ++++ 4 files changed, 198 insertions(+), 20 deletions(-) create mode 100644 camel-k-runtime-core/src/test/java/org/apache/camel/k/support/NameCustomizer.java create mode 100644 camel-k-runtime-core/src/test/java/org/apache/camel/k/support/RuntimeSupportTest.java create mode 100644 camel-k-runtime-core/src/test/resources/META-INF/services/org/apache/camel/k/customizer/name 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 170d1158d..597b9ccb4 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 @@ -25,8 +25,13 @@ import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; +import java.util.ArrayList; +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.atomic.AtomicInteger; import org.apache.camel.CamelContext; @@ -41,48 +46,82 @@ import org.apache.camel.spi.FactoryFinder; import org.apache.camel.spi.RestConfiguration; import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.StringHelper; import org.apache.commons.io.FilenameUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public final class RuntimeSupport { + private static final Logger LOGGER = LoggerFactory.getLogger(RuntimeSupport.class); private RuntimeSupport() { } - public static void configureContext(CamelContext context, Runtime.Registry registry) { + public static List configureContext(CamelContext context, Runtime.Registry registry) { + List appliedCustomizers = new ArrayList<>(); + Set customizers = lookupCustomizerIDs(context); + + // this is to initialize all customizers that might be already present in + // the context injected by other means. + for (Map.Entry entry: context.getRegistry().findByTypeWithName(ContextCustomizer.class).entrySet()) { + if (!customizers.remove(entry.getKey())) { + continue; + } + + applyCustomizer(context, entry.getKey(), entry.getValue(), registry); + + appliedCustomizers.add(entry.getValue()); + } + try { FactoryFinder finder = context.getFactoryFinder(Constants.CONTEXT_CUSTOMIZER_RESOURCE_PATH); - String customizerIDs = System.getenv().getOrDefault(Constants.ENV_CAMEL_K_CUSTOMIZERS, ""); - if (ObjectHelper.isEmpty(customizerIDs)) { - PropertiesComponent component = context.getComponent("properties", PropertiesComponent.class); - Properties properties = component.getInitialProperties(); + for (String customizerId : customizers) { + ContextCustomizer customizer = (ContextCustomizer) finder.newInstance(customizerId); + applyCustomizer(context, customizerId, customizer, registry); - if (properties != null) { - customizerIDs = properties.getProperty(Constants.PROPERTY_CAMEL_K_CUSTOMIZER, ""); - } - } - - if (ObjectHelper.isNotEmpty(customizerIDs)) { - for (String customizerId : customizerIDs.split(",", -1)) { - configureContext(context, customizerId, (ContextCustomizer) finder.newInstance(customizerId), registry); - } + appliedCustomizers.add(customizer); } } catch (NoFactoryAvailableException e) { - // ignored + throw new RuntimeException(e); } - //this is to initialize all customizers that might be already present in the context injected by other means. - context.getRegistry().findByTypeWithName(ContextCustomizer.class).forEach( - (customizerId, customizer) -> configureContext(context, customizerId, customizer, registry) - ); + return appliedCustomizers; } - public static void configureContext(CamelContext context, String customizerId, ContextCustomizer customizer, Runtime.Registry registry) { + public static void applyCustomizer(CamelContext context, String customizerId, ContextCustomizer customizer, Runtime.Registry registry) { + ObjectHelper.notNull(customizer, "customizer"); + StringHelper.notEmpty(customizerId, "customizerId"); + + LOGGER.info("Apply ContextCustomizer with id={} and type={}", customizerId, customizer.getClass().getName()); + bindProperties(context, customizer, "customizer." + customizerId + "."); customizer.apply(context, registry); } + public static Set lookupCustomizerIDs(CamelContext context) { + Set customizers = new TreeSet<>(); + + String customizerIDs = System.getenv().getOrDefault(Constants.ENV_CAMEL_K_CUSTOMIZERS, ""); + if (ObjectHelper.isEmpty(customizerIDs)) { + PropertiesComponent component = context.getComponent("properties", PropertiesComponent.class); + Properties properties = component.getInitialProperties(); + + if (properties != null) { + customizerIDs = properties.getProperty(Constants.PROPERTY_CAMEL_K_CUSTOMIZER, ""); + } + } + + if (ObjectHelper.isNotEmpty(customizerIDs)) { + for (String customizerId : customizerIDs.split(",", -1)) { + customizers.add(customizerId); + } + } + + return customizers; + } + public static void configureRest(CamelContext context) { RestConfiguration configuration = new RestConfiguration(); diff --git a/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/NameCustomizer.java b/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/NameCustomizer.java new file mode 100644 index 000000000..38bad6493 --- /dev/null +++ b/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/NameCustomizer.java @@ -0,0 +1,39 @@ +/** + * 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 org.apache.camel.CamelContext; +import org.apache.camel.impl.ExplicitCamelContextNameStrategy; +import org.apache.camel.k.ContextCustomizer; +import org.apache.camel.k.Runtime; + +public final class NameCustomizer implements ContextCustomizer { + private String name; + + public NameCustomizer() { + this("default"); + } + + public NameCustomizer(String name) { + this.name = name; + } + + @Override + public void apply(CamelContext camelContext, Runtime.Registry registry) { + camelContext.setNameStrategy(new ExplicitCamelContextNameStrategy(name)); + } +} 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 new file mode 100644 index 000000000..592d633fc --- /dev/null +++ b/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/RuntimeSupportTest.java @@ -0,0 +1,82 @@ +/** + * 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.util.List; +import java.util.Properties; + +import org.apache.camel.CamelContext; +import org.apache.camel.component.properties.PropertiesComponent; +import org.apache.camel.impl.DefaultCamelContext; +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.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RuntimeSupportTest { + + @Test + public void testLoadCustomizers() { + PropertiesComponent pc = new PropertiesComponent(); + Runtime.Registry registry = new InMemoryRegistry(); + CamelContext context = new DefaultCamelContext(registry); + context.addComponent("properties", pc); + + NameCustomizer customizer = new NameCustomizer("from-registry"); + registry.bind("name", customizer); + + List customizers = RuntimeSupport.configureContext(context, registry); + assertThat(context.getName()).isNotEqualTo("from-registry"); + assertThat(context.getName()).isNotEqualTo("default"); + assertThat(customizers).hasSize(0); + + Properties properties = new Properties(); + properties.setProperty(Constants.PROPERTY_CAMEL_K_CUSTOMIZER, "name"); + pc.setInitialProperties(properties); + + customizers = RuntimeSupport.configureContext(context, registry); + assertThat(context.getName()).isEqualTo("from-registry"); + assertThat(customizers).hasSize(1); + } + + @Test + public void testLoadCustomizersOrdering() { + PropertiesComponent pc = new PropertiesComponent(); + Runtime.Registry registry = new InMemoryRegistry(); + CamelContext context = new DefaultCamelContext(registry); + context.addComponent("properties", pc); + + Properties properties = new Properties(); + properties.setProperty(Constants.PROPERTY_CAMEL_K_CUSTOMIZER, "name"); + pc.setInitialProperties(properties); + + List customizers = RuntimeSupport.configureContext(context, registry); + assertThat(context.getName()).isEqualTo("default"); + assertThat(customizers).hasSize(1); + + NameCustomizer customizer = new NameCustomizer("from-registry"); + registry.bind("name", customizer); + + customizers = RuntimeSupport.configureContext(context, registry); + assertThat(context.getName()).isEqualTo("from-registry"); + assertThat(customizers).hasSize(1); + } + +} diff --git a/camel-k-runtime-core/src/test/resources/META-INF/services/org/apache/camel/k/customizer/name b/camel-k-runtime-core/src/test/resources/META-INF/services/org/apache/camel/k/customizer/name new file mode 100644 index 000000000..c1bd738c4 --- /dev/null +++ b/camel-k-runtime-core/src/test/resources/META-INF/services/org/apache/camel/k/customizer/name @@ -0,0 +1,18 @@ +# +# 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. +# + +class=org.apache.camel.k.support.NameCustomizer \ No newline at end of file