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