This is an automated email from the ASF dual-hosted git repository.
davsclaus 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 870581aa CAMEL-15148: camel-main - Fix configuring via fluent builder
to work and as well to mix with application.properties.
870581aa is described below
commit 870581aa36d1b0bc6445601667aeaf3489ad96d9
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Jun 16 10:11:20 2020 +0200
CAMEL-15148: camel-main - Fix configuring via fluent builder to work and as
well to mix with application.properties.
---
.../org/apache/camel/main/BaseMainSupport.java | 115 +++++++++++++++------
.../apache/camel/main/MainFaultToleranceTest.java | 59 +++++++++++
.../apache/camel/main/MainLogPlaceholderTest.java | 1 +
.../java/org/apache/camel/main/MainRestTest.java | 58 +++++++++++
4 files changed, 202 insertions(+), 31 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 7360e9a..96f92bb 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
@@ -61,6 +61,7 @@ import org.apache.camel.spi.DataFormat;
import org.apache.camel.spi.Language;
import org.apache.camel.spi.PropertiesComponent;
import org.apache.camel.spi.PropertyConfigurer;
+import org.apache.camel.spi.PropertyConfigurerGetter;
import org.apache.camel.spi.RestConfiguration;
import org.apache.camel.spi.ThreadPoolProfile;
import org.apache.camel.support.CamelContextHelper;
@@ -115,6 +116,58 @@ public abstract class BaseMainSupport extends BaseService {
return key;
}
+ protected static boolean setPropertiesOnTarget(CamelContext context,
Object target, Object source) throws Exception {
+ ObjectHelper.notNull(context, "context");
+ ObjectHelper.notNull(target, "target");
+
+ boolean rc = false;
+
+ PropertyConfigurer targetConfigurer = null;
+ if (target instanceof Component) {
+ // the component needs to be initialized to have the configurer
ready
+ ServiceHelper.initService(target);
+ targetConfigurer = ((Component)
target).getComponentPropertyConfigurer();
+ }
+ if (targetConfigurer == null) {
+ String name = target.getClass().getSimpleName();
+ if (target instanceof ExtendedCamelContext) {
+ // special for camel context itself as we have an extended
configurer
+ name = "ExtendedCamelContext";
+ }
+ // see if there is a configurer for it
+ targetConfigurer = context.adapt(ExtendedCamelContext.class)
+ .getConfigurerResolver().resolvePropertyConfigurer(name,
context);
+ }
+
+ PropertyConfigurer sourceConfigurer = null;
+ if (source instanceof Component) {
+ // the component needs to be initialized to have the configurer
ready
+ ServiceHelper.initService(source);
+ sourceConfigurer = ((Component)
source).getComponentPropertyConfigurer();
+ }
+ if (sourceConfigurer == null) {
+ String name = source.getClass().getSimpleName();
+ if (source instanceof ExtendedCamelContext) {
+ // special for camel context itself as we have an extended
configurer
+ name = "ExtendedCamelContext";
+ }
+ // see if there is a configurer for it
+ sourceConfigurer = context.adapt(ExtendedCamelContext.class)
+ .getConfigurerResolver().resolvePropertyConfigurer(name,
context);
+ }
+
+ if (targetConfigurer != null && sourceConfigurer instanceof
PropertyConfigurerGetter) {
+ PropertyConfigurerGetter getter = (PropertyConfigurerGetter)
sourceConfigurer;
+ for (String key : getter.getAllOptions(source).keySet()) {
+ Object value = getter.getOptionValue(source, key, true);
+ if (value != null) {
+ rc |= targetConfigurer.configure(context, target, key,
value, true);
+ }
+ }
+ }
+ return rc;
+ }
+
protected static boolean setPropertiesOnTarget(CamelContext context,
Object target, Map<String, Object> properties,
String optionPrefix,
boolean failIfNotSet, boolean ignoreCase,
Map<String, String>
autoConfiguredProperties) throws Exception {
@@ -750,6 +803,8 @@ public abstract class BaseMainSupport extends BaseService {
}
}
+ ModelCamelContext model = camelContext.adapt(ModelCamelContext.class);
+
// create beans first as they may be used later
if (!beansProperties.isEmpty()) {
LOG.debug("Creating and binding beans to registry from loaded
properties: {}", beansProperties.size());
@@ -761,49 +816,54 @@ public abstract class BaseMainSupport extends BaseService
{
setPropertiesOnTarget(camelContext, camelContext,
contextProperties, "camel.context.",
mainConfigurationProperties.isAutoConfigurationFailFast(),
true, autoConfiguredProperties);
}
+
+ HystrixConfigurationProperties hystrix =
mainConfigurationProperties.hystrix();
if (!hystrixProperties.isEmpty()) {
LOG.debug("Auto-configuring Hystrix Circuit Breaker EIP from
loaded properties: {}", hystrixProperties.size());
- ModelCamelContext model =
camelContext.adapt(ModelCamelContext.class);
- HystrixConfigurationDefinition hystrix =
model.getHystrixConfiguration(null);
- if (hystrix == null) {
- hystrix = new HystrixConfigurationDefinition();
- model.setHystrixConfiguration(hystrix);
- }
setPropertiesOnTarget(camelContext, hystrix, hystrixProperties,
"camel.hystrix.",
mainConfigurationProperties.isAutoConfigurationFailFast(),
true, autoConfiguredProperties);
}
+ HystrixConfigurationDefinition hystrixModel =
model.getHystrixConfiguration(null);
+ if (hystrixModel == null) {
+ hystrixModel = new HystrixConfigurationDefinition();
+ model.setHystrixConfiguration(hystrixModel);
+ }
+ setPropertiesOnTarget(camelContext, hystrixModel, hystrix);
+
+ Resilience4jConfigurationProperties resilience4j =
mainConfigurationProperties.resilience4j();
if (!resilience4jProperties.isEmpty()) {
LOG.debug("Auto-configuring Resilience4j Circuit Breaker EIP from
loaded properties: {}", resilience4jProperties.size());
- ModelCamelContext model =
camelContext.adapt(ModelCamelContext.class);
- Resilience4jConfigurationDefinition resilience4j =
model.getResilience4jConfiguration(null);
- if (resilience4j == null) {
- resilience4j = new Resilience4jConfigurationDefinition();
- model.setResilience4jConfiguration(resilience4j);
- }
setPropertiesOnTarget(camelContext, resilience4j,
resilience4jProperties, "camel.resilience4j.",
mainConfigurationProperties.isAutoConfigurationFailFast(),
true, autoConfiguredProperties);
}
+ Resilience4jConfigurationDefinition resilience4jModel =
model.getResilience4jConfiguration(null);
+ if (resilience4jModel == null) {
+ resilience4jModel = new Resilience4jConfigurationDefinition();
+ model.setResilience4jConfiguration(resilience4jModel);
+ }
+ setPropertiesOnTarget(camelContext, resilience4jModel, resilience4j);
+
+ FaultToleranceConfigurationProperties faultTolerance =
mainConfigurationProperties.faultTolerance();
if (!faultToleranceProperties.isEmpty()) {
LOG.debug("Auto-configuring MicroProfile Fault Tolerance Circuit
Breaker EIP from loaded properties: {}", faultToleranceProperties.size());
- ModelCamelContext model =
camelContext.adapt(ModelCamelContext.class);
- FaultToleranceConfigurationDefinition faultTolerance =
model.getFaultToleranceConfiguration(null);
- if (faultTolerance == null) {
- faultTolerance = new FaultToleranceConfigurationDefinition();
- model.setFaultToleranceConfiguration(faultTolerance);
- }
setPropertiesOnTarget(camelContext, faultTolerance,
faultToleranceProperties, "camel.faulttolerance.",
mainConfigurationProperties.isAutoConfigurationFailFast(),
true, autoConfiguredProperties);
}
+ FaultToleranceConfigurationDefinition faultToleranceModel =
model.getFaultToleranceConfiguration(null);
+ if (faultToleranceModel == null) {
+ faultToleranceModel = new FaultToleranceConfigurationDefinition();
+ model.setFaultToleranceConfiguration(faultToleranceModel);
+ }
+ setPropertiesOnTarget(camelContext, faultToleranceModel,
faultTolerance);
+
+ RestConfigurationProperties rest = mainConfigurationProperties.rest();
if (!restProperties.isEmpty()) {
LOG.debug("Auto-configuring Rest DSL from loaded properties: {}",
restProperties.size());
- RestConfiguration rest = camelContext.getRestConfiguration();
- if (rest == null) {
- rest = new RestConfiguration();
- camelContext.setRestConfiguration(rest);
- }
setPropertiesOnTarget(camelContext, rest, restProperties,
"camel.rest.",
mainConfigurationProperties.isAutoConfigurationFailFast(),
true, autoConfiguredProperties);
}
+ camelContext.setRestConfiguration(rest);
+
if (!threadPoolProperties.isEmpty()) {
LOG.debug("Auto-configuring Thread Pool from loaded properties:
{}", threadPoolProperties.size());
setThreadPoolProperties(camelContext, threadPoolProperties,
mainConfigurationProperties.isAutoConfigurationFailFast(),
autoConfiguredProperties);
@@ -829,28 +889,21 @@ public abstract class BaseMainSupport extends BaseService
{
});
}
if (!hystrixProperties.isEmpty()) {
- ModelCamelContext model =
camelContext.adapt(ModelCamelContext.class);
- HystrixConfigurationDefinition hystrix =
model.getHystrixConfiguration(null);
hystrixProperties.forEach((k, v) -> {
LOG.warn("Property not auto-configured: camel.hystrix.{}={} on
bean: {}", k, v, hystrix);
});
}
if (!resilience4jProperties.isEmpty()) {
- ModelCamelContext model =
camelContext.adapt(ModelCamelContext.class);
- Resilience4jConfigurationDefinition resilience4j =
model.getResilience4jConfiguration(null);
resilience4jProperties.forEach((k, v) -> {
LOG.warn("Property not auto-configured:
camel.resilience4j.{}={} on bean: {}", k, v, resilience4j);
});
}
if (!faultToleranceProperties.isEmpty()) {
- ModelCamelContext model =
camelContext.adapt(ModelCamelContext.class);
- FaultToleranceConfigurationDefinition faulttolerance =
model.getFaultToleranceConfiguration(null);
faultToleranceProperties.forEach((k, v) -> {
- LOG.warn("Property not auto-configured:
camel.faulttolerance.{}={} on bean: {}", k, v, faulttolerance);
+ LOG.warn("Property not auto-configured:
camel.faulttolerance.{}={} on bean: {}", k, v, faultTolerance);
});
}
if (!restProperties.isEmpty()) {
- RestConfiguration rest = camelContext.getRestConfiguration();
restProperties.forEach((k, v) -> {
LOG.warn("Property not auto-configured: camel.rest.{}={} on
bean: {}", k, v, rest);
});
diff --git
a/core/camel-main/src/test/java/org/apache/camel/main/MainFaultToleranceTest.java
b/core/camel-main/src/test/java/org/apache/camel/main/MainFaultToleranceTest.java
new file mode 100644
index 0000000..8c6c377
--- /dev/null
+++
b/core/camel-main/src/test/java/org/apache/camel/main/MainFaultToleranceTest.java
@@ -0,0 +1,59 @@
+/*
+ * 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 org.apache.camel.CamelContext;
+import org.apache.camel.model.FaultToleranceConfigurationDefinition;
+import org.apache.camel.model.ModelCamelContext;
+import org.junit.Assert;
+import org.junit.jupiter.api.Test;
+
+public class MainFaultToleranceTest {
+
+ @Test
+ public void testMain() throws Exception {
+ Main main = new Main();
+
+ main.addInitialProperty("camel.faulttolerance.failure-ratio", "555");
+ main.addInitialProperty("camel.faulttolerance.timeout-pool-size",
"20");
+
+ main.configure().faultTolerance()
+ .withBulkheadEnabled(true)
+ .withDelay(500L)
+ .withSuccessThreshold(123)
+ .withTimeoutPoolSize(5)
+ .end();
+
+ main.start();
+
+ CamelContext context = main.getCamelContext();
+ Assert.assertNotNull(context);
+
+ ModelCamelContext mcc = context.adapt(ModelCamelContext.class);
+ FaultToleranceConfigurationDefinition def =
mcc.getFaultToleranceConfiguration(null);
+ Assert.assertNotNull(def);
+
+ Assert.assertEquals("500", def.getDelay());
+ Assert.assertEquals("123", def.getSuccessThreshold());
+ Assert.assertEquals("20", def.getTimeoutPoolSize());
+ Assert.assertEquals("555", def.getFailureRatio());
+ Assert.assertEquals("true", def.getBulkheadEnabled());
+
+ main.stop();
+ }
+
+}
diff --git
a/core/camel-main/src/test/java/org/apache/camel/main/MainLogPlaceholderTest.java
b/core/camel-main/src/test/java/org/apache/camel/main/MainLogPlaceholderTest.java
index 47efc4d..3d1cca6 100644
---
a/core/camel-main/src/test/java/org/apache/camel/main/MainLogPlaceholderTest.java
+++
b/core/camel-main/src/test/java/org/apache/camel/main/MainLogPlaceholderTest.java
@@ -21,6 +21,7 @@ import org.apache.camel.component.mock.MockEndpoint;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
+
public class MainLogPlaceholderTest {
@Test
diff --git
a/core/camel-main/src/test/java/org/apache/camel/main/MainRestTest.java
b/core/camel-main/src/test/java/org/apache/camel/main/MainRestTest.java
new file mode 100644
index 0000000..a663c72
--- /dev/null
+++ b/core/camel-main/src/test/java/org/apache/camel/main/MainRestTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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 org.apache.camel.CamelContext;
+import org.apache.camel.spi.RestConfiguration;
+import org.junit.Assert;
+import org.junit.jupiter.api.Test;
+
+public class MainRestTest {
+
+ @Test
+ public void testMain() throws Exception {
+ Main main = new Main();
+
+ main.addInitialProperty("camel.rest.bindingMode", "json");
+ main.addInitialProperty("camel.rest.apiContextPath", "bar");
+
+ main.configure().rest()
+ .withComponent("servlet")
+ .withContextPath("foo")
+ .withUseXForwardHeaders(true)
+ .withPort(1234)
+ .end();
+
+ main.start();
+
+ CamelContext context = main.getCamelContext();
+ Assert.assertNotNull(context);
+
+ RestConfiguration def = context.getRestConfiguration();
+ Assert.assertNotNull(def);
+
+ Assert.assertEquals(1234, def.getPort());
+ Assert.assertTrue(def.isUseXForwardHeaders());
+ Assert.assertEquals("foo", def.getContextPath());
+ Assert.assertEquals("bar", def.getApiContextPath());
+ Assert.assertEquals("servlet", def.getComponent());
+ Assert.assertEquals(RestConfiguration.RestBindingMode.json,
def.getBindingMode());
+
+ main.stop();
+ }
+
+}