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.spi.Metadata;
026
027/**
028 * Throws an exception
029 */
030@Metadata(label = "error")
031@XmlRootElement(name = "throwException")
032@XmlAccessorType(XmlAccessType.FIELD)
033public class ThrowExceptionDefinition extends NoOutputDefinition<ThrowExceptionDefinition> {
034    @XmlAttribute
035    private String ref;
036    @XmlAttribute
037    private String message;
038    @XmlTransient
039    private Exception exception;
040    @XmlAttribute
041    private String exceptionType;
042    @XmlTransient
043    private Class<? extends Exception> exceptionClass;
044
045    public ThrowExceptionDefinition() {
046    }
047
048    @Override
049    public String toString() {
050        return "ThrowException[" + description() + "]";
051    }
052
053    protected String description() {
054        if (exception != null) {
055            return exception.getClass().getCanonicalName();
056        } else if (ref != null) {
057            return "ref:" + ref;
058        } else {
059            return "";
060        }
061    }
062
063    @Override
064    public String getShortName() {
065        return "throwException";
066    }
067
068    @Override
069    public String getLabel() {
070        return "throwException[" + description() + "]";
071    }
072
073    public String getRef() {
074        return ref;
075    }
076
077    /**
078     * Reference to the exception instance to lookup from the registry to throw
079     */
080    public void setRef(String ref) {
081        this.ref = ref;
082    }
083
084    public Exception getException() {
085        return exception;
086    }
087
088    public void setException(Exception exception) {
089        this.exception = exception;
090    }
091
092    public String getMessage() {
093        return message;
094    }
095
096    /**
097     * To create a new exception instance and use the given message as caused
098     * message (supports simple language)
099     */
100    public void setMessage(String message) {
101        this.message = message;
102    }
103
104    public String getExceptionType() {
105        return exceptionType;
106    }
107
108    /**
109     * The class of the exception to create using the message.
110     *
111     * @see #setMessage(String)
112     */
113    public void setExceptionType(String exceptionType) {
114        this.exceptionType = exceptionType;
115    }
116
117    public Class<? extends Exception> getExceptionClass() {
118        return exceptionClass;
119    }
120
121    /**
122     * The class of the exception to create using the message.
123     *
124     * @see #setMessage(String)
125     */
126    public void setExceptionClass(Class<? extends Exception> exceptionClass) {
127        this.exceptionClass = exceptionClass;
128    }
129}