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