Coverage Report - org.apache.camel.spring.CamelContextFactoryBean
 
Classes in this File Line Coverage Branch Coverage Complexity
CamelContextFactoryBean
80% 
89% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.spring;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.List;
 21  
 
 22  
 import javax.xml.bind.annotation.XmlAccessType;
 23  
 import javax.xml.bind.annotation.XmlAccessorType;
 24  
 import javax.xml.bind.annotation.XmlElement;
 25  
 import javax.xml.bind.annotation.XmlElements;
 26  
 import javax.xml.bind.annotation.XmlRootElement;
 27  
 import javax.xml.bind.annotation.XmlTransient;
 28  
 
 29  
 import org.apache.camel.RuntimeCamelException;
 30  
 import org.apache.camel.builder.RouteBuilder;
 31  
 import org.apache.camel.model.IdentifiedType;
 32  
 import org.apache.camel.model.RouteContainer;
 33  
 import org.apache.camel.model.RouteType;
 34  
 import org.apache.commons.logging.Log;
 35  
 import org.apache.commons.logging.LogFactory;
 36  
 
 37  
 import org.springframework.beans.factory.DisposableBean;
 38  
 import org.springframework.beans.factory.FactoryBean;
 39  
 import org.springframework.beans.factory.InitializingBean;
 40  
 import org.springframework.context.ApplicationContext;
 41  
 import org.springframework.context.ApplicationContextAware;
 42  
 import org.springframework.context.ApplicationEvent;
 43  
 import org.springframework.context.ApplicationListener;
 44  
 import org.springframework.context.event.ContextRefreshedEvent;
 45  
 
 46  
 /**
 47  
  * A Spring {@link FactoryBean} to create and initialize a
 48  
  * {@link SpringCamelContext} and install routes either explicitly configured in
 49  
  * Spring XML or found by searching the classpath for Java classes which extend
 50  
  * {@link RouteBuilder} using the nested {@link #setPackages(String[])}.
 51  
  * 
 52  
  * @version $Revision: 565270 $
 53  
  */
 54  
 @XmlRootElement(name = "camelContext")
 55  
 @XmlAccessorType(XmlAccessType.FIELD)
 56  109
 public class CamelContextFactoryBean extends IdentifiedType implements RouteContainer, FactoryBean, InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener {
 57  1
     private static final Log LOG = LogFactory.getLog(CamelContextFactoryBean.class);
 58  
     @XmlElement(name = "package", required = false)
 59  109
     private String[] packages = {};
 60  
     @XmlElements({@XmlElement(name = "beanPostProcessor", type = CamelBeanPostProcessor.class, required = false), @XmlElement(name = "proxy", type = CamelProxyFactoryType.class, required = false),
 61  
                    @XmlElement(name = "export", type = CamelServiceExporterType.class, required = false) })
 62  
     private List beans;
 63  
     @XmlElement(name = "endpoint", required = false)
 64  
     private List<EndpointFactoryBean> endpoints;
 65  
     @XmlElement(name = "route", required = false)
 66  109
     private List<RouteType> routes = new ArrayList<RouteType>();
 67  
     @XmlTransient
 68  
     private SpringCamelContext context;
 69  
     @XmlTransient
 70  
     private RouteBuilder routeBuilder;
 71  
     @XmlTransient
 72  109
     private List<RouteBuilder> additionalBuilders = new ArrayList<RouteBuilder>();
 73  
     @XmlTransient
 74  
     private ApplicationContext applicationContext;
 75  
 
 76  
     public Object getObject() throws Exception {
 77  54
         return getContext();
 78  
     }
 79  
 
 80  
     public Class getObjectType() {
 81  233
         return SpringCamelContext.class;
 82  
     }
 83  
 
 84  
     public boolean isSingleton() {
 85  214
         return true;
 86  
     }
 87  
 
 88  
     public void afterPropertiesSet() throws Exception {
 89  
 
 90  
         // lets force any lazy creation
 91  56
         getContext();
 92  
 
 93  56
         LOG.debug("Found JAXB created routes: " + getRoutes());
 94  56
         String[] names = applicationContext.getBeanNamesForType(SpringInstrumentationAgent.class);
 95  56
         if (names.length == 1) {
 96  3
             applicationContext.getBean(names[0], SpringInstrumentationAgent.class);
 97  
         }
 98  
         
 99  56
         findRouteBuiders();
 100  56
         installRoutes();
 101  56
     }
 102  
 
 103  
     public void destroy() throws Exception {
 104  28
         getContext().stop();
 105  28
     }
 106  
 
 107  
     public void onApplicationEvent(ApplicationEvent event) {
 108  3
         if (LOG.isDebugEnabled()) {
 109  0
             LOG.debug("Publishing event: " + event);
 110  
         }
 111  
 
 112  3
         if (event instanceof ContextRefreshedEvent) {
 113  
             // now lets start the CamelContext so that all its possible
 114  
             // dependencies are initailized
 115  
             try {
 116  3
                 LOG.debug("Starting the context now!");
 117  3
                 getContext().start();
 118  0
             } catch (Exception e) {
 119  0
                 throw new RuntimeCamelException(e);
 120  3
             }
 121  
         }
 122  
         /*
 123  
          * if (context != null) { context.onApplicationEvent(event); }
 124  
          */
 125  3
     }
 126  
 
 127  
     // Properties
 128  
     // -------------------------------------------------------------------------
 129  
     public SpringCamelContext getContext() throws Exception {
 130  193
         if (context == null) {
 131  56
             context = new SpringCamelContext(getApplicationContext());
 132  
         }
 133  193
         return context;
 134  
     }
 135  
 
 136  
     public void setContext(SpringCamelContext context) {
 137  0
         this.context = context;
 138  0
     }
 139  
 
 140  
     public List<RouteType> getRoutes() {
 141  109
         return routes;
 142  
     }
 143  
 
 144  
     public void setRoutes(List<RouteType> routes) {
 145  53
         this.routes = routes;
 146  53
     }
 147  
 
 148  
     public RouteBuilder getRouteBuilder() {
 149  0
         return routeBuilder;
 150  
     }
 151  
 
 152  
     /**
 153  
      * Set a single {@link RouteBuilder} to be used to create the default routes
 154  
      * on startup
 155  
      */
 156  
     public void setRouteBuilder(RouteBuilder routeBuilder) {
 157  0
         this.routeBuilder = routeBuilder;
 158  0
     }
 159  
 
 160  
     /**
 161  
      * Set a collection of {@link RouteBuilder} instances to be used to create
 162  
      * the default routes on startup
 163  
      */
 164  
     public void setRouteBuilders(RouteBuilder[] builders) {
 165  0
         for (RouteBuilder builder : builders) {
 166  0
             additionalBuilders.add(builder);
 167  
         }
 168  0
     }
 169  
 
 170  
     public ApplicationContext getApplicationContext() {
 171  56
         return applicationContext;
 172  
     }
 173  
 
 174  
     public void setApplicationContext(ApplicationContext applicationContext) {
 175  56
         this.applicationContext = applicationContext;
 176  56
     }
 177  
 
 178  
     public String[] getPackages() {
 179  57
         return packages;
 180  
     }
 181  
 
 182  
     /**
 183  
      * Sets the package names to be recursively searched for Java classes which
 184  
      * extend {@link RouteBuilder} to be auto-wired up to the
 185  
      * {@link SpringCamelContext} as a route. Note that classes are excluded if
 186  
      * they are specifically configured in the spring.xml
 187  
      * 
 188  
      * @param packages the package names which are recursively searched
 189  
      */
 190  
     public void setPackages(String[] packages) {
 191  7
         this.packages = packages;
 192  7
     }
 193  
 
 194  
     // Implementation methods
 195  
     // -------------------------------------------------------------------------
 196  
 
 197  
     /**
 198  
      * Strategy to install all available routes into the context
 199  
      */
 200  
     protected void installRoutes() throws Exception {
 201  56
         for (RouteBuilder routeBuilder : additionalBuilders) {
 202  6
             getContext().addRoutes(routeBuilder);
 203  6
         }
 204  56
         if (routeBuilder != null) {
 205  0
             getContext().addRoutes(routeBuilder);
 206  
         }
 207  56
         for (RouteType route : routes) {
 208  39
             route.addRoutes(getContext());
 209  39
         }
 210  56
     }
 211  
 
 212  
     /**
 213  
      * Strategy method to try find {@link RouteBuilder} instances on the
 214  
      * classpath
 215  
      */
 216  
     protected void findRouteBuiders() throws Exception, InstantiationException {
 217  56
         if (packages != null && packages.length > 0) {
 218  7
             RouteBuilderFinder finder = new RouteBuilderFinder(getContext(), packages);
 219  7
             finder.appendBuilders(additionalBuilders);
 220  
         }
 221  56
     }
 222  
 
 223  
 }