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.processor;
018
019import java.lang.reflect.Constructor;
020
021import org.apache.camel.AsyncCallback;
022import org.apache.camel.AsyncProcessor;
023import org.apache.camel.CamelContext;
024import org.apache.camel.CamelContextAware;
025import org.apache.camel.CamelExchangeException;
026import org.apache.camel.Exchange;
027import org.apache.camel.Expression;
028import org.apache.camel.Traceable;
029import org.apache.camel.spi.IdAware;
030import org.apache.camel.support.ServiceSupport;
031import org.apache.camel.util.AsyncProcessorHelper;
032import org.apache.camel.util.ObjectHelper;
033
034/**
035 * The processor which sets an {@link Exception} on the {@link Exchange}
036 */
037public class ThrowExceptionProcessor extends ServiceSupport implements AsyncProcessor, Traceable, IdAware, CamelContextAware {
038    private String id;
039    private CamelContext camelContext;
040    private Expression simple;
041    private final Exception exception;
042    private final Class<? extends Exception> type;
043    private final String message;
044
045    public ThrowExceptionProcessor(Exception exception) {
046        this(exception, null, null);
047    }
048
049    public ThrowExceptionProcessor(Exception exception, Class<? extends Exception> type, String message) {
050        this.exception = exception;
051        this.type = type;
052        this.message = message;
053    }
054
055    public void process(Exchange exchange) throws Exception {
056        AsyncProcessorHelper.process(this, exchange);
057    }
058
059    @SuppressWarnings("unchecked")
060    public boolean process(Exchange exchange, AsyncCallback callback) {
061        Exception cause = exception;
062
063        try {
064            if (message != null && type != null) {
065                // create the message using simple language so it can be dynamic
066                String text = simple.evaluate(exchange, String.class);
067                // create a new exception of that type, and provide the message as
068                Constructor<?> constructor = type.getDeclaredConstructor(String.class);
069                cause = (Exception) constructor.newInstance(text);
070                exchange.setException(cause);
071            } else {
072                exchange.setException(cause);
073            }
074        } catch (Throwable e) {
075            exchange.setException(new CamelExchangeException("Error creating new instance of " + exception.getClass(), exchange, e));
076        }
077
078        callback.done(true);
079        return true;
080    }
081
082    public String getTraceLabel() {
083        String className = this.exception == null ? this.type.getSimpleName() : this.exception.getClass().getSimpleName();
084        return "throwException[" + className + "]";
085    }
086
087    public String getId() {
088        return id;
089    }
090
091    public void setId(String id) {
092        this.id = id;
093    }
094
095    public Exception getException() {
096        return exception;
097    }
098
099    public Class<? extends Exception> getType() {
100        return type;
101    }
102
103    public String getMessage() {
104        return message;
105    }
106
107    public CamelContext getCamelContext() {
108        return camelContext;
109    }
110
111    public void setCamelContext(CamelContext camelContext) {
112        this.camelContext = camelContext;
113    }
114
115    public String toString() {
116        return "ThrowException";
117    }
118
119    @Override
120    protected void doStart() throws Exception {
121        ObjectHelper.notNull(camelContext, "camelContext", this);
122
123        if (message != null) {
124            simple = camelContext.resolveLanguage("simple").createExpression(message);
125        }
126    }
127
128    @Override
129    protected void doStop() throws Exception {
130        // noop
131    }
132}