001 /** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 package org.apache.camel.spring; 018 019 import java.lang.reflect.Modifier; 020 import java.util.List; 021 import java.util.Map; 022 import java.util.Set; 023 024 import org.apache.camel.RoutesBuilder; 025 import org.apache.camel.spi.PackageScanClassResolver; 026 import org.springframework.beans.factory.config.BeanPostProcessor; 027 import org.springframework.context.ApplicationContext; 028 029 /** 030 * A helper class which will find all {@link org.apache.camel.builder.RouteBuilder} instances on the classpath 031 * 032 * @version $Revision: 938746 $ 033 */ 034 public class RouteBuilderFinder { 035 private final SpringCamelContext camelContext; 036 private final String[] packages; 037 private PackageScanClassResolver resolver; 038 private ApplicationContext applicationContext; 039 private BeanPostProcessor beanPostProcessor; 040 041 public RouteBuilderFinder(SpringCamelContext camelContext, String[] packages, ClassLoader classLoader, 042 BeanPostProcessor postProcessor, PackageScanClassResolver resolver) { 043 this.camelContext = camelContext; 044 this.applicationContext = camelContext.getApplicationContext(); 045 this.packages = packages; 046 this.beanPostProcessor = postProcessor; 047 this.resolver = resolver; 048 // add our provided loader as well 049 resolver.addClassLoader(classLoader); 050 } 051 052 /** 053 * Appends all the {@link org.apache.camel.builder.RouteBuilder} instances that can be found on the classpath 054 */ 055 public void appendBuilders(List<RoutesBuilder> list) throws IllegalAccessException, InstantiationException { 056 Set<Class<?>> classes = resolver.findImplementations(RoutesBuilder.class, packages); 057 for (Class aClass : classes) { 058 if (shouldIgnoreBean(aClass)) { 059 continue; 060 } 061 if (isValidClass(aClass)) { 062 RoutesBuilder builder = instantiateBuilder(aClass); 063 if (beanPostProcessor != null) { 064 // Inject the annotated resource 065 beanPostProcessor.postProcessBeforeInitialization(builder, builder.toString()); 066 } 067 list.add(builder); 068 } 069 } 070 } 071 072 /** 073 * Lets ignore beans that are not explicitly configured in the spring.xml 074 */ 075 protected boolean shouldIgnoreBean(Class<?> type) { 076 Map beans = applicationContext.getBeansOfType(type, true, true); 077 if (beans == null || beans.isEmpty()) { 078 return false; 079 } 080 return true; 081 } 082 083 /** 084 * Returns true if the object is non-abstract and supports a zero argument constructor 085 */ 086 protected boolean isValidClass(Class type) { 087 if (!Modifier.isAbstract(type.getModifiers()) && !type.isInterface()) { 088 return true; 089 } 090 return false; 091 } 092 093 @SuppressWarnings("unchecked") 094 protected RoutesBuilder instantiateBuilder(Class type) throws IllegalAccessException, InstantiationException { 095 return (RoutesBuilder) camelContext.getInjector().newInstance(type); 096 } 097 }