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/12/05 14:10:05 UTC

[camel] 02/02: CAMEL-15764: api component should not use reflection when configuring their endpoints.

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 4899ea9d5aaf0b7757411736982e60036e0618b3
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Dec 5 15:09:17 2020 +0100

    CAMEL-15764: api component should not use reflection when configuring their endpoints.
---
 .../camel/component/twilio/TwilioEndpointTest.java | 49 ++++++++++++++++++++++
 .../support/component/AbstractApiEndpoint.java     | 34 ++++++++++++++-
 2 files changed, 81 insertions(+), 2 deletions(-)

diff --git a/components/camel-twilio/src/test/java/org/apache/camel/component/twilio/TwilioEndpointTest.java b/components/camel-twilio/src/test/java/org/apache/camel/component/twilio/TwilioEndpointTest.java
new file mode 100644
index 0000000..62f7f3e
--- /dev/null
+++ b/components/camel-twilio/src/test/java/org/apache/camel/component/twilio/TwilioEndpointTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.component.twilio;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.LoggingLevel;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TwilioEndpointTest extends AbstractTwilioTestSupport {
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
+        ecc.getBeanIntrospection().setLoggingLevel(LoggingLevel.INFO);
+        return context;
+    }
+
+    @Test
+    public void testTwilioEndpoint() throws Exception {
+        // should not use reflection when creating and configuring endpoint
+
+        ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class);
+        long before = ecc.getBeanIntrospection().getInvokedCounter();
+
+        TwilioEndpoint te = context.getEndpoint("twilio:account/fetcher?pathSid=123", TwilioEndpoint.class);
+        AccountEndpointConfiguration aec = (AccountEndpointConfiguration) te.getConfiguration();
+        Assertions.assertEquals("123", aec.getPathSid());
+
+        long after = ecc.getBeanIntrospection().getInvokedCounter();
+        Assertions.assertEquals(before, after);
+    }
+}
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiEndpoint.java b/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiEndpoint.java
index 6e2cd7f..c2d1499 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiEndpoint.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/component/AbstractApiEndpoint.java
@@ -31,8 +31,11 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.Component;
 import org.apache.camel.Consumer;
 import org.apache.camel.spi.ExecutorServiceManager;
+import org.apache.camel.spi.PropertyConfigurer;
 import org.apache.camel.spi.ThreadPoolProfile;
 import org.apache.camel.spi.UriParam;
+import org.apache.camel.support.PropertyBindingSupport;
+import org.apache.camel.support.PropertyConfigurerHelper;
 import org.apache.camel.support.ScheduledPollEndpoint;
 import org.apache.camel.util.ObjectHelper;
 import org.slf4j.Logger;
@@ -97,8 +100,35 @@ public abstract class AbstractApiEndpoint<E extends ApiName, T>
 
     @Override
     public void configureProperties(Map<String, Object> options) {
-        super.configureProperties(options);
-        setProperties(getConfiguration(), options);
+        if (options != null && !options.isEmpty()) {
+            // configure scheduler first
+            configureScheduledPollConsumerProperties(options);
+
+            PropertyConfigurer configurer = getComponent().getEndpointPropertyConfigurer();
+            PropertyConfigurer configurer2
+                    = PropertyConfigurerHelper.resolvePropertyConfigurer(getCamelContext(), getConfiguration());
+
+            // we have a mix of options that are general endpoint and then specialized
+            // so we need to configure first without reflection
+            // use configurer and ignore case as end users may type an option name with mixed case
+            PropertyBindingSupport.build().withConfigurer(configurer)
+                    .withIgnoreCase(true).withReflection(false)
+                    .bind(getCamelContext(), this, options);
+            PropertyBindingSupport.build().withConfigurer(configurer2)
+                    .withIgnoreCase(true).withReflection(false)
+                    .bind(getCamelContext(), getConfiguration(), options);
+
+            // after reflection-free then we fallback to allow reflection
+            // in case some options are still left
+            if (!options.isEmpty()) {
+                PropertyBindingSupport.build().withConfigurer(configurer)
+                        .withIgnoreCase(true).withReflection(true)
+                        .bind(getCamelContext(), this, options);
+                PropertyBindingSupport.build().withConfigurer(configurer2)
+                        .withIgnoreCase(true).withReflection(true)
+                        .bind(getCamelContext(), getConfiguration(), options);
+            }
+        }
 
         // validate and initialize state
         initState();