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.impl; 018 019 import java.io.IOException; 020 021 import org.apache.camel.CamelContext; 022 import org.apache.camel.Component; 023 import org.apache.camel.NoFactoryAvailableException; 024 import org.apache.camel.spi.ComponentResolver; 025 import org.apache.camel.spi.FactoryFinder; 026 import org.apache.commons.logging.Log; 027 import org.apache.commons.logging.LogFactory; 028 029 /** 030 * The default implementation of {@link ComponentResolver} which tries to find 031 * components by using the URI scheme prefix and searching for a file of the URI 032 * scheme name in the <b>META-INF/services/org/apache/camel/component/</b> 033 * directory on the classpath. 034 * 035 * @version $Revision: 754461 $ 036 */ 037 public class DefaultComponentResolver implements ComponentResolver { 038 039 public static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/component/"; 040 private static final transient Log LOG = LogFactory.getLog(DefaultComponentResolver.class); 041 private FactoryFinder factoryFinder; 042 043 @SuppressWarnings("unchecked") 044 public Component resolveComponent(String name, CamelContext context) { 045 // lookup in registry first 046 Object bean = null; 047 try { 048 bean = context.getRegistry().lookup(name); 049 if (bean != null && getLog().isDebugEnabled()) { 050 getLog().debug("Found component: " + name + " in registry: " + bean); 051 } 052 } catch (Exception e) { 053 getLog().debug("Ignored error looking up bean: " + name, e); 054 } 055 if (bean != null) { 056 if (bean instanceof Component) { 057 return (Component) bean; 058 } 059 // we do not throw the exception here and try to auto create a component 060 } 061 062 // not in registry then use component factory 063 Class type; 064 try { 065 type = findComponent(name, context); 066 if (type == null) { 067 // not found 068 return null; 069 } 070 } catch (NoFactoryAvailableException e) { 071 return null; 072 } catch (Exception e) { 073 throw new IllegalArgumentException("Invalid URI, no Component registered for scheme: " + name, e); 074 } 075 076 if (getLog().isDebugEnabled()) { 077 getLog().debug("Found component: " + name + " via type: " + type.getName() + " via: " + factoryFinder.getResourcePath() + name); 078 } 079 080 // create the component 081 if (Component.class.isAssignableFrom(type)) { 082 return (Component) context.getInjector().newInstance(type); 083 } else { 084 throw new IllegalArgumentException("Type is not a Component implementation. Found: " + type.getName()); 085 } 086 } 087 088 private Class findComponent(String name, CamelContext context) throws ClassNotFoundException, IOException { 089 if (factoryFinder == null) { 090 factoryFinder = context.getFactoryFinder(RESOURCE_PATH); 091 } 092 return factoryFinder.findClass(name); 093 } 094 095 protected Log getLog() { 096 return LOG; 097 } 098 099 }