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    import javax.xml.bind.annotation.XmlAccessType;
022    import javax.xml.bind.annotation.XmlAccessorType;
023    import javax.xml.bind.annotation.XmlAttribute;
024    import javax.xml.bind.annotation.XmlRootElement;
025    import javax.xml.bind.annotation.XmlTransient;
026    
027    import org.apache.camel.Processor;
028    import org.apache.camel.processor.ThrowExceptionProcessor;
029    import org.apache.camel.spi.RouteContext;
030    import org.apache.camel.util.ObjectHelper;
031    
032    /**
033     * Represents an XML <throwException/> element
034     */
035    @XmlRootElement(name = "throwException")
036    @XmlAccessorType(XmlAccessType.FIELD)
037    public class ThrowExceptionDefinition extends ProcessorDefinition<ThrowExceptionDefinition> {
038        @XmlAttribute(name = "ref", required = false)
039        private String ref;
040        @XmlTransient
041        private Exception exception;
042    
043        public ThrowExceptionDefinition() {
044        }
045    
046        @Override
047        public String getShortName() {
048            return "throwException";
049        }
050    
051        @Override
052        public String toString() {
053            return "ThrowException[" + (exception != null ? exception.getClass().getCanonicalName() : "ref:" + ref) + "]";
054        }
055    
056        @Override
057        public Processor createProcessor(RouteContext routeContext) {
058            if (ref != null && exception == null) {
059                this.exception = routeContext.getCamelContext().getRegistry().lookup(ref, Exception.class);
060            }
061    
062            ObjectHelper.notNull(exception, "exception or ref", this);
063            return new ThrowExceptionProcessor(exception);
064        }
065    
066        @Override
067        @SuppressWarnings("unchecked")
068        public List<ProcessorDefinition> getOutputs() {
069            return Collections.EMPTY_LIST;
070        }
071    
072        public Exception getException() {
073            return exception;
074        }
075    
076        public void setException(Exception exception) {
077            this.exception = exception;
078        }
079    }