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:42 UTC

[camel] 07/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 83d8be8cef3f0c5709f2c6e2527c6c0235fadac2
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jun 17 07:14:59 2019 +0200

    CAMEL-13647: Allow to do autowrire by classpath. Quick and dirty prototype.
---
 .../java/org/apache/camel/maven/AutowireMojo.java  |  3 ++-
 .../java/org/apache/camel/main/MainSupport.java    | 15 ++++++++-------
 .../camel/support/PropertyBindingSupport.java      | 22 +++++++++++++++++-----
 .../src/main/resources/application.properties      |  4 ++--
 4 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/catalog/camel-main-maven-plugin/src/main/java/org/apache/camel/maven/AutowireMojo.java b/catalog/camel-main-maven-plugin/src/main/java/org/apache/camel/maven/AutowireMojo.java
index ab205f3..1519298 100644
--- a/catalog/camel-main-maven-plugin/src/main/java/org/apache/camel/maven/AutowireMojo.java
+++ b/catalog/camel-main-maven-plugin/src/main/java/org/apache/camel/maven/AutowireMojo.java
@@ -55,6 +55,7 @@ import org.apache.maven.model.Exclusion;
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
 import org.apache.maven.plugins.annotations.Mojo;
 import org.apache.maven.plugins.annotations.Parameter;
 import org.apache.maven.plugins.annotations.ResolutionScope;
@@ -68,7 +69,7 @@ import org.reflections.util.ConfigurationBuilder;
 /**
  * Pre scans your project and prepare autowiring by classpath scanning
  */
-@Mojo(name = "autowire", threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE)
+@Mojo(name = "autowire", defaultPhase = LifecyclePhase.PROCESS_CLASSES, threadSafe = true, requiresDependencyResolution = ResolutionScope.COMPILE)
 public class AutowireMojo extends AbstractExecMojo {
 
     /**
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 71d427c..2fe20ef 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
@@ -1003,14 +1003,15 @@ public abstract class MainSupport extends ServiceSupport {
             String stringValue = value != null ? value.toString() : null;
 
             LOG.debug("Setting property {} on {} with value {}", name, target, stringValue);
-            boolean hit = PropertyBindingSupport.bindProperty(context, target, name, stringValue);
-
-            if (hit) {
-                it.remove();
+            if (failIfNotSet) {
+                PropertyBindingSupport.bindMandatoryProperty(context, target, name, stringValue);
                 rc = true;
-            } else if (failIfNotSet) {
-                throw new IllegalArgumentException("Cannot configure option [" + name + "] with value [" + stringValue + "] as the bean class ["
-                    + ObjectHelper.classCanonicalName(target) + "] has no suitable setter method, or not possible to lookup a bean with the id [" + stringValue + "] in Camel registry");
+            } else {
+                boolean hit = PropertyBindingSupport.bindProperty(context, target, name, stringValue);
+                if (hit) {
+                    it.remove();
+                    rc = true;
+                }
             }
         }
 
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
index 8a4ad00..462cdf2 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/PropertyBindingSupport.java
@@ -507,7 +507,7 @@ public final class PropertyBindingSupport {
     public static boolean bindProperty(CamelContext camelContext, Object target, String name, Object value) {
         try {
             if (target != null && name != null) {
-                return setProperty(camelContext, target, name, value, true, true, true, true, true);
+                return setProperty(camelContext, target, name, value, false, true, true, true, true, true);
             }
         } catch (Exception e) {
             throw new PropertyBindingException(target, name, e);
@@ -520,7 +520,7 @@ public final class PropertyBindingSupport {
                                 boolean nesting, boolean deepNesting, boolean fluentBuilder, boolean reference, boolean placeholder) {
         try {
             if (target != null && name != null) {
-                return setProperty(camelContext, target, name, value, nesting, deepNesting, fluentBuilder, reference, placeholder);
+                return setProperty(camelContext, target, name, value, false, nesting, deepNesting, fluentBuilder, reference, placeholder);
             }
         } catch (Exception e) {
             throw new PropertyBindingException(target, name, e);
@@ -540,7 +540,7 @@ public final class PropertyBindingSupport {
     public static void bindMandatoryProperty(CamelContext camelContext, Object target, String name, Object value) {
         try {
             if (target != null && name != null) {
-                boolean bound = setProperty(camelContext, target, name, value, true, true, true, true, true);
+                boolean bound = setProperty(camelContext, target, name, value, true, true, true, true, true, true);
                 if (!bound) {
                     throw new PropertyBindingException(target, name);
                 }
@@ -550,7 +550,7 @@ public final class PropertyBindingSupport {
         }
     }
 
-    private static boolean setProperty(CamelContext context, Object target, String name, Object value,
+    private static boolean setProperty(CamelContext context, Object target, String name, Object value, boolean mandatory,
                                        boolean nesting, boolean deepNesting, boolean fluentBuilder, boolean reference, boolean placeholder) throws Exception {
         String refName = null;
 
@@ -563,6 +563,8 @@ public final class PropertyBindingSupport {
             }
         }
 
+        String ognlPath = name;
+
         // if name has dot then we need to OGNL walk it
         if (nesting) {
             if (name.indexOf('.') > 0) {
@@ -591,6 +593,11 @@ public final class PropertyBindingSupport {
                                 newTarget = instance;
                                 newClass = newTarget.getClass();
                             }
+                        } else {
+                            if (mandatory) {
+                                // there is no getter with this given name, so lets report this as a problem
+                                throw new IllegalArgumentException("Cannot find nested getter method: " + part + " on bean: " + newClass + " when binding property: " + ognlPath);
+                            }
                         }
                     } else {
                         newTarget = prop;
@@ -642,7 +649,12 @@ public final class PropertyBindingSupport {
             }
         }
 
-        return IntrospectionSupport.setProperty(context, context.getTypeConverter(), target, name, value, refName, fluentBuilder);
+        boolean hit = IntrospectionSupport.setProperty(context, context.getTypeConverter(), target, name, value, refName, fluentBuilder);
+        if (!hit && mandatory) {
+            // there is no setter with this given name, so lets report this as a problem
+            throw new IllegalArgumentException("Cannot find setter method: " + name + " on bean: " + target + " when binding property: " + ognlPath);
+        }
+        return hit;
     }
 
     private static Object getOrElseProperty(Object target, String property, Object defaultValue) {
diff --git a/examples/camel-example-main/src/main/resources/application.properties b/examples/camel-example-main/src/main/resources/application.properties
index cd2942e..9af7830 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.connectionFactory.#private#brokerURL=tcp://localhost:61616
+### camel.component.jms.configuration.connectionFactory=#class:org.apache.activemq.artemis.jms.client.ActiveMQJMSConnectionFactory
+### camel.component.jms.configuration.connectionFactory.#private#brokerURL=tcp://localhost:61616
 
 # properties used in the route
 myCron = 0/2 * * * * ?