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