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 org.apache.camel.CamelContext;
021    import static org.apache.camel.util.ObjectHelper.notNull;
022    import org.springframework.beans.BeansException;
023    import org.springframework.beans.factory.BeanFactory;
024    import org.springframework.beans.factory.BeanFactoryAware;
025    import org.springframework.beans.factory.FactoryBean;
026    import org.springframework.beans.factory.InitializingBean;
027    
028    import java.util.ArrayList;
029    
030    /**
031     * A {@link FactoryBean} which creates a RouteBuilder by parsing an XML file. This factory bean
032     * must be injected with a context and will then install the rules in the context when the routing rules
033     * are created.
034     *
035     * @version $Revision: 521369 $
036     */
037    public class RouteBuilderFactoryBean implements FactoryBean, BeanFactoryAware, InitializingBean {
038        private ArrayList<BuilderStatement> routes;
039        private BeanFactory beanFactory;
040        private CamelContext context;
041        private StatementRouteBuilder builder = new StatementRouteBuilder();
042    
043        public Object getObject() throws Exception {
044            return builder;
045        }
046    
047        public Class getObjectType() {
048            return StatementRouteBuilder.class;
049        }
050    
051        public boolean isSingleton() {
052            return true;
053        }
054    
055        public ArrayList<BuilderStatement> getRoutes() {
056            return routes;
057        }
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    }