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: 41908 $
038     */
039    @XmlRootElement(name = "catch")
040    @XmlAccessorType(XmlAccessType.FIELD)
041    public class CatchType extends ProcessorType<ProcessorType> {
042        @XmlElement(name = "exception")
043        private List<String> exceptions = new ArrayList<String>();
044        @XmlElementRef
045        private List<ProcessorType<?>> outputs = new ArrayList<ProcessorType<?>>();
046        @XmlTransient
047        private List<Class> exceptionClasses;
048    
049        public CatchType() {
050        }
051    
052        public CatchType(List<Class> exceptionClasses) {
053            this.exceptionClasses = exceptionClasses;
054        }
055    
056        public CatchType(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 getLabel() {
068            return getExceptionClasses().toString();
069        }
070    
071        @Override
072        public CatchProcessor createProcessor(RouteContext routeContext) throws Exception {
073            Processor childProcessor = routeContext.createProcessor(this);
074            return new CatchProcessor(getExceptionClasses(), childProcessor);
075        }
076    
077        public List<ProcessorType<?>> getOutputs() {
078            return outputs;
079        }
080    
081        public void setOutputs(List<ProcessorType<?>> outputs) {
082            this.outputs = outputs;
083        }
084    
085        public List<Class> getExceptionClasses() {
086            if (exceptionClasses == null) {
087                exceptionClasses = createExceptionClasses();
088            }
089            return exceptionClasses;
090        }
091    
092        public void setExceptionClasses(List<Class> exceptionClasses) {
093            this.exceptionClasses = exceptionClasses;
094        }
095    
096        public List<String> getExceptions() {
097            return exceptions;
098        }
099    
100        public void setExceptions(List<String> exceptions) {
101            this.exceptions = exceptions;
102        }
103    
104        protected List<Class> createExceptionClasses() {
105            List<String> list = getExceptions();
106            List<Class> answer = new ArrayList<Class>(list.size());
107            for (String name : list) {
108                Class type = ObjectHelper.loadClass(name, getClass().getClassLoader());
109                answer.add(type);
110            }
111            return answer;
112        }
113    }