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: 734076 $
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 ResolverUtil resolver;
042 private ApplicationContext applicationContext;
043 private BeanPostProcessor beanPostProcessor;
044
045 public RouteBuilderFinder(SpringCamelContext camelContext, String[] packages, ClassLoader classLoader, BeanPostProcessor postProcessor, ResolverUtil resolverUtil) {
046 this.camelContext = camelContext;
047 this.applicationContext = camelContext.getApplicationContext();
048 this.packages = packages;
049 this.beanPostProcessor = postProcessor;
050 this.resolver = resolverUtil;
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 public String[] getPackages() {
065 return packages;
066 }
067
068 public ApplicationContext getApplicationContext() {
069 return applicationContext;
070 }
071
072
073 /**
074 * Appends all the {@link RouteBuilder} instances that can be found on the classpath
075 */
076 public void appendBuilders(List<Routes> list) throws IllegalAccessException, InstantiationException {
077 resolver.findImplementations(Routes.class, packages);
078 Set<Class> classes = resolver.getClasses();
079 for (Class aClass : classes) {
080 if (shouldIgnoreBean(aClass)) {
081 continue;
082 }
083 if (isValidClass(aClass)) {
084 Routes builder = instantiateBuilder(aClass);
085 if (beanPostProcessor != null) {
086 // Inject the annotated resource
087 beanPostProcessor.postProcessBeforeInitialization(builder, builder.toString());
088 }
089 list.add(builder);
090 }
091 }
092 }
093
094 public void destroy() throws Exception {
095 }
096
097 /**
098 * Lets ignore beans that are not explicitly configured in the spring.xml
099 */
100 protected boolean shouldIgnoreBean(Class type) {
101 Map beans = applicationContext.getBeansOfType(type, true, true);
102 if (beans == null || beans.isEmpty()) {
103 return false;
104 }
105 // TODO apply some filter?
106 return true;
107 }
108
109 /**
110 * Returns true if the object is non-abstract and supports a zero argument constructor
111 */
112 protected boolean isValidClass(Class type) {
113 if (!Modifier.isAbstract(type.getModifiers()) && !type.isInterface()) {
114 return true;
115 }
116 return false;
117 }
118
119 protected Routes instantiateBuilder(Class type) throws IllegalAccessException, InstantiationException {
120 return (Routes) camelContext.getInjector().newInstance(type);
121 }
122 }