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.util.List;
020 import java.util.Map;
021 import java.util.Map.Entry;
022
023 import org.apache.camel.RoutesBuilder;
024 import org.apache.camel.spi.PackageScanFilter;
025 import org.apache.commons.logging.Log;
026 import org.apache.commons.logging.LogFactory;
027 import org.osgi.service.blueprint.container.BlueprintContainer;
028
029 /**
030 * A helper class which will find all {@link org.apache.camel.builder.RouteBuilder} instances in the
031 * {@link org.osgi.service.blueprint.container.BlueprintContainer}.
032 *
033 * @version $Revision$
034 */
035 public class ContextScanRouteBuilderFinder {
036 private static final transient Log LOG = LogFactory.getLog(ContextScanRouteBuilderFinder.class);
037 private final BlueprintContainer blueprintContainer;
038 private final PackageScanFilter filter;
039
040 public ContextScanRouteBuilderFinder(BlueprintCamelContext camelContext, PackageScanFilter filter) {
041 this.blueprintContainer = camelContext.getBlueprintContainer();
042 this.filter = filter;
043 }
044
045 /**
046 * Appends all the {@link org.apache.camel.builder.RouteBuilder} instances that can be found in the context
047 */
048 public void appendBuilders(List<RoutesBuilder> list) {
049 Map<String, RoutesBuilder> beans = BlueprintContainerRegistry.lookupByType(blueprintContainer, RoutesBuilder.class);
050
051 for (Entry<String, RoutesBuilder> entry : beans.entrySet()) {
052 String key = entry.getKey();
053 Object bean = entry.getValue();
054
055 if (LOG.isTraceEnabled()) {
056 LOG.trace("Found RouteBuilder with id: " + key + " -> " + bean);
057 }
058
059 // certain beans should be ignored
060 if (shouldIgnoreBean(bean)) {
061 if (LOG.isDebugEnabled()) {
062 LOG.debug("Ignoring RouteBuilder id: " + key);
063 }
064 continue;
065 }
066
067 if (!isFilteredClass(bean)) {
068 if (LOG.isDebugEnabled()) {
069 LOG.debug("Ignoring filtered RouteBuilder id: " + key + " as class: " + bean.getClass());
070 }
071 continue;
072 }
073
074 if (LOG.isDebugEnabled()) {
075 LOG.debug("Adding instantiated RouteBuilder id: " + key + " as class: " + bean.getClass());
076 }
077 list.add((RoutesBuilder) bean);
078 }
079 }
080
081 protected boolean shouldIgnoreBean(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 }