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.javaconfig;
018    
019    import java.util.List;
020    
021    import org.apache.camel.CamelContext;
022    import org.apache.camel.Routes;
023    import org.apache.camel.builder.RouteBuilder;
024    import org.apache.camel.spring.CamelBeanPostProcessor;
025    import org.apache.camel.spring.SpringCamelContext;
026    import org.springframework.config.java.annotation.Bean;
027    import org.springframework.config.java.annotation.Configuration;
028    import org.springframework.config.java.support.ConfigurationSupport;
029    
030    /**
031     * A useful base class for writing
032     * <a href="http://www.springsource.org/javaconfig">Spring JavaConfig</a>
033     * configurations for working with Camel
034     *
035     * @version $Revision: 758543 $
036     */
037    @Configuration
038    public abstract class CamelConfiguration extends ConfigurationSupport {
039    
040        @Bean
041        public CamelBeanPostProcessor camelBeanPostProcessor() throws Exception {
042            CamelBeanPostProcessor answer = new CamelBeanPostProcessor();
043    
044            CamelContext camelContext = getBean(CamelContext.class);
045            // lets lookup a bean
046            answer.setCamelContext(camelContext);        
047            return answer;
048        }
049    
050        /**
051         * Returns the CamelContext
052         */
053        @Bean
054        public CamelContext camelContext() throws Exception {
055            SpringCamelContext camelContext = new SpringCamelContext();        
056            setupCamelContext(camelContext);
057            List<RouteBuilder> routes = routes();
058            for (Routes route : routes) {
059                camelContext.addRoutes(route);
060            }        
061            return camelContext;
062        }
063        
064        // maybe register the camel component, language here
065        public void setupCamelContext(CamelContext camelContext) throws Exception {
066            
067        }
068    
069    
070        /**
071         * Returns the list of routes to use in this configuration
072         */
073        public abstract List<RouteBuilder> routes();
074    
075    }