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 java.util.List;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlElementRef;
025import javax.xml.bind.annotation.XmlRootElement;
026import javax.xml.bind.annotation.XmlTransient;
027
028import org.apache.camel.spi.Metadata;
029import org.apache.camel.spi.Policy;
030import org.apache.camel.spi.TransactedPolicy;
031
032/**
033 * Enables transaction on the route
034 */
035@Metadata(label = "configuration")
036@XmlRootElement(name = "transacted")
037@XmlAccessorType(XmlAccessType.FIELD)
038public class TransactedDefinition extends OutputDefinition<TransactedDefinition> {
039
040    // JAXB does not support changing the ref attribute from required to
041    // optional
042    // if we extend PolicyDefinition so we must make a copy of the class
043    @XmlTransient
044    public static final String PROPAGATION_REQUIRED = "PROPAGATION_REQUIRED";
045
046    @XmlTransient
047    protected Class<? extends Policy> type = TransactedPolicy.class;
048    @XmlAttribute
049    protected String ref;
050    @XmlTransient
051    private Policy policy;
052
053    public TransactedDefinition() {
054    }
055
056    public TransactedDefinition(Policy policy) {
057        this.policy = policy;
058    }
059
060    @Override
061    public List<ProcessorDefinition<?>> getOutputs() {
062        return outputs;
063    }
064
065    @XmlElementRef
066    @Override
067    public void setOutputs(List<ProcessorDefinition<?>> outputs) {
068        super.setOutputs(outputs);
069    }
070
071    @Override
072    public String toString() {
073        String desc = description();
074        if (org.apache.camel.util.ObjectHelper.isEmpty(desc)) {
075            return "Transacted";
076        } else {
077            return "Transacted[" + desc + "]";
078        }
079    }
080
081    protected String description() {
082        if (ref != null) {
083            return "ref:" + ref;
084        } else if (policy != null) {
085            return policy.toString();
086        } else {
087            return "";
088        }
089    }
090
091    @Override
092    public String getShortName() {
093        return "transacted";
094    }
095
096    @Override
097    public String getLabel() {
098        String desc = description();
099        if (org.apache.camel.util.ObjectHelper.isEmpty(desc)) {
100            return "transacted";
101        } else {
102            return "transacted[" + desc + "]";
103        }
104    }
105
106    @Override
107    public boolean isAbstract() {
108        return true;
109    }
110
111    @Override
112    public boolean isTopLevelOnly() {
113        // transacted is top level as we only allow have it configured once per
114        // route
115        return true;
116    }
117
118    @Override
119    public boolean isWrappingEntireOutput() {
120        return true;
121    }
122
123    public Policy getPolicy() {
124        return policy;
125    }
126
127    public String getRef() {
128        return ref;
129    }
130
131    public void setRef(String ref) {
132        this.ref = ref;
133    }
134
135    public Class<? extends Policy> getType() {
136        return type;
137    }
138
139    /**
140     * Sets a policy type that this definition should scope within.
141     * <p/>
142     * Is used for convention over configuration situations where the policy
143     * should be automatic looked up in the registry and it should be based on
144     * this type. For instance a {@link org.apache.camel.spi.TransactedPolicy}
145     * can be set as type for easy transaction configuration.
146     * <p/>
147     * Will by default scope to the wide {@link Policy}
148     *
149     * @param type the policy type
150     */
151    public void setType(Class<? extends Policy> type) {
152        this.type = type;
153    }
154
155    /**
156     * Sets a reference to use for lookup the policy in the registry.
157     *
158     * @param ref the reference
159     * @return the builder
160     */
161    public TransactedDefinition ref(String ref) {
162        setRef(ref);
163        return this;
164    }
165
166}