You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by nf...@apache.org on 2019/04/19 10:00:08 UTC
[camel-k-runtime] branch master updated: customizer: honour
cutomizer list property/env
This is an automated email from the ASF dual-hosted git repository.
nferraro 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 e07ef27 customizer: honour cutomizer list property/env
new da362ed Merge pull request #49 from lburgazzoli/customizer-cleanup
e07ef27 is described below
commit e07ef27c54ecf4e86fccb0dc04a8eaa9ae3d0a7a
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Thu Apr 18 11:22:19 2019 +0200
customizer: honour cutomizer list property/env
---
.../org/apache/camel/k/support/RuntimeSupport.java | 79 +++++++++++++++------
.../org/apache/camel/k/support/NameCustomizer.java | 39 ++++++++++
.../apache/camel/k/support/RuntimeSupportTest.java | 82 ++++++++++++++++++++++
.../services/org/apache/camel/k/customizer/name | 18 +++++
4 files changed, 198 insertions(+), 20 deletions(-)
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 170d115..597b9cc 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.Path;
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.k.adapter.Introspection;
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<ContextCustomizer> configureContext(CamelContext context, Runtime.Registry registry) {
+ List<ContextCustomizer> appliedCustomizers = new ArrayList<>();
+ Set<String> customizers = lookupCustomizerIDs(context);
+
+ // this is to initialize all customizers that might be already present in
+ // the context injected by other means.
+ for (Map.Entry<String, ContextCustomizer> 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<String> lookupCustomizerIDs(CamelContext context) {
+ Set<String> 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 0000000..38bad64
--- /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 0000000..592d633
--- /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<ContextCustomizer> 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<ContextCustomizer> 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 0000000..c1bd738
--- /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