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