You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2019/12/03 15:17:30 UTC
[camel] branch master updated: [CAMEL-14245] camel-main - allow to
enable/disable auto confioguiration per component/language/data-format
This is an automated email from the ASF dual-hosted git repository.
lburgazzoli 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 42f912a [CAMEL-14245] camel-main - allow to enable/disable auto confioguiration per component/language/data-format
42f912a is described below
commit 42f912aaaccdfc2d40719d13baf667bc7564e280
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Tue Dec 3 14:42:06 2019 +0100
[CAMEL-14245] camel-main - allow to enable/disable auto confioguiration per component/language/data-format
---
.../org/apache/camel/main/BaseMainSupport.java | 137 ++++++++++++--------
.../camel/main/ServicesAutoConfigurationTest.java | 143 +++++++++++++++++++++
2 files changed, 228 insertions(+), 52 deletions(-)
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
index 584fec5..991e6ab 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java
@@ -31,6 +31,7 @@ import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Function;
import org.apache.camel.CamelContext;
import org.apache.camel.Component;
@@ -496,7 +497,6 @@ public abstract class BaseMainSupport extends ServiceSupport {
if (pc.getLocations().isEmpty()) {
pc.addLocation(defaultPropertyPlaceholderLocation);
}
-
LOG.info("Using properties from {}", defaultPropertyPlaceholderLocation);
}
@@ -887,67 +887,35 @@ public abstract class BaseMainSupport extends ServiceSupport {
Map<PropertyOptionKey, Map<String, Object>> properties = new LinkedHashMap<>();
for (String key : prop.stringPropertyNames()) {
- if (key.startsWith("camel.component.")) {
- // grab name
- int dot = key.indexOf(".", 16);
- String name = dot == -1 ? key.substring(16) : key.substring(16, dot);
- // skip properties as its already configured earlier
- if ("properties".equals(name)) {
- continue;
- }
- Component component = camelContext.getComponent(name);
- if (component == null) {
+ computeProperties("camel.component.", key, prop, properties, name -> {
+ Component target = camelContext.getComponent(name);
+ if (target == null) {
throw new IllegalArgumentException("Error configuring property: " + key + " because cannot find component with name " + name
+ ". Make sure you have the component on the classpath");
}
- String option = dot == -1 ? "" : key.substring(dot + 1);
- String value = prop.getProperty(key, "");
- String prefix = dot == -1 ? "" : key.substring(0, dot + 1);
- validateOptionAndValue(key, option, value);
- PropertyOptionKey pok = new PropertyOptionKey(key, component, prefix);
- Map<String, Object> values = properties.getOrDefault(pok, new LinkedHashMap<>());
- // we ignore case for property keys (so we should store them in canonical style
- values.put(optionKey(option), value);
- properties.put(pok, values);
- }
- if (key.startsWith("camel.dataformat.")) {
- // grab name
- int dot = key.indexOf(".", 17);
- String name = dot == -1 ? key.substring(17) : key.substring(17, dot);
- DataFormat dataformat = camelContext.resolveDataFormat(name);
- if (dataformat == null) {
+
+ return target;
+ });
+ computeProperties("camel.dataformat.", key, prop, properties, name -> {
+ DataFormat target = camelContext.resolveDataFormat(name);
+ if (target == null) {
throw new IllegalArgumentException("Error configuring property: " + key + " because cannot find dataformat with name " + name
+ ". Make sure you have the dataformat on the classpath");
}
- String option = dot == -1 ? "" : key.substring(dot + 1);
- String value = prop.getProperty(key, "");
- String prefix = dot == -1 ? "" : key.substring(0, dot + 1);
- validateOptionAndValue(key, option, value);
- PropertyOptionKey pok = new PropertyOptionKey(key, dataformat, prefix);
- Map<String, Object> values = properties.getOrDefault(pok, new LinkedHashMap<>());
- values.put(optionKey(option), value);
- properties.put(pok, values);
- }
- if (key.startsWith("camel.language.")) {
- // grab name
- int dot = key.indexOf(".", 15);
- String name = dot == -1 ? key.substring(15) : key.substring(15, dot);
- Language language;
+
+ return target;
+ });
+ computeProperties("camel.language.", key, prop, properties, name -> {
+ Language target;
try {
- language = camelContext.resolveLanguage(name);
+ target = camelContext.resolveLanguage(name);
} catch (NoSuchLanguageException e) {
throw new IllegalArgumentException("Error configuring property: " + key + " because cannot find language with name " + name
+ ". Make sure you have the language on the classpath");
}
- String option = dot == -1 ? "" : key.substring(dot + 1);
- String value = prop.getProperty(key, "");
- String prefix = dot == -1 ? "" : key.substring(0, dot + 1);
- validateOptionAndValue(key, option, value);
- PropertyOptionKey pok = new PropertyOptionKey(key, language, prefix);
- Map<String, Object> values = properties.getOrDefault(pok, new LinkedHashMap<>());
- values.put(optionKey(option), value);
- properties.put(pok, values);
- }
+
+ return target;
+ });
}
if (!properties.isEmpty()) {
@@ -986,7 +954,7 @@ public abstract class BaseMainSupport extends ServiceSupport {
});
}
- protected void validateOptionAndValue(String key, String option, String value) {
+ protected static void validateOptionAndValue(String key, String option, String value) {
if (ObjectHelper.isEmpty(option)) {
throw new IllegalArgumentException("Error configuring property: " + key + " because option is empty");
}
@@ -1082,4 +1050,69 @@ public abstract class BaseMainSupport extends ServiceSupport {
return Objects.hash(key, instance);
}
}
+
+ public static void computeProperties(String keyPrefix, String key, Properties prop, Map<PropertyOptionKey, Map<String, Object>> properties, Function<String, Object> supplier) {
+ if (key.startsWith(keyPrefix)) {
+ // grab name
+ final int dot = key.indexOf(".", keyPrefix.length());
+ final String name = dot == -1 ? key.substring(keyPrefix.length()) : key.substring(keyPrefix.length(), dot);
+
+ // enabled is a virtual property
+ if ("enabled".equals(name)) {
+ return;
+ }
+ // skip properties as its already keyPrefix earlier
+ if ("properties".equals(name)) {
+ return;
+ }
+
+ // determine if the service is enabled or not by taking into account two options:
+ //
+ // 1. ${keyPrefix}.enabled = true|false
+ // 2. ${keyPrefix}.${name}.enabled = true|false
+ //
+ // The option [2] has the higher priority so as example:
+ //
+ // camel.component.enabled = false
+ // camel.component.seda.enabled = true
+ //
+ // enables auto configuration of the seda component only
+ if (!isServiceEnabled(keyPrefix, name, prop)) {
+ return;
+ }
+
+ Object target = supplier.apply(name);
+ String prefix = dot == -1 ? "" : key.substring(0, dot + 1);
+ String option = dot == -1 ? "" : key.substring(dot + 1);
+ String value = prop.getProperty(key, "");
+
+ // enabled is a virtual property
+ if ("enabled".equalsIgnoreCase(option)) {
+ return;
+ }
+
+ validateOptionAndValue(key, option, value);
+
+ PropertyOptionKey pok = new PropertyOptionKey(key, target, prefix);
+ Map<String, Object> values = properties.computeIfAbsent(pok, k -> new LinkedHashMap<>());
+
+ // we ignore case for property keys (so we should store them in canonical style
+ values.put(optionKey(option), value);
+ }
+ }
+
+ public static boolean isServiceEnabled(String prefix, String name, Properties properties) {
+ ObjectHelper.notNull(prefix, "prefix");
+ ObjectHelper.notNull(name, "name");
+ ObjectHelper.notNull(properties, "properties");
+
+ if (!prefix.endsWith(".")) {
+ prefix = prefix + ".";
+ }
+
+ final String group = properties.getProperty(prefix + "enabled", "true");
+ final String item = properties.getProperty(prefix + name + ".enabled", group);
+
+ return Boolean.valueOf(item);
+ }
}
diff --git a/core/camel-main/src/test/java/org/apache/camel/main/ServicesAutoConfigurationTest.java b/core/camel-main/src/test/java/org/apache/camel/main/ServicesAutoConfigurationTest.java
new file mode 100644
index 0000000..581619c
--- /dev/null
+++ b/core/camel-main/src/test/java/org/apache/camel/main/ServicesAutoConfigurationTest.java
@@ -0,0 +1,143 @@
+/*
+ * 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.main;
+
+import java.util.Properties;
+
+import org.apache.camel.component.seda.SedaComponent;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class ServicesAutoConfigurationTest extends Assert {
+ @Test
+ public void testComponentAutoConfiguredWhenGlobalAutoConfigurationIsDisabled() {
+ Properties properties = new Properties();
+ properties.put("camel.component.enabled", "false");
+ properties.put("camel.component.seda.queue-size", "1234");
+
+ Main main = new Main();
+ try {
+ main.setDefaultPropertyPlaceholderLocation("false");
+ main.setInitialProperties(properties);
+ main.start();
+
+ SedaComponent component = main.getCamelContext().getComponent("seda", SedaComponent.class);
+ assertNotEquals(1234, component.getQueueSize());
+ } finally {
+ main.stop();
+ }
+ }
+
+ @Test
+ public void testComponentAutoConfiguredWhenGlobalAutoConfigurationIsEnabled() {
+ Properties properties = new Properties();
+ properties.put("camel.component.enabled", "true");
+ properties.put("camel.component.seda.queue-size", "1234");
+
+ Main main = new Main();
+ try {
+ main.setDefaultPropertyPlaceholderLocation("false");
+ main.setInitialProperties(properties);
+ main.start();
+
+ SedaComponent component = main.getCamelContext().getComponent("seda", SedaComponent.class);
+ assertEquals(1234, component.getQueueSize());
+ } finally {
+ main.stop();
+ }
+ }
+
+ @Test
+ public void testComponentAutoConfiguredWhenGlobalAutoConfigurationIsDisabledButComponentAutoConfigurationIsEnabled() {
+ Properties properties = new Properties();
+ properties.put("camel.component.enabled", "false");
+ properties.put("camel.component.seda.enabled", "true");
+ properties.put("camel.component.seda.queue-size", "1234");
+
+ Main main = new Main();
+ try {
+ main.setDefaultPropertyPlaceholderLocation("false");
+ main.setInitialProperties(properties);
+ main.start();
+
+ SedaComponent component = main.getCamelContext().getComponent("seda", SedaComponent.class);
+ assertEquals(1234, component.getQueueSize());
+ } finally {
+ main.stop();
+ }
+ }
+
+ @Test
+ public void testComponentAutoConfiguredWhenGlobalAutoConfigurationIsEnabledButComponentAutoConfigurationIsDisabled() {
+ Properties properties = new Properties();
+ properties.put("camel.component.enabled", "true");
+ properties.put("camel.component.seda.enabled", "false");
+ properties.put("camel.component.seda.queue-size", "1234");
+
+ Main main = new Main();
+ try {
+ main.setDefaultPropertyPlaceholderLocation("false");
+ main.setInitialProperties(properties);
+ main.start();
+
+ SedaComponent component = main.getCamelContext().getComponent("seda", SedaComponent.class);
+ assertNotEquals(1234, component.getQueueSize());
+ } finally {
+ main.stop();
+ }
+ }
+
+ @Test
+ public void testComponentAutoConfiguredDisabled() {
+ Properties properties = new Properties();
+ properties.put("camel.component.enabled", "false");
+ properties.put("camel.component.seda.enabled", "false");
+ properties.put("camel.component.seda.queue-size", "1234");
+
+ Main main = new Main();
+ try {
+ main.setDefaultPropertyPlaceholderLocation("false");
+ main.setInitialProperties(properties);
+ main.start();
+
+ SedaComponent component = main.getCamelContext().getComponent("seda", SedaComponent.class);
+ assertNotEquals(1234, component.getQueueSize());
+ } finally {
+ main.stop();
+ }
+ }
+
+ @Test
+ public void testComponentAutoConfiguredEnabled() {
+ Properties properties = new Properties();
+ properties.put("camel.component.enabled", "true");
+ properties.put("camel.component.seda.enabled", "true");
+ properties.put("camel.component.seda.queue-size", "1234");
+
+ Main main = new Main();
+ try {
+ main.setDefaultPropertyPlaceholderLocation("false");
+ main.setInitialProperties(properties);
+ main.start();
+
+ SedaComponent component = main.getCamelContext().getComponent("seda", SedaComponent.class);
+ assertEquals(1234, component.getQueueSize());
+ } finally {
+ main.stop();
+ }
+ }
+}