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