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.ResolverHelper; 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 042 Component componentReg = ResolverHelper.lookupComponentInRegistryWithFallback(context, name, new ResolverHelper.LookupExceptionHandler() { 043 @Override 044 public void handleException(Exception e, Logger log, String name) { 045 if (getException(NoSuchComponentException.class, e) != null) { 046 // if the caused error is NoSuchComponentException then that can be expected so ignore 047 } else { 048 LOG.trace("Ignored error looking up bean: " + name + " due: " + e.getMessage(), e); 049 } 050 } 051 }); 052 053 if (componentReg != null) { 054 return componentReg; 055 } 056 057 try { 058 Object bean = context.getRegistry().lookupByName(".camelBlueprint.componentResolver." + name); 059 if (bean instanceof ComponentResolver) { 060 LOG.debug("Found component resolver: {} in registry: {}", name, bean); 061 return ((ComponentResolver) bean).resolveComponent(name, context); 062 } 063 } catch (Exception e) { 064 LOG.trace("Ignored error looking up bean: " + name + " due: " + e.getMessage(), e); 065 } 066 return getComponent(name, context); 067 } 068 069}