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