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.model; 018 019 import java.util.ArrayList; 020 import java.util.List; 021 022 import javax.xml.bind.annotation.XmlAccessType; 023 import javax.xml.bind.annotation.XmlAccessorType; 024 import javax.xml.bind.annotation.XmlElement; 025 import javax.xml.bind.annotation.XmlElementRef; 026 import javax.xml.bind.annotation.XmlRootElement; 027 import javax.xml.bind.annotation.XmlTransient; 028 029 import org.apache.camel.Processor; 030 import org.apache.camel.processor.CatchProcessor; 031 import org.apache.camel.spi.RouteContext; 032 import org.apache.camel.util.ObjectHelper; 033 034 /** 035 * Represents an XML <catch/> element 036 * 037 * @version $Revision: 750806 $ 038 */ 039 @XmlRootElement(name = "catch") 040 @XmlAccessorType(XmlAccessType.FIELD) 041 public class CatchDefinition extends ProcessorDefinition<CatchDefinition> { 042 @XmlElement(name = "exception") 043 private List<String> exceptions = new ArrayList<String>(); 044 @XmlElementRef 045 private List<ProcessorDefinition> outputs = new ArrayList<ProcessorDefinition>(); 046 @XmlTransient 047 private List<Class> exceptionClasses; 048 049 public CatchDefinition() { 050 } 051 052 public CatchDefinition(List<Class> exceptionClasses) { 053 this.exceptionClasses = exceptionClasses; 054 } 055 056 public CatchDefinition(Class exceptionType) { 057 exceptionClasses = new ArrayList<Class>(); 058 exceptionClasses.add(exceptionType); 059 } 060 061 @Override 062 public String toString() { 063 return "Catch[ " + getExceptionClasses() + " -> " + getOutputs() + "]"; 064 } 065 066 @Override 067 public String getShortName() { 068 return "catch"; 069 } 070 071 @Override 072 public String getLabel() { 073 return getExceptionClasses().toString(); 074 } 075 076 @Override 077 public CatchProcessor createProcessor(RouteContext routeContext) throws Exception { 078 Processor childProcessor = routeContext.createProcessor(this); 079 return new CatchProcessor(getExceptionClasses(), childProcessor); 080 } 081 082 public List<ProcessorDefinition> getOutputs() { 083 return outputs; 084 } 085 086 public void setOutputs(List<ProcessorDefinition> outputs) { 087 this.outputs = outputs; 088 } 089 090 public List<Class> getExceptionClasses() { 091 if (exceptionClasses == null) { 092 exceptionClasses = createExceptionClasses(); 093 } 094 return exceptionClasses; 095 } 096 097 public void setExceptionClasses(List<Class> exceptionClasses) { 098 this.exceptionClasses = exceptionClasses; 099 } 100 101 // Fluent API 102 //------------------------------------------------------------------------- 103 /** 104 * Sets the exceptionClasses of the CatchType 105 * 106 * @param exceptionClasses a list of the exception classes 107 * @return the builder 108 */ 109 public CatchDefinition exceptionClasses(List<Class> exceptionClasses) { 110 setExceptionClasses(exceptionClasses); 111 return this; 112 } 113 114 /** 115 * Sets the exception class that the CatchType want to catch 116 * 117 * @param exception the exception of class 118 * @return the builder 119 */ 120 public CatchDefinition exceptionClasses(Class exception) { 121 List<Class> list = getExceptionClasses(); 122 list.add(exception); 123 return this; 124 } 125 126 public List<String> getExceptions() { 127 return exceptions; 128 } 129 130 public void setExceptions(List<String> exceptions) { 131 this.exceptions = exceptions; 132 } 133 134 protected List<Class> createExceptionClasses() { 135 List<String> list = getExceptions(); 136 List<Class> answer = new ArrayList<Class>(list.size()); 137 for (String name : list) { 138 Class type = ObjectHelper.loadClass(name, getClass().getClassLoader()); 139 answer.add(type); 140 } 141 return answer; 142 } 143 }