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