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 */
017package org.apache.camel.impl;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.NoFactoryAvailableException;
021import org.apache.camel.NoSuchLanguageException;
022import org.apache.camel.spi.FactoryFinder;
023import org.apache.camel.spi.Language;
024import org.apache.camel.spi.LanguageResolver;
025import org.slf4j.Logger;
026import 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 */
034public 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 (ClassNotFoundException 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 Logger getLog() {
121        return LOG;
122    }
123}