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 return "throwException[" + exception.getClass().getSimpleName() + "]"; 084 } 085 086 public String getId() { 087 return id; 088 } 089 090 public void setId(String id) { 091 this.id = id; 092 } 093 094 public Exception getException() { 095 return exception; 096 } 097 098 public Class<? extends Exception> getType() { 099 return type; 100 } 101 102 public String getMessage() { 103 return message; 104 } 105 106 public CamelContext getCamelContext() { 107 return camelContext; 108 } 109 110 public void setCamelContext(CamelContext camelContext) { 111 this.camelContext = camelContext; 112 } 113 114 public String toString() { 115 return "ThrowException"; 116 } 117 118 @Override 119 protected void doStart() throws Exception { 120 ObjectHelper.notNull(camelContext, "camelContext", this); 121 122 if (message != null) { 123 simple = camelContext.resolveLanguage("simple").createExpression(message); 124 } 125 } 126 127 @Override 128 protected void doStop() throws Exception { 129 // noop 130 } 131}