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.model; 018 019import java.util.ArrayList; 020import java.util.Arrays; 021import java.util.List; 022 023import javax.xml.bind.annotation.XmlAccessType; 024import javax.xml.bind.annotation.XmlAccessorType; 025import javax.xml.bind.annotation.XmlElement; 026import javax.xml.bind.annotation.XmlElementRef; 027import javax.xml.bind.annotation.XmlRootElement; 028import javax.xml.bind.annotation.XmlTransient; 029 030import org.apache.camel.Predicate; 031import org.apache.camel.spi.AsPredicate; 032import org.apache.camel.spi.Metadata; 033 034/** 035 * Catches exceptions as part of a try, catch, finally block 036 */ 037@Metadata(label = "error") 038@XmlRootElement(name = "doCatch") 039@XmlAccessorType(XmlAccessType.FIELD) 040public class CatchDefinition extends ProcessorDefinition<CatchDefinition> implements OutputNode { 041 @XmlElement(name = "exception") 042 private List<String> exceptions = new ArrayList<>(); 043 @XmlElement(name = "onWhen") 044 @AsPredicate 045 private WhenDefinition onWhen; 046 @XmlElementRef 047 private List<ProcessorDefinition<?>> outputs = new ArrayList<>(); 048 @XmlTransient 049 private List<Class<? extends Throwable>> exceptionClasses; 050 051 public CatchDefinition() { 052 } 053 054 public CatchDefinition(List<Class<? extends Throwable>> exceptionClasses) { 055 this.exceptionClasses = exceptionClasses; 056 } 057 058 public CatchDefinition(Class<? extends Throwable> exceptionType) { 059 exceptionClasses = new ArrayList<>(); 060 exceptionClasses.add(exceptionType); 061 } 062 063 @Override 064 public String toString() { 065 return "DoCatch[ " + getExceptionClasses() + " -> " + getOutputs() + "]"; 066 } 067 068 @Override 069 public String getShortName() { 070 return "doCatch"; 071 } 072 073 @Override 074 public String getLabel() { 075 return "doCatch[ " + getExceptionClasses() + "]"; 076 } 077 078 @Override 079 public List<ProcessorDefinition<?>> getOutputs() { 080 return outputs; 081 } 082 083 public void setOutputs(List<ProcessorDefinition<?>> outputs) { 084 this.outputs = outputs; 085 } 086 087 public List<Class<? extends Throwable>> getExceptionClasses() { 088 return exceptionClasses; 089 } 090 091 public void setExceptionClasses(List<Class<? extends Throwable>> exceptionClasses) { 092 this.exceptionClasses = exceptionClasses; 093 } 094 095 // Fluent API 096 // ------------------------------------------------------------------------- 097 /** 098 * The exceptions to catch. 099 * 100 * @param exceptionClasses a list of the exception classes 101 * @return the builder 102 * @deprecated use {@link #exception(Class[])} 103 */ 104 @Deprecated 105 public CatchDefinition exceptionClasses(List<Class<? extends Throwable>> exceptionClasses) { 106 setExceptionClasses(exceptionClasses); 107 return this; 108 } 109 110 /** 111 * The exception(s) to catch. 112 * 113 * @param exceptions one or more exceptions 114 * @return the builder 115 */ 116 public CatchDefinition exception(Class<? extends Throwable>... exceptions) { 117 if (exceptionClasses == null) { 118 exceptionClasses = new ArrayList<>(); 119 } 120 if (exceptions != null) { 121 exceptionClasses.addAll(Arrays.asList(exceptions)); 122 } 123 return this; 124 } 125 126 /** 127 * Sets an additional predicate that should be true before the onCatch is 128 * triggered. 129 * <p/> 130 * To be used for fine grained controlling whether a thrown exception should 131 * be intercepted by this exception type or not. 132 * 133 * @param predicate predicate that determines true or false 134 * @return the builder 135 */ 136 public CatchDefinition onWhen(@AsPredicate Predicate predicate) { 137 setOnWhen(new WhenDefinition(predicate)); 138 return this; 139 } 140 141 /** 142 * Sets the exception class that the CatchType want to catch 143 * 144 * @param exception the exception of class 145 * @return the builder 146 * @deprecated use {@link #exception(Class[])} 147 */ 148 @Deprecated 149 public CatchDefinition exceptionClasses(Class<? extends Throwable> exception) { 150 List<Class<? extends Throwable>> list = getExceptionClasses(); 151 list.add(exception); 152 return this; 153 } 154 155 public List<String> getExceptions() { 156 return exceptions; 157 } 158 159 public void setExceptions(List<String> exceptions) { 160 this.exceptions = exceptions; 161 } 162 163 public WhenDefinition getOnWhen() { 164 return onWhen; 165 } 166 167 public void setOnWhen(WhenDefinition onWhen) { 168 this.onWhen = onWhen; 169 } 170 171}