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