Coverage Report - org.apache.camel.spring.RouteBuilderFinder
 
Classes in this File Line Coverage Branch Coverage Complexity
RouteBuilderFinder
63% 
100% 
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.builder.RouteBuilder;
 21  
 import org.apache.camel.util.ResolverUtil;
 22  
 import org.springframework.beans.BeansException;
 23  
 import org.springframework.context.ApplicationContext;
 24  
 import org.springframework.context.ApplicationContextAware;
 25  
 
 26  
 import java.lang.reflect.Constructor;
 27  
 import java.lang.reflect.Modifier;
 28  
 import java.util.List;
 29  
 import java.util.Map;
 30  
 import java.util.Set;
 31  
 
 32  
 /**
 33  
  * A helper class which will find all {@link RouteBuilder} instances on the classpath
 34  
  *
 35  
  * @version $Revision: 521369 $
 36  
  */
 37  
 public class RouteBuilderFinder implements ApplicationContextAware {
 38  6
     private String[] packages = {};
 39  
     private ApplicationContext applicationContext;
 40  6
     private ResolverUtil resolver = new ResolverUtil();
 41  
 
 42  0
     public RouteBuilderFinder(ApplicationContext applicationContext, String[] packages) {
 43  0
         this.applicationContext = applicationContext;
 44  0
         this.packages = packages;
 45  0
     }
 46  
 
 47  6
     public RouteBuilderFinder(CamelContextFactoryBean factoryBean) {
 48  6
         this.applicationContext = factoryBean.getApplicationContext();
 49  6
         this.packages = factoryBean.getPackages();
 50  6
     }
 51  
 
 52  
     public String[] getPackages() {
 53  0
         return packages;
 54  
     }
 55  
 
 56  
     public void setPackages(String[] packages) {
 57  0
         this.packages = packages;
 58  0
     }
 59  
 
 60  
     public ApplicationContext getApplicationContext() {
 61  0
         return applicationContext;
 62  
     }
 63  
 
 64  
     public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
 65  0
         this.applicationContext = applicationContext;
 66  0
     }
 67  
 
 68  
     /**
 69  
      * Appends all the {@link RouteBuilder} instances that can be found on the classpath
 70  
      */
 71  
     public void appendBuilders(List<RouteBuilder> list) throws IllegalAccessException, InstantiationException {
 72  6
         resolver.findImplementations(RouteBuilder.class, packages);
 73  
         //resolver.findAnnotated(Endpoint.class, packages);
 74  6
         Set<Class> classes = resolver.getClasses();
 75  6
         for (Class aClass : classes) {
 76  6
             if (shouldIgnoreBean(aClass)) {
 77  0
                 continue;
 78  
             }
 79  6
             if (isValidClass(aClass)) {
 80  6
                 list.add(instantiateBuilder(aClass));
 81  
             }
 82  6
         }
 83  6
     }
 84  
 
 85  
     public void destroy() throws Exception {
 86  0
     }
 87  
 
 88  
     /**
 89  
      * Lets ignore beans that are not explicitly configured in the spring.xml
 90  
      */
 91  
     protected boolean shouldIgnoreBean(Class type) {
 92  6
         Map beans = applicationContext.getBeansOfType(type, true, true);
 93  6
         if (beans == null || beans.isEmpty()) {
 94  6
             return false;
 95  
         }
 96  
         // TODO apply some filter?
 97  0
         return true;
 98  
     }
 99  
 
 100  
     /**
 101  
      * Returns true if the object is non-abstract and supports a zero argument constructor
 102  
      */
 103  
     protected boolean isValidClass(Class type) {
 104  6
         if (!Modifier.isAbstract(type.getModifiers()) && !type.isInterface()) {
 105  6
             Constructor[] constructors = type.getDeclaredConstructors();
 106  6
             for (Constructor constructor : constructors) {
 107  6
                 Class[] classes = constructor.getParameterTypes();
 108  6
                 if (classes.length == 0) {
 109  6
                     return true;
 110  
                 }
 111  
             }
 112  
         }
 113  0
         return false;
 114  
     }
 115  
 
 116  
     protected RouteBuilder instantiateBuilder(Class type) throws IllegalAccessException, InstantiationException {
 117  
         // TODO we could support spring-injection for these types
 118  6
         return (RouteBuilder) type.newInstance();
 119  
     }
 120  
 }