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/08/24 20:55:59 UTC

[camel] branch master updated: CAMEL-13870: Even faster endpoint and component configurer with switch instead of Map, as suggested by Luca.

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 3fc6f61  CAMEL-13870: Even faster endpoint and component configurer with switch instead of Map, as suggested by Luca.
3fc6f61 is described below

commit 3fc6f61d2b91a3ca01f7541eb509309547c7de79
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Aug 24 22:31:13 2019 +0200

    CAMEL-13870: Even faster endpoint and component configurer with switch instead of Map, as suggested by Luca.
---
 ...gurer.java => GeneratedPropertyConfigurer.java} | 31 ++++++++++------------
 .../org/apache/camel/support/DefaultComponent.java | 10 +++----
 .../camel/support/PropertyBindingSupport.java      | 19 +++++--------
 .../src/main/resources/application.properties      |  4 +--
 .../apt/ComponentPropertyConfigurerGenerator.java  | 23 +++++++---------
 .../tools/apt/EndpointAnnotationProcessor.java     |  4 +--
 .../apt/EndpointPropertyConfigurerGenerator.java   | 24 +++++++----------
 7 files changed, 49 insertions(+), 66 deletions(-)

diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/TriPropertyConfigurer.java b/core/camel-api/src/main/java/org/apache/camel/spi/GeneratedPropertyConfigurer.java
similarity index 51%
rename from core/camel-api/src/main/java/org/apache/camel/spi/TriPropertyConfigurer.java
rename to core/camel-api/src/main/java/org/apache/camel/spi/GeneratedPropertyConfigurer.java
index df870dc..0367da8 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/TriPropertyConfigurer.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/GeneratedPropertyConfigurer.java
@@ -1,13 +1,13 @@
-/**
+/*
  * 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
- * <p>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
+ *
+ *      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.
@@ -16,25 +16,22 @@
  */
 package org.apache.camel.spi;
 
-import java.util.Map;
-
 import org.apache.camel.CamelContext;
-import org.apache.camel.util.function.TriConsumer;
 
 /**
- * Property configurer for Camel {@link org.apache.camel.Endpoint} or {@link org.apache.camel.Component}
- * which allows fast configurations without using Java reflection.
+ * A auto generated {@link PropertyConfigurer} for fast configuration of Camel components & endpoints.
  */
