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.NoFactoryAvailableException;
023    import org.apache.camel.NoSuchLanguageException;
024    import org.apache.camel.spi.Language;
025    import org.apache.camel.spi.LanguageResolver;
026    import org.apache.camel.util.FactoryFinder;
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    
030    /**
031     * Default language resolver that looks for language factories in <b>META-INF/services/org/apache/camel/language/</b> and
032     * language resolvers in <b>META-INF/services/org/apache/camel/language/resolver/</b>.
033     *
034     * @version $Revision: 752893 $
035     */
036    public class DefaultLanguageResolver implements LanguageResolver {    
037        protected static final FactoryFinder LANGUAGE_FACTORY = new FactoryFinder("META-INF/services/org/apache/camel/language/");
038        protected static final FactoryFinder LANGUAGE_RESOLVER = new FactoryFinder("META-INF/services/org/apache/camel/language/resolver/");
039        private static final transient Log LOG = LogFactory.getLog(DefaultLanguageResolver.class);
040        
041        protected Log getLog() {
042            return LOG;
043        }
044    
045        @SuppressWarnings("unchecked")
046        public Language resolveLanguage(String name, CamelContext context) {
047            Object bean = null;
048            try {
049                bean = context.getRegistry().lookup(name);
050                if (bean != null && getLog().isDebugEnabled()) {
051                    getLog().debug("Found language: " + name + " in registry: " + bean);
052                }
053            } catch (Exception e) {
054                getLog().debug("Ignored error looking up bean: " + name + ". Error: " + e);
055            }
056            if (bean != null) {
057                if (bean instanceof Language) {
058                    return (Language)bean;
059                }
060                // we do not throw the exception here and try to auto create a Language from META-INF
061            }
062            Class type = null;
063            try {
064                type = findLanguage(name);
065            } catch (NoFactoryAvailableException e) {
066                // ignore
067            } catch (Exception e) {
068                throw new IllegalArgumentException("Invalid URI, no Language registered for scheme: " + name, e);
069            }
070            if (type != null) {
071                if (Language.class.isAssignableFrom(type)) {
072                    return (Language)context.getInjector().newInstance(type);
073                } else {
074                    throw new IllegalArgumentException("Type is not a Language implementation. Found: " + type.getName());
075                }
076            }
077            return noSpecificLanguageFound(name, context);
078        }
079    
080        @SuppressWarnings("unchecked")
081        protected Language noSpecificLanguageFound(String name, CamelContext context) {
082            Class type = null;
083            try {
084                type = findLanguageResolver("default");
085            } catch (NoFactoryAvailableException e) {
086                // ignore
087            } catch (Exception e) {
088                throw new IllegalArgumentException("Invalid URI, no Language registered for scheme: " + name, e);
089            }
090            if (type != null) {
091                if (LanguageResolver.class.isAssignableFrom(type)) {
092                    LanguageResolver resolver = (LanguageResolver)context.getInjector().newInstance(type);
093                    return resolver.resolveLanguage(name, context);
094                } else {
095                    throw new IllegalArgumentException("Type is not a LanguageResolver implementation. Found: " + type.getName());
096                }
097            }
098            throw new NoSuchLanguageException(name);
099        }
100        
101        protected Class findLanguage(String name) throws Exception {
102            return LANGUAGE_FACTORY.findClass(name);
103        }
104        
105        protected Class findLanguageResolver(String name) throws Exception {
106            return LANGUAGE_RESOLVER.findClass("default");
107        }
108    }