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