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.lang.reflect.Method;
020import java.util.Map;
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlRootElement;
025import javax.xml.bind.annotation.XmlTransient;
026
027import org.apache.camel.NoSuchBeanException;
028import org.apache.camel.Processor;
029import org.apache.camel.RuntimeCamelException;
030import org.apache.camel.Service;
031import org.apache.camel.processor.WrapProcessor;
032import org.apache.camel.spi.Metadata;
033import org.apache.camel.spi.Policy;
034import org.apache.camel.spi.RouteContext;
035import org.apache.camel.spi.TransactedPolicy;
036import org.apache.camel.util.CamelContextHelper;
037import org.apache.camel.util.ObjectHelper;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041/**
042 * Enables transaction on the route
043 *
044 * @version 
045 */
046@Metadata(label = "configuration")
047@XmlRootElement(name = "transacted")
048@XmlAccessorType(XmlAccessType.FIELD)
049public class TransactedDefinition extends OutputDefinition<TransactedDefinition> {
050
051    // TODO: Align this code with PolicyDefinition
052    // TODO: Camel 3 should be NoOutputDefinition
053
054    // JAXB does not support changing the ref attribute from required to optional
055    // if we extend PolicyDefinition so we must make a copy of the class
056    @XmlTransient
057    public static final String PROPAGATION_REQUIRED = "PROPAGATION_REQUIRED";
058
059    private static final Logger LOG = LoggerFactory.getLogger(TransactedDefinition.class);
060
061    @XmlTransient
062    protected Class<? extends Policy> type = TransactedPolicy.class;
063    @XmlAttribute
064    protected String ref;
065    @XmlTransient
066    private Policy policy;
067
068    public TransactedDefinition() {
069    }
070
071    public TransactedDefinition(Policy policy) {
072        this.policy = policy;
073    }
074
075    @Override
076    public String toString() {
077        String desc = description();
078        if (ObjectHelper.isEmpty(desc)) {
079            return "Transacted";
080        } else {
081            return "Transacted[" + desc + "]";
082        }
083    }
084    
085    protected String description() {
086        if (ref != null) {
087            return "ref:" + ref;
088        } else if (policy != null) {
089            return policy.toString();
090        } else {
091            return "";
092        }
093    }
094
095    @Override
096    public String getLabel() {
097        String desc = description();
098        if (ObjectHelper.isEmpty(desc)) {
099            return "transacted";
100        } else {
101            return "transacted[" + desc + "]";
102        }
103    }
104
105    @Override
106    public boolean isAbstract() {
107        return true;
108    }
109
110    @Override
111    public boolean isTopLevelOnly() {
112        // transacted is top level as we only allow have it configured once per route
113        return true;
114    }
115
116    @Override
117    public boolean isWrappingEntireOutput() {
118        return true;
119    }
120
121    public String getRef() {
122        return ref;
123    }
124
125    public void setRef(String ref) {
126        this.ref = ref;
127    }
128
129    /**
130     * Sets a policy type that this definition should scope within.
131     * <p/>
132     * Is used for convention over configuration situations where the policy
133     * should be automatic looked up in the registry and it should be based
134     * on this type. For instance a {@link org.apache.camel.spi.TransactedPolicy}
135     * can be set as type for easy transaction configuration.
136     * <p/>
137     * Will by default scope to the wide {@link Policy}
138     *
139     * @param type the policy type
140     */
141    public void setType(Class<? extends Policy> type) {
142        this.type = type;
143    }
144
145    /**
146     * Sets a reference to use for lookup the policy in the registry.
147     *
148     * @param ref the reference
149     * @return the builder
150     */
151    public TransactedDefinition ref(String ref) {
152        setRef(ref);
153        return this;
154    }
155
156    @Override
157    public Processor createProcessor(RouteContext routeContext) throws Exception {
158        Policy policy = resolvePolicy(routeContext);
159        ObjectHelper.notNull(policy, "policy", this);
160
161        // before wrap
162        policy.beforeWrap(routeContext, this);
163
164        // create processor after the before wrap
165        Processor childProcessor = this.createChildProcessor(routeContext, true);
166
167        // wrap
168        Processor target = policy.wrap(routeContext, childProcessor);
169
170        if (!(target instanceof Service)) {
171            // wrap the target so it becomes a service and we can manage its lifecycle
172            target = new WrapProcessor(target, childProcessor);
173        }
174        return target;
175    }
176
177    protected Policy resolvePolicy(RouteContext routeContext) {
178        if (policy != null) {
179            return policy;
180        }
181        return doResolvePolicy(routeContext, getRef(), type);
182    }
183
184    protected static Policy doResolvePolicy(RouteContext routeContext, String ref, Class<? extends Policy> type) {
185        // explicit ref given so lookup by it
186        if (ObjectHelper.isNotEmpty(ref)) {
187            return CamelContextHelper.mandatoryLookup(routeContext.getCamelContext(), ref, Policy.class);
188        }
189
190        // no explicit reference given from user so we can use some convention over configuration here
191
192        // try to lookup by scoped type
193        Policy answer = null;
194        if (type != null) {
195            // try find by type, note that this method is not supported by all registry
196            Map<String, ?> types = routeContext.lookupByType(type);
197            if (types.size() == 1) {
198                // only one policy defined so use it
199                Object found = types.values().iterator().next();
200                if (type.isInstance(found)) {
201                    return type.cast(found);
202                }
203            }
204        }
205
206        // for transacted routing try the default REQUIRED name
207        if (type == TransactedPolicy.class) {
208            // still not found try with the default name PROPAGATION_REQUIRED
209            answer = routeContext.lookup(PROPAGATION_REQUIRED, TransactedPolicy.class);
210        }
211
212        // this logic only applies if we are a transacted policy
213        // still no policy found then try lookup the platform transaction manager and use it as policy
214        if (answer == null && type == TransactedPolicy.class) {
215            Class<?> tmClazz = routeContext.getCamelContext().getClassResolver().resolveClass("org.springframework.transaction.PlatformTransactionManager");
216            if (tmClazz != null) {
217                // see if we can find the platform transaction manager in the registry
218                Map<String, ?> maps = routeContext.lookupByType(tmClazz);
219                if (maps.size() == 1) {
220                    // only one platform manager then use it as default and create a transacted
221                    // policy with it and default to required
222
223                    // as we do not want dependency on spring jars in the camel-core we use
224                    // reflection to lookup classes and create new objects and call methods
225                    // as this is only done during route building it does not matter that we
226                    // use reflection as performance is no a concern during route building
227                    Object transactionManager = maps.values().iterator().next();
228                    LOG.debug("One instance of PlatformTransactionManager found in registry: {}", transactionManager);
229                    Class<?> txClazz = routeContext.getCamelContext().getClassResolver().resolveClass("org.apache.camel.spring.spi.SpringTransactionPolicy");
230                    if (txClazz != null) {
231                        LOG.debug("Creating a new temporary SpringTransactionPolicy using the PlatformTransactionManager: {}", transactionManager);
232                        TransactedPolicy txPolicy = ObjectHelper.newInstance(txClazz, TransactedPolicy.class);
233                        Method method;
234                        try {
235                            method = txClazz.getMethod("setTransactionManager", tmClazz);
236                        } catch (NoSuchMethodException e) {
237                            throw new RuntimeCamelException("Cannot get method setTransactionManager(PlatformTransactionManager) on class: " + txClazz);
238                        }
239                        ObjectHelper.invokeMethod(method, txPolicy, transactionManager);
240                        return txPolicy;
241                    } else {
242                        // camel-spring is missing on the classpath
243                        throw new RuntimeCamelException("Cannot create a transacted policy as camel-spring.jar is not on the classpath!");
244                    }
245                } else {
246                    if (maps.isEmpty()) {
247                        throw new NoSuchBeanException(null, "PlatformTransactionManager");
248                    } else {
249                        throw new IllegalArgumentException("Found " + maps.size() + " PlatformTransactionManager in registry. "
250                                + "Cannot determine which one to use. Please configure a TransactionTemplate on the transacted policy.");
251                    }
252                }
253            }
254        }
255
256        return answer;
257    }
258
259}