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.apache.commons.logging.Log;
026 import org.apache.commons.logging.LogFactory;
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 $Revision: 773945 $
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 transient Log LOG = LogFactory.getLog(DefaultLanguageResolver.class);
039
040 protected FactoryFinder languageFactory;
041 protected FactoryFinder languageResolver;
042
043 @SuppressWarnings("unchecked")
044 public Language resolveLanguage(String name, CamelContext context) {
045 // lookup in registry first
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
062 Class type = null;
063 try {
064 type = findLanguage(name, context);
065 } catch (NoFactoryAvailableException e) {
066 // ignore
067 } catch (Exception e) {
068 throw new IllegalArgumentException("Invalid URI, no Language registered for scheme: " + name, e);
069 }
070
071 if (type != null) {
072 if (Language.class.isAssignableFrom(type)) {
073 return (Language)context.getInjector().newInstance(type);
074 } else {
075 throw new IllegalArgumentException("Resolving language: " + name + " detected type conflict: Not a Language implementation. Found: " + type.getName());
076 }
077 } else {
078 // no specific language found then try fallback
079 return noSpecificLanguageFound(name, context);
080 }
081 }
082
083 @SuppressWarnings("unchecked")
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 Log getLog() {
119 return LOG;
120 }
121 }