This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push: new 4c904aa CAMEL-17793: camel-core - EIPs better support for ref beans with #bean #class #type syntax 4c904aa is described below commit 4c904aa0a0ab7acb00b53ff4e6c88bcdbec4fd6a Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Tue Mar 15 11:01:35 2022 +0100 CAMEL-17793: camel-core - EIPs better support for ref beans with #bean #class #type syntax --- .../hystrix/processor/HystrixReifier.java | 3 +- .../faulttolerance/FaultToleranceReifier.java | 4 +- .../component/resilience4j/ResilienceReifier.java | 4 +- .../spring/spi/TransactionErrorHandlerReifier.java | 2 +- .../camel/reifier/AbstractPolicyReifier.java | 2 +- .../org/apache/camel/reifier/AbstractReifier.java | 112 +++------------------ .../org/apache/camel/reifier/AggregateReifier.java | 3 +- .../apache/camel/reifier/ClaimCheckReifier.java | 2 +- .../java/org/apache/camel/reifier/LogReifier.java | 2 +- .../org/apache/camel/reifier/MulticastReifier.java | 2 +- .../org/apache/camel/reifier/ProcessorReifier.java | 8 +- .../apache/camel/reifier/RecipientListReifier.java | 2 +- .../org/apache/camel/reifier/SplitReifier.java | 2 +- .../camel/reifier/ThrowExceptionReifier.java | 2 +- .../errorhandler/DefaultErrorHandlerReifier.java | 2 +- .../reifier/errorhandler/ErrorHandlerReifier.java | 2 +- .../transformer/CustomTransformeReifier.java | 2 +- .../transformer/EndpointTransformeReifier.java | 2 +- .../reifier/validator/CustomValidatorReifier.java | 2 +- .../validator/EndpointValidatorReifier.java | 2 +- .../camel/processor/ProcessRefBeanIdTest.java | 47 +++++++++ .../camel/processor/ProcessRefBeanTypeTest.java | 47 +++++++++ .../apache/camel/processor/ProcessRefHashTest.java | 47 +++++++++ .../org/apache/camel/support/EndpointHelper.java | 53 +++++++++- 24 files changed, 233 insertions(+), 123 deletions(-) diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixReifier.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixReifier.java index c9e3b75..979cf70 100644 --- a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixReifier.java +++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixReifier.java @@ -235,7 +235,8 @@ public class HystrixReifier extends ProcessorReifier<CircuitBreakerDefinition> { // camel context takes the precedence over those in the registry loadProperties(properties, Suppliers.firstNotNull( () -> camelContext.getExtension(Model.class).getHystrixConfiguration(null), - () -> lookup(HystrixConstants.DEFAULT_HYSTRIX_CONFIGURATION_ID, HystrixConfigurationDefinition.class)), + () -> lookupByNameAndType(HystrixConstants.DEFAULT_HYSTRIX_CONFIGURATION_ID, + HystrixConfigurationDefinition.class)), configurer); // Extract properties from referenced configuration, the one configured diff --git a/components/camel-microprofile/camel-microprofile-fault-tolerance/src/main/java/org/apache/camel/component/microprofile/faulttolerance/FaultToleranceReifier.java b/components/camel-microprofile/camel-microprofile-fault-tolerance/src/main/java/org/apache/camel/component/microprofile/faulttolerance/FaultToleranceReifier.java index 9a6c4fe..866a6ac 100644 --- a/components/camel-microprofile/camel-microprofile-fault-tolerance/src/main/java/org/apache/camel/component/microprofile/faulttolerance/FaultToleranceReifier.java +++ b/components/camel-microprofile/camel-microprofile-fault-tolerance/src/main/java/org/apache/camel/component/microprofile/faulttolerance/FaultToleranceReifier.java @@ -116,7 +116,7 @@ public class FaultToleranceReifier extends ProcessorReifier<CircuitBreakerDefini if (config.getBulkheadExecutorService() != null) { String ref = config.getBulkheadExecutorService(); boolean shutdownThreadPool = false; - ExecutorService executorService = lookup(ref, ExecutorService.class); + ExecutorService executorService = lookupByNameAndType(ref, ExecutorService.class); if (executorService == null) { executorService = lookupExecutorServiceRef("CircuitBreaker", definition, ref); shutdownThreadPool = true; @@ -141,7 +141,7 @@ public class FaultToleranceReifier extends ProcessorReifier<CircuitBreakerDefini // camel context takes the precedence over those in the registry loadProperties(properties, Suppliers.firstNotNull( () -> camelContext.getExtension(Model.class).getFaultToleranceConfiguration(null), - () -> lookup(FaultToleranceConstants.DEFAULT_FAULT_TOLERANCE_CONFIGURATION_ID, + () -> lookupByNameAndType(FaultToleranceConstants.DEFAULT_FAULT_TOLERANCE_CONFIGURATION_ID, FaultToleranceConfigurationDefinition.class)), configurer); diff --git a/components/camel-resilience4j/src/main/java/org/apache/camel/component/resilience4j/ResilienceReifier.java b/components/camel-resilience4j/src/main/java/org/apache/camel/component/resilience4j/ResilienceReifier.java index 780f490..ecdbccd 100644 --- a/components/camel-resilience4j/src/main/java/org/apache/camel/component/resilience4j/ResilienceReifier.java +++ b/components/camel-resilience4j/src/main/java/org/apache/camel/component/resilience4j/ResilienceReifier.java @@ -153,7 +153,7 @@ public class ResilienceReifier extends ProcessorReifier<CircuitBreakerDefinition if (config.getTimeoutExecutorService() != null) { String ref = config.getTimeoutExecutorService(); boolean shutdownThreadPool = false; - ExecutorService executorService = lookup(ref, ExecutorService.class); + ExecutorService executorService = lookupByNameAndType(ref, ExecutorService.class); if (executorService == null) { executorService = lookupExecutorServiceRef("CircuitBreaker", definition, ref); shutdownThreadPool = true; @@ -178,7 +178,7 @@ public class ResilienceReifier extends ProcessorReifier<CircuitBreakerDefinition // camel context takes the precedence over those in the registry loadProperties(properties, Suppliers.firstNotNull( () -> camelContext.getExtension(Model.class).getResilience4jConfiguration(null), - () -> lookup(ResilienceConstants.DEFAULT_RESILIENCE_CONFIGURATION_ID, + () -> lookupByNameAndType(ResilienceConstants.DEFAULT_RESILIENCE_CONFIGURATION_ID, Resilience4jConfigurationDefinition.class)), configurer); diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/TransactionErrorHandlerReifier.java b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/TransactionErrorHandlerReifier.java index d9253a9..763a1b6 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/TransactionErrorHandlerReifier.java +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/TransactionErrorHandlerReifier.java @@ -55,7 +55,7 @@ public class TransactionErrorHandlerReifier extends DefaultErrorHandlerReifier<T } if (transactionTemplate == null) { - TransactedPolicy policy = lookup(PROPAGATION_REQUIRED, TransactedPolicy.class); + TransactedPolicy policy = lookupByNameAndType(PROPAGATION_REQUIRED, TransactedPolicy.class); if (policy instanceof SpringTransactionPolicy) { transactionTemplate = ((SpringTransactionPolicy) policy).getTransactionTemplate(); } diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractPolicyReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractPolicyReifier.java index 8eebc2a..1850949 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractPolicyReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractPolicyReifier.java @@ -73,7 +73,7 @@ public abstract class AbstractPolicyReifier<T extends ProcessorDefinition<?>> ex // for transacted routing try the default REQUIRED name if (type == TransactedPolicy.class) { // still not found try with the default name PROPAGATION_REQUIRED - answer = lookup(PROPAGATION_REQUIRED, TransactedPolicy.class); + answer = lookupByNameAndType(PROPAGATION_REQUIRED, TransactedPolicy.class); } // this logic only applies if we are a transacted policy diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractReifier.java index 0e10011..518184d 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AbstractReifier.java @@ -27,15 +27,13 @@ import org.apache.camel.NoSuchBeanException; import org.apache.camel.NoSuchEndpointException; import org.apache.camel.Predicate; import org.apache.camel.Route; -import org.apache.camel.RuntimeCamelException; import org.apache.camel.model.ExpressionSubElementDefinition; import org.apache.camel.model.language.ExpressionDefinition; import org.apache.camel.reifier.language.ExpressionReifier; import org.apache.camel.spi.BeanRepository; import org.apache.camel.support.CamelContextHelper; -import org.apache.camel.support.PropertyBindingSupport; +import org.apache.camel.support.EndpointHelper; import org.apache.camel.util.ObjectHelper; -import org.apache.camel.util.StringHelper; public abstract class AbstractReifier implements BeanRepository { @@ -141,48 +139,34 @@ public abstract class AbstractReifier implements BeanRepository { return camelContext.getRegistry(); } - public <T> T mandatoryLookup(String name, Class<T> beanType) { - // lookup in registry first - Object obj = CamelContextHelper.lookup(camelContext, name, beanType); + public <T> T mandatoryLookup(String name, Class<T> type) { + Object obj = lookupByNameAndType(name, type); if (obj == null) { - // fallback in case the name uses #class #type or other syntax - obj = lookupByName(name); + throw new NoSuchBeanException(name, type.getName()); } - if (obj != null) { - obj = camelContext.getTypeConverter().convertTo(beanType, obj); - } - if (obj == null) { - throw new NoSuchBeanException(name, beanType.getName()); - } - return beanType.cast(obj); + return type.cast(obj); } @Override public Object lookupByName(String name) { - if (name != null && name.startsWith("#class:")) { - return createBean(name, Object.class); - } else if (name != null && name.startsWith("#type:")) { - return lookupBean(name, Object.class); - } else { - return getRegistry().lookupByName(name); + if (name == null) { + return null; } - } - public <T> T lookup(String name, Class<T> type) { - if (name != null && name.startsWith("#class:")) { - return createBean(name, type); - } else if (name != null && name.startsWith("#type:")) { - return lookupBean(name, type); + if (EndpointHelper.isReferenceParameter(name)) { + return EndpointHelper.resolveReferenceParameter(camelContext, name, Object.class, false); } else { - return lookupByNameAndType(name, type); + return getRegistry().lookupByName(name); } } public <T> T lookupByNameAndType(String name, Class<T> type) { - if (name != null && name.startsWith("#class:")) { - return createBean(name, type); - } else if (name != null && name.startsWith("#type:")) { - return lookupBean(name, type); + if (name == null) { + return null; + } + + if (EndpointHelper.isReferenceParameter(name)) { + return EndpointHelper.resolveReferenceParameter(camelContext, name, type, false); } else { return getRegistry().lookupByNameAndType(name, type); } @@ -207,68 +191,4 @@ public abstract class AbstractReifier implements BeanRepository { return CamelContextHelper.getMandatoryEndpoint(camelContext, uri); } - private <T> T createBean(String name, Class<T> type) { - try { - return doCreateBean(name, type); - } catch (Exception e) { - throw RuntimeCamelException.wrapRuntimeException(e); - } - } - - private <T> T doCreateBean(String name, Class<T> type) throws Exception { - Object answer; - - // if there is a factory method then the class/bean should be created in a different way - String className; - String factoryMethod = null; - String parameters = null; - className = name.substring(7); - if (className.endsWith(")") && className.indexOf('(') != -1) { - parameters = StringHelper.after(className, "("); - parameters = parameters.substring(0, parameters.length() - 1); // clip last ) - className = StringHelper.before(className, "("); - } - if (className != null && className.indexOf('#') != -1) { - factoryMethod = StringHelper.after(className, "#"); - className = StringHelper.before(className, "#"); - } - Class<?> clazz = camelContext.getClassResolver().resolveMandatoryClass(className); - - if (factoryMethod != null && parameters != null) { - answer = PropertyBindingSupport.newInstanceFactoryParameters(camelContext, clazz, factoryMethod, parameters); - } else if (factoryMethod != null) { - answer = camelContext.getInjector().newInstance(type, factoryMethod); - } else if (parameters != null) { - answer = PropertyBindingSupport.newInstanceConstructorParameters(camelContext, clazz, parameters); - } else { - answer = camelContext.getInjector().newInstance(clazz); - } - if (answer == null) { - throw new IllegalStateException("Cannot create bean: " + name); - } - return type.cast(answer); - } - - private <T> T lookupBean(String name, Class<T> type) { - try { - return doLookupBean(name, type); - } catch (Exception e) { - throw RuntimeCamelException.wrapRuntimeException(e); - } - } - - private <T> T doLookupBean(String name, Class<T> type) throws ClassNotFoundException { - Class<?> clazz = camelContext.getClassResolver().resolveMandatoryClass(name.substring(6)); - Set<?> found = getRegistry().findByType(clazz); - if (found == null || found.isEmpty()) { - throw new NoSuchBeanException(null, clazz.getName()); - } else if (found.size() > 1) { - throw new NoSuchBeanException( - "Found " + found.size() + " beans of type: " + clazz + ". Only one bean expected."); - } else { - Object answer = found.iterator().next(); - return type.cast(answer); - } - } - } diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AggregateReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AggregateReifier.java index 925de6c..8ee1e11 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AggregateReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/AggregateReifier.java @@ -93,7 +93,8 @@ public class AggregateReifier extends ProcessorReifier<AggregateDefinition> { ScheduledExecutorService timeoutThreadPool = definition.getTimeoutCheckerExecutorServiceBean(); if (timeoutThreadPool == null && definition.getTimeoutCheckerExecutorService() != null) { // lookup existing thread pool - timeoutThreadPool = lookup(definition.getTimeoutCheckerExecutorService(), ScheduledExecutorService.class); + timeoutThreadPool + = lookupByNameAndType(definition.getTimeoutCheckerExecutorService(), ScheduledExecutorService.class); if (timeoutThreadPool == null) { // then create a thread pool assuming the ref is a thread pool // profile id diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ClaimCheckReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ClaimCheckReifier.java index d1888da..55c1569 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ClaimCheckReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ClaimCheckReifier.java @@ -114,7 +114,7 @@ public class ClaimCheckReifier extends ProcessorReifier<ClaimCheckDefinition> { AggregationStrategy strategy = definition.getAggregationStrategyBean(); String ref = parseString(definition.getAggregationStrategy()); if (strategy == null && ref != null) { - Object aggStrategy = lookup(ref, Object.class); + Object aggStrategy = lookupByName(ref); if (aggStrategy instanceof AggregationStrategy) { strategy = (AggregationStrategy) aggStrategy; } else if (aggStrategy instanceof BiFunction) { diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/LogReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/LogReifier.java index 96b6b14..9354cef 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/LogReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/LogReifier.java @@ -115,7 +115,7 @@ public class LogReifier extends ProcessorReifier<LogDefinition> { private MaskingFormatter getMaskingFormatter() { if (route.isLogMask()) { - MaskingFormatter formatter = lookup(MaskingFormatter.CUSTOM_LOG_MASK_REF, MaskingFormatter.class); + MaskingFormatter formatter = lookupByNameAndType(MaskingFormatter.CUSTOM_LOG_MASK_REF, MaskingFormatter.class); if (formatter == null) { formatter = new DefaultMaskingFormatter(); } diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/MulticastReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/MulticastReifier.java index 674cd05..2f638e8 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/MulticastReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/MulticastReifier.java @@ -85,7 +85,7 @@ public class MulticastReifier extends ProcessorReifier<MulticastDefinition> { AggregationStrategy strategy = definition.getAggregationStrategyBean(); String ref = parseString(definition.getAggregationStrategy()); if (strategy == null && ref != null) { - Object aggStrategy = lookup(ref, Object.class); + Object aggStrategy = lookupByName(ref); if (aggStrategy instanceof AggregationStrategy) { strategy = (AggregationStrategy) aggStrategy; } else if (aggStrategy instanceof BiFunction) { diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ProcessorReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ProcessorReifier.java index 1f6a3fe..55d3710 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ProcessorReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ProcessorReifier.java @@ -334,7 +334,7 @@ public abstract class ProcessorReifier<T extends ProcessorDefinition<?>> extends // no there is a custom thread pool configured return false; } else if (definition.getExecutorServiceRef() != null) { - ExecutorService answer = lookup(definition.getExecutorServiceRef(), ExecutorService.class); + ExecutorService answer = lookupByNameAndType(definition.getExecutorServiceRef(), ExecutorService.class); // if no existing thread pool, then we will have to create a new // thread pool return answer == null; @@ -470,7 +470,7 @@ public abstract class ProcessorReifier<T extends ProcessorDefinition<?>> extends ObjectHelper.notNull(executorServiceRef, "executorServiceRef"); // lookup in registry first and use existing thread pool if exists - ScheduledExecutorService answer = lookup(executorServiceRef, ScheduledExecutorService.class); + ScheduledExecutorService answer = lookupByNameAndType(executorServiceRef, ScheduledExecutorService.class); if (answer == null) { // then create a thread pool assuming the ref is a thread pool // profile id @@ -502,7 +502,7 @@ public abstract class ProcessorReifier<T extends ProcessorDefinition<?>> extends ObjectHelper.notNull(executorServiceRef, "executorServiceRef"); // lookup in registry first and use existing thread pool if exists - ExecutorService answer = lookup(executorServiceRef, ExecutorService.class); + ExecutorService answer = lookupByNameAndType(executorServiceRef, ExecutorService.class); if (answer == null) { // then create a thread pool assuming the ref is a thread pool // profile id @@ -899,7 +899,7 @@ public abstract class ProcessorReifier<T extends ProcessorDefinition<?>> extends public AggregationStrategy getConfiguredAggregationStrategy(AggregationStrategyAwareDefinition definition) { AggregationStrategy strategy = definition.getAggregationStrategyBean(); if (strategy == null && definition.getAggregationStrategyRef() != null) { - Object aggStrategy = lookup(definition.getAggregationStrategyRef(), Object.class); + Object aggStrategy = lookupByName(definition.getAggregationStrategyRef()); if (aggStrategy instanceof AggregationStrategy) { strategy = (AggregationStrategy) aggStrategy; } else if (aggStrategy instanceof BiFunction) { diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RecipientListReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RecipientListReifier.java index 4d114a1..bfc3ad6 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RecipientListReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/RecipientListReifier.java @@ -114,7 +114,7 @@ public class RecipientListReifier extends ProcessorReifier<RecipientListDefiniti AggregationStrategy strategy = definition.getAggregationStrategyBean(); String ref = parseString(definition.getAggregationStrategy()); if (strategy == null && ref != null) { - Object aggStrategy = lookup(ref, Object.class); + Object aggStrategy = lookupByName(ref); if (aggStrategy instanceof AggregationStrategy) { strategy = (AggregationStrategy) aggStrategy; } else if (aggStrategy instanceof BiFunction) { diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/SplitReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/SplitReifier.java index c223480..471beee 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/SplitReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/SplitReifier.java @@ -82,7 +82,7 @@ public class SplitReifier extends ExpressionReifier<SplitDefinition> { private AggregationStrategy createAggregationStrategy() { AggregationStrategy strategy = definition.getAggregationStrategyBean(); if (strategy == null && definition.getAggregationStrategy() != null) { - Object aggStrategy = lookup(definition.getAggregationStrategy(), Object.class); + Object aggStrategy = lookupByName(definition.getAggregationStrategy()); if (aggStrategy instanceof AggregationStrategy) { strategy = (AggregationStrategy) aggStrategy; } else if (aggStrategy instanceof BiFunction) { diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ThrowExceptionReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ThrowExceptionReifier.java index 51e0924..7b68343 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ThrowExceptionReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/ThrowExceptionReifier.java @@ -33,7 +33,7 @@ public class ThrowExceptionReifier extends ProcessorReifier<ThrowExceptionDefini Exception exception = definition.getException(); String ref = parseString(definition.getRef()); if (exception == null && ref != null) { - exception = lookup(ref, Exception.class); + exception = lookupByNameAndType(ref, Exception.class); } Class<? extends Exception> exceptionClass = definition.getExceptionClass(); diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/errorhandler/DefaultErrorHandlerReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/errorhandler/DefaultErrorHandlerReifier.java index 2becbd3..445b69a 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/errorhandler/DefaultErrorHandlerReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/errorhandler/DefaultErrorHandlerReifier.java @@ -60,7 +60,7 @@ public class DefaultErrorHandlerReifier<T extends DefaultErrorHandlerProperties> // camel context will shutdown the executor when it shutdown so no // need to shut it down when stopping if (executorServiceRef != null) { - executorService = lookup(executorServiceRef, ScheduledExecutorService.class); + executorService = lookupByNameAndType(executorServiceRef, ScheduledExecutorService.class); if (executorService == null) { ExecutorServiceManager manager = camelContext.getExecutorServiceManager(); ThreadPoolProfile profile = manager.getThreadPoolProfile(executorServiceRef); diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/errorhandler/ErrorHandlerReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/errorhandler/ErrorHandlerReifier.java index 7ac9428..43e9e18 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/errorhandler/ErrorHandlerReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/errorhandler/ErrorHandlerReifier.java @@ -349,7 +349,7 @@ public abstract class ErrorHandlerReifier<T extends ErrorHandlerFactory> extends protected <T> T getBean(Class<T> clazz, T bean, String ref) { if (bean == null && ref != null) { - bean = lookup(ref, clazz); + bean = lookupByNameAndType(ref, clazz); } return bean; } diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/transformer/CustomTransformeReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/transformer/CustomTransformeReifier.java index ae3531d..493d846 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/transformer/CustomTransformeReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/transformer/CustomTransformeReifier.java @@ -35,7 +35,7 @@ public class CustomTransformeReifier extends TransformerReifier<CustomTransforme Transformer transformer; String ref = parseString(definition.getRef()); if (ref != null) { - transformer = lookup(ref, Transformer.class); + transformer = lookupByNameAndType(ref, Transformer.class); if (transformer == null) { throw new IllegalArgumentException("Cannot find transformer with ref:" + definition.getRef()); } diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/transformer/EndpointTransformeReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/transformer/EndpointTransformeReifier.java index bb593ee..97d1521 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/transformer/EndpointTransformeReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/transformer/EndpointTransformeReifier.java @@ -35,7 +35,7 @@ public class EndpointTransformeReifier extends TransformerReifier<EndpointTransf protected Transformer doCreateTransformer() { Endpoint endpoint = definition.getUri() != null ? camelContext.getEndpoint(definition.getUri()) - : lookup(parseString(definition.getRef()), Endpoint.class); + : lookupByNameAndType(parseString(definition.getRef()), Endpoint.class); SendProcessor processor = new SendProcessor(endpoint, ExchangePattern.InOut); return new ProcessorTransformer(camelContext).setProcessor(processor) .setModel(parseString(definition.getScheme())) diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/validator/CustomValidatorReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/validator/CustomValidatorReifier.java index 29bb6bc1..360329e3 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/validator/CustomValidatorReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/validator/CustomValidatorReifier.java @@ -34,7 +34,7 @@ public class CustomValidatorReifier extends ValidatorReifier<CustomValidatorDefi } Validator validator; if (definition.getRef() != null) { - validator = lookup(definition.getRef(), Validator.class); + validator = lookupByNameAndType(definition.getRef(), Validator.class); if (validator == null) { throw new IllegalArgumentException("Cannot find validator with ref:" + definition.getRef()); } diff --git a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/validator/EndpointValidatorReifier.java b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/validator/EndpointValidatorReifier.java index f4e4012..f550f32 100644 --- a/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/validator/EndpointValidatorReifier.java +++ b/core/camel-core-reifier/src/main/java/org/apache/camel/reifier/validator/EndpointValidatorReifier.java @@ -34,7 +34,7 @@ public class EndpointValidatorReifier extends ValidatorReifier<EndpointValidator @Override protected Validator doCreateValidator() { Endpoint endpoint = definition.getUri() != null - ? camelContext.getEndpoint(definition.getUri()) : lookup(definition.getRef(), Endpoint.class); + ? camelContext.getEndpoint(definition.getUri()) : lookupByNameAndType(definition.getRef(), Endpoint.class); SendProcessor processor = new SendProcessor(endpoint, ExchangePattern.InOut); return new ProcessorValidator(camelContext).setProcessor(processor).setType(definition.getType()); } diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/ProcessRefBeanIdTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/ProcessRefBeanIdTest.java new file mode 100644 index 0000000..0e0de69 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/processor/ProcessRefBeanIdTest.java @@ -0,0 +1,47 @@ +/* + * 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.processor; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +public class ProcessRefBeanIdTest extends ContextTestSupport { + + @Test + public void testProcessRefBeanId() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("CamelCamel"); + + template.sendBody("direct:start", "Camel"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + context.getRegistry().bind("myEcho", new EchoProcessor()); + + from("direct:start") + .process("#bean:myEcho") + .to("mock:result"); + } + }; + } +} diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/ProcessRefBeanTypeTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/ProcessRefBeanTypeTest.java new file mode 100644 index 0000000..23582c8 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/processor/ProcessRefBeanTypeTest.java @@ -0,0 +1,47 @@ +/* + * 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.processor; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +public class ProcessRefBeanTypeTest extends ContextTestSupport { + + @Test + public void testProcessRefBeanType() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("CamelCamel"); + + template.sendBody("direct:start", "Camel"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + context.getRegistry().bind("myEcho", new EchoProcessor()); + + from("direct:start") + .process("#type:org.apache.camel.Processor") + .to("mock:result"); + } + }; + } +} diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/ProcessRefHashTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/ProcessRefHashTest.java new file mode 100644 index 0000000..d7d8da4 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/processor/ProcessRefHashTest.java @@ -0,0 +1,47 @@ +/* + * 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.processor; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +public class ProcessRefHashTest extends ContextTestSupport { + + @Test + public void testProcessRefHash() throws Exception { + getMockEndpoint("mock:result").expectedBodiesReceived("CamelCamel"); + + template.sendBody("direct:start", "Camel"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + context.getRegistry().bind("myEcho", new EchoProcessor()); + + from("direct:start") + .process("#myEcho") + .to("mock:result"); + } + }; + } +} diff --git a/core/camel-support/src/main/java/org/apache/camel/support/EndpointHelper.java b/core/camel-support/src/main/java/org/apache/camel/support/EndpointHelper.java index d9ced10..2b85575 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/EndpointHelper.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/EndpointHelper.java @@ -348,14 +348,24 @@ public final class EndpointHelper { * @throws NoSuchBeanException if object was not found in registry and <code>mandatory</code> is <code>true</code>. */ public static <T> T resolveReferenceParameter(CamelContext context, String value, Class<T> type, boolean mandatory) { - // it may refer to a type - if (value.startsWith("#type:")) { + if (value.startsWith("#class:")) { + Object answer; + try { + answer = createBean(context, value, type); + } catch (Exception e) { + throw new NoSuchBeanException(value, e); + } + if (mandatory && answer == null) { + throw new NoSuchBeanException(value); + } + return type.cast(answer); + } else if (value.startsWith("#type:")) { try { Object answer = null; String valueNoHash = value.substring(6); Class<?> clazz = context.getClassResolver().resolveMandatoryClass(valueNoHash); - Set<T> set = context.getRegistry().findByType(type); + Set<?> set = context.getRegistry().findByType(clazz); if (set.size() == 1) { answer = set.iterator().next(); } else if (set.size() > 1) { @@ -365,6 +375,9 @@ public final class EndpointHelper { if (mandatory && answer == null) { throw new NoSuchBeanException(value); } + if (answer != null) { + answer = context.getTypeConverter().convertTo(type, answer); + } return type.cast(answer); } catch (ClassNotFoundException e) { throw new NoSuchBeanException(value, e); @@ -380,6 +393,40 @@ public final class EndpointHelper { } } + private static <T> T createBean(CamelContext camelContext, String name, Class<T> type) throws Exception { + Object answer; + + // if there is a factory method then the class/bean should be created in a different way + String className; + String factoryMethod = null; + String parameters = null; + className = name.substring(7); + if (className.endsWith(")") && className.indexOf('(') != -1) { + parameters = StringHelper.after(className, "("); + parameters = parameters.substring(0, parameters.length() - 1); // clip last ) + className = StringHelper.before(className, "("); + } + if (className != null && className.indexOf('#') != -1) { + factoryMethod = StringHelper.after(className, "#"); + className = StringHelper.before(className, "#"); + } + Class<?> clazz = camelContext.getClassResolver().resolveMandatoryClass(className); + + if (factoryMethod != null && parameters != null) { + answer = PropertyBindingSupport.newInstanceFactoryParameters(camelContext, clazz, factoryMethod, parameters); + } else if (factoryMethod != null) { + answer = camelContext.getInjector().newInstance(type, factoryMethod); + } else if (parameters != null) { + answer = PropertyBindingSupport.newInstanceConstructorParameters(camelContext, clazz, parameters); + } else { + answer = camelContext.getInjector().newInstance(clazz); + } + if (answer == null) { + throw new IllegalStateException("Cannot create bean: " + name); + } + return type.cast(answer); + } + /** * Resolves a reference list parameter by making lookups in the registry. The parameter value must be one of the * following: