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.camel.util.CamelContextHelper;
027    import org.slf4j.Logger;
028    import 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     */
038    public class DefaultComponentResolver implements ComponentResolver {
039    
040        public static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/component/";
041        private static final Logger LOG = LoggerFactory.getLogger(DefaultComponentResolver.class);
042        private FactoryFinder factoryFinder;
043    
044        public Component resolveComponent(String name, CamelContext context) {
045            // lookup in registry first
046            Object bean = null;
047            try {
048                bean = context.getRegistry().lookupByName(name);
049                getLog().debug("Found component: {} in registry: {}", name, bean);
050            } catch (Exception e) {
051                getLog().debug("Ignored error looking up bean: " + name, e);
052            }
053            if (bean != null) {
054                if (bean instanceof Component) {
055                    return (Component) bean;
056                } else {
057                    // lets use Camel's type conversion mechanism to convert things like CamelContext
058                    // and other types into a valid Component
059                    Component component = CamelContextHelper.convertTo(context, Component.class, bean);
060                    if (component != null) {
061                        return component;
062                    }
063                }
064                // we do not throw the exception here and try to auto create a component
065            }
066    
067            // not in registry then use component factory
068            Class<?> type;
069            try {
070                type = findComponent(name, context);
071                if (type == null) {
072                    // not found
073                    return null;
074                }
075            } catch (NoFactoryAvailableException e) {
076                return null;
077            } catch (Exception e) {
078                throw new IllegalArgumentException("Invalid URI, no Component registered for scheme: " + name, e);
079            }
080    
081            if (getLog().isDebugEnabled()) {
082                getLog().debug("Found component: {} via type: {} via: {}{}", new Object[]{name, type.getName(), factoryFinder.getResourcePath(), name});
083            }
084    
085            // create the component
086            if (Component.class.isAssignableFrom(type)) {
087                return (Component) context.getInjector().newInstance(type);
088            } else {
089                throw new IllegalArgumentException("Type is not a Component implementation. Found: " + type.getName());
090            }
091        }
092    
093        private Class<?> findComponent(String name, CamelContext context) throws ClassNotFoundException, IOException {
094            if (factoryFinder == null) {
095                factoryFinder = context.getFactoryFinder(RESOURCE_PATH);
096            }
097            return factoryFinder.findClass(name);
098        }
099    
100        protected Logger getLog() {
101            return LOG;
102        }
103    
104    }