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.util.List; 020import java.util.Map; 021import java.util.Map.Entry; 022 023import org.apache.camel.RoutesBuilder; 024import org.apache.camel.spi.PackageScanFilter; 025import org.apache.camel.spring.SpringCamelContext; 026import org.slf4j.Logger; 027import org.slf4j.LoggerFactory; 028import org.springframework.context.ApplicationContext; 029 030/** 031 * A helper class which will find all {@link org.apache.camel.builder.RouteBuilder} instances on the Spring 032 * {@link org.springframework.context.ApplicationContext}. 033 */ 034public class ContextScanRouteBuilderFinder { 035 private static final Logger LOG = LoggerFactory.getLogger(ContextScanRouteBuilderFinder.class); 036 private final ApplicationContext applicationContext; 037 private final PackageScanFilter filter; 038 private final boolean includeNonSingletons; 039 040 public ContextScanRouteBuilderFinder(SpringCamelContext camelContext, PackageScanFilter filter, 041 boolean includeNonSingletons) { 042 this.applicationContext = camelContext.getApplicationContext(); 043 this.filter = filter; 044 this.includeNonSingletons = includeNonSingletons; 045 } 046 047 /** 048 * Appends all the {@link org.apache.camel.builder.RouteBuilder} instances that can be found in the context 049 */ 050 public void appendBuilders(List<RoutesBuilder> list) { 051 Map<String, RoutesBuilder> beans = applicationContext.getBeansOfType(RoutesBuilder.class, includeNonSingletons, true); 052 053 for (Entry<String, RoutesBuilder> entry : beans.entrySet()) { 054 Object bean = entry.getValue(); 055 Object key = entry.getKey(); 056 057 LOG.trace("Found RouteBuilder with id: {} -> {}", key, bean); 058 059 // certain beans should be ignored 060 if (shouldIgnoreBean(bean)) { 061 LOG.debug("Ignoring RouteBuilder id: {}", key); 062 continue; 063 } 064 065 if (!isFilteredClass(bean)) { 066 LOG.debug("Ignoring filtered RouteBuilder id: {} as class: {}", key, bean.getClass()); 067 continue; 068 } 069 070 LOG.debug("Adding instantiated RouteBuilder id: {} as class: {}", key, bean.getClass()); 071 list.add((RoutesBuilder) bean); 072 } 073 } 074 075 /** 076 * Whether or not to ignore the bean 077 * 078 * @param bean the bean to be checked if should be ignored or not 079 * @return 080 */ 081 protected boolean shouldIgnoreBean(@SuppressWarnings("unused") Object bean) { 082 return false; 083 } 084 085 protected boolean isFilteredClass(Object bean) { 086 if (filter != null) { 087 return filter.matches(bean.getClass()); 088 } else { 089 return false; 090 } 091 } 092 093}