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        @Override
050        public String getShortName() {
051            return "throwFault";
052        }
053    
054        public void setFault(Throwable fault) {
055            this.fault = fault;
056        }
057    
058        public Throwable getFault() {
059            return fault;
060        }
061    
062        public void setFaultRef(String ref) {
063            this.faultRef = ref;
064        }
065    
066        public String getFaultRef() {
067            return faultRef;
068        }
069    
070        @Override
071        public Processor createProcessor(RouteContext routeContext) {
072    
073            if (processor == null) {
074                if (fault == null) {
075                    fault = routeContext.lookup(faultRef, Throwable.class);
076                    if (fault == null) {
077                        // can't find the fault instance, create a new one
078                        fault = new CamelException(faultRef);
079                    }
080                }
081                processor = new ThrowFaultProcessor(fault);
082            }
083            return processor;
084        }
085    
086        @Override
087        public List<ProcessorType<?>> getOutputs() {
088            return Collections.EMPTY_LIST;
089        }
090    }