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(required = true)
039    // the ref is required from tooling and XML DSL
040    private String ref;
041    @XmlTransient
042    private Exception exception;
043
044    public ThrowExceptionDefinition() {
045    }
046
047    @Override
048    public String toString() {
049        return "ThrowException[" + description() + "]";
050    }
051
052    protected String description() {
053        return exception != null ? exception.getClass().getCanonicalName() : "ref:" + ref;
054    }
055
056    @Override
057    public String getLabel() {
058        return "throwException[" + description() + "]";
059    }
060    
061    @Override
062    public Processor createProcessor(RouteContext routeContext) {
063        if (ref != null && exception == null) {
064            this.exception = routeContext.getCamelContext().getRegistry().lookupByNameAndType(ref, Exception.class);
065        }
066
067        ObjectHelper.notNull(exception, "exception or ref", this);
068        return new ThrowExceptionProcessor(exception);
069    }
070
071    public String getRef() {
072        return ref;
073    }
074
075    /**
076     * Reference to the exception instance to lookup from the registry to throw
077     */
078    public void setRef(String ref) {
079        this.ref = ref;
080    }
081
082    public Exception getException() {
083        return exception;
084    }
085
086    public void setException(Exception exception) {
087        this.exception = exception;
088    }
089}