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.blueprint;
018    
019    import java.lang.reflect.Modifier;
020    import java.util.List;
021    import java.util.Set;
022    
023    import org.apache.camel.RoutesBuilder;
024    import org.apache.camel.spi.PackageScanClassResolver;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.osgi.service.blueprint.container.BlueprintContainer;
028    import org.osgi.service.blueprint.reflect.BeanMetadata;
029    
030    /**
031     * A helper class which will find all {@link org.apache.camel.builder.RouteBuilder} instances on the classpath
032     *
033     * @version $Revision$
034     */
035    public class PackageScanRouteBuilderFinder {
036        private static final transient Log LOG = LogFactory.getLog(PackageScanRouteBuilderFinder.class);
037        private final BlueprintCamelContext camelContext;
038        private final String[] packages;
039        private final PackageScanClassResolver resolver;
040        private final BlueprintContainer blueprintContainer;
041    //    private final BeanPostProcessor beanPostProcessor;
042    
043        public PackageScanRouteBuilderFinder(BlueprintCamelContext camelContext, String[] packages, ClassLoader classLoader,
044                                             /*BeanPostProcessor postProcessor,*/ PackageScanClassResolver resolver) {
045            this.camelContext = camelContext;
046            this.blueprintContainer = camelContext.getBlueprintContainer();
047            this.packages = packages;
048    //        this.beanPostProcessor = postProcessor;
049            this.resolver = resolver;
050            // add our provided loader as well
051            resolver.addClassLoader(classLoader);
052        }
053    
054        /**
055         * Appends all the {@link org.apache.camel.builder.RouteBuilder} instances that can be found on the classpath
056         */
057        public void appendBuilders(List<RoutesBuilder> list) throws IllegalAccessException, InstantiationException {
058            Set<Class<?>> classes = resolver.findImplementations(RoutesBuilder.class, packages);
059            for (Class aClass : classes) {
060                if (LOG.isTraceEnabled()) {
061                    LOG.trace("Found RouteBuilder class: " + aClass);
062                }
063    
064                // certain beans should be ignored
065                if (shouldIgnoreBean(aClass)) {
066                    if (LOG.isDebugEnabled()) {
067                        LOG.debug("Ignoring RouteBuilder class: " + aClass);
068                    }
069                    continue;
070                }
071    
072                if (!isValidClass(aClass)) {
073                    if (LOG.isDebugEnabled()) {
074                        LOG.debug("Ignoring invalid RouteBuilder class: " + aClass);
075                    }
076                    continue;
077                }
078    
079                // type is valid so create and instantiate the builder
080                RoutesBuilder builder = instantiateBuilder(aClass);
081    //            if (beanPostProcessor != null) {
082                // Inject the annotated resource
083    //                beanPostProcessor.postProcessBeforeInitialization(builder, builder.toString());
084    //            }
085                if (LOG.isDebugEnabled()) {
086                    LOG.debug("Adding instantiated RouteBuilder: " + builder);
087                }
088                list.add(builder);
089            }
090        }
091    
092        /**
093         * Lets ignore beans that are explicitly configured in the Spring XML files
094         */
095        protected boolean shouldIgnoreBean(Class<?> type) {
096            for (BeanMetadata metadata : blueprintContainer.getMetadata(BeanMetadata.class)) {
097                if (BeanMetadata.SCOPE_SINGLETON.equals(metadata.getScope())) {
098                    Object bean = blueprintContainer.getComponentInstance(metadata.getId());
099                    if (type.isInstance(bean)) {
100                        return true;
101                    }
102                }
103            }
104            return false;
105        }
106    
107        /**
108         * Returns true if the object is non-abstract and supports a zero argument constructor
109         */
110        protected boolean isValidClass(Class type) {
111            if (!Modifier.isAbstract(type.getModifiers()) && !type.isInterface()) {
112                return true;
113            }
114            return false;
115        }
116    
117        @SuppressWarnings("unchecked")
118        protected RoutesBuilder instantiateBuilder(Class type) throws IllegalAccessException, InstantiationException {
119            return (RoutesBuilder) camelContext.getInjector().newInstance(type);
120        }
121    }