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.TransactionInterceptor;
022    
023    import org.springframework.context.ApplicationContext;
024    import org.springframework.transaction.support.TransactionTemplate;
025    
026    /**
027     * An extension of the {@link RouteBuilder} to provide some additional helper
028     * methods
029     * 
030     * @version $Revision: 642782 $
031     */
032    public abstract class SpringRouteBuilder extends RouteBuilder {
033        private ApplicationContext applicationContext;
034    
035        public TransactionInterceptor transactionInterceptor() {
036            return new TransactionInterceptor(bean(TransactionTemplate.class));
037        }
038    
039        /**
040         * Looks up the bean with the given name in the application context and
041         * returns it, or throws an exception if the bean is not present or is not
042         * of the given type
043         * 
044         * @param type the type of the bean
045         * @param beanName the name of the bean in the application context
046         * @return the bean
047         */
048        public <T> T bean(Class<T> type, String beanName) {
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        public <T> T bean(Class<T> type) {
062            ApplicationContext context = getApplicationContext();
063            String[] names = context.getBeanNamesForType(type, true, true);
064            if (names != null) {
065                int count = names.length;
066                if (count == 1) {
067                    // lets instantiate the single bean
068                    return (T)context.getBean(names[0]);
069                } else if (count > 1) {
070                    throw new IllegalArgumentException("Too many beans in the application context of type: " + type + ". Found: " + count);
071                }
072            }
073            throw new IllegalArgumentException("No bean available in the application context of type: " + type);
074        }
075    
076        /**
077         * Returns the application context which has been configured via the
078         * {@link #setApplicationContext(ApplicationContext)} method or from the
079         * underlying {@link SpringCamelContext}
080         */
081        public ApplicationContext getApplicationContext() {
082            if (applicationContext == null) {
083                CamelContext camelContext = getContext();
084                if (camelContext instanceof SpringCamelContext) {
085                    SpringCamelContext springCamelContext = (SpringCamelContext)camelContext;
086                    return springCamelContext.getApplicationContext();
087                } else {
088                    throw new IllegalArgumentException("This SpringBuilder is not being used with a SpringCamelContext and there is no applicationContext property configured");
089                }
090            }
091            return applicationContext;
092        }
093    
094        /**
095         * Sets the application context to use to lookup beans
096         */
097        public void setApplicationContext(ApplicationContext applicationContext) {
098            this.applicationContext = applicationContext;
099        }
100    }