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.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 org.apache.camel.builder.RouteBuilder} instances on the classpath 033 * 034 * @version $Revision: 954861 $ 035 */ 036 public class PackageScanRouteBuilderFinder { 037 private static final transient Log LOG = LogFactory.getLog(PackageScanRouteBuilderFinder.class); 038 private final SpringCamelContext camelContext; 039 private final String[] packages; 040 private final PackageScanClassResolver resolver; 041 private final ApplicationContext applicationContext; 042 private final BeanPostProcessor beanPostProcessor; 043 044 public PackageScanRouteBuilderFinder(SpringCamelContext camelContext, String[] packages, ClassLoader classLoader, 045 BeanPostProcessor postProcessor, PackageScanClassResolver resolver) { 046 this.camelContext = camelContext; 047 this.applicationContext = camelContext.getApplicationContext(); 048 this.packages = packages; 049 this.beanPostProcessor = postProcessor; 050 this.resolver = resolver; 051 // add our provided loader as well 052 resolver.addClassLoader(classLoader); 053 } 054 055 /** 056 * Appends all the {@link org.apache.camel.builder.RouteBuilder} instances that can be found on the classpath 057 */ 058 public void appendBuilders(List<RoutesBuilder> list) throws IllegalAccessException, InstantiationException { 059 Set<Class<?>> classes = resolver.findImplementations(RoutesBuilder.class, packages); 060 for (Class aClass : classes) { 061 if (LOG.isTraceEnabled()) { 062 LOG.trace("Found RouteBuilder class: " + aClass); 063 } 064 065 // certain beans should be ignored 066 if (shouldIgnoreBean(aClass)) { 067 if (LOG.isDebugEnabled()) { 068 LOG.debug("Ignoring RouteBuilder class: " + aClass); 069 } 070 continue; 071 } 072 073 if (!isValidClass(aClass)) { 074 if (LOG.isDebugEnabled()) { 075 LOG.debug("Ignoring invalid RouteBuilder class: " + aClass); 076 } 077 continue; 078 } 079 080 // type is valid so create and instantiate the builder 081 RoutesBuilder builder = instantiateBuilder(aClass); 082 if (beanPostProcessor != null) { 083 // Inject the annotated resource 084 beanPostProcessor.postProcessBeforeInitialization(builder, builder.toString()); 085 } 086 if (LOG.isDebugEnabled()) { 087 LOG.debug("Adding instantiated RouteBuilder: " + builder); 088 } 089 list.add(builder); 090 } 091 } 092 093 /** 094 * Lets ignore beans that are explicitly configured in the Spring XML files 095 */ 096 protected boolean shouldIgnoreBean(Class<?> type) { 097 Map beans = applicationContext.getBeansOfType(type, true, true); 098 if (beans == null || beans.isEmpty()) { 099 return false; 100 } 101 return true; 102 } 103 104 /** 105 * Returns true if the object is non-abstract and supports a zero argument constructor 106 */ 107 protected boolean isValidClass(Class type) { 108 if (!Modifier.isAbstract(type.getModifiers()) && !type.isInterface()) { 109 return true; 110 } 111 return false; 112 } 113 114 @SuppressWarnings("unchecked") 115 protected RoutesBuilder instantiateBuilder(Class type) throws IllegalAccessException, InstantiationException { 116 return (RoutesBuilder) camelContext.getInjector().newInstance(type); 117 } 118 }