001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.spring;
019    
020    import org.apache.camel.CamelContext;
021    import org.apache.camel.Exchange;
022    import org.apache.camel.builder.RouteBuilder;
023    import org.springframework.context.ApplicationContext;
024    
025    /**
026     * An extension of the {@link RouteBuilder} to provide some additional helper methods
027     *
028     * @version $Revision: 1.1 $
029     */
030    public abstract class SpringRouteBuilder extends RouteBuilder {
031        private ApplicationContext applicationContext;
032    
033        /**
034         * Looks up the bean with the given name in the application context and returns it, or throws an exception if the
035         * bean is not present or is not of the given type
036         *
037         * @param type     the type of the bean
038         * @param beanName the name of the bean in the application context
039         * @return the bean
040         */
041        public <T> T bean(Class<T> type, String beanName) {
042            ApplicationContext context = getApplicationContext();
043            return (T) context.getBean(beanName, type);
044        }
045    
046        /**
047         * Looks up the bean with the given type in the application context and returns it, or throws an exception if the
048         * bean is not present or there are multiple possible beans to choose from for the given type
049         *
050         * @param type the type of the bean
051         * @return the bean
052         */
053        public <T> T bean(Class<T> type) {
054            ApplicationContext context = getApplicationContext();
055            String[] names = context.getBeanNamesForType(type, true, true);
056            if (names != null) {
057                int count = names.length;
058                if (count == 1) {
059                    // lets instantiate the single bean
060                    return (T) context.getBean(names[0]);
061                }
062                else if (count > 1) {
063                    throw new IllegalArgumentException("Too many beans in the application context of type: " + type + ". Found: " + count);
064                }
065            }
066            throw new IllegalArgumentException("No bean available in the application context of type: " + type);
067        }
068    
069        /**
070         * Returns the application context which has been configured via the {@link #setApplicationContext(ApplicationContext)}
071         * method  or from the underlying {@link SpringCamelContext}
072         * 
073         * @return
074         */
075        public ApplicationContext getApplicationContext() {
076            if (applicationContext == null) {
077                CamelContext camelContext = getContext();
078                if (camelContext instanceof SpringCamelContext) {
079                    SpringCamelContext springCamelContext = (SpringCamelContext) camelContext;
080                    return springCamelContext.getApplicationContext();
081                }
082                else {
083                    throw new IllegalArgumentException("This SpringBuilder is not being used with a SpringCamelContext and there is no applicationContext property configured");
084                }
085            }
086            return applicationContext;
087        }
088    
089        /**
090         * Sets the application context to use to lookup beans
091         */
092        public void setApplicationContext(ApplicationContext applicationContext) {
093            this.applicationContext = applicationContext;
094        }
095    }