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.builder.RouteBuilder; 025 import org.apache.camel.util.ResolverUtil; 026 import org.apache.commons.logging.Log; 027 import org.apache.commons.logging.LogFactory; 028 import org.springframework.beans.factory.config.BeanPostProcessor; 029 import org.springframework.context.ApplicationContext; 030 031 /** 032 * A helper class which will find all {@link RouteBuilder} instances on the classpath 033 * 034 * @version $Revision: 656103 $ 035 */ 036 public class RouteBuilderFinder { 037 private static final transient Log LOG = LogFactory.getLog(RouteBuilderFinder.class); 038 private final SpringCamelContext camelContext; 039 private final String[] packages; 040 private ApplicationContext applicationContext; 041 private ResolverUtil resolver = new ResolverUtil(); 042 private BeanPostProcessor beanPostProcessor; 043 044 public RouteBuilderFinder(SpringCamelContext camelContext, String[] packages, ClassLoader classLoader, BeanPostProcessor postProcessor) { 045 this.camelContext = camelContext; 046 this.applicationContext = camelContext.getApplicationContext(); 047 this.packages = packages; 048 this.beanPostProcessor = postProcessor; 049 050 // lets add all the available class loaders just in case of weirdness 051 // we could make this more strict once we've worked out all the gremlins 052 // in servicemix-camel 053 Set set = resolver.getClassLoaders(); 054 set.clear(); 055 set.add(classLoader); 056 /* 057 set.add(classLoader); 058 set.add(applicationContext.getClassLoader()); 059 set.add(getClass().getClassLoader()); 060 */ 061 } 062 063 064 public String[] getPackages() { 065 return packages; 066 } 067 068 public ApplicationContext getApplicationContext() { 069 return applicationContext; 070 } 071 072 073 /** 074 * Appends all the {@link RouteBuilder} instances that can be found on the classpath 075 */ 076 public void appendBuilders(List<RouteBuilder> list) throws IllegalAccessException, InstantiationException { 077 resolver.findImplementations(RouteBuilder.class, packages); 078 Set<Class> classes = resolver.getClasses(); 079 for (Class aClass : classes) { 080 if (shouldIgnoreBean(aClass)) { 081 continue; 082 } 083 if (isValidClass(aClass)) { 084 RouteBuilder builder = instantiateBuilder(aClass); 085 if (beanPostProcessor != null) { 086 // Inject the annotated resource 087 beanPostProcessor.postProcessBeforeInitialization(builder, builder.toString()); 088 } 089 list.add(builder); 090 } 091 } 092 } 093 094 public void destroy() throws Exception { 095 } 096 097 /** 098 * Lets ignore beans that are not explicitly configured in the spring.xml 099 */ 100 protected boolean shouldIgnoreBean(Class type) { 101 Map beans = applicationContext.getBeansOfType(type, true, true); 102 if (beans == null || beans.isEmpty()) { 103 return false; 104 } 105 // TODO apply some filter? 106 return true; 107 } 108 109 /** 110 * Returns true if the object is non-abstract and supports a zero argument constructor 111 */ 112 protected boolean isValidClass(Class type) { 113 if (!Modifier.isAbstract(type.getModifiers()) && !type.isInterface()) { 114 return true; 115 } 116 return false; 117 } 118 119 protected RouteBuilder instantiateBuilder(Class type) throws IllegalAccessException, InstantiationException { 120 return (RouteBuilder) camelContext.getInjector().newInstance(type); 121 } 122 }