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    @XmlElementRef
061    @Override
062    public void setOutputs(List<ProcessorDefinition<?>> outputs) {
063        super.setOutputs(outputs);
064    }
065
066    @Override
067    public String toString() {
068        String desc = description();
069        if (org.apache.camel.util.ObjectHelper.isEmpty(desc)) {
070            return "Transacted";
071        } else {
072            return "Transacted[" + desc + "]";
073        }
074    }
075
076    protected String description() {
077        if (ref != null) {
078            return "ref:" + ref;
079        } else if (policy != null) {
080            return policy.toString();
081        } else {
082            return "";
083        }
084    }
085
086    @Override
087    public String getShortName() {
088        return "transacted";
089    }
090
091    @Override
092    public String getLabel() {
093        String desc = description();
094        if (org.apache.camel.util.ObjectHelper.isEmpty(desc)) {
095            return "transacted";
096        } else {
097            return "transacted[" + desc + "]";
098        }
099    }
100
101    @Override
102    public boolean isAbstract() {
103        return true;
104    }
105
106    @Override
107    public boolean isTopLevelOnly() {
108        // transacted is top level as we only allow have it configured once per
109        // route
110        return true;
111    }
112
113    @Override
114    public boolean isWrappingEntireOutput() {
115        return true;
116    }
117
118    public Policy getPolicy() {
119        return policy;
120    }
121
122    public String getRef() {
123        return ref;
124    }
125
126    public void setRef(String ref) {
127        this.ref = ref;
128    }
129
130    public Class<? extends Policy> getType() {
131        return type;
132    }
133
134    /**
135     * Sets a policy type that this definition should scope within.
136     * <p/>
137     * Is used for convention over configuration situations where the policy
138     * should be automatic looked up in the registry and it should be based on
139     * this type. For instance a {@link org.apache.camel.spi.TransactedPolicy}
140     * can be set as type for easy transaction configuration.
141     * <p/>
142     * Will by default scope to the wide {@link Policy}
143     *
144     * @param type the policy type
145     */
146    public void setType(Class<? extends Policy> type) {
147        this.type = type;
148    }
149
150    /**
151     * Sets a reference to use for lookup the policy in the registry.
152     *
153     * @param ref the reference
154     * @return the builder
155     */
156    public TransactedDefinition ref(String ref) {
157        setRef(ref);
158        return this;
159    }
160
161}