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