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 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 if (attributeName.equals("xmlns") || attributeName.startsWith("xmlns:")) { 059 return false; 060 } 061 // CHECKSTYLE:OFF 062 return !attributeName.equals("type") 063 && !attributeName.equals("onRedeliveryRef") 064 && !attributeName.equals("onRetryWhileRef") 065 && !attributeName.equals("onPrepareFailureRef") 066 && !attributeName.equals("onExceptionOccurredRef") 067 && !attributeName.equals("redeliveryPolicyRef") 068 && !attributeName.equals("transactionTemplateRef") 069 && !attributeName.equals("transactionManagerRef"); 070 // CHECKSTYLE:ON 071 } 072 073 @Override 074 protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { 075 super.doParse(element, parserContext, builder); 076 077 String id = element.getAttribute("id"); 078 079 ErrorHandlerType type = ErrorHandlerType.DefaultErrorHandler; 080 if (ObjectHelper.isNotEmpty(element.getAttribute("type"))) { 081 type = ErrorHandlerType.valueOf(element.getAttribute("type")); 082 } 083 if (type.equals(ErrorHandlerType.DefaultErrorHandler) 084 || type.equals(ErrorHandlerType.DeadLetterChannel) 085 || type.equals(ErrorHandlerType.TransactionErrorHandler)) { 086 NodeList list = element.getChildNodes(); 087 int size = list.getLength(); 088 for (int i = 0; i < size; i++) { 089 Node child = list.item(i); 090 if (child instanceof Element) { 091 Element childElement = (Element)child; 092 String localName = child.getLocalName(); 093 // set the redeliveryPolicy 094 if (localName.equals("redeliveryPolicy")) { 095 // cannot have redeliveryPolicyRef attribute as well, only one is allowed 096 if (ObjectHelper.isNotEmpty(element.getAttribute("redeliveryPolicyRef"))) { 097 throw new IllegalArgumentException("Cannot set both redeliveryPolicyRef and redeliveryPolicy," 098 + " only one allowed, in error handler with id: " + id); 099 } 100 BeanDefinition redeliveryPolicyDefinition = redeliveryPolicyParser.parse(childElement, parserContext); 101 builder.addPropertyValue(localName, redeliveryPolicyDefinition); 102 } 103 } 104 } 105 parserRefAttribute(element, "onRedeliveryRef", "onRedelivery", builder); 106 parserRefAttribute(element, "onRetryWhileRef", "onRetryWhile", builder); 107 parserRefAttribute(element, "onPrepareFailureRef", "onPrepareFailure", builder); 108 parserRefAttribute(element, "onExceptionOccurredRef", "onExceptionOccurred", builder); 109 parserRefAttribute(element, "redeliveryPolicyRef", "redeliveryPolicy", builder); 110 if (type.equals(ErrorHandlerType.TransactionErrorHandler)) { 111 parserRefAttribute(element, "transactionTemplateRef", "transactionTemplate", builder); 112 parserRefAttribute(element, "transactionManagerRef", "transactionManager", builder); 113 } 114 } 115 116 // validate attributes according to type 117 118 String deadLetterUri = element.getAttribute("deadLetterUri"); 119 if (ObjectHelper.isNotEmpty(deadLetterUri) && !type.equals(ErrorHandlerType.DeadLetterChannel)) { 120 throw new IllegalArgumentException("Attribute deadLetterUri can only be used if type is " 121 + ErrorHandlerType.DeadLetterChannel.name() + ", in error handler with id: " + id); 122 } 123 String deadLetterHandleNewException = element.getAttribute("deadLetterHandleNewException"); 124 if (ObjectHelper.isNotEmpty(deadLetterHandleNewException) && !type.equals(ErrorHandlerType.DeadLetterChannel)) { 125 throw new IllegalArgumentException("Attribute deadLetterHandleNewException can only be used if type is " 126 + ErrorHandlerType.DeadLetterChannel.name() + ", in error handler with id: " + id); 127 } 128 String transactionTemplateRef = element.getAttribute("transactionTemplateRef"); 129 if (ObjectHelper.isNotEmpty(transactionTemplateRef) && !type.equals(ErrorHandlerType.TransactionErrorHandler)) { 130 throw new IllegalArgumentException("Attribute transactionTemplateRef can only be used if type is " 131 + ErrorHandlerType.TransactionErrorHandler.name() + ", in error handler with id: " + id); 132 } 133 String transactionManagerRef = element.getAttribute("transactionManagerRef"); 134 if (ObjectHelper.isNotEmpty(transactionManagerRef) && !type.equals(ErrorHandlerType.TransactionErrorHandler)) { 135 throw new IllegalArgumentException("Attribute transactionManagerRef can only be used if type is " 136 + ErrorHandlerType.TransactionErrorHandler.name() + ", in error handler with id: " + id); 137 } 138 String rollbackLoggingLevel = element.getAttribute("rollbackLoggingLevel"); 139 if (ObjectHelper.isNotEmpty(rollbackLoggingLevel) && (!type.equals(ErrorHandlerType.TransactionErrorHandler))) { 140 throw new IllegalArgumentException("Attribute rollbackLoggingLevel can only be used if type is " 141 + ErrorHandlerType.TransactionErrorHandler.name() + ", in error handler with id: " + id); 142 } 143 String useOriginalMessage = element.getAttribute("useOriginalMessage"); 144 if (ObjectHelper.isNotEmpty(useOriginalMessage) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) { 145 throw new IllegalArgumentException("Attribute useOriginalMessage is not supported by error handler type: " 146 + type.name() + ", in error handler with id: " + id); 147 } 148 String onRedeliveryRef = element.getAttribute("onRedeliveryRef"); 149 if (ObjectHelper.isNotEmpty(onRedeliveryRef) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) { 150 throw new IllegalArgumentException("Attribute onRedeliveryRef is not supported by error handler type: " 151 + type.name() + ", in error handler with id: " + id); 152 } 153 String onExceptionOccurredRef = element.getAttribute("onExceptionOccurredRef"); 154 if (ObjectHelper.isNotEmpty(onExceptionOccurredRef) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) { 155 throw new IllegalArgumentException("Attribute onExceptionOccurredRef is not supported by error handler type: " 156 + type.name() + ", in error handler with id: " + id); 157 } 158 String onPrepareFailureRef = element.getAttribute("onPrepareFailureRef"); 159 if (ObjectHelper.isNotEmpty(onPrepareFailureRef) && (type.equals(ErrorHandlerType.TransactionErrorHandler) || type.equals(ErrorHandlerType.LoggingErrorHandler) 160 || type.equals(ErrorHandlerType.NoErrorHandler))) { 161 throw new IllegalArgumentException("Attribute onPrepareFailureRef is not supported by error handler type: " 162 + type.name() + ", in error handler with id: " + id); 163 } 164 String retryWhileRef = element.getAttribute("retryWhileRef"); 165 if (ObjectHelper.isNotEmpty(retryWhileRef) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) { 166 throw new IllegalArgumentException("Attribute retryWhileRef is not supported by error handler type: " 167 + type.name() + ", in error handler with id: " + id); 168 } 169 String redeliveryPolicyRef = element.getAttribute("redeliveryPolicyRef"); 170 if (ObjectHelper.isNotEmpty(redeliveryPolicyRef) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) { 171 throw new IllegalArgumentException("Attribute redeliveryPolicyRef is not supported by error handler type: " 172 + type.name() + ", in error handler with id: " + id); 173 } 174 String executorServiceRef = element.getAttribute("executorServiceRef"); 175 if (ObjectHelper.isNotEmpty(executorServiceRef) && (type.equals(ErrorHandlerType.LoggingErrorHandler) || type.equals(ErrorHandlerType.NoErrorHandler))) { 176 throw new IllegalArgumentException("Attribute executorServiceRef is not supported by error handler type: " 177 + type.name() + ", in error handler with id: " + id); 178 } 179 String logName = element.getAttribute("logName"); 180 if (ObjectHelper.isNotEmpty(logName) && (!type.equals(ErrorHandlerType.LoggingErrorHandler))) { 181 throw new IllegalArgumentException("Attribute logName is not supported by error handler type: " 182 + type.name() + ", in error handler with id: " + id); 183 } 184 String level = element.getAttribute("level"); 185 if (ObjectHelper.isNotEmpty(level) && (!type.equals(ErrorHandlerType.LoggingErrorHandler))) { 186 throw new IllegalArgumentException("Attribute level is not supported by error handler type: " 187 + type.name() + ", in error handler with id: " + id); 188 } 189 } 190 191 private void parserRefAttribute(Element element, String attributeName, String propertyName, BeanDefinitionBuilder builder) { 192 NamedNodeMap attributes = element.getAttributes(); 193 for (int x = 0; x < attributes.getLength(); x++) { 194 Attr attribute = (Attr) attributes.item(x); 195 String name = attribute.getLocalName(); 196 if (name.equals(attributeName)) { 197 Assert.state(StringUtils.hasText(propertyName), 198 "Illegal property name returned from 'extractPropertyName(String)': cannot be null or empty."); 199 builder.addPropertyReference(propertyName, attribute.getValue()); 200 } 201 } 202 } 203 204 protected class RedeliveryPolicyDefinitionParser extends BeanDefinitionParser { 205 206 public RedeliveryPolicyDefinitionParser(Class<?> type) { 207 super(type, false); 208 } 209 210 protected boolean shouldGenerateId() { 211 return true; 212 } 213 } 214 215}