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