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.Exchange;
020    import org.apache.camel.Processor;
021    import org.apache.camel.RollbackExchangeException;
022    
023    /**
024     * Processor for marking an {@link org.apache.camel.Exchange} to rollback.
025     *
026     * @version $Revision: 886781 $
027     */
028    public class RollbackProcessor implements Processor, Traceable {
029    
030        private boolean markRollbackOnly;
031        private boolean markRollbackOnlyLast;
032        private String message;
033    
034        public RollbackProcessor() {
035        }
036    
037        public RollbackProcessor(String message) {
038            this.message = message;
039        }
040    
041        public void process(Exchange exchange) throws Exception {
042            if (isMarkRollbackOnlyLast()) {
043                // only mark the last route (current) as rollback
044                // this is needed when you have multiple transactions in play
045                exchange.setProperty(Exchange.ROLLBACK_ONLY_LAST, Boolean.TRUE);
046            } else {
047                // default to mark the entire route as rollback
048                exchange.setProperty(Exchange.ROLLBACK_ONLY, Boolean.TRUE);
049            }
050    
051            if (markRollbackOnly || markRollbackOnlyLast) {
052                // do not do anything more as we should only mark the rollback
053                return;
054            }
055    
056            if (message != null) {
057                exchange.setException(new RollbackExchangeException(message, exchange));
058            } else {
059                exchange.setException(new RollbackExchangeException(exchange));
060            }
061        }
062    
063        @Override
064        public String toString() {
065            if (message != null) {
066                return "Rollback[" + message + "]";
067            } else {
068                return "Rollback";
069            }
070        }
071    
072        public String getTraceLabel() {
073            return "rollback";
074        }
075    
076        public boolean isMarkRollbackOnly() {
077            return markRollbackOnly;
078        }
079    
080        public void setMarkRollbackOnly(boolean markRollbackOnly) {
081            this.markRollbackOnly = markRollbackOnly;
082        }
083    
084        public boolean isMarkRollbackOnlyLast() {
085            return markRollbackOnlyLast;
086        }
087    
088        public void setMarkRollbackOnlyLast(boolean markRollbackOnlyLast) {
089            this.markRollbackOnlyLast = markRollbackOnlyLast;
090        }
091    }