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 org.apache.camel.CamelContext;
020    import org.apache.camel.Component;
021    import org.apache.camel.core.osgi.OsgiComponentResolver;
022    import org.apache.camel.spi.ComponentResolver;
023    import org.apache.camel.util.CamelContextHelper;
024    import org.osgi.framework.BundleContext;
025    import org.slf4j.Logger;
026    import org.slf4j.LoggerFactory;
027    
028    public class BlueprintComponentResolver extends OsgiComponentResolver {
029    
030        private static final transient Logger LOG = LoggerFactory.getLogger(BlueprintComponentResolver.class);
031    
032        public BlueprintComponentResolver(BundleContext bundleContext) {
033            super(bundleContext);
034        }
035    
036        @Override
037        public Component resolveComponent(String name, CamelContext context) throws Exception {
038            try {
039                Object bean = context.getRegistry().lookup(name);
040                if (bean instanceof Component) {
041                    LOG.debug("Found component: {} in registry: {}", name, bean);
042                    return (Component) bean;
043                } else {
044                    // let's use Camel's type conversion mechanism to convert things like CamelContext
045                    // and other types into a valid Component
046                    Component component = CamelContextHelper.convertTo(context, Component.class, bean);
047                    if (component != null) {
048                        return component;
049                    }
050                }
051            } catch (Exception e) {
052                LOG.debug("Ignored error looking up bean: " + name + ". Error: " + e);
053            }
054            try {
055                Object bean = context.getRegistry().lookup(".camelBlueprint.componentResolver." + name);
056                if (bean instanceof ComponentResolver) {
057                    LOG.debug("Found component resolver: {} in registry: {}", name, bean);
058                    return ((ComponentResolver) bean).resolveComponent(name, context);
059                }
060            } catch (Exception e) {
061                LOG.debug("Ignored error looking up bean: " + name + ". Error: " + e);
062            }
063            return getComponent(name, context);
064        }
065    
066    }