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.spring;
018    
019    import org.apache.camel.CamelContext;
020    import org.apache.camel.builder.RouteBuilder;
021    import org.apache.camel.spring.spi.SpringTransactionPolicy;
022    import org.apache.camel.spring.spi.TransactionErrorHandler;
023    import org.apache.camel.spring.spi.TransactionErrorHandlerBuilder;
024    import org.springframework.context.ApplicationContext;
025    import org.springframework.context.ApplicationContextAware;
026    import org.springframework.transaction.PlatformTransactionManager;
027    import org.springframework.transaction.support.TransactionTemplate;
028    
029    /**
030     * An extension of the {@link RouteBuilder} to provide some additional helper
031     * methods
032     *
033     * @version $Revision: 784803 $
034     */
035    public abstract class SpringRouteBuilder extends RouteBuilder implements ApplicationContextAware {
036        private ApplicationContext applicationContext;
037    
038        /**
039         * Looks up the bean with the given name in the application context and
040         * returns it, or throws an exception if the bean is not present or is not
041         * of the given type
042         *
043         * @param beanName the name of the bean in the application context
044         * @param type the type of the bean
045         * @return the bean
046         */
047        @SuppressWarnings("unchecked")
048        public <T> T lookup(String beanName, Class<T> type) {
049            ApplicationContext context = getApplicationContext();
050            return (T)context.getBean(beanName, type);
051        }
052    
053        /**
054         * Looks up the bean with the given type in the application context and
055         * returns it, or throws an exception if the bean is not present or there
056         * are multiple possible beans to choose from for the given type
057         *
058         * @param type the type of the bean
059         * @return the bean
060         */
061        @SuppressWarnings("unchecked")
062        public <T> T lookup(Class<T> type) {
063            ApplicationContext context = getApplicationContext();
064            String[] names = context.getBeanNamesForType(type, true, true);
065            if (names != null) {
066                int count = names.length;
067                if (count == 1) {
068                    // lets instantiate the single bean
069                    return (T)context.getBean(names[0]);
070                } else if (count > 1) {
071                    throw new IllegalArgumentException("Too many beans in the application context of type: " + type + ". Found: " + count);
072                }
073            }
074            throw new IllegalArgumentException("No bean available in the application context of type: " + type);
075        }
076    
077        /**
078         * Returns the application context which has been configured via the
079         * {@link #setApplicationContext(ApplicationContext)} method or from the
080         * underlying {@link SpringCamelContext}
081         */
082        public ApplicationContext getApplicationContext() {
083            if (applicationContext == null) {
084                CamelContext camelContext = getContext();
085                if (camelContext instanceof SpringCamelContext) {
086                    SpringCamelContext springCamelContext = (SpringCamelContext)camelContext;
087                    return springCamelContext.getApplicationContext();
088                } else {
089                    throw new IllegalArgumentException("This SpringBuilder is not being used with a SpringCamelContext and there is no applicationContext property configured");
090                }
091            }
092            return applicationContext;
093        }
094    
095        /**
096         * Sets the application context to use to lookup beans
097         */
098        public void setApplicationContext(ApplicationContext applicationContext) {        
099            this.applicationContext = applicationContext;
100        }
101    
102        /**
103         * Creates a transaction error handler that will lookup in application context for
104         * an exiting transaction manager.
105         *
106         * @return the created error handler
107         */
108        public TransactionErrorHandlerBuilder transactionErrorHandler() {
109            return new TransactionErrorHandlerBuilder();
110        }
111    
112        /**
113         * Creates a transaction error handler.
114         *
115         * @param policy   using this transaction policy (eg: required, supports, ...)
116         * @return the created error handler
117         */
118        public TransactionErrorHandlerBuilder transactionErrorHandler(SpringTransactionPolicy policy) {
119            return transactionErrorHandler(policy.getTransactionTemplate());
120        }
121    
122        /**
123         * Creates a transaction error handler.
124         *
125         * @param template the spring transaction template
126         * @return the created error handler
127         */
128        public TransactionErrorHandlerBuilder transactionErrorHandler(TransactionTemplate template) {
129            TransactionErrorHandlerBuilder answer = new TransactionErrorHandlerBuilder();
130            answer.setTransactionTemplate(template);
131            return answer;
132        }
133    
134        /**
135         * Creates a transaction error handler.
136         *
137         * @param transactionManager the spring transaction manager
138         * @return the created error handler
139         */
140        public TransactionErrorHandlerBuilder transactionErrorHandler(PlatformTransactionManager transactionManager) {
141            TransactionTemplate template = new TransactionTemplate(transactionManager);
142            return transactionErrorHandler(template);
143        }
144    
145    }