Coverage Report - org.apache.camel.spring.CamelContextFactoryBean
 
Classes in this File Line Coverage Branch Coverage Complexity
CamelContextFactoryBean
78% 
80% 
0
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.spring;
 19  
 
 20  
 import org.apache.camel.CamelContext;
 21  
 import org.apache.camel.builder.RouteBuilder;
 22  
 import org.springframework.beans.factory.DisposableBean;
 23  
 import org.springframework.beans.factory.FactoryBean;
 24  
 import org.springframework.beans.factory.InitializingBean;
 25  
 import org.springframework.context.ApplicationContext;
 26  
 import org.springframework.context.ApplicationContextAware;
 27  
 
 28  
 import java.util.ArrayList;
 29  
 import java.util.List;
 30  
 
 31  
 /**
 32  
  * A Spring {@link FactoryBean} to create and initialize a {@link SpringCamelContext}
 33  
  * and install routes either explicitly configured in Spring XML or found by searching the classpath for Java classes
 34  
  * which extend {@link RouteBuilder} using the nested {@link #setPackages(String[])}.
 35  
  *
 36  
  * @version $Revision: 534941 $
 37  
  */
 38  30
 public class CamelContextFactoryBean implements FactoryBean, InitializingBean, DisposableBean, ApplicationContextAware {
 39  
     private CamelContext context;
 40  
     private RouteBuilder routeBuilder;
 41  30
     private List<RouteBuilder> additionalBuilders = new ArrayList<RouteBuilder>();
 42  30
     private String[] packages = {};
 43  
     private ApplicationContext applicationContext;
 44  
 
 45  
     public Object getObject() throws Exception {
 46  28
         return getContext();
 47  
     }
 48  
 
 49  
     public Class getObjectType() {
 50  42
         return SpringCamelContext.class;
 51  
     }
 52  
 
 53  
     public boolean isSingleton() {
 54  79
         return true;
 55  
     }
 56  
 
 57  
     public void afterPropertiesSet() throws Exception {
 58  
 
 59  
         // lets force any lazy creation
 60  30
         getContext();
 61  
 
 62  30
         findRouteBuiders();
 63  30
         installRoutes();
 64  
 
 65  
         // now lets activate the routes
 66  30
         getContext().start();
 67  30
     }
 68  
 
 69  
     public void destroy() throws Exception {
 70  11
         getContext().stop();
 71  11
     }
 72  
 
 73  
     // Properties
 74  
     //-------------------------------------------------------------------------
 75  
     public CamelContext getContext() throws Exception {
 76  105
         if (context == null) {
 77  30
             context = new SpringCamelContext(getApplicationContext());
 78  
         }
 79  105
         return context;
 80  
     }
 81  
 
 82  
     public void setContext(CamelContext context) {
 83  0
         this.context = context;
 84  0
     }
 85  
 
 86  
     public RouteBuilder getRouteBuilder() {
 87  0
         return routeBuilder;
 88  
     }
 89  
 
 90  
     /**
 91  
      * Set a single {@link RouteBuilder} to be used to create the default routes on startup
 92  
      */
 93  
     public void setRouteBuilder(RouteBuilder routeBuilder) {
 94  0
         this.routeBuilder = routeBuilder;
 95  0
     }
 96  
 
 97  
     /**
 98  
      * Set a collection of {@link RouteBuilder} instances to be used to create the default routes on startup
 99  
      */
 100  
     public void setRouteBuilders(RouteBuilder[] builders) {
 101  0
         for (RouteBuilder builder : builders) {
 102  0
             additionalBuilders.add(builder);
 103  
         }
 104  0
     }
 105  
 
 106  
     public ApplicationContext getApplicationContext() {
 107  36
         return applicationContext;
 108  
     }
 109  
 
 110  
     public void setApplicationContext(ApplicationContext applicationContext) {
 111  30
         this.applicationContext = applicationContext;
 112  30
     }
 113  
 
 114  
     public String[] getPackages() {
 115  6
         return packages;
 116  
     }
 117  
 
 118  
     /**
 119  
      * Sets the package names to be recursively searched for Java classes which extend {@link RouteBuilder} to be auto-wired up to the
 120  
      * {@link SpringCamelContext} as a route. Note that classes are excluded if they are specifically configured in the spring.xml
 121  
      *
 122  
      * @param packages the package names which are recursively searched
 123  
      */
 124  
     public void setPackages(String[] packages) {
 125  6
         this.packages = packages;
 126  6
     }
 127  
 
 128  
     // Implementation methods
 129  
     //-------------------------------------------------------------------------
 130  
 
 131  
     /**
 132  
      * Strategy to install all available routes into the context
 133  
      */
 134  
     protected void installRoutes() throws Exception {
 135  30
         for (RouteBuilder routeBuilder : additionalBuilders) {
 136  6
             getContext().addRoutes(routeBuilder);
 137  6
         }
 138  30
         if (routeBuilder != null) {
 139  0
             getContext().addRoutes(routeBuilder);
 140  
         }
 141  30
     }
 142  
 
 143  
     /**
 144  
      * Strategy method to try find {@link RouteBuilder} instances on the classpath
 145  
      */
 146  
     protected void findRouteBuiders() throws IllegalAccessException, InstantiationException {
 147  30
         if (packages != null && packages.length > 0) {
 148  6
             RouteBuilderFinder finder = new RouteBuilderFinder(this);
 149  6
             finder.appendBuilders(additionalBuilders);
 150  
         }
 151  30
     }
 152  
 }