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.Exchange;
022    import org.apache.camel.spi.ComponentResolver;
023    import org.apache.camel.util.FactoryFinder;
024    import org.apache.camel.util.NoFactoryAvailableException;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    
028    /**
029     * The default implementation of {@link ComponentResolver} which tries to find
030     * components by using the URI scheme prefix and searching for a file of the URI
031     * scheme name in the <b>META-INF/services/org/apache/camel/component/</b>
032     * directory on the classpath.
033     *
034     * @version $Revision: 2542 $
035     */
036    public class DefaultComponentResolver<E extends Exchange> implements ComponentResolver<E> {
037        protected static final FactoryFinder COMPONENT_FACTORY =
038                new FactoryFinder("META-INF/services/org/apache/camel/component/");
039        private static final transient Log LOG = LogFactory.getLog(DefaultComponentResolver.class);
040    
041        public Component<E> 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 + ". Error: " + 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 (Throwable e) {
063                throw new IllegalArgumentException("Invalid URI, no Component registered for scheme : "
064                        + name, e);
065            }
066            if (type == null) {
067                return null;
068            }
069            if (LOG.isDebugEnabled()) {
070                LOG.debug("Found component: " + name + " via type: " + type.getName() + " via " + COMPONENT_FACTORY.getPath() + name);
071            }
072            if (Component.class.isAssignableFrom(type)) {
073                return (Component<E>) context.getInjector().newInstance(type);
074            } else {
075                throw new IllegalArgumentException("Type is not a Component implementation. Found: "
076                        + type.getName());
077            }
078        }
079    }