This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch CAMEL-13870 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 4249ce806ff2d7c3004772ac93e8e397e94e5962 Author: Claus Ibsen <[email protected]> AuthorDate: Thu Aug 22 10:24:49 2019 +0200 CAMEL-13870: Fast property configuration of Camel endpoints. Work in progress. --- .../apache/camel/component/log/LogComponent.java | 10 +++---- .../org/apache/camel/spi/BeanIntrospection.java | 32 ++++++++++++++-------- .../impl/engine/DefaultBeanIntrospection.java | 2 ++ .../component/ApiMethodPropertiesHelperTest.java | 4 +-- .../org/apache/camel/support/DefaultComponent.java | 5 +++- .../camel/support/PropertyBindingSupport.java | 2 -- .../camel/support/ScheduledPollEndpoint.java | 3 +- .../support/component/AbstractApiEndpoint.java | 5 ++-- .../component/ApiMethodPropertiesHelper.java | 14 +++++----- 9 files changed, 44 insertions(+), 33 deletions(-) diff --git a/components/camel-log/src/main/java/org/apache/camel/component/log/LogComponent.java b/components/camel-log/src/main/java/org/apache/camel/component/log/LogComponent.java index 1164041..403fc89 100644 --- a/components/camel-log/src/main/java/org/apache/camel/component/log/LogComponent.java +++ b/components/camel-log/src/main/java/org/apache/camel/component/log/LogComponent.java @@ -58,18 +58,18 @@ public class LogComponent extends DefaultComponent { } // first, try to pick up the ExchangeFormatter from the registry - ExchangeFormatter localFormatter = getCamelContext().getRegistry().lookupByNameAndType("logFormatter", ExchangeFormatter.class); - if (localFormatter != null) { - setProperties(localFormatter, parameters); + ExchangeFormatter logFormatter = getCamelContext().getRegistry().lookupByNameAndType("logFormatter", ExchangeFormatter.class); + if (logFormatter != null) { + setProperties(logFormatter, parameters); } else if (exchangeFormatter != null) { // do not set properties, the exchangeFormatter is explicitly set, therefore the // user would have set its properties explicitly too - localFormatter = exchangeFormatter; + logFormatter = exchangeFormatter; } LogEndpoint endpoint = new LogEndpoint(uri, this); endpoint.setLevel(level.name()); - endpoint.setLocalFormatter(localFormatter); + endpoint.setExchangeFormatter(logFormatter); if (providedLogger == null) { endpoint.setLoggerName(remaining); } else { diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/BeanIntrospection.java b/core/camel-api/src/main/java/org/apache/camel/spi/BeanIntrospection.java index 98a5a81..805b98d 100644 --- a/core/camel-api/src/main/java/org/apache/camel/spi/BeanIntrospection.java +++ b/core/camel-api/src/main/java/org/apache/camel/spi/BeanIntrospection.java @@ -24,6 +24,14 @@ import java.util.Set; import org.apache.camel.CamelContext; import org.apache.camel.TypeConverter; +// TODO: Keep only public used methods so we can remove the tech debt + +/** + * Used for introspecting beans properties via Java reflection; such as extracting current property values, + * or updating one or more properties etc. + * + * End users should favour using org.apache.camel.support.PropertyBindingSupport instead. + */ public interface BeanIntrospection { boolean isGetter(Method method); @@ -73,13 +81,13 @@ public interface BeanIntrospection { */ boolean getProperties(Object target, Map<String, Object> properties, String optionPrefix, boolean includeNull); - /** - * Introspects the given class. - * - * @param clazz the class - * @return the introspection result as a {@link ClassInfo} structure. - */ - // TODO: +// /** +// * Introspects the given class. +// * +// * @param clazz the class +// * @return the introspection result as a {@link ClassInfo} structure. +// */ + // TODO: JMX uses this so we should find a way //ClassInfo cacheClass(Class<?> clazz); boolean hasProperties(Map<String, Object> properties, String optionPrefix); @@ -99,13 +107,13 @@ public interface BeanIntrospection { boolean isPropertyIsGetter(Class<?> type, String propertyName); /** - * @deprecated use {@link PropertyBindingSupport} + * @deprecated use org.apache.camel.support.PropertyBindingSupport */ @Deprecated boolean setProperties(Object target, Map<String, Object> properties, String optionPrefix, boolean allowBuilderPattern) throws Exception; /** - * @deprecated use {@link PropertyBindingSupport} + * @deprecated use org.apache.camel.support.PropertyBindingSupport */ @Deprecated boolean setProperties(Object target, Map<String, Object> properties, String optionPrefix) throws Exception; @@ -118,19 +126,19 @@ public interface BeanIntrospection { Map<String, String> extractStringProperties(Map<String, Object> properties); /** - * @deprecated use {@link PropertyBindingSupport} + * @deprecated use org.apache.camel.support.PropertyBindingSupport */ @Deprecated boolean setProperties(CamelContext context, TypeConverter typeConverter, Object target, Map<String, Object> properties) throws Exception; /** - * @deprecated use {@link PropertyBindingSupport} + * @deprecated use org.apache.camel.support.PropertyBindingSupport */ @Deprecated boolean setProperties(TypeConverter typeConverter, Object target, Map<String, Object> properties) throws Exception; /** - * @deprecated use {@link PropertyBindingSupport} + * @deprecated use org.apache.camel.support.PropertyBindingSupport */ @Deprecated boolean setProperties(Object target, Map<String, Object> properties) throws Exception; diff --git a/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultBeanIntrospection.java b/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultBeanIntrospection.java index f9f9b79..768ff50 100644 --- a/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultBeanIntrospection.java +++ b/core/camel-base/src/main/java/org/apache/camel/impl/engine/DefaultBeanIntrospection.java @@ -30,6 +30,8 @@ import org.apache.camel.support.service.ServiceSupport; @SuppressWarnings("deprecation") public class DefaultBeanIntrospection extends ServiceSupport implements BeanIntrospection { + // TODO: Add runtime statistics so we can report how much Java reflection has been in use + @Override public boolean isGetter(Method method) { return IntrospectionSupport.isGetter(method); diff --git a/core/camel-core/src/test/java/org/apache/camel/support/component/ApiMethodPropertiesHelperTest.java b/core/camel-core/src/test/java/org/apache/camel/support/component/ApiMethodPropertiesHelperTest.java index 8f8d8b4..27ea7c9 100644 --- a/core/camel-core/src/test/java/org/apache/camel/support/component/ApiMethodPropertiesHelperTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/support/component/ApiMethodPropertiesHelperTest.java @@ -71,7 +71,7 @@ public class ApiMethodPropertiesHelperTest { endpointConfiguration.setProperty2(VALUE_2); endpointConfiguration.setProperty3(VALUE_3); endpointConfiguration.setProperty4(Boolean.valueOf(VALUE_4)); - propertiesHelper.getEndpointProperties(endpointConfiguration, properties); + propertiesHelper.getEndpointProperties(new DefaultCamelContext(), endpointConfiguration, properties); assertEquals(2, properties.size()); } @@ -80,7 +80,7 @@ public class ApiMethodPropertiesHelperTest { final TestEndpointConfiguration endpointConfiguration = new TestEndpointConfiguration(); endpointConfiguration.setProperty1(VALUE_1); endpointConfiguration.setProperty4(Boolean.valueOf(VALUE_4)); - assertEquals(1, propertiesHelper.getEndpointPropertyNames(endpointConfiguration).size()); + assertEquals(1, propertiesHelper.getEndpointPropertyNames(new DefaultCamelContext(), endpointConfiguration).size()); } @Test 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 01e56f78..e461970 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 @@ -210,7 +210,10 @@ public abstract class DefaultComponent extends ServiceSupport implements Compone return null; } - // setup whether to use basic property binding or not which must be done before we set properties + // inject camel context + endpoint.setCamelContext(getCamelContext()); + + // setup whether to use basic property binding or not which must be done before we set properties boolean basic = getAndRemoveParameter(parameters, "basicPropertyBinding", boolean.class, basicPropertyBinding); if (endpoint instanceof DefaultEndpoint) { ((DefaultEndpoint) endpoint).setBasicPropertyBinding(basic); 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 ccc40ad..1d3e7a9 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 @@ -52,8 +52,6 @@ import static org.apache.camel.util.ObjectHelper.isNotEmpty; * then you specify the method as shown: #class:com.foo.MyClassType#myFactoryMethod</li>. * <li>ignore case - Whether to ignore case for property keys<li> * </ul> - * <p/> - * This implementations reuses parts of {@link IntrospectionSupport}. */ public final class PropertyBindingSupport { diff --git a/core/camel-support/src/main/java/org/apache/camel/support/ScheduledPollEndpoint.java b/core/camel-support/src/main/java/org/apache/camel/support/ScheduledPollEndpoint.java index 4d06813..a5d6c98 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/ScheduledPollEndpoint.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/ScheduledPollEndpoint.java @@ -113,7 +113,8 @@ public abstract class ScheduledPollEndpoint extends DefaultEndpoint { setSchedulerProperties(schedulerProperties); } - String schedulerName = (String) options.get("scheduler"); + // options take precedence + String schedulerName = (String) options.getOrDefault("scheduler", scheduler); if (schedulerName != null) { if ("spring".equals(schedulerName)) { // special for scheduler if its "spring" or "quartz" 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 87e95b8..da4b2c9 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 @@ -32,7 +32,6 @@ import org.apache.camel.spi.ExecutorServiceManager; import org.apache.camel.spi.ThreadPoolProfile; import org.apache.camel.spi.UriParam; import org.apache.camel.support.DefaultEndpoint; -import org.apache.camel.support.EndpointHelper; import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -126,9 +125,9 @@ public abstract class AbstractApiEndpoint<E extends ApiName, T> // compute endpoint property names and values this.endpointPropertyNames = Collections.unmodifiableSet( - getPropertiesHelper().getEndpointPropertyNames(configuration)); + getPropertiesHelper().getEndpointPropertyNames(getCamelContext(), configuration)); final HashMap<String, Object> properties = new HashMap<>(); - getPropertiesHelper().getEndpointProperties(configuration, properties); + getPropertiesHelper().getEndpointProperties(getCamelContext(), configuration, properties); this.endpointProperties = Collections.unmodifiableMap(properties); // get endpoint property names diff --git a/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodPropertiesHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodPropertiesHelper.java index 0aca298..3016a78 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodPropertiesHelper.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/component/ApiMethodPropertiesHelper.java @@ -23,8 +23,9 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; +import org.apache.camel.CamelContext; import org.apache.camel.Exchange; -import org.apache.camel.support.IntrospectionSupport; +import org.apache.camel.ExtendedCamelContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -78,7 +79,7 @@ public abstract class ApiMethodPropertiesHelper<C> { nProperties++; } else if (camelCasePrefix != null && key.startsWith(camelCasePrefix)) { // assuming all property names start with a lowercase character - final String propertyName = String.valueOf(Character.toLowerCase(key.charAt(prefixLength - 1))) + final String propertyName = Character.toLowerCase(key.charAt(prefixLength - 1)) + key.substring(prefixLength); properties.put(propertyName, entry.getValue()); nProperties++; @@ -88,10 +89,9 @@ public abstract class ApiMethodPropertiesHelper<C> { return properties; } - public void getEndpointProperties(Object endpointConfiguration, Map<String, Object> properties) { + public void getEndpointProperties(CamelContext context, Object endpointConfiguration, Map<String, Object> properties) { Set<String> names = null; - // TODO: camel context - if (IntrospectionSupport.getProperties(endpointConfiguration, properties, null, false)) { + if (context.getExtension(ExtendedCamelContext.class).getBeanIntrospection().getProperties(endpointConfiguration, properties, null, false)) { names = properties.keySet(); // remove component config properties so we only have endpoint properties names.removeAll(componentConfigFields); @@ -99,9 +99,9 @@ public abstract class ApiMethodPropertiesHelper<C> { LOG.debug("Found endpoint properties {}", names); } - public Set<String> getEndpointPropertyNames(Object endpointConfiguration) { + public Set<String> getEndpointPropertyNames(CamelContext context, Object endpointConfiguration) { Map<String, Object> properties = new HashMap<>(); - getEndpointProperties(endpointConfiguration, properties); + getEndpointProperties(context, endpointConfiguration, properties); return Collections.unmodifiableSet(properties.keySet()); }
