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