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.Collections;
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.XmlAttribute;
025    import javax.xml.bind.annotation.XmlRootElement;
026    import javax.xml.bind.annotation.XmlTransient;
027    
028    import org.apache.camel.CamelException;
029    import org.apache.camel.Processor;
030    import org.apache.camel.processor.ThrowFaultProcessor;
031    import org.apache.camel.spi.RouteContext;
032    
033    /**
034     * Represents an XML <throwFault/> element
035     */
036    @XmlRootElement(name = "throwFault")
037    @XmlAccessorType(XmlAccessType.FIELD)
038    public class ThrowFaultType extends ProcessorType<ThrowFaultType> {
039        @XmlTransient
040        private Throwable fault;
041        @XmlTransient
042        private Processor processor;
043        @XmlAttribute (required = true)
044        private String faultRef;
045    
046        public ThrowFaultType() {
047        }
048    
049        public void setFault(Throwable fault) {
050            this.fault = fault;
051        }
052    
053        public Throwable getFault() {
054            return fault;
055        }
056    
057        public void setFaultRef(String ref) {
058            this.faultRef = ref;
059        }
060    
061        public String getFaultRef() {
062            return faultRef;
063        }
064    
065        @Override
066        public Processor createProcessor(RouteContext routeContext) {
067    
068            if (processor == null) {
069                if (fault == null) {
070                    fault = routeContext.lookup(faultRef, Throwable.class);
071                    if (fault == null) {
072                        // can't find the fault instance, create a new one
073                        fault = new CamelException(faultRef);
074                    }
075                }
076                processor = new ThrowFaultProcessor(fault);
077            }
078            return processor;
079        }
080    
081        @Override
082        public List<ProcessorType<?>> getOutputs() {
083            return Collections.EMPTY_LIST;
084        }
085    }