-public interface TriPropertyConfigurer extends PropertyConfigurer {
+public interface GeneratedPropertyConfigurer extends PropertyConfigurer {
 
     /**
-     * To update properties using the tri-function.
-     * 
-     * The key in the map is the property name.
-     * The 1st parameter in the tri-function is {@link CamelContext}
-     * The 2nd parameter in the tri-function is the target object
-     * The 3rd parameter in the tri-function is the value
+     * Configures the property
+     *
+     * @param camelContext  the Camel context
+     * @param target        the target instance such as {@link org.apache.camel.Endpoint} or {@link org.apache.camel.Component}.
+     * @param name          the property name
+     * @param value         the property value
+     * @return <tt>true</tt> if the configurer configured the property, <tt>false</tt> if the property does not exists
      */
-    Map<String, TriConsumer<CamelContext, Object, Object>> getWriteOptions(CamelContext camelContext);
+    boolean configure(CamelContext camelContext, Object target, String name, Object value);
 
 }
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java b/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java
index 4c6aa71..723c48e 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/DefaultComponent.java
@@ -35,10 +35,10 @@ import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.NoFactoryAvailableException;
 import org.apache.camel.ResolveEndpointFailedException;
 import org.apache.camel.component.extension.ComponentExtension;
+import org.apache.camel.spi.GeneratedPropertyConfigurer;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.PropertyConfigurer;
 import org.apache.camel.spi.PropertyConfigurerAware;
-import org.apache.camel.spi.TriPropertyConfigurer;
 import org.apache.camel.support.service.ServiceSupport;
 import org.apache.camel.util.ObjectHelper;
 import org.apache.camel.util.PropertiesHelper;
@@ -59,8 +59,8 @@ public abstract class DefaultComponent extends ServiceSupport implements Compone
 
     private static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/configurer/";
 
-    private volatile TriPropertyConfigurer componentPropertyConfigurer;
-    private volatile TriPropertyConfigurer endpointPropertyConfigurer;
+    private volatile GeneratedPropertyConfigurer componentPropertyConfigurer;
+    private volatile GeneratedPropertyConfigurer endpointPropertyConfigurer;
     private final List<Supplier<ComponentExtension>> extensions = new ArrayList<>();
     private CamelContext camelContext;
 
@@ -343,14 +343,14 @@ public abstract class DefaultComponent extends ServiceSupport implements Compone
                 log.trace("Discovering optional component property configurer class for component: {}", name);
                 Optional<Class<?>> clazz = getCamelContext().adapt(ExtendedCamelContext.class).getFactoryFinder(RESOURCE_PATH)
                         .findOptionalClass(name + "-component", null);
-                clazz.ifPresent(c -> componentPropertyConfigurer = org.apache.camel.support.ObjectHelper.newInstance(c, TriPropertyConfigurer.class));
+                clazz.ifPresent(c -> componentPropertyConfigurer = org.apache.camel.support.ObjectHelper.newInstance(c, GeneratedPropertyConfigurer.class));
                 if (log.isDebugEnabled() && componentPropertyConfigurer != null) {
                     log.debug("Discovered component property configurer: {} -> {}", name, componentPropertyConfigurer);
                 }
                 log.trace("Discovering optional endpoint property configurer class for component: {}", name);
                 clazz = getCamelContext().adapt(ExtendedCamelContext.class).getFactoryFinder(RESOURCE_PATH)
                         .findOptionalClass(name + "-endpoint", null);
-                clazz.ifPresent(c -> endpointPropertyConfigurer = org.apache.camel.support.ObjectHelper.newInstance(c, TriPropertyConfigurer.class));
+                clazz.ifPresent(c -> endpointPropertyConfigurer = org.apache.camel.support.ObjectHelper.newInstance(c, GeneratedPropertyConfigurer.class));
                 if (log.isDebugEnabled() && endpointPropertyConfigurer != null) {
                     log.debug("Discovered endpoint property configurer: {} -> {}", name, endpointPropertyConfigurer);
                 }
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 02794f5..814b089 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
@@ -25,15 +25,13 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
-import java.util.function.Consumer;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.PropertyBindingException;
+import org.apache.camel.spi.GeneratedPropertyConfigurer;
 import org.apache.camel.spi.PropertyConfigurer;
-import org.apache.camel.spi.TriPropertyConfigurer;
 import org.apache.camel.util.StringHelper;
-import org.apache.camel.util.function.TriConsumer;
 
 import static org.apache.camel.util.ObjectHelper.isNotEmpty;
 
@@ -461,11 +459,9 @@ public final class PropertyBindingSupport {
         org.apache.camel.util.ObjectHelper.notNull(properties, "properties");
         boolean rc = false;
 
-        // if there is a property configurer then use it to set the properties
-        if (configurer instanceof TriPropertyConfigurer) {
-            TriPropertyConfigurer tri = (TriPropertyConfigurer) configurer;
+        if (configurer instanceof GeneratedPropertyConfigurer) {
+            GeneratedPropertyConfigurer gen = (GeneratedPropertyConfigurer) configurer;
 
-            Map<String, TriConsumer<CamelContext, Object, Object>> writeProperties = tri.getWriteOptions(camelContext);
             for (Iterator<Map.Entry<String, Object>> iter = properties.entrySet().iterator(); iter.hasNext();) {
                 Map.Entry<String, Object> entry = iter.next();
                 String key = entry.getKey();
@@ -475,12 +471,9 @@ public final class PropertyBindingSupport {
                     // property configurer does not support nested names so skip if the name has a dot
                     valid = key.indexOf('.') == -1;
                 }
-                if (valid && writeProperties.containsKey(key)) {
-                    writeProperties.get(key).accept(camelContext, target, value);
-                    if (removeParameter) {
-                        iter.remove();
-                        rc = true;
-                    }
+                if (valid && removeParameter && gen.configure(camelContext, target, key, value)) {
+                    iter.remove();
+                    rc = true;
                 }
             };
         }
diff --git a/examples/camel-example-main/src/main/resources/application.properties b/examples/camel-example-main/src/main/resources/application.properties
index 242773d..ac155bd 100644
--- a/examples/camel-example-main/src/main/resources/application.properties
+++ b/examples/camel-example-main/src/main/resources/application.properties
@@ -21,8 +21,8 @@ camel.main.name = MyCoolCamel
 camel.main.jmx-enabled = false
 
 # extended runtime statistics about bean introspection usage (java reflection)
-# camel.main.bean-introspection-extended-statistics=true
-# camel.main.bean-introspection-logging-level=INFO
+camel.main.bean-introspection-extended-statistics=true
+camel.main.bean-introspection-logging-level=INFO
 
 # enable tracing
 # camel.main.tracing = true
diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/ComponentPropertyConfigurerGenerator.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/ComponentPropertyConfigurerGenerator.java
index 9bfb41e..3672522 100644
--- a/tooling/apt/src/main/java/org/apache/camel/tools/apt/ComponentPropertyConfigurerGenerator.java
+++ b/tooling/apt/src/main/java/org/apache/camel/tools/apt/ComponentPropertyConfigurerGenerator.java
@@ -32,6 +32,8 @@ import org.apache.camel.tools.apt.model.EndpointOption;
 
 import static org.apache.camel.tools.apt.AnnotationProcessorHelper.dumpExceptionToErrorFile;
 
+// TODO: ComponentPropertyConfigurerGenerator and EndpointPropertyConfigurerGenerator can be merged to one
+
 public final class ComponentPropertyConfigurerGenerator {
 
     private ComponentPropertyConfigurerGenerator() {
@@ -83,31 +85,26 @@ public final class ComponentPropertyConfigurerGenerator {
             w.write("import java.util.Map;\n");
             w.write("\n");
             w.write("import org.apache.camel.CamelContext;\n");
-            w.write("import org.apache.camel.spi.TriPropertyConfigurer;\n");
+            w.write("import org.apache.camel.spi.GeneratedPropertyConfigurer;\n");
             w.write("import org.apache.camel.support.component.PropertyConfigurerSupport;\n");
-            w.write("import org.apache.camel.util.function.TriConsumer;\n");
             w.write("\n");
             w.write("/**\n");
             w.write(" * Source code generated by org.apache.camel:apt\n");
             w.write(" */\n");
             w.write("@SuppressWarnings(\"unchecked\")\n");
-            w.write("public class " + cn + " extends PropertyConfigurerSupport implements TriPropertyConfigurer {\n");
+            w.write("public class " + cn + " extends PropertyConfigurerSupport implements GeneratedPropertyConfigurer {\n");
             w.write("\n");
-            w.write("    private static final Map<String, TriConsumer<CamelContext, Object, Object>> WRITES;\n");
-            w.write("    static {\n");
-            w.write("        Map<String, TriConsumer<CamelContext, Object, Object>> map = new HashMap<>(" + size + ");\n");
+            w.write("    @Override\n");
+            w.write("    public boolean configure(CamelContext camelContext, Object component, String name, Object value) {\n");
+            w.write("        switch (name) {\n");
             for (ComponentOption option : options) {
                 String getOrSet = option.getName();
                 getOrSet = Character.toUpperCase(getOrSet.charAt(0)) + getOrSet.substring(1);
                 String setterLambda = setterLambda(en, getOrSet, option.getType(), option.getConfigurationField());
-                w.write("        map.put(\"" + option.getName() + "\", (camelContext, component, value) -> " + setterLambda + ");\n");
+                w.write(String.format("        case \"%s\": %s; return true;\n", option.getName(), setterLambda));
             }
-            w.write("        WRITES = map;\n");
-            w.write("    }\n");
-            w.write("\n");
-            w.write("    @Override\n");
-            w.write("    public Map<String, TriConsumer<CamelContext, Object, Object>> getWriteOptions(CamelContext camelContext) {\n");
-            w.write("        return WRITES;\n");
+            w.write("            default: return false;\n");
+            w.write("        }\n");
             w.write("    }\n");
             w.write("\n");
             w.write("}\n");
diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java
index 4d7621b..38c5f88 100644
--- a/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java
+++ b/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointAnnotationProcessor.java
@@ -184,7 +184,7 @@ public class EndpointAnnotationProcessor extends AbstractCamelAnnotationProcesso
             // special for activemq and amqp scheme which should reuse jms
             parent = findTypeElement(processingEnv, roundEnv, "org.apache.camel.component.jms.JmsComponentConfigurer");
         } else {
-            parent = findTypeElement(processingEnv, roundEnv, "org.apache.camel.spi.TriPropertyConfigurer");
+            parent = findTypeElement(processingEnv, roundEnv, "org.apache.camel.spi.GeneratedPropertyConfigurer");
         }
         String fqComponentClassName = componentModel.getJavaType();
         String componentClassName = fqComponentClassName.substring(fqComponentClassName.lastIndexOf('.') + 1);
@@ -211,7 +211,7 @@ public class EndpointAnnotationProcessor extends AbstractCamelAnnotationProcesso
             // special for activemq and amqp scheme which should reuse jms
             parent = findTypeElement(processingEnv, roundEnv, "org.apache.camel.component.jms.JmsEndpointConfigurer");
         } else {
-            parent = findTypeElement(processingEnv, roundEnv, "org.apache.camel.spi.TriPropertyConfigurer");
+            parent = findTypeElement(processingEnv, roundEnv, "org.apache.camel.spi.GeneratedPropertyConfigurer");
         }
         String fqEndpointClassName = classElement.getQualifiedName().toString();
         String packageName = fqEndpointClassName.substring(0, fqEndpointClassName.lastIndexOf('.'));
diff --git a/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointPropertyConfigurerGenerator.java b/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointPropertyConfigurerGenerator.java
index a8c5dcc..f289679 100644
--- a/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointPropertyConfigurerGenerator.java
+++ b/tooling/apt/src/main/java/org/apache/camel/tools/apt/EndpointPropertyConfigurerGenerator.java
@@ -31,6 +31,8 @@ import org.apache.camel.tools.apt.model.EndpointOption;
 
 import static org.apache.camel.tools.apt.AnnotationProcessorHelper.dumpExceptionToErrorFile;
 
+// TODO: ComponentPropertyConfigurerGenerator and EndpointPropertyConfigurerGenerator can be merged to one
+
 public final class EndpointPropertyConfigurerGenerator {
 
     private EndpointPropertyConfigurerGenerator() {
@@ -82,31 +84,26 @@ public final class EndpointPropertyConfigurerGenerator {
             w.write("import java.util.Map;\n");
             w.write("\n");
             w.write("import org.apache.camel.CamelContext;\n");
-            w.write("import org.apache.camel.spi.TriPropertyConfigurer;\n");
+            w.write("import org.apache.camel.spi.GeneratedPropertyConfigurer;\n");
             w.write("import org.apache.camel.support.component.PropertyConfigurerSupport;\n");
-            w.write("import org.apache.camel.util.function.TriConsumer;\n");
             w.write("\n");
             w.write("/**\n");
             w.write(" * Source code generated by org.apache.camel:apt\n");
             w.write(" */\n");
             w.write("@SuppressWarnings(\"unchecked\")\n");
-            w.write("public class " + cn + " extends PropertyConfigurerSupport implements TriPropertyConfigurer {\n");
+            w.write("public class " + cn + " extends PropertyConfigurerSupport implements GeneratedPropertyConfigurer {\n");
             w.write("\n");
-            w.write("    private static final Map<String, TriConsumer<CamelContext, Object, Object>> WRITES;\n");
-            w.write("    static {\n");
-            w.write("        Map<String, TriConsumer<CamelContext, Object, Object>> map = new HashMap<>(" + size + ");\n");
+            w.write("    @Override\n");
+            w.write("    public boolean configure(CamelContext camelContext, Object endpoint, String name, Object value) {\n");
+            w.write("        switch (name) {\n");
             for (EndpointOption option : options) {
                 String getOrSet = option.getName();
                 getOrSet = Character.toUpperCase(getOrSet.charAt(0)) + getOrSet.substring(1);
                 String setterLambda = setterLambda(en, getOrSet, option.getType(), option.getConfigurationField());
-                w.write("        map.put(\"" + option.getName() + "\", (camelContext, endpoint, value) -> " + setterLambda + ");\n");
+                w.write(String.format("        case \"%s\": %s; return true;\n", option.getName(), setterLambda));
             }
-            w.write("        WRITES = map;\n");
-            w.write("    }\n");
-            w.write("\n");
-            w.write("    @Override\n");
-            w.write("    public Map<String, TriConsumer<CamelContext, Object, Object>> getWriteOptions(CamelContext camelContext) {\n");
-            w.write("        return WRITES;\n");
+            w.write("            default: return false;\n");
+            w.write("        }\n");
             w.write("    }\n");
             w.write("\n");
             w.write("}\n");
@@ -130,7 +127,6 @@ public final class EndpointPropertyConfigurerGenerator {
             getOrSet = "set" + getOrSet;
         }
 
-        // ((LogEndpoint) endpoint).setGroupSize(property(camelContext, java.lang.Integer.class, value))
         return String.format("((%s) endpoint).%s(property(camelContext, %s.class, value))", en, getOrSet, type);
     }