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: 43540 $
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 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<ProcessorType<?>> getOutputs() {
083            return outputs;
084        }
085    
086        public void setOutputs(List<ProcessorType<?>> 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        public List<String> getExceptions() {
102            return exceptions;
103        }
104    
105        public void setExceptions(List<String> exceptions) {
106            this.exceptions = exceptions;
107        }
108    
109        protected List<Class> createExceptionClasses() {
110            List<String> list = getExceptions();
111            List<Class> answer = new ArrayList<Class>(list.size());
112            for (String name : list) {
113                Class type = ObjectHelper.loadClass(name, getClass().getClassLoader());
114                answer.add(type);
115            }
116            return answer;
117        }
118    }