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.springframework.beans.factory.config.BeanPostProcessor;
027    import org.springframework.context.ApplicationContext;
028    
029    /**
030     * A helper class which will find all {@link org.apache.camel.builder.RouteBuilder} instances on the classpath
031     *
032     * @version $Revision: 835198 $
033     */
034    public class RouteBuilderFinder {
035        private final SpringCamelContext camelContext;
036        private final String[] packages;
037        private PackageScanClassResolver resolver;
038        private ApplicationContext applicationContext;    
039        private BeanPostProcessor beanPostProcessor;
040    
041        @SuppressWarnings("unchecked")
042        public RouteBuilderFinder(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                if (shouldIgnoreBean(aClass)) {
060                    continue;
061                }
062                if (isValidClass(aClass)) {
063                    RoutesBuilder builder = instantiateBuilder(aClass);
064                    if (beanPostProcessor != null) {
065                        // Inject the annotated resource
066                        beanPostProcessor.postProcessBeforeInitialization(builder, builder.toString());
067                    }
068                    list.add(builder);
069                }
070            }
071        }
072    
073        /**
074         * Lets ignore beans that are not explicitly configured in the spring.xml
075         */
076        protected boolean shouldIgnoreBean(Class type) {
077            Map beans = applicationContext.getBeansOfType(type, true, true);
078            if (beans == null || beans.isEmpty()) {
079                return false;
080            }
081            return true;
082        }
083    
084        /**
085         * Returns true if the object is non-abstract and supports a zero argument constructor
086         */
087        protected boolean isValidClass(Class type) {
088            if (!Modifier.isAbstract(type.getModifiers()) && !type.isInterface()) {
089                return true;
090            }
091            return false;
092        }
093    
094        @SuppressWarnings("unchecked")
095        protected RoutesBuilder instantiateBuilder(Class type) throws IllegalAccessException, InstantiationException {
096            return (RoutesBuilder) camelContext.getInjector().newInstance(type);
097        }
098    }