This is an automated email from the ASF dual-hosted git repository. gnodet pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit eccc13cf676fe823522b7590087d3eae648873d1 Author: Guillaume Nodet <gno...@gmail.com> AuthorDate: Mon May 27 22:40:31 2019 +0200 [CAMEL-13564] Move more methods to ErrorHandlerReifier --- .../cdi/transaction/JtaTransactionPolicy.java | 4 +- .../camel/spring/spi/SpringTransactionPolicy.java | 4 +- .../camel/builder/ErrorHandlerBuilderRef.java | 101 +-------------------- .../org/apache/camel/model/RouteDefinition.java | 3 +- .../reifier/errorhandler/ErrorHandlerReifier.java | 99 ++++++++++++++++++++ .../DefaultManagementObjectNameStrategy.java | 7 +- 6 files changed, 110 insertions(+), 108 deletions(-) diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/transaction/JtaTransactionPolicy.java b/components/camel-cdi/src/main/java/org/apache/camel/cdi/transaction/JtaTransactionPolicy.java index d2dd5ebe..e0e6c46 100644 --- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/transaction/JtaTransactionPolicy.java +++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/transaction/JtaTransactionPolicy.java @@ -87,9 +87,9 @@ public abstract class JtaTransactionPolicy implements TransactedPolicy { // only lookup if there was explicit an error handler builder configured // otherwise its just the "default" that has not explicit been configured // and if so then we can safely replace that with our transacted error handler - if (ErrorHandlerBuilderRef.isErrorHandlerFactoryConfigured(ref)) { + if (ErrorHandlerReifier.isErrorHandlerFactoryConfigured(ref)) { LOG.debug("Looking up ErrorHandlerBuilder with ref: {}", ref); - builder = (ErrorHandlerBuilder) ErrorHandlerBuilderRef.lookupErrorHandlerFactory(routeContext, ref); + builder = (ErrorHandlerBuilder) ErrorHandlerReifier.lookupErrorHandlerFactory(routeContext, ref); } } diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringTransactionPolicy.java b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringTransactionPolicy.java index a67b4f3..a35f9b2 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringTransactionPolicy.java +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/spi/SpringTransactionPolicy.java @@ -80,9 +80,9 @@ public class SpringTransactionPolicy implements TransactedPolicy { // only lookup if there was explicit an error handler builder configured // otherwise its just the "default" that has not explicit been configured // and if so then we can safely replace that with our transacted error handler - if (ErrorHandlerBuilderRef.isErrorHandlerFactoryConfigured(ref)) { + if (ErrorHandlerReifier.isErrorHandlerFactoryConfigured(ref)) { LOG.debug("Looking up ErrorHandlerBuilder with ref: {}", ref); - builder = (ErrorHandlerBuilder)ErrorHandlerBuilderRef.lookupErrorHandlerFactory(routeContext, ref); + builder = (ErrorHandlerBuilder) ErrorHandlerReifier.lookupErrorHandlerFactory(routeContext, ref); } } diff --git a/core/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderRef.java b/core/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderRef.java index 27e4c42..2d854e3 100644 --- a/core/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderRef.java +++ b/core/camel-core/src/main/java/org/apache/camel/builder/ErrorHandlerBuilderRef.java @@ -20,12 +20,9 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.apache.camel.CamelContext; import org.apache.camel.ErrorHandlerFactory; -import org.apache.camel.ExtendedCamelContext; import org.apache.camel.Processor; import org.apache.camel.model.OnExceptionDefinition; -import org.apache.camel.model.RouteDefinition; import org.apache.camel.reifier.errorhandler.ErrorHandlerReifier; import org.apache.camel.spi.RouteContext; import org.apache.camel.util.ObjectHelper; @@ -34,7 +31,6 @@ import org.apache.camel.util.ObjectHelper; * Represents a proxy to an error handler builder which is resolved by named reference */ public class ErrorHandlerBuilderRef extends ErrorHandlerBuilderSupport { - public static final String DEFAULT_ERROR_HANDLER_BUILDER = "CamelDefaultErrorHandlerBuilder"; private final String ref; private final Map<RouteContext, ErrorHandlerBuilder> handlers = new HashMap<>(); private boolean supportTransacted; @@ -88,107 +84,12 @@ public class ErrorHandlerBuilderRef extends ErrorHandlerBuilderSupport { other.supportTransacted = supportTransacted; } - /** - * Lookup the error handler by the given ref - * - * @param routeContext the route context - * @param ref reference id for the error handler - * @return the error handler - */ - public static ErrorHandlerFactory lookupErrorHandlerFactory(RouteContext routeContext, String ref) { - return lookupErrorHandlerFactory(routeContext, ref, true); - } - - /** - * Lookup the error handler by the given ref - * - * @param routeContext the route context - * @param ref reference id for the error handler - * @param mandatory whether the error handler must exists, if not a {@link org.apache.camel.NoSuchBeanException} is thrown - * @return the error handler - */ - public static ErrorHandlerFactory lookupErrorHandlerFactory(RouteContext routeContext, String ref, boolean mandatory) { - ErrorHandlerFactory answer; - - // if the ref is the default then we do not have any explicit error handler configured - // if that is the case then use error handlers configured on the route, as for instance - // the transacted error handler could have been configured on the route so we should use that one - if (!isErrorHandlerFactoryConfigured(ref)) { - // see if there has been configured a route builder on the route - RouteDefinition route = (RouteDefinition) routeContext.getRoute(); - answer = route.getErrorHandlerFactory(); - if (answer == null && route.getErrorHandlerRef() != null) { - answer = routeContext.lookup(route.getErrorHandlerRef(), ErrorHandlerBuilder.class); - } - if (answer == null) { - // fallback to the default error handler if none configured on the route - answer = new DefaultErrorHandlerBuilder(); - } - // check if its also a ref with no error handler configuration like me - if (answer instanceof ErrorHandlerBuilderRef) { - ErrorHandlerBuilderRef other = (ErrorHandlerBuilderRef) answer; - String otherRef = other.getRef(); - if (!isErrorHandlerFactoryConfigured(otherRef)) { - // the other has also no explicit error handler configured then fallback to the handler - // configured on the parent camel context - answer = lookupErrorHandlerFactory(routeContext.getCamelContext()); - } - if (answer == null) { - // the other has also no explicit error handler configured then fallback to the default error handler - // otherwise we could recursive loop forever (triggered by createErrorHandler method) - answer = new DefaultErrorHandlerBuilder(); - } - // inherit the error handlers from the other as they are to be shared - // this is needed by camel-spring when none error handler has been explicit configured - ((ErrorHandlerBuilder)answer).setErrorHandlers(routeContext, other.getErrorHandlers(routeContext)); - } - } else { - // use specific configured error handler - if (mandatory) { - answer = routeContext.mandatoryLookup(ref, ErrorHandlerBuilder.class); - } else { - answer = routeContext.lookup(ref, ErrorHandlerBuilder.class); - } - } - - return answer; - } - - protected static ErrorHandlerFactory lookupErrorHandlerFactory(CamelContext camelContext) { - ErrorHandlerFactory answer = camelContext.adapt(ExtendedCamelContext.class).getErrorHandlerFactory(); - if (answer instanceof ErrorHandlerBuilderRef) { - ErrorHandlerBuilderRef other = (ErrorHandlerBuilderRef) answer; - String otherRef = other.getRef(); - if (isErrorHandlerFactoryConfigured(otherRef)) { - answer = camelContext.getRegistry().lookupByNameAndType(otherRef, ErrorHandlerBuilder.class); - if (answer == null) { - throw new IllegalArgumentException("ErrorHandlerBuilder with id " + otherRef + " not found in registry."); - } - } - } - - return answer; - } - - /** - * Returns whether a specific error handler builder has been configured or not. - * <p/> - * Can be used to test if none has been configured and then install a custom error handler builder - * replacing the default error handler (that would have been used as fallback otherwise). - * <br/> - * This is for instance used by the transacted policy to setup a TransactedErrorHandlerBuilder - * in camel-spring. - */ - public static boolean isErrorHandlerFactoryConfigured(String ref) { - return !DEFAULT_ERROR_HANDLER_BUILDER.equals(ref); - } - public String getRef() { return ref; } private ErrorHandlerBuilder createErrorHandler(RouteContext routeContext) { - ErrorHandlerBuilder handler = (ErrorHandlerBuilder) lookupErrorHandlerFactory(routeContext, getRef()); + ErrorHandlerBuilder handler = (ErrorHandlerBuilder) ErrorHandlerReifier.lookupErrorHandlerFactory(routeContext, getRef()); ObjectHelper.notNull(handler, "error handler '" + ref + "'"); // configure if the handler support transacted diff --git a/core/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java b/core/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java index dcc96ac..c1b4305 100644 --- a/core/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java +++ b/core/camel-core/src/main/java/org/apache/camel/model/RouteDefinition.java @@ -36,6 +36,7 @@ import org.apache.camel.ShutdownRunningTask; import org.apache.camel.builder.ErrorHandlerBuilderRef; import org.apache.camel.model.rest.RestBindingDefinition; import org.apache.camel.model.rest.RestDefinition; +import org.apache.camel.reifier.errorhandler.ErrorHandlerReifier; import org.apache.camel.spi.AsEndpointUri; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.RoutePolicy; @@ -909,7 +910,7 @@ public class RouteDefinition extends ProcessorDefinition<RouteDefinition> { } // return a reference to the default error handler - return new ErrorHandlerBuilderRef(ErrorHandlerBuilderRef.DEFAULT_ERROR_HANDLER_BUILDER); + return new ErrorHandlerBuilderRef(ErrorHandlerReifier.DEFAULT_ERROR_HANDLER_BUILDER); } @XmlTransient diff --git a/core/camel-core/src/main/java/org/apache/camel/reifier/errorhandler/ErrorHandlerReifier.java b/core/camel-core/src/main/java/org/apache/camel/reifier/errorhandler/ErrorHandlerReifier.java index ca15e64..a7649ca 100644 --- a/core/camel-core/src/main/java/org/apache/camel/reifier/errorhandler/ErrorHandlerReifier.java +++ b/core/camel-core/src/main/java/org/apache/camel/reifier/errorhandler/ErrorHandlerReifier.java @@ -23,15 +23,18 @@ import java.util.function.Function; import org.apache.camel.CamelContext; import org.apache.camel.ErrorHandlerFactory; +import org.apache.camel.ExtendedCamelContext; import org.apache.camel.Processor; import org.apache.camel.RuntimeCamelException; import org.apache.camel.builder.DeadLetterChannelBuilder; import org.apache.camel.builder.DefaultErrorHandlerBuilder; +import org.apache.camel.builder.ErrorHandlerBuilder; import org.apache.camel.builder.ErrorHandlerBuilderRef; import org.apache.camel.builder.ErrorHandlerBuilderSupport; import org.apache.camel.builder.NoErrorHandlerBuilder; import org.apache.camel.model.OnExceptionDefinition; import org.apache.camel.model.RedeliveryPolicyDefinition; +import org.apache.camel.model.RouteDefinition; import org.apache.camel.processor.ErrorHandler; import org.apache.camel.processor.errorhandler.ExceptionPolicy; import org.apache.camel.processor.errorhandler.ExceptionPolicy.RedeliveryOption; @@ -44,6 +47,7 @@ import org.apache.camel.util.ObjectHelper; public abstract class ErrorHandlerReifier<T extends ErrorHandlerBuilderSupport> { + public static final String DEFAULT_ERROR_HANDLER_BUILDER = "CamelDefaultErrorHandlerBuilder"; private static final Map<Class<?>, Function<ErrorHandlerFactory, ErrorHandlerReifier<? extends ErrorHandlerFactory>>> ERROR_HANDLERS; static { Map<Class<?>, Function<ErrorHandlerFactory, ErrorHandlerReifier<? extends ErrorHandlerFactory>>> map = new HashMap<>(); @@ -138,6 +142,101 @@ public abstract class ErrorHandlerReifier<T extends ErrorHandlerBuilderSupport> } /** + * Lookup the error handler by the given ref + * + * @param routeContext the route context + * @param ref reference id for the error handler + * @return the error handler + */ + public static ErrorHandlerFactory lookupErrorHandlerFactory(RouteContext routeContext, String ref) { + return lookupErrorHandlerFactory(routeContext, ref, true); + } + + /** + * Lookup the error handler by the given ref + * + * @param routeContext the route context + * @param ref reference id for the error handler + * @param mandatory whether the error handler must exists, if not a {@link org.apache.camel.NoSuchBeanException} is thrown + * @return the error handler + */ + public static ErrorHandlerFactory lookupErrorHandlerFactory(RouteContext routeContext, String ref, boolean mandatory) { + ErrorHandlerFactory answer; + + // if the ref is the default then we do not have any explicit error handler configured + // if that is the case then use error handlers configured on the route, as for instance + // the transacted error handler could have been configured on the route so we should use that one + if (!isErrorHandlerFactoryConfigured(ref)) { + // see if there has been configured a route builder on the route + RouteDefinition route = (RouteDefinition) routeContext.getRoute(); + answer = route.getErrorHandlerFactory(); + if (answer == null && route.getErrorHandlerRef() != null) { + answer = routeContext.lookup(route.getErrorHandlerRef(), ErrorHandlerBuilder.class); + } + if (answer == null) { + // fallback to the default error handler if none configured on the route + answer = new DefaultErrorHandlerBuilder(); + } + // check if its also a ref with no error handler configuration like me + if (answer instanceof ErrorHandlerBuilderRef) { + ErrorHandlerBuilderRef other = (ErrorHandlerBuilderRef) answer; + String otherRef = other.getRef(); + if (!isErrorHandlerFactoryConfigured(otherRef)) { + // the other has also no explicit error handler configured then fallback to the handler + // configured on the parent camel context + answer = lookupErrorHandlerFactory(routeContext.getCamelContext()); + } + if (answer == null) { + // the other has also no explicit error handler configured then fallback to the default error handler + // otherwise we could recursive loop forever (triggered by createErrorHandler method) + answer = new DefaultErrorHandlerBuilder(); + } + // inherit the error handlers from the other as they are to be shared + // this is needed by camel-spring when none error handler has been explicit configured + ((ErrorHandlerBuilderSupport) answer).setErrorHandlers(routeContext, other.getErrorHandlers(routeContext)); + } + } else { + // use specific configured error handler + if (mandatory) { + answer = routeContext.mandatoryLookup(ref, ErrorHandlerBuilder.class); + } else { + answer = routeContext.lookup(ref, ErrorHandlerBuilder.class); + } + } + + return answer; + } + + protected static ErrorHandlerFactory lookupErrorHandlerFactory(CamelContext camelContext) { + ErrorHandlerFactory answer = camelContext.adapt(ExtendedCamelContext.class).getErrorHandlerFactory(); + if (answer instanceof ErrorHandlerBuilderRef) { + ErrorHandlerBuilderRef other = (ErrorHandlerBuilderRef) answer; + String otherRef = other.getRef(); + if (isErrorHandlerFactoryConfigured(otherRef)) { + answer = camelContext.getRegistry().lookupByNameAndType(otherRef, ErrorHandlerBuilder.class); + if (answer == null) { + throw new IllegalArgumentException("ErrorHandlerBuilder with id " + otherRef + " not found in registry."); + } + } + } + + return answer; + } + + /** + * Returns whether a specific error handler builder has been configured or not. + * <p/> + * Can be used to test if none has been configured and then install a custom error handler builder + * replacing the default error handler (that would have been used as fallback otherwise). + * <br/> + * This is for instance used by the transacted policy to setup a TransactedErrorHandlerBuilder + * in camel-spring. + */ + public static boolean isErrorHandlerFactoryConfigured(String ref) { + return !DEFAULT_ERROR_HANDLER_BUILDER.equals(ref); + } + + /** * Creates the error handler * * @param routeContext the route context diff --git a/core/camel-management-impl/src/main/java/org/apache/camel/management/DefaultManagementObjectNameStrategy.java b/core/camel-management-impl/src/main/java/org/apache/camel/management/DefaultManagementObjectNameStrategy.java index ad49c11..8122534 100644 --- a/core/camel-management-impl/src/main/java/org/apache/camel/management/DefaultManagementObjectNameStrategy.java +++ b/core/camel-management-impl/src/main/java/org/apache/camel/management/DefaultManagementObjectNameStrategy.java @@ -54,6 +54,7 @@ import org.apache.camel.management.mbean.ManagedRouteController; import org.apache.camel.management.mbean.ManagedService; import org.apache.camel.management.mbean.ManagedStep; import org.apache.camel.management.mbean.ManagedThreadPool; +import org.apache.camel.reifier.errorhandler.ErrorHandlerReifier; import org.apache.camel.spi.DataFormat; import org.apache.camel.spi.EventNotifier; import org.apache.camel.spi.ManagementObjectNameStrategy; @@ -300,7 +301,7 @@ public class DefaultManagementObjectNameStrategy implements ManagementObjectName // it has not then its an indirection and we should do some work to lookup the real builder ref = builderRef.getRef(); - ErrorHandlerFactory refBuilder = ErrorHandlerBuilderRef.lookupErrorHandlerFactory(routeContext, builderRef.getRef(), false); + ErrorHandlerFactory refBuilder = ErrorHandlerReifier.lookupErrorHandlerFactory(routeContext, builderRef.getRef(), false); if (refBuilder != null) { builder = refBuilder; } @@ -311,8 +312,8 @@ public class DefaultManagementObjectNameStrategy implements ManagementObjectName if (builder instanceof ErrorHandlerBuilderRef) { builderRef = (ErrorHandlerBuilderRef) builder; // does it refer to a non default error handler then do a 2nd lookup - if (!builderRef.getRef().equals(ErrorHandlerBuilderRef.DEFAULT_ERROR_HANDLER_BUILDER)) { - refBuilder = ErrorHandlerBuilderRef.lookupErrorHandlerFactory(routeContext, builderRef.getRef(), false); + if (!builderRef.getRef().equals(ErrorHandlerReifier.DEFAULT_ERROR_HANDLER_BUILDER)) { + refBuilder = ErrorHandlerReifier.lookupErrorHandlerFactory(routeContext, builderRef.getRef(), false); if (refBuilder != null) { ref = builderRef.getRef(); builder = refBuilder;