Search in sources :

Example 1 with ErrorHandlerType

use of org.apache.camel.spring.ErrorHandlerType in project camel by apache.

the class ErrorHandlerDefinitionParser method doParse.

@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
    super.doParse(element, parserContext, builder);
    String id = element.getAttribute("id");
    ErrorHandlerType type = ErrorHandlerType.DefaultErrorHandler;
    if (ObjectHelper.isNotEmpty(element.getAttribute("type"))) {
        type = ErrorHandlerType.valueOf(element.getAttribute("type"));
    }
    if (type.equals(ErrorHandlerType.DefaultErrorHandler) || type.equals(ErrorHandlerType.DeadLetterChannel) || type.equals(ErrorHandlerType.TransactionErrorHandler)) {
        NodeList list = element.getChildNodes();
        int size = list.getLength();
        for (int i = 0; i < size; i++) {
            Node child = list.item(i);
            if (child instanceof Element) {
                Element childElement = (Element) child;
                String localName = child.getLocalName();
                // set the redeliveryPolicy
                if (localName.equals("redeliveryPolicy")) {
                    // cannot have redeliveryPolicyRef attribute as well, only one is allowed
                    if (ObjectHelper.isNotEmpty(element.getAttribute("redeliveryPolicyRef"))) {
                        throw new IllegalArgumentException("Cannot set both redeliveryPolicyRef and redeliveryPolicy," + " only one allowed, in error handler with id: " + id);
                    }
                    BeanDefinition redeliveryPolicyDefinition = redeliveryPolicyParser.parse(childElement, parserContext);
                    builder.addPropertyValue(localName, redeliveryPolicyDefinition);
                }
            }
        }
        parserRefAttribute(element, "onRedeliveryRef", "onRedelivery", builder);
        parserRefAttribute(element, "onRetryWhileRef", "onRetryWhile", builder);
        parserRefAttribute(element, "onPrepareFailureRef", "onPrepareFailure", builder);
        parserRefAttribute(element, "onExceptionOccurredRef", "onExceptionOccurred", builder);
        parserRefAttribute(element, "redeliveryPolicyRef", "redeliveryPolicy", builder);
        if (type.equals(ErrorHandlerType.TransactionErrorHandler)) {
            parserRefAttribute(element, "transactionTemplateRef", "transactionTemplate", builder);
            parserRefAttribute(element, "transactionManagerRef", "transactionManager", builder);
        }
    }
    // validate attributes according to type
    String deadLetterUri = element.getAttribute("deadLetterUri");
    if (ObjectHelper.isNotEmpty(deadLetterUri) && !type.equals(ErrorHandlerType.DeadLetterChannel)) {
        throw new IllegalArgumentException("Attribute deadLetterUri can only be used if type is " + ErrorHandlerType.DeadLetterChannel.name() + ", in error handler with id: " + id);
    }
    String deadLetterHandleNewException = element.getAttribute("deadLetterHandleNewException");
    if (ObjectHelper.isNotEmpty(deadLetterHandleNewException) && !type.equals(ErrorHandlerType.DeadLetterChannel)) {
        throw new IllegalArgumentException("Attribute deadLetterHandleNewException can only be used if type is " + ErrorHandlerType.DeadLetterChannel.name() + ", in error handler with id: " + id);
    }
    String transactionTemplateRef = element.getAttribute("transactionTemplateRef");
    if (ObjectHelper.isNotEmpty(transactionTemplateRef) && !type.equals(ErrorHandlerType.TransactionErrorHandler)) {
        throw new IllegalArgumentException("Attribute transactionTemplateRef can only be used if type is " + ErrorHandlerType.TransactionErrorHandler.name() + ", in error handler with id: " + id);
    }
    String transactionManagerRef = element.getAttribute("transactionManagerRef");
    if (ObjectHelper.isNotEmpty(transactionManagerRef) && !type.equals(ErrorHandlerType.TransactionErrorHandler)) {
        throw new IllegalArgumentException("Attribute transactionManagerRef can only be used if type is " + ErrorHandlerType.TransactionErrorHandler.name() + ", in error handler with id: " + id);
    }
    String rollbackLoggingLevel = element.getAttribute("rollbackLoggingLevel");
    if (ObjectHelper.isNotEmpty(rollbackLoggingLevel) && (!type.equals(ErrorHandlerType.TransactionErrorHandler))) {
        throw new IllegalArgumentException("Attribute rollbackLoggingLevel can only be used if type is " + ErrorHandlerType.TransactionErrorHandler.name() + ", in error handler with id: " + id);
    }
    String useOriginalMessage = element.getAttribute("useOriginalMessage");
    if (ObjectHelper.isNotEmpty(useOriginalMessage) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) {
        throw new IllegalArgumentException("Attribute useOriginalMessage is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String onRedeliveryRef = element.getAttribute("onRedeliveryRef");
    if (ObjectHelper.isNotEmpty(onRedeliveryRef) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) {
        throw new IllegalArgumentException("Attribute onRedeliveryRef is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String onExceptionOccurredRef = element.getAttribute("onExceptionOccurredRef");
    if (ObjectHelper.isNotEmpty(onExceptionOccurredRef) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) {
        throw new IllegalArgumentException("Attribute onExceptionOccurredRef is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String onPrepareFailureRef = element.getAttribute("onPrepareFailureRef");
    if (ObjectHelper.isNotEmpty(onPrepareFailureRef) && (type.equals(ErrorHandlerType.TransactionErrorHandler) || type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) {
        throw new IllegalArgumentException("Attribute onPrepareFailureRef is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String retryWhileRef = element.getAttribute("retryWhileRef");
    if (ObjectHelper.isNotEmpty(retryWhileRef) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) {
        throw new IllegalArgumentException("Attribute retryWhileRef is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String redeliveryPolicyRef = element.getAttribute("redeliveryPolicyRef");
    if (ObjectHelper.isNotEmpty(redeliveryPolicyRef) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) {
        throw new IllegalArgumentException("Attribute redeliveryPolicyRef is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String executorServiceRef = element.getAttribute("executorServiceRef");
    if (ObjectHelper.isNotEmpty(executorServiceRef) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) {
        throw new IllegalArgumentException("Attribute executorServiceRef is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String logName = element.getAttribute("logName");
    if (ObjectHelper.isNotEmpty(logName) && (!type.equals(ErrorHandlerType.LoggingErrorHandler))) {
        throw new IllegalArgumentException("Attribute logName is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
    String level = element.getAttribute("level");
    if (ObjectHelper.isNotEmpty(level) && (!type.equals(ErrorHandlerType.LoggingErrorHandler))) {
        throw new IllegalArgumentException("Attribute level is not supported by error handler type: " + type.name() + ", in error handler with id: " + id);
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) ErrorHandlerType(org.apache.camel.spring.ErrorHandlerType)

Aggregations

ErrorHandlerType (org.apache.camel.spring.ErrorHandlerType)1 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1