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 */
017 package org.apache.camel.spring.handler;
018
019 import org.w3c.dom.Attr;
020 import org.w3c.dom.Element;
021 import org.w3c.dom.NamedNodeMap;
022 import org.w3c.dom.Node;
023 import org.w3c.dom.NodeList;
024
025 import org.apache.camel.processor.RedeliveryPolicy;
026 import org.apache.camel.spring.ErrorHandlerType;
027 import org.apache.camel.util.ObjectHelper;
028 import org.springframework.beans.factory.config.BeanDefinition;
029 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
030 import org.springframework.beans.factory.xml.ParserContext;
031 import org.springframework.util.Assert;
032 import org.springframework.util.StringUtils;
033
034 /**
035 * The DefinitionParser to deal with the ErrorHandler
036 */
037 public class ErrorHandlerDefinitionParser extends BeanDefinitionParser {
038 protected BeanDefinitionParser redeliveryPolicyParser = new RedeliveryPolicyDefinitionParser(RedeliveryPolicy.class);
039
040 public ErrorHandlerDefinitionParser() {
041 // need to override the default
042 super(null);
043 }
044
045 protected Class getBeanClass(Element element) {
046 ErrorHandlerType type = ErrorHandlerType.DefaultErrorHandler;
047
048 if (ObjectHelper.isNotEmpty(element.getAttribute("type"))) {
049 type = ErrorHandlerType.valueOf(element.getAttribute("type"));
050 }
051 return type.getTypeAsClass();
052 }
053
054 protected boolean isEligibleAttribute(String attributeName) {
055 if (attributeName == null || ID_ATTRIBUTE.equals(attributeName)) {
056 return false;
057 }
058 return !attributeName.equals("xmlns") && !attributeName.startsWith("xmlns:")
059 && !attributeName.equals("type") && !attributeName.equals("onRedeliveryRef")
060 && !attributeName.equals("transactionTemplateRef")
061 && !attributeName.equals("transactionManagerRef");
062 }
063
064 @Override
065 protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
066 super.doParse(element, parserContext, builder);
067 ErrorHandlerType type = ErrorHandlerType.DefaultErrorHandler;
068 if (ObjectHelper.isNotEmpty(element.getAttribute("type"))) {
069 type = ErrorHandlerType.valueOf(element.getAttribute("type"));
070 }
071 if (type.equals(ErrorHandlerType.NoErrorHandler) || type.equals(ErrorHandlerType.LoggingErrorHandler)) {
072 // don't need to parser other stuff
073 return;
074 }
075 if (type.equals(ErrorHandlerType.DefaultErrorHandler)
076 || type.equals(ErrorHandlerType.DeadLetterChannel)
077 || type.equals(ErrorHandlerType.TransactionErrorHandler)) {
078 NodeList list = element.getChildNodes();
079 int size = list.getLength();
080 for (int i = 0; i < size; i++) {
081 Node child = list.item(i);
082 if (child instanceof Element) {
083 Element childElement = (Element)child;
084 String localName = child.getLocalName();
085 // set the redeliveryPolicy
086 if (localName.equals("redeliveryPolicy")) {
087 BeanDefinition redeliveryPolicyDefinition = redeliveryPolicyParser.parse(childElement, parserContext);
088 builder.addPropertyValue(localName, redeliveryPolicyDefinition);
089 }
090 }
091 }
092 // deal with onRedeliveryRef
093 parserRefAttribute(element, "onRedeliveryRef", "onRedelivery", builder);
094 }
095 if (type.equals(ErrorHandlerType.TransactionErrorHandler)) {
096 // deal with transactionTemplateRef
097 parserRefAttribute(element, "transactionTemplateRef", "transactionTemplate", builder);
098 parserRefAttribute(element, "transactionManagerRef", "transactionManager", builder);
099 }
100 }
101
102 private void parserRefAttribute(Element element, String attributeName, String propertyName, BeanDefinitionBuilder builder) {
103 NamedNodeMap attributes = element.getAttributes();
104 for (int x = 0; x < attributes.getLength(); x++) {
105 Attr attribute = (Attr) attributes.item(x);
106 String name = attribute.getLocalName();
107 if (name.equals(attributeName)) {
108 Assert.state(StringUtils.hasText(propertyName),
109 "Illegal property name returned from 'extractPropertyName(String)': cannot be null or empty.");
110 builder.addPropertyReference(propertyName, attribute.getValue());
111 }
112 }
113 }
114
115 protected class RedeliveryPolicyDefinitionParser extends BeanDefinitionParser {
116 public RedeliveryPolicyDefinitionParser(Class type) {
117 super(type);
118 }
119
120 protected boolean shouldGenerateId() {
121 return true;
122 }
123 }
124
125 }