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.builder.RouteBuilder;
026    import org.apache.camel.spi.PackageScanClassResolver;
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: 788067 $
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 PackageScanClassResolver resolver;
042        private ApplicationContext applicationContext;    
043        private BeanPostProcessor beanPostProcessor;
044    
045        @SuppressWarnings("unchecked")
046        public RouteBuilderFinder(SpringCamelContext camelContext, String[] packages, ClassLoader classLoader,
047                                  BeanPostProcessor postProcessor, PackageScanClassResolver resolver) {
048            this.camelContext = camelContext;
049            this.applicationContext = camelContext.getApplicationContext();
050            this.packages = packages;
051            this.beanPostProcessor = postProcessor;
052            this.resolver = resolver;
053            // add our provided loader as well
054            resolver.addClassLoader(classLoader);
055        }
056    
057        public String[] getPackages() {
058            return packages;
059        }
060    
061        public ApplicationContext getApplicationContext() {
062            return applicationContext;
063        }
064    
065        /**
066         * Appends all the {@link RouteBuilder} instances that can be found on the classpath
067         */
068        public void appendBuilders(List<RoutesBuilder> list) throws IllegalAccessException, InstantiationException {
069            Set<Class> classes = resolver.findImplementations(RoutesBuilder.class, packages);
070            for (Class aClass : classes) {
071                if (shouldIgnoreBean(aClass)) {
072                    continue;
073                }
074                if (isValidClass(aClass)) {
075                    RoutesBuilder builder = instantiateBuilder(aClass);
076                    if (beanPostProcessor != null) {
077                        // Inject the annotated resource
078                        beanPostProcessor.postProcessBeforeInitialization(builder, builder.toString());
079                    }
080                    list.add(builder);
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                return true;
106            }
107            return false;
108        }
109    
110        @SuppressWarnings("unchecked")
111        protected RoutesBuilder instantiateBuilder(Class type) throws IllegalAccessException, InstantiationException {
112            return (RoutesBuilder) camelContext.getInjector().newInstance(type);
113        }
114    }