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 2020/03/22 09:07:09 UTC

[camel] 02/13: CAMEL-14762: camel-core - Configurer to include API for method name and type

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 a92f87d8d085362a94665f3436e67bf573600f17
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Mar 21 21:14:41 2020 +0100

    CAMEL-14762: camel-core - Configurer to include API for method name and type
---
 ...nfigurer.java => PropertyConfigurerGetter.java} | 22 ++++++++--
 .../packaging/PropertyConfigurerGenerator.java     | 50 +++++++++++++++++++---
 2 files changed, 64 insertions(+), 8 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/PropertyOptionsConfigurer.java b/core/camel-api/src/main/java/org/apache/camel/spi/PropertyConfigurerGetter.java
similarity index 55%
rename from core/camel-api/src/main/java/org/apache/camel/spi/PropertyOptionsConfigurer.java
rename to core/camel-api/src/main/java/org/apache/camel/spi/PropertyConfigurerGetter.java
index 9c7b3e9..c0d24f9 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/PropertyOptionsConfigurer.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/PropertyConfigurerGetter.java
@@ -21,14 +21,30 @@ import java.util.Map;
 /**
  * A marker interface to identify the object as being a configurer which can
  * provide details about the options the configurer supports.
+ * <p/>
+ * This is used in Camel to have fast property configuration of Camel components & endpoints,
+ * and for EIP patterns as well.
+ *
+ * @see PropertyConfigurer
  */
-public interface PropertyOptionsConfigurer {
+public interface PropertyConfigurerGetter {
 
     /**
      * Provides a map of which options the cofigurer supports and their class type.
      *
-     * @return options as map name -> class type.
+     * @param target  the target instance such as {@link org.apache.camel.Endpoint} or {@link org.apache.camel.Component}.
+     * @return configurable options from the target as a Map name -> class type.
+     */
+    Map<String, Object> getAllOptions(Object target);
+
+    /**
+     * Gets the property value
+     *
+     * @param target  the target instance such as {@link org.apache.camel.Endpoint} or {@link org.apache.camel.Component}.
+     * @param name          the property name
+     * @param ignoreCase    whether to ignore case for matching the property name
+     * @return the property value
      */
-    Map<String, Object> options();
+    Object getOptionValue(Object target, String name, boolean ignoreCase);
 
 }
diff --git a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PropertyConfigurerGenerator.java b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PropertyConfigurerGenerator.java
index 86229bf..5a26889 100644
--- a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PropertyConfigurerGenerator.java
+++ b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PropertyConfigurerGenerator.java
@@ -40,7 +40,7 @@ public final class PropertyConfigurerGenerator {
         w.write("\n");
         w.write("import org.apache.camel.CamelContext;\n");
         w.write("import org.apache.camel.spi.GeneratedPropertyConfigurer;\n");
-        w.write("import org.apache.camel.spi.PropertyOptionsConfigurer;\n");
+        w.write("import org.apache.camel.spi.PropertyConfigurerGetter;\n");
         w.write("import org.apache.camel.util.CaseInsensitiveMap;\n");
         w.write("import "  + pfqn + ";\n");
         w.write("\n");
@@ -48,7 +48,7 @@ public final class PropertyConfigurerGenerator {
         w.write(" * " + AbstractGeneratorMojo.GENERATED_MSG + "\n");
         w.write(" */\n");
         w.write("@SuppressWarnings(\"unchecked\")\n");
-        w.write("public class " + cn + " extends " + psn + " implements GeneratedPropertyConfigurer, PropertyOptionsConfigurer {\n");
+        w.write("public class " + cn + " extends " + psn + " implements GeneratedPropertyConfigurer, PropertyConfigurerGetter {\n");
         w.write("\n");
         if (!options.isEmpty() || !hasSuper) {
 
@@ -88,12 +88,12 @@ public final class PropertyConfigurerGenerator {
             }
             w.write("    }\n");
 
-            // generate API that returns which
+            // generate API that returns all the options
             w.write("\n");
             w.write("    @Override\n");
-            w.write("    public Map<String, Object> options() {\n");
+            w.write("    public Map<String, Object> getAllOptions(Object target) {\n");
             if (hasSuper) {
-                w.write("        Map<String, Object> answer = super.options();\n");
+                w.write("        Map<String, Object> answer = super.getAllOptions(target);\n");
             } else {
                 w.write("        Map<String, Object> answer = new CaseInsensitiveMap();\n");
             }
@@ -110,6 +110,31 @@ public final class PropertyConfigurerGenerator {
                 w.write("        return answer;\n");
                 w.write("    }\n");
             }
+
+            // generate API for getting a property
+            w.write("\n");
+            w.write("    @Override\n");
+            w.write("    public Object getOptionValue(Object obj, String name, boolean ignoreCase) {\n");
+            if (!options.isEmpty()) {
+                w.write("        " + en + " target = (" + en + ") obj;\n");
+                w.write("        switch (ignoreCase ? name.toLowerCase() : name) {\n");
+                for (BaseOptionModel option : options) {
+                    String getOrSet = option.getName();
+                    getOrSet = Character.toUpperCase(getOrSet.charAt(0)) + getOrSet.substring(1);
+                    String getterLambda = getterLambda(getOrSet, option.getJavaType(), option.getConfigurationField(), component);
+                    if (!option.getName().toLowerCase().equals(option.getName())) {
+                        w.write(String.format("        case \"%s\":\n", option.getName().toLowerCase()));
+                    }
+                    w.write(String.format("        case \"%s\": %s; return true;\n", option.getName(), getterLambda));
+                }
+                if (hasSuper) {
+                    w.write("        default: return super.getOptionValue(obj, name, ignoreCase);\n");
+                } else {
+                    w.write("        default: return null;\n");
+                }
+                w.write("        }\n");
+            }
+            w.write("    }\n");
         }
 
         w.write("}\n");
@@ -141,6 +166,21 @@ public final class PropertyConfigurerGenerator {
         return String.format("%s(property(camelContext, %s.class, value))", getOrSet, type);
     }
 
+    private static String getterLambda(String getOrSet, String type, String configurationField, boolean component) {
+        String prefix = "boolean".equals(type) ? "is" : "get";
+        if (configurationField != null) {
+            if (component) {
+                getOrSet = "getOrCreateConfiguration(target)." + prefix + getOrSet;
+            } else {
+                getOrSet = "target.get" + Character.toUpperCase(configurationField.charAt(0)) + configurationField.substring(1) + "()." + prefix + getOrSet;
+            }
+        } else {
+            getOrSet = "target." + prefix + getOrSet;
+        }
+
+        return getOrSet + "()";
+    }
+
     private static String createGetOrCreateConfiguration(String targetClass, String configurationClass, String configurationField) {
         String getter = "get" + Character.toUpperCase(configurationField.charAt(0)) + configurationField.substring(1);
         String setter = "set" + Character.toUpperCase(configurationField.charAt(0)) + configurationField.substring(1);