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 org.apache.camel.AsyncCallback;
020import org.apache.camel.AsyncProcessor;
021import org.apache.camel.Exchange;
022import org.apache.camel.RollbackExchangeException;
023import org.apache.camel.Traceable;
024import org.apache.camel.spi.IdAware;
025import org.apache.camel.support.ServiceSupport;
026import org.apache.camel.util.AsyncProcessorHelper;
027
028/**
029 * Processor for marking an {@link org.apache.camel.Exchange} to rollback.
030 *
031 * @version 
032 */
033public class RollbackProcessor extends ServiceSupport implements AsyncProcessor, Traceable, IdAware {
034
035    private String id;
036    private boolean markRollbackOnly;
037    private boolean markRollbackOnlyLast;
038    private String message;
039
040    public RollbackProcessor() {
041    }
042
043    public RollbackProcessor(String message) {
044        this.message = message;
045    }
046
047    public void process(Exchange exchange) throws Exception {
048        AsyncProcessorHelper.process(this, exchange);
049    }
050
051    public boolean process(Exchange exchange, AsyncCallback callback) {
052        if (isMarkRollbackOnlyLast()) {
053            // only mark the last route (current) as rollback
054            // this is needed when you have multiple transactions in play
055            exchange.setProperty(Exchange.ROLLBACK_ONLY_LAST, Boolean.TRUE);
056        } else {
057            // default to mark the entire route as rollback
058            exchange.setProperty(Exchange.ROLLBACK_ONLY, Boolean.TRUE);
059        }
060
061        if (markRollbackOnly || markRollbackOnlyLast) {
062            // do not do anything more as we should only mark the rollback
063            callback.done(true);
064            return true;
065        }
066
067        // throw exception to rollback
068        if (message != null) {
069            exchange.setException(new RollbackExchangeException(message, exchange));
070        } else {
071            exchange.setException(new RollbackExchangeException(exchange));
072        }
073
074        callback.done(true);
075        return true;
076    }
077
078    @Override
079    public String toString() {
080        if (message != null) {
081            return "Rollback[" + message + "]";
082        } else {
083            return "Rollback";
084        }
085    }
086
087    public String getTraceLabel() {
088        return "rollback";
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 String getMessage() {
100        return message;
101    }
102
103    public boolean isMarkRollbackOnly() {
104        return markRollbackOnly;
105    }
106
107    public void setMarkRollbackOnly(boolean markRollbackOnly) {
108        this.markRollbackOnly = markRollbackOnly;
109    }
110
111    public boolean isMarkRollbackOnlyLast() {
112        return markRollbackOnlyLast;
113    }
114
115    public void setMarkRollbackOnlyLast(boolean markRollbackOnlyLast) {
116        this.markRollbackOnlyLast = markRollbackOnlyLast;
117    }
118
119    @Override
120    protected void doStart() throws Exception {
121        // noop
122    }
123
124    @Override
125    protected void doStop() throws Exception {
126        // noop
127    }
128}