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.model;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023
024import org.apache.camel.Processor;
025import org.apache.camel.processor.RollbackProcessor;
026import org.apache.camel.spi.Metadata;
027import org.apache.camel.spi.RouteContext;
028
029/**
030 * Forces a rollback by stopping routing the message
031 */
032@Metadata(label = "eip,routing")
033@XmlRootElement(name = "rollback")
034@XmlAccessorType(XmlAccessType.FIELD)
035public class RollbackDefinition extends NoOutputDefinition<RollbackDefinition> {
036    @XmlAttribute
037    private Boolean markRollbackOnly;
038    @XmlAttribute
039    private Boolean markRollbackOnlyLast;
040    @XmlAttribute
041    private String message;
042
043    public RollbackDefinition() {
044    }
045
046    public RollbackDefinition(String message) {
047        this.message = message;
048    }
049
050    @Override
051    public String toString() {
052        if (message != null) {
053            return "Rollback[" + message + "]";
054        } else {
055            return "Rollback";
056        }
057    }
058
059    @Override
060    public String getShortName() {
061        return "rollback";
062    }
063
064    @Override
065    public String getLabel() {
066        return "rollback";
067    }
068
069    @Override
070    public Processor createProcessor(RouteContext routeContext) {
071        boolean isMarkRollbackOnly = getMarkRollbackOnly() != null && getMarkRollbackOnly();
072        boolean isMarkRollbackOnlyLast = getMarkRollbackOnlyLast() != null && getMarkRollbackOnlyLast();
073
074        // validate that only either mark rollbacks is chosen and not both
075        if (isMarkRollbackOnly && isMarkRollbackOnlyLast) {
076            throw new IllegalArgumentException("Only either one of markRollbackOnly and markRollbackOnlyLast is possible to select as true");
077        }
078
079        RollbackProcessor answer = new RollbackProcessor(message);
080        answer.setMarkRollbackOnly(isMarkRollbackOnly);
081        answer.setMarkRollbackOnlyLast(isMarkRollbackOnlyLast);
082        return answer;
083    }
084
085    public String getMessage() {
086        return message;
087    }
088
089    /**
090     * Message to use in rollback exception
091     */
092    public void setMessage(String message) {
093        this.message = message;
094    }
095
096    public Boolean getMarkRollbackOnly() {
097        return markRollbackOnly;
098    }
099
100    /**
101     * Mark the transaction for rollback only (cannot be overruled to commit)
102     */
103    public void setMarkRollbackOnly(Boolean markRollbackOnly) {
104        this.markRollbackOnly = markRollbackOnly;
105    }
106
107    public Boolean getMarkRollbackOnlyLast() {
108        return markRollbackOnlyLast;
109    }
110
111    /**
112     * Mark only last sub transaction for rollback only.
113     * <p/>
114     * When using sub transactions (if the transaction manager support this)
115     */
116    public void setMarkRollbackOnlyLast(Boolean markRollbackOnlyLast) {
117        this.markRollbackOnlyLast = markRollbackOnlyLast;
118    }
119
120}