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