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 */
017package org.apache.camel.model;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023import javax.xml.bind.annotation.XmlTransient;
024
025import org.apache.camel.Processor;
026import org.apache.camel.processor.ThrowExceptionProcessor;
027import org.apache.camel.spi.Metadata;
028import org.apache.camel.spi.RouteContext;
029import org.apache.camel.util.ObjectHelper;
030
031/**
032 * Throws an exception
033 */
034@Metadata(label = "error")
035@XmlRootElement(name = "throwException")
036@XmlAccessorType(XmlAccessType.FIELD)
037public class ThrowExceptionDefinition extends NoOutputDefinition<ThrowExceptionDefinition> {
038    @XmlAttribute
039    private String ref;
040    @XmlAttribute
041    private String message;
042    @XmlTransient
043    private Exception exception;
044    @XmlAttribute
045    private String exceptionType;
046    @XmlTransient
047    private Class<? extends Exception> exceptionClass;
048
049    public ThrowExceptionDefinition() {
050    }
051
052    @Override
053    public String toString() {
054        return "ThrowException[" + description() + "]";
055    }
056
057    protected String description() {
058        return exception != null ? exception.getClass().getCanonicalName() : "ref:" + ref;
059    }
060
061    @Override
062    public String getLabel() {
063        return "throwException[" + description() + "]";
064    }
065    
066    @Override
067    public Processor createProcessor(RouteContext routeContext) {
068        if (ref != null && exception == null) {
069            this.exception = routeContext.getCamelContext().getRegistry().lookupByNameAndType(ref, Exception.class);
070        }
071
072        if (exceptionType != null && exceptionClass == null) {
073            try {
074                this.exceptionClass = routeContext.getCamelContext().getClassResolver().resolveMandatoryClass(exceptionType, Exception.class);
075            } catch (ClassNotFoundException e) {
076                throw ObjectHelper.wrapRuntimeCamelException(e);
077            }
078        }
079
080        if (exception == null && exceptionClass == null) {
081            throw new IllegalArgumentException("exception or exceptionClass/exceptionType must be configured on: " + this);
082        }
083        return new ThrowExceptionProcessor(exception, exceptionClass, message);
084    }
085
086    public String getRef() {
087        return ref;
088    }
089
090    /**
091     * Reference to the exception instance to lookup from the registry to throw
092     */
093    public void setRef(String ref) {
094        this.ref = ref;
095    }
096
097    public Exception getException() {
098        return exception;
099    }
100
101    public void setException(Exception exception) {
102        this.exception = exception;
103    }
104
105    public String getMessage() {
106        return message;
107    }
108
109    /**
110     * To create a new exception instance and use the given message as caused message (supports simple language)
111     */
112    public void setMessage(String message) {
113        this.message = message;
114    }
115
116    public String getExceptionType() {
117        return exceptionType;
118    }
119
120    /**
121     * The class of the exception to create using the message.
122     *
123     * @see #setMessage(String)
124     */
125    public void setExceptionType(String exceptionType) {
126        this.exceptionType = exceptionType;
127    }
128
129    public Class<? extends Exception> getExceptionClass() {
130        return exceptionClass;
131    }
132
133    /**
134     * The class of the exception to create using the message.
135     *
136     * @see #setMessage(String)
137     */
138    public void setExceptionClass(Class<? extends Exception> exceptionClass) {
139        this.exceptionClass = exceptionClass;
140    }
141}