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