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.xml;
019    
020    import static org.apache.camel.util.ObjectHelper.notNull;
021    
022    import java.util.ArrayList;
023    
024    import org.apache.camel.CamelContext;
025    import org.springframework.beans.BeansException;
026    import org.springframework.beans.factory.BeanFactory;
027    import org.springframework.beans.factory.BeanFactoryAware;
028    import org.springframework.beans.factory.FactoryBean;
029    import org.springframework.beans.factory.InitializingBean;
030    
031    /**
032     * A {@link FactoryBean} which creates a RouteBuilder by parsing an XML file. This factory bean
033     * must be injected with a context and will then install the rules in the context when the routing rules
034     * are created. 
035     *
036     * @version $Revision: 521369 $
037     */
038    public class RouteBuilderFactoryBean implements FactoryBean, BeanFactoryAware, InitializingBean {
039            private ArrayList<BuilderStatement> routes;
040            private BeanFactory beanFactory;
041        private CamelContext context;
042        private StatementRouteBuilder builder = new StatementRouteBuilder();
043    
044        public Object getObject() throws Exception {
045            return builder;
046            }
047    
048            public Class getObjectType() {
049                    return StatementRouteBuilder.class;
050            }
051    
052            public boolean isSingleton() {
053                    return true;
054            }
055    
056            public ArrayList<BuilderStatement> getRoutes() {
057                    return routes;
058            }
059            public void setRoutes(ArrayList<BuilderStatement> routes) {
060                    this.routes = routes;
061            }
062    
063            public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
064                    this.beanFactory = beanFactory;
065            }
066    
067        public CamelContext getContext() {
068            return context;
069        }
070    
071        public void setContext(CamelContext context) {
072            this.context = context;
073        }
074    
075        public void afterPropertiesSet() throws Exception {
076            notNull(context, "context");
077            notNull(routes, "routes");
078                    builder.setBeanFactory(beanFactory);
079                    builder.setRoutes(routes);
080    
081            // now lets install the routes in the context
082            context.addRoutes(builder);
083        }
084    }