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