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