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 getLabel() {
061        return "rollback";
062    }
063
064    @Override
065    public Processor createProcessor(RouteContext routeContext) {
066        boolean isMarkRollbackOnly = getMarkRollbackOnly() != null && getMarkRollbackOnly();
067        boolean isMarkRollbackOnlyLast = getMarkRollbackOnlyLast() != null && getMarkRollbackOnlyLast();
068
069        // validate that only either mark rollbacks is chosen and not both
070        if (isMarkRollbackOnly && isMarkRollbackOnlyLast) {
071            throw new IllegalArgumentException("Only either one of markRollbackOnly and markRollbackOnlyLast is possible to select as true");
072        }
073
074        RollbackProcessor answer = new RollbackProcessor(message);
075        answer.setMarkRollbackOnly(isMarkRollbackOnly);
076        answer.setMarkRollbackOnlyLast(isMarkRollbackOnlyLast);
077        return answer;
078    }
079
080    public String getMessage() {
081        return message;
082    }
083
084    /**
085     * Message to use in rollback exception
086     */
087    public void setMessage(String message) {
088        this.message = message;
089    }
090
091    public Boolean getMarkRollbackOnly() {
092        return markRollbackOnly;
093    }
094
095    /**
096     * Mark the transaction for rollback only (cannot be overruled to commit)
097     */
098    public void setMarkRollbackOnly(Boolean markRollbackOnly) {
099        this.markRollbackOnly = markRollbackOnly;
100    }
101
102    public Boolean getMarkRollbackOnlyLast() {
103        return markRollbackOnlyLast;
104    }
105
106    /**
107     * Mark only last sub transaction for rollback only.
108     * <p/>
109     * When using sub transactions (if the transaction manager support this)
110     */
111    public void setMarkRollbackOnlyLast(Boolean markRollbackOnlyLast) {
112        this.markRollbackOnlyLast = markRollbackOnlyLast;
113    }
114
115}