You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2019/06/17 12:04:44 UTC
[camel] 09/20: CAMEL-13647: Allow to do autowrire by classpath.
Quick and dirty prototype.
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
commit 33f0f4b7d1396b3bcbd5f85b8561187eb4a3b377
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jun 17 08:36:25 2019 +0200
CAMEL-13647: Allow to do autowrire by classpath. Quick and dirty prototype.
---
.../camel/main/MainConfigurationProperties.java | 4 +--
.../java/org/apache/camel/main/MainSupport.java | 33 ++++++++++++++++++++--
examples/camel-example-main/pom.xml | 11 ++++++++
.../src/main/resources/application.properties | 4 +--
.../src/main/resources/log4j2.properties | 3 ++
5 files changed, 49 insertions(+), 6 deletions(-)
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainConfigurationProperties.java b/core/camel-main/src/main/java/org/apache/camel/main/MainConfigurationProperties.java
index ba00f22..c43ced7 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainConfigurationProperties.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainConfigurationProperties.java
@@ -59,8 +59,8 @@ public class MainConfigurationProperties extends DefaultConfigurationProperties<
/**
* Whether auto configuration of components/dataformats/languages is enabled or not.
* When enabled the configuration parameters are loaded from the properties component
- * and configured as defaults (similar to spring-boot auto-configuration). You can prefix
- * the parameters in the properties file with:
+ * and optionally from the classpath file META-INF/services/org/apache/camel/autowire.properties.
+ * You can prefix the parameters in the properties file with:
* - camel.component.name.option1=value1
* - camel.component.name.option2=value2
* - camel.dataformat.name.option1=value1
diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java
index 2fe20ef..4e841f3 100644
--- a/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java
+++ b/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java
@@ -18,6 +18,7 @@ package org.apache.camel.main;
import java.io.File;
import java.io.FileInputStream;
+import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
@@ -52,7 +53,9 @@ import org.apache.camel.support.PropertyBindingSupport;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.FileUtil;
+import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.OrderedProperties;
import org.apache.camel.util.concurrent.ThreadHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -897,8 +900,34 @@ public abstract class MainSupport extends ServiceSupport {
}
protected void autoConfigurationFromProperties(CamelContext camelContext) throws Exception {
- // load properties
- Properties prop = camelContext.getPropertiesComponent().loadProperties();
+ // load optional META-INF/services/org/apache/camel/autowire.properties
+ Properties prop = new OrderedProperties();
+ try {
+ InputStream is = camelContext.getClassResolver().loadResourceAsStream("/META-INF/services/org/apache/camel/autowire.properties");
+ prop.load(is);
+ if (!prop.isEmpty()) {
+ LOG.info("Loaded {} properties from classpath: META-INF/services/org/apache/camel/autowire.properties", prop.size());
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Properties from classpath: META-INF/services/org/apache/camel/autowire.properties:");
+ for (String key : prop.stringPropertyNames()) {
+ LOG.debug(" {}={}", key, prop.getProperty(key));
+ }
+ }
+ }
+ IOHelper.close(is);
+ } catch (Throwable e) {
+ // ignore as this file is optional
+ }
+
+ // load properties from properties component
+ Properties propPC = camelContext.getPropertiesComponent().loadProperties();
+ if (propPC != null) {
+ prop.putAll(propPC);
+ LOG.debug("Properties from Camel properties component:");
+ for (String key : propPC.stringPropertyNames()) {
+ LOG.debug(" {}={}", key, propPC.getProperty(key));
+ }
+ }
Map<Object, Map<String, Object>> properties = new LinkedHashMap<>();
diff --git a/examples/camel-example-main/pom.xml b/examples/camel-example-main/pom.xml
index 6a4c5bb..e56f087 100644
--- a/examples/camel-example-main/pom.xml
+++ b/examples/camel-example-main/pom.xml
@@ -97,6 +97,8 @@
</configuration>
</plugin>
+ <!-- generate autowire.properties file that can automatic detect resources
+ from the classpath to make convention over configuration for Camel components -->
<plugin>
<groupId>org.apache.camel</groupId>
<artifactId>camel-main-maven-plugin</artifactId>
@@ -104,6 +106,15 @@
<configuration>
<logClasspath>false</logClasspath>
</configuration>
+ <executions>
+ <execution>
+ <id>generate</id>
+ <goals>
+ <goal>autowire</goal>
+ </goals>
+ <phase>process-classes</phase>
+ </execution>
+ </executions>
</plugin>
</plugins>
diff --git a/examples/camel-example-main/src/main/resources/application.properties b/examples/camel-example-main/src/main/resources/application.properties
index fb4eb9f..e76d2a2 100644
--- a/examples/camel-example-main/src/main/resources/application.properties
+++ b/examples/camel-example-main/src/main/resources/application.properties
@@ -42,8 +42,8 @@ camel.component.quartz2.start-delayed-seconds = 3
# you can configure whether OS environment should override (=2 which is default) or as fallback (=1)
### camel.component.properties.environment-variable-mode=1
-camel.component.jms.configuration.connectionFactory=#class:org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory
-camel.component.jms.configuration.xxxconnectionFactory.#private#brokerURL=tcp://localhost:61616
+# setup JMS component with connection to ActiveMQ Artemis broker
+camel.component.jms.configuration.connectionFactory.#private#brokerURL=tcp://localhost:61616
# properties used in the route
myCron = 0/2 * * * * ?
diff --git a/examples/camel-example-main/src/main/resources/log4j2.properties b/examples/camel-example-main/src/main/resources/log4j2.properties
index d406a9f..d050a4f 100644
--- a/examples/camel-example-main/src/main/resources/log4j2.properties
+++ b/examples/camel-example-main/src/main/resources/log4j2.properties
@@ -21,3 +21,6 @@ appender.out.layout.type = PatternLayout
appender.out.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n
rootLogger.level = INFO
rootLogger.appenderRef.out.ref = out
+
+logger.camel-main.name = org.apache.camel.main
+logger.camel-main.level = DEBUG