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: 1.1 $
036     */
037    @XmlRootElement(name = "catch")
038    @XmlAccessorType(XmlAccessType.FIELD)
039    public class CatchType extends ProcessorType {
040        @XmlElementRef
041        private List<InterceptorType> interceptors = new ArrayList<InterceptorType>();
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 CatchProcessor createProcessor(RouteContext routeContext) throws Exception {
068            Processor childProcessor = routeContext.createProcessor(this);
069            return new CatchProcessor(getExceptionClasses(), childProcessor);
070        }
071    
072        public List<InterceptorType> getInterceptors() {
073            return interceptors;
074        }
075    
076        public void setInterceptors(List<InterceptorType> interceptors) {
077            this.interceptors = interceptors;
078        }
079    
080        public List<ProcessorType> getOutputs() {
081            return outputs;
082        }
083    
084        public void setOutputs(List<ProcessorType> outputs) {
085            this.outputs = outputs;
086        }
087    
088        public List<Class> getExceptionClasses() {
089            if (exceptionClasses == null) {
090                exceptionClasses = createExceptionClasses();
091            }
092            return exceptionClasses;
093        }
094    
095        public void setExceptionClasses(List<Class> exceptionClasses) {
096            this.exceptionClasses = exceptionClasses;
097        }
098    
099        public List<String> getExceptions() {
100            return exceptions;
101        }
102    
103        public void setExceptions(List<String> exceptions) {
104            this.exceptions = exceptions;
105        }
106    
107        protected List<Class> createExceptionClasses() {
108            List<String> list = getExceptions();
109            List<Class> answer = new ArrayList<Class>(list.size());
110            for (String name : list) {
111                Class type = ObjectHelper.loadClass(name, getClass().getClassLoader());
112                answer.add(type);
113            }
114            return answer;
115        }
116    }