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.slf4j.Logger;
026    import org.slf4j.LoggerFactory;
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 
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 Logger LOG = LoggerFactory.getLogger(DefaultLanguageResolver.class);
039    
040        protected FactoryFinder languageFactory;
041        protected FactoryFinder languageResolver;
042    
043        public Language resolveLanguage(String name, CamelContext context) {
044            // lookup in registry first
045            Object bean = null;
046            try {
047                bean = context.getRegistry().lookupByName(name);
048                if (bean != null) {
049                    getLog().debug("Found language: {} in registry: {}", name, bean);
050                }
051            } catch (Exception e) {
052                if (getLog().isDebugEnabled()) {
053                    getLog().debug("Ignored error looking up bean: " + name + ". Error: " + e);
054                }
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    
063            Class<?> type = null;
064            try {
065                type = findLanguage(name, context);
066            } catch (NoFactoryAvailableException e) {
067                // ignore
068            } catch (Exception e) {
069                throw new IllegalArgumentException("Invalid URI, no Language registered for scheme: " + name, e);
070            }
071    
072            if (type != null) {
073                if (Language.class.isAssignableFrom(type)) {
074                    return (Language)context.getInjector().newInstance(type);
075                } else {
076                    throw new IllegalArgumentException("Resolving language: " + name + " detected type conflict: Not a Language implementation. Found: " + type.getName());
077                }
078            } else {
079                // no specific language found then try fallback
080                return noSpecificLanguageFound(name, context);
081            }
082        }
083    
084        protected Language noSpecificLanguageFound(String name, CamelContext context) {
085            Class<?> type = null;
086            try {
087                type = findLanguageResolver("default", context);
088            } catch (NoFactoryAvailableException e) {
089                // ignore
090            } catch (Exception e) {
091                throw new IllegalArgumentException("Invalid URI, no LanguageResolver registered for scheme: " + name, e);
092            }
093            if (type != null) {
094                if (LanguageResolver.class.isAssignableFrom(type)) {
095                    LanguageResolver resolver = (LanguageResolver)context.getInjector().newInstance(type);
096                    return resolver.resolveLanguage(name, context);
097                } else {
098                    throw new IllegalArgumentException("Resolving language: " + name + " detected type conflict: Not a LanguageResolver implementation. Found: " + type.getName());
099                }
100            }
101            throw new NoSuchLanguageException(name);
102        }
103        
104        protected Class<?> findLanguage(String name, CamelContext context) throws Exception {
105            if (languageFactory == null) {
106                languageFactory = context.getFactoryFinder(LANGUAGE_RESOURCE_PATH);
107            }
108            return languageFactory.findClass(name);
109        }
110        
111        protected Class<?> findLanguageResolver(String name, CamelContext context) throws Exception {
112            if (languageResolver == null) {
113                languageResolver = context.getFactoryFinder(LANGUAGE_RESOLVER_RESOURCE_PATH);
114            }
115            return languageResolver.findClass(name);
116        }
117    
118        protected Logger getLog() {
119            return LOG;
120        }
121    }