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