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