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        @Override
055        public String toString() {
056            if (faultRef != null) {
057                return "ThrowFault[ref: " + faultRef + "]";
058            } else {
059                return "ThrowFault[" + fault.getClass().getCanonicalName();
060            }
061        }
062    
063        public void setFault(Throwable fault) {
064            this.fault = fault;
065        }
066    
067        public Throwable getFault() {
068            return fault;
069        }
070    
071        public void setFaultRef(String ref) {
072            this.faultRef = ref;
073        }
074    
075        public String getFaultRef() {
076            return faultRef;
077        }
078    
079        @Override
080        public Processor createProcessor(RouteContext routeContext) {
081            if (processor == null) {
082                if (fault == null) {
083                    fault = routeContext.lookup(faultRef, Throwable.class);
084                    if (fault == null) {
085                        // can't find the fault instance, create a new one
086                        fault = new CamelException(faultRef);
087                    }
088                }
089                processor = new ThrowFaultProcessor(fault);
090            }
091            return processor;
092        }
093    
094        @Override
095        public List<ProcessorType<?>> getOutputs() {
096            return Collections.EMPTY_LIST;
097        }
098    }