001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.spring.handler;
018
019import org.w3c.dom.Attr;
020import org.w3c.dom.Element;
021import org.w3c.dom.NamedNodeMap;
022import org.w3c.dom.Node;
023import org.w3c.dom.NodeList;
024
025import org.apache.camel.spring.CamelRedeliveryPolicyFactoryBean;
026import org.apache.camel.spring.ErrorHandlerType;
027import org.apache.camel.util.ObjectHelper;
028import org.springframework.beans.factory.config.BeanDefinition;
029import org.springframework.beans.factory.support.BeanDefinitionBuilder;
030import org.springframework.beans.factory.xml.ParserContext;
031import org.springframework.util.Assert;
032import org.springframework.util.StringUtils;
033
034/**
035 * The DefinitionParser to deal with the ErrorHandler
036 */
037public class ErrorHandlerDefinitionParser extends BeanDefinitionParser {
038    protected BeanDefinitionParser redeliveryPolicyParser = new RedeliveryPolicyDefinitionParser(CamelRedeliveryPolicyFactoryBean.class);
039    
040    public ErrorHandlerDefinitionParser() {
041        // need to override the default
042        super(null, false);
043    }
044
045    @Override
046    protected Class<?> getBeanClass(Element element) {
047        ErrorHandlerType type = ErrorHandlerType.DefaultErrorHandler;
048
049        if (ObjectHelper.isNotEmpty(element.getAttribute("type"))) {
050            type = ErrorHandlerType.valueOf(element.getAttribute("type"));
051        }
052        return type.getTypeAsClass();
053    }
054    
055    @Override
056    protected boolean isEligibleAttribute(String attributeName) {
057        if (attributeName == null || ID_ATTRIBUTE.equals(attributeName)) {
058            return false;
059        }
060        if (attributeName.equals("xmlns") || attributeName.startsWith("xmlns:")) {
061            return false;
062        }
063        // CHECKSTYLE:OFF
064        return !attributeName.equals("type")
065                && !attributeName.equals("onRedeliveryRef")
066                && !attributeName.equals("onRetryWhileRef")
067                && !attributeName.equals("onPrepareFailureRef")
068                && !attributeName.equals("onExceptionOccurredRef")
069                && !attributeName.equals("redeliveryPolicyRef")
070                && !attributeName.equals("transactionTemplateRef")
071                && !attributeName.equals("transactionManagerRef");
072        // CHECKSTYLE:ON
073    }
074
075    @Override
076    protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
077        super.doParse(element, parserContext, builder);
078
079        String id = element.getAttribute("id");
080
081        ErrorHandlerType type = ErrorHandlerType.DefaultErrorHandler;
082        if (ObjectHelper.isNotEmpty(element.getAttribute("type"))) {
083            type = ErrorHandlerType.valueOf(element.getAttribute("type"));
084        }
085        if (type.equals(ErrorHandlerType.DefaultErrorHandler)
086            || type.equals(ErrorHandlerType.DeadLetterChannel) 
087            || type.equals(ErrorHandlerType.TransactionErrorHandler)) {
088            NodeList list = element.getChildNodes();
089            int size = list.getLength();
090            for (int i = 0; i < size; i++) {
091                Node child = list.item(i);
092                if (child instanceof Element) {
093                    Element childElement = (Element)child;
094                    String localName = child.getLocalName();
095                    // set the redeliveryPolicy
096                    if (localName.equals("redeliveryPolicy")) {
097                        // cannot have redeliveryPolicyRef attribute as well, only one is allowed
098                        if (ObjectHelper.isNotEmpty(element.getAttribute("redeliveryPolicyRef"))) {
099                            throw new IllegalArgumentException("Cannot set both redeliveryPolicyRef and redeliveryPolicy,"
100                                    + " only one allowed, in error handler with id: " + id);
101                        }
102                        BeanDefinition redeliveryPolicyDefinition = redeliveryPolicyParser.parse(childElement, parserContext);
103                        builder.addPropertyValue(localName, redeliveryPolicyDefinition);
104                    }
105                }
106            }
107            parserRefAttribute(element, "onRedeliveryRef", "onRedelivery", builder);
108            parserRefAttribute(element, "onRetryWhileRef", "onRetryWhile", builder);
109            parserRefAttribute(element, "onPrepareFailureRef", "onPrepareFailure", builder);
110            parserRefAttribute(element, "onExceptionOccurredRef", "onExceptionOccurred", builder);
111            parserRefAttribute(element, "redeliveryPolicyRef", "redeliveryPolicy", builder);
112            if (type.equals(ErrorHandlerType.TransactionErrorHandler)) {
113                parserRefAttribute(element, "transactionTemplateRef", "transactionTemplate", builder);
114                parserRefAttribute(element, "transactionManagerRef", "transactionManager", builder);
115            }
116        }
117
118        // validate attributes according to type
119
120        String deadLetterUri = element.getAttribute("deadLetterUri");
121        if (ObjectHelper.isNotEmpty(deadLetterUri) && !type.equals(ErrorHandlerType.DeadLetterChannel)) {
122            throw new IllegalArgumentException("Attribute deadLetterUri can only be used if type is "
123                    + ErrorHandlerType.DeadLetterChannel.name() + ", in error handler with id: " + id);
124        }
125        String deadLetterHandleNewException = element.getAttribute("deadLetterHandleNewException");
126        if (ObjectHelper.isNotEmpty(deadLetterHandleNewException) && !type.equals(ErrorHandlerType.DeadLetterChannel)) {
127            throw new IllegalArgumentException("Attribute deadLetterHandleNewException can only be used if type is "
128                    + ErrorHandlerType.DeadLetterChannel.name() + ", in error handler with id: " + id);
129        }
130        String transactionTemplateRef = element.getAttribute("transactionTemplateRef");
131        if (ObjectHelper.isNotEmpty(transactionTemplateRef) && !type.equals(ErrorHandlerType.TransactionErrorHandler)) {
132            throw new IllegalArgumentException("Attribute transactionTemplateRef can only be used if type is "
133                    + ErrorHandlerType.TransactionErrorHandler.name() + ", in error handler with id: " + id);
134        }
135        String transactionManagerRef = element.getAttribute("transactionManagerRef");
136        if (ObjectHelper.isNotEmpty(transactionManagerRef) && !type.equals(ErrorHandlerType.TransactionErrorHandler)) {
137            throw new IllegalArgumentException("Attribute transactionManagerRef can only be used if type is "
138                    + ErrorHandlerType.TransactionErrorHandler.name() + ", in error handler with id: " + id);
139        }
140        String rollbackLoggingLevel = element.getAttribute("rollbackLoggingLevel");
141        if (ObjectHelper.isNotEmpty(rollbackLoggingLevel) && (!type.equals(ErrorHandlerType.TransactionErrorHandler))) {
142            throw new IllegalArgumentException("Attribute rollbackLoggingLevel can only be used if type is "
143                    + ErrorHandlerType.TransactionErrorHandler.name() + ", in error handler with id: " + id);
144        }
145        String useOriginalMessage = element.getAttribute("useOriginalMessage");
146        if (ObjectHelper.isNotEmpty(useOriginalMessage) && type.equals(ErrorHandlerType.NoErrorHandler)) {
147            throw new IllegalArgumentException("Attribute useOriginalMessage is not supported by error handler type: "
148                    + type.name() + ", in error handler with id: " + id);
149        }
150        String useOriginalBody = element.getAttribute("useOriginalBody");
151        if (ObjectHelper.isNotEmpty(useOriginalBody) && type.equals(ErrorHandlerType.NoErrorHandler)) {
152            throw new IllegalArgumentException("Attribute useOriginalBody is not supported by error handler type: "
153                    + type.name() + ", in error handler with id: " + id);
154        }
155        String onRedeliveryRef = element.getAttribute("onRedeliveryRef");
156        if (ObjectHelper.isNotEmpty(onRedeliveryRef) && type.equals(ErrorHandlerType.NoErrorHandler)) {
157            throw new IllegalArgumentException("Attribute onRedeliveryRef is not supported by error handler type: "
158                    + type.name() + ", in error handler with id: " + id);
159        }
160        String onExceptionOccurredRef = element.getAttribute("onExceptionOccurredRef");
161        if (ObjectHelper.isNotEmpty(onExceptionOccurredRef) && type.equals(ErrorHandlerType.NoErrorHandler)) {
162            throw new IllegalArgumentException("Attribute onExceptionOccurredRef is not supported by error handler type: "
163                    + type.name() + ", in error handler with id: " + id);
164        }
165        String onPrepareFailureRef = element.getAttribute("onPrepareFailureRef");
166        if (ObjectHelper.isNotEmpty(onPrepareFailureRef) && (type.equals(ErrorHandlerType.TransactionErrorHandler)
167            || type.equals(ErrorHandlerType.NoErrorHandler))) {
168            throw new IllegalArgumentException("Attribute onPrepareFailureRef is not supported by error handler type: "
169                    + type.name() + ", in error handler with id: " + id);
170        }
171        String retryWhileRef = element.getAttribute("retryWhileRef");
172        if (ObjectHelper.isNotEmpty(retryWhileRef) && type.equals(ErrorHandlerType.NoErrorHandler)) {
173            throw new IllegalArgumentException("Attribute retryWhileRef is not supported by error handler type: "
174                    + type.name() + ", in error handler with id: " + id);
175        }
176        String redeliveryPolicyRef = element.getAttribute("redeliveryPolicyRef");
177        if (ObjectHelper.isNotEmpty(redeliveryPolicyRef) && type.equals(ErrorHandlerType.NoErrorHandler)) {
178            throw new IllegalArgumentException("Attribute redeliveryPolicyRef is not supported by error handler type: "
179                    + type.name() + ", in error handler with id: " + id);
180        }
181        String executorServiceRef = element.getAttribute("executorServiceRef");
182        if (ObjectHelper.isNotEmpty(executorServiceRef) && type.equals(ErrorHandlerType.NoErrorHandler)) {
183            throw new IllegalArgumentException("Attribute executorServiceRef is not supported by error handler type: "
184                    + type.name() + ", in error handler with id: " + id);
185        }
186    }
187
188    private void parserRefAttribute(Element element, String attributeName, String propertyName, BeanDefinitionBuilder builder) {
189        NamedNodeMap attributes = element.getAttributes();
190        for (int x = 0; x < attributes.getLength(); x++) {
191            Attr attribute = (Attr) attributes.item(x);
192            String name = attribute.getLocalName();
193            if (name.equals(attributeName)) {
194                Assert.state(StringUtils.hasText(propertyName),
195                        "Illegal property name returned from 'extractPropertyName(String)': cannot be null or empty.");
196                builder.addPropertyReference(propertyName, attribute.getValue());
197            }
198        }
199    }
200    
201    protected class RedeliveryPolicyDefinitionParser extends BeanDefinitionParser {
202
203        public RedeliveryPolicyDefinitionParser(Class<?> type) {
204            super(type, false);
205        }
206
207        @Override
208        protected boolean shouldGenerateId() {
209            return true;
210        }
211    }
212    
213}