001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.camel.spring;
019
020 import org.apache.camel.builder.RouteBuilder;
021 import org.apache.camel.util.ResolverUtil;
022 import org.springframework.beans.BeansException;
023 import org.springframework.context.ApplicationContext;
024 import org.springframework.context.ApplicationContextAware;
025
026 import java.lang.reflect.Constructor;
027 import java.lang.reflect.Modifier;
028 import java.util.List;
029 import java.util.Map;
030 import java.util.Set;
031
032 /**
033 * A helper class which will find all {@link RouteBuilder} instances on the classpath
034 *
035 * @version $Revision: 521369 $
036 */
037 public class RouteBuilderFinder implements ApplicationContextAware {
038 private String[] packages = {};
039 private ApplicationContext applicationContext;
040 private ResolverUtil resolver = new ResolverUtil();
041
042 public RouteBuilderFinder(ApplicationContext applicationContext, String[] packages) {
043 this.applicationContext = applicationContext;
044 this.packages = packages;
045 }
046
047 public RouteBuilderFinder(CamelContextFactoryBean factoryBean) {
048 this.applicationContext = factoryBean.getApplicationContext();
049 this.packages = factoryBean.getPackages();
050 }
051
052 public String[] getPackages() {
053 return packages;
054 }
055
056 public void setPackages(String[] packages) {
057 this.packages = packages;
058 }
059
060 public ApplicationContext getApplicationContext() {
061 return applicationContext;
062 }
063
064 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
065 this.applicationContext = applicationContext;
066 }
067
068 /**
069 * Appends all the {@link RouteBuilder} instances that can be found on the classpath
070 */
071 public void appendBuilders(List<RouteBuilder> list) throws IllegalAccessException, InstantiationException {
072 resolver.findImplementations(RouteBuilder.class, packages);
073 //resolver.findAnnotated(Endpoint.class, packages);
074 Set<Class> classes = resolver.getClasses();
075 for (Class aClass : classes) {
076 if (shouldIgnoreBean(aClass)) {
077 continue;
078 }
079 if (isValidClass(aClass)) {
080 list.add(instantiateBuilder(aClass));
081 }
082 }
083 }
084
085 public void destroy() throws Exception {
086 }
087
088 /**
089 * Lets ignore beans that are not explicitly configured in the spring.xml
090 */
091 protected boolean shouldIgnoreBean(Class type) {
092 Map beans = applicationContext.getBeansOfType(type, true, true);
093 if (beans == null || beans.isEmpty()) {
094 return false;
095 }
096 // TODO apply some filter?
097 return true;
098 }
099
100 /**
101 * Returns true if the object is non-abstract and supports a zero argument constructor
102 */
103 protected boolean isValidClass(Class type) {
104 if (!Modifier.isAbstract(type.getModifiers()) && !type.isInterface()) {
105 Constructor[] constructors = type.getDeclaredConstructors();
106 for (Constructor constructor : constructors) {
107 Class[] classes = constructor.getParameterTypes();
108 if (classes.length == 0) {
109 return true;
110 }
111 }
112 }
113 return false;
114 }
115
116 protected RouteBuilder instantiateBuilder(Class type) throws IllegalAccessException, InstantiationException {
117 // TODO we could support spring-injection for these types
118 return (RouteBuilder) type.newInstance();
119 }
120 }