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    public boolean process(Exchange exchange, AsyncCallback callback) {
060        Exception cause = exception;
061
062        try {
063            if (message != null && type != null) {
064                // create the message using simple language so it can be dynamic
065                String text = simple.evaluate(exchange, String.class);
066                // create a new exception of that type, and provide the message as
067                Constructor<?> constructor = type.getDeclaredConstructor(String.class);
068                cause = (Exception) constructor.newInstance(text);
069                exchange.setException(cause);
070            } else if (cause == null && type != null) {
071                // create a new exception of that type using its default constructor
072                Constructor<?> constructor = type.getDeclaredConstructor();
073                cause = (Exception) constructor.newInstance();
074                exchange.setException(cause);
075            } else {
076                exchange.setException(cause);
077            }
078        } catch (Throwable e) {
079            exchange.setException(new CamelExchangeException("Error creating new instance of " + exception.getClass(), exchange, e));
080        }
081
082        callback.done(true);
083        return true;
084    }
085
086    public String getTraceLabel() {
087        String className = this.exception == null ? this.type.getSimpleName() : this.exception.getClass().getSimpleName();
088        return "throwException[" + className + "]";
089    }
090
091    public String getId() {
092        return id;
093    }
094
095    public void setId(String id) {
096        this.id = id;
097    }
098
099    public Exception getException() {
100        return exception;
101    }
102
103    public Class<? extends Exception> getType() {
104        return type;
105    }
106
107    public String getMessage() {
108        return message;
109    }
110
111    public CamelContext getCamelContext() {
112        return camelContext;
113    }
114
115    public void setCamelContext(CamelContext camelContext) {
116        this.camelContext = camelContext;
117    }
118
119    public String toString() {
120        return "ThrowException";
121    }
122
123    @Override
124    protected void doStart() throws Exception {
125        ObjectHelper.notNull(camelContext, "camelContext", this);
126
127        if (message != null) {
128            simple = camelContext.resolveLanguage("simple").createExpression(message);
129        }
130    }
131
132    @Override
133    protected void doStop() throws Exception {
134        // noop
135    }
136}