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 java.io.IOException;
020
021 import org.apache.camel.CamelContext;
022 import org.apache.camel.NoSuchLanguageException;
023 import org.apache.camel.spi.Language;
024 import org.apache.camel.spi.LanguageResolver;
025 import org.apache.camel.util.FactoryFinder;
026 import org.apache.camel.util.NoFactoryAvailableException;
027 import org.apache.commons.logging.Log;
028 import org.apache.commons.logging.LogFactory;
029
030
031 /**
032 * Default language resolver that looks for language factories in <b>META-INF/services/org/apache/camel/language/</b> and
033 * language resolvers in <b>META-INF/services/org/apache/camel/language/resolver/</b>.
034 *
035 * @version $Revision: 752787 $
036 */
037 public class DefaultLanguageResolver implements LanguageResolver {
038 protected static final FactoryFinder LANGUAGE_FACTORY = new FactoryFinder("META-INF/services/org/apache/camel/language/");
039 protected static final FactoryFinder LANGUAGE_RESOLVER = new FactoryFinder("META-INF/services/org/apache/camel/language/resolver/");
040 private static final transient Log LOG = LogFactory.getLog(DefaultLanguageResolver.class);
041
042 protected Log getLog() {
043 return LOG;
044 }
045 public Language resolveLanguage(String name, CamelContext context) {
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 getLog().debug("Ignored error looking up bean: " + name + ". Error: " + e);
054 }
055 if (bean != null) {
056 if (bean instanceof Language) {
057 return (Language)bean;
058 }
059 // we do not throw the exception here and try to auto create a Language from META-INF
060 }
061 Class type = null;
062 try {
063 type = findLanguage(name);
064 } catch (NoFactoryAvailableException e) {
065 // ignore
066 } catch (Throwable e) {
067 throw new IllegalArgumentException("Invalid URI, no Language registered for scheme : " + name, e);
068 }
069 if (type != null) {
070 if (Language.class.isAssignableFrom(type)) {
071 return (Language)context.getInjector().newInstance(type);
072 } else {
073 throw new IllegalArgumentException("Type is not a Language implementation. Found: " + type.getName());
074 }
075 }
076 return noSpecificLanguageFound(name, context);
077 }
078
079 protected Language noSpecificLanguageFound(String name, CamelContext context) {
080 Class type = null;
081 try {
082 type = findLanguageResolver("default");
083 } catch (NoFactoryAvailableException e) {
084 // ignore
085 } catch (Throwable e) {
086 throw new IllegalArgumentException("Invalid URI, no Language registered for scheme : " + name, e);
087 }
088 if (type != null) {
089 if (LanguageResolver.class.isAssignableFrom(type)) {
090 LanguageResolver resolver = (LanguageResolver)context.getInjector().newInstance(type);
091 return resolver.resolveLanguage(name, context);
092 } else {
093 throw new IllegalArgumentException("Type is not a LanguageResolver implementation. Found: " + type.getName());
094 }
095 }
096 throw new NoSuchLanguageException(name);
097 }
098
099 protected Class findLanguage(String name) throws Exception {
100 return LANGUAGE_FACTORY.findClass(name);
101 }
102
103 protected Class findLanguageResolver(String name) throws Exception {
104 return LANGUAGE_RESOLVER.findClass("default");
105 }
106 }