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 027 import org.springframework.context.ApplicationContext; 028 029 /** 030 * A helper class which will find all {@link RouteBuilder} instances on the classpath 031 * 032 * @version $Revision: 563665 $ 033 */ 034 public class RouteBuilderFinder { 035 private final SpringCamelContext camelContext; 036 private final String[] packages; 037 private ApplicationContext applicationContext; 038 private ResolverUtil resolver = new ResolverUtil(); 039 040 public RouteBuilderFinder(SpringCamelContext camelContext, String[] packages) { 041 this.camelContext = camelContext; 042 this.applicationContext = camelContext.getApplicationContext(); 043 this.packages = packages; 044 } 045 046 public String[] getPackages() { 047 return packages; 048 } 049 050 public ApplicationContext getApplicationContext() { 051 return applicationContext; 052 } 053 054 055 /** 056 * Appends all the {@link RouteBuilder} instances that can be found on the classpath 057 */ 058 public void appendBuilders(List<RouteBuilder> list) throws IllegalAccessException, InstantiationException { 059 resolver.findImplementations(RouteBuilder.class, packages); 060 //resolver.findAnnotated(Endpoint.class, packages); 061 Set<Class> classes = resolver.getClasses(); 062 for (Class aClass : classes) { 063 if (shouldIgnoreBean(aClass)) { 064 continue; 065 } 066 if (isValidClass(aClass)) { 067 RouteBuilder builder = instantiateBuilder(aClass); 068 list.add(builder); 069 } 070 } 071 } 072 073 public void destroy() throws Exception { 074 } 075 076 /** 077 * Lets ignore beans that are not explicitly configured in the spring.xml 078 */ 079 protected boolean shouldIgnoreBean(Class type) { 080 Map beans = applicationContext.getBeansOfType(type, true, true); 081 if (beans == null || beans.isEmpty()) { 082 return false; 083 } 084 // TODO apply some filter? 085 return true; 086 } 087 088 /** 089 * Returns true if the object is non-abstract and supports a zero argument constructor 090 */ 091 protected boolean isValidClass(Class type) { 092 if (!Modifier.isAbstract(type.getModifiers()) && !type.isInterface()) { 093 return true; 094 } 095 return false; 096 } 097 098 protected RouteBuilder instantiateBuilder(Class type) throws IllegalAccessException, InstantiationException { 099 return (RouteBuilder) camelContext.getInjector().newInstance(type); 100 } 101 }