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 */
017package org.apache.camel.spring.xml;
018
019import java.lang.reflect.Modifier;
020import java.util.List;
021import java.util.Map;
022import java.util.Set;
023
024import org.apache.camel.RoutesBuilder;
025import org.apache.camel.spi.PackageScanClassResolver;
026import org.apache.camel.spring.SpringCamelContext;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029import org.springframework.beans.factory.config.BeanPostProcessor;
030import org.springframework.context.ApplicationContext;
031
032/**
033 * A helper class which will find all {@link org.apache.camel.builder.RouteBuilder} instances on the classpath
034 */
035public class PackageScanRouteBuilderFinder {
036    private static final Logger LOG = LoggerFactory.getLogger(PackageScanRouteBuilderFinder.class);
037    private final SpringCamelContext camelContext;
038    private final String[] packages;
039    private final PackageScanClassResolver resolver;
040    private final ApplicationContext applicationContext;
041    private final BeanPostProcessor beanPostProcessor;
042
043    public PackageScanRouteBuilderFinder(SpringCamelContext camelContext, String[] packages, ClassLoader classLoader,
044                                         BeanPostProcessor postProcessor, PackageScanClassResolver resolver) {
045        this.camelContext = camelContext;
046        this.applicationContext = camelContext.getApplicationContext();
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) {
058        Set<Class<?>> classes = resolver.findImplementations(RoutesBuilder.class, packages);
059        for (Class<?> aClass : classes) {
060            LOG.trace("Found RouteBuilder class: {}", aClass);
061
062            // certain beans should be ignored
063            if (shouldIgnoreBean(aClass)) {
064                LOG.debug("Ignoring RouteBuilder class: {}", aClass);
065                continue;
066            }
067
068            if (!isValidClass(aClass)) {
069                LOG.debug("Ignoring invalid RouteBuilder class: {}", aClass);
070                continue;
071            }
072
073            // type is valid so create and instantiate the builder
074            @SuppressWarnings("unchecked")
075            RoutesBuilder builder = instantiateBuilder((Class<? extends RoutesBuilder>) aClass);
076            if (beanPostProcessor != null) {
077                // Inject the annotated resource
078                beanPostProcessor.postProcessBeforeInitialization(builder, builder.toString());
079            }
080            LOG.debug("Adding instantiated RouteBuilder: {}", builder);
081            list.add(builder);
082        }
083    }
084
085    /**
086     * Lets ignore beans that are explicitly configured in the Spring XML files
087     */
088    protected boolean shouldIgnoreBean(Class<?> type) {
089        Map<String, ?> beans = applicationContext.getBeansOfType(type, true, true);
090        if (beans == null || beans.isEmpty()) {
091            return false;
092        }
093        return true;
094    }
095
096    /**
097     * Returns <tt>true</tt>if the class is a public, non-abstract class
098     */
099    protected boolean isValidClass(Class<?> type) {
100        // should skip non public classes
101        if (!Modifier.isPublic(type.getModifiers())) {
102            return false;
103        }
104
105        if (!Modifier.isAbstract(type.getModifiers()) && !type.isInterface()) {
106            return true;
107        }
108        return false;
109    }
110
111    protected RoutesBuilder instantiateBuilder(Class<? extends RoutesBuilder> type) {
112        return camelContext.getInjector().newInstance(type);
113    }
114}