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 */
017 package org.apache.camel.processor;
018
019 import org.apache.camel.AsyncCallback;
020 import org.apache.camel.AsyncProcessor;
021 import org.apache.camel.CamelException;
022 import org.apache.camel.Exchange;
023 import org.apache.camel.Message;
024 import org.apache.camel.util.AsyncProcessorHelper;
025
026 public class HandleFaultProcessor extends DelegateProcessor implements AsyncProcessor {
027
028 @Override
029 public String toString() {
030 return "HandleFaultProcessor(" + processor + ")";
031 }
032
033 @Override
034 public void process(Exchange exchange) throws Exception {
035 AsyncProcessorHelper.process(this, exchange);
036 }
037
038 public boolean process(final Exchange exchange, final AsyncCallback callback) {
039 if (processor == null) {
040 // no processor so nothing to process, so return
041 callback.done(true);
042 return true;
043 }
044
045 if (processor instanceof AsyncProcessor) {
046 return ((AsyncProcessor)processor).process(exchange, new AsyncCallback() {
047
048 public void done(boolean doneSynchronously) {
049 // Take the fault message out before we keep on going
050 Message faultMessage = exchange.getFault(false);
051 if (faultMessage != null) {
052 final Object faultBody = faultMessage.getBody();
053 if (faultBody != null) {
054 faultMessage.setBody(null); // Reset it since we are handling it.
055 if (faultBody instanceof Throwable) {
056 exchange.setException((Throwable)faultBody);
057 } else {
058 exchange.setException(new CamelException("Message contains fault of type "
059 + faultBody.getClass().getName() + ":\n" + faultBody));
060 }
061 }
062 }
063 callback.done(doneSynchronously);
064 }
065 });
066 }
067
068 try {
069 processor.process(exchange);
070 } catch (Throwable e) {
071 exchange.setException(e);
072 }
073
074 final Message faultMessage = exchange.getFault(false);
075 if (faultMessage != null) {
076 final Object faultBody = faultMessage.getBody();
077 if (faultBody != null) {
078 faultMessage.setBody(null); // Reset it since we are handling it.
079 if (faultBody instanceof Throwable) {
080 exchange.setException((Throwable)faultBody);
081 } else {
082 exchange.setException(new CamelException("Message contains fault of type "
083 + faultBody.getClass().getName() + ":\n" + faultBody));
084 }
085 }
086 }
087 callback.done(true);
088 return true;
089 }
090 }