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