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                    if (LOG.isDebugEnabled()) {
042                        LOG.debug("Found component: " + name + " in registry: " + bean);
043                    }
044                    return (Component) bean;
045                } else {
046                    // lets use Camel's type conversion mechanism to convert things like CamelContext
047                    // and other types into a valid Component
048                    Component component = CamelContextHelper.convertTo(context, Component.class, bean);
049                    if (component != null) {
050                        return component;
051                    }
052                }
053            } catch (Exception e) {
054                LOG.debug("Ignored error looking up bean: " + name + ". Error: " + e);
055            }
056            try {
057                Object bean = context.getRegistry().lookup(".camelBlueprint.componentResolver." + name);
058                if (bean instanceof ComponentResolver) {
059                    if (LOG.isDebugEnabled()) {
060                        LOG.debug("Found component resolver: " + name + " in registry: " + bean);
061                    }
062                    return ((ComponentResolver) bean).resolveComponent(name, context);
063                }
064            } catch (Exception e) {
065                LOG.debug("Ignored error looking up bean: " + name + ". Error: " + e);
066            }
067            return getComponent(name, context);
068        }
069    
070    }