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.NoSuchLanguageException;
021    import org.apache.camel.spi.Language;
022    import org.apache.camel.spi.LanguageResolver;
023    import org.apache.camel.util.FactoryFinder;
024    import org.apache.camel.util.NoFactoryAvailableException;
025    
026    /**
027     * Default language resolver that looks for language factories in <b>META-INF/services/org/apache/camel/language/</b> and
028     * language resolvers in <b>META-INF/services/org/apache/camel/language/resolver/</b>.
029     *
030     * @version $Revision: 41838 $
031     */
032    public class DefaultLanguageResolver implements LanguageResolver {
033        protected static final FactoryFinder LANGUAGE_FACTORY = new FactoryFinder("META-INF/services/org/apache/camel/language/");
034        protected static final FactoryFinder LANGUAGE_RESOLVER = new FactoryFinder("META-INF/services/org/apache/camel/language/resolver/");
035    
036        public Language resolveLanguage(String name, CamelContext context) {
037            Class type = null;
038            try {
039                type = LANGUAGE_FACTORY.findClass(name);
040            } catch (NoFactoryAvailableException e) {
041                // ignore
042            } catch (Throwable e) {
043                throw new IllegalArgumentException("Invalid URI, no Language registered for scheme : " + name, e);
044            }
045            if (type != null) {
046                if (Language.class.isAssignableFrom(type)) {
047                    return (Language)context.getInjector().newInstance(type);
048                } else {
049                    throw new IllegalArgumentException("Type is not a Language implementation. Found: " + type.getName());
050                }
051            }
052            return noSpecificLanguageFound(name, context);
053        }
054    
055        protected Language noSpecificLanguageFound(String name, CamelContext context) {
056            Class type = null;
057            try {
058                type = LANGUAGE_RESOLVER.findClass("default");
059            } catch (NoFactoryAvailableException e) {
060                // ignore
061            } catch (Throwable e) {
062                throw new IllegalArgumentException("Invalid URI, no Language registered for scheme : " + name, e);
063            }
064            if (type != null) {
065                if (LanguageResolver.class.isAssignableFrom(type)) {
066                    LanguageResolver resolver = (LanguageResolver)context.getInjector().newInstance(type);
067                    return resolver.resolveLanguage(name, context);
068                } else {
069                    throw new IllegalArgumentException("Type is not a LanguageResolver implementation. Found: " + type.getName());
070                }
071            }
072            throw new NoSuchLanguageException(name);
073        }
074    }