Coverage Report - org.apache.camel.impl.DefaultLanguageResolver
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultLanguageResolver
35% 
50% 
8.5
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.impl;
 18  
 
 19  
 import org.apache.camel.CamelContext;
 20  
 import org.apache.camel.NoSuchLanguageException;
 21  
 import org.apache.camel.spi.Language;
 22  
 import org.apache.camel.spi.LanguageResolver;
 23  
 import org.apache.camel.util.FactoryFinder;
 24  
 import org.apache.camel.util.NoFactoryAvailableException;
 25  
 
 26  
 /**
 27  
  * @version $Revision: 1.1 $
 28  
  */
 29  363
 public class DefaultLanguageResolver implements LanguageResolver {
 30  3
     protected static final FactoryFinder LANGUAGE_FACTORY = new FactoryFinder("META-INF/services/org/apache/camel/language/");
 31  3
     protected static final FactoryFinder LANGUAGE_RESOLVER = new FactoryFinder("META-INF/services/org/apache/camel/language/resolver/");
 32  
 
 33  
     public Language resolveLanguage(String name, CamelContext context) {
 34  39
         Class type = null;
 35  
         try {
 36  39
             type = LANGUAGE_FACTORY.findClass(name);
 37  0
         } catch (NoFactoryAvailableException e) {
 38  
             // ignore
 39  0
         } catch (Throwable e) {
 40  0
             throw new IllegalArgumentException("Invalid URI, no Language registered for scheme : " + name, e);
 41  39
         }
 42  39
         if (type != null) {
 43  39
             if (Language.class.isAssignableFrom(type)) {
 44  39
                 return (Language)context.getInjector().newInstance(type);
 45  
             } else {
 46  0
                 throw new IllegalArgumentException("Type is not a Language implementation. Found: " + type.getName());
 47  
             }
 48  
         }
 49  0
         return noSpecificLanguageFound(name, context);
 50  
     }
 51  
 
 52  
     protected Language noSpecificLanguageFound(String name, CamelContext context) {
 53  0
         Class type = null;
 54  
         try {
 55  0
             type = LANGUAGE_RESOLVER.findClass("default");
 56  0
         } catch (NoFactoryAvailableException e) {
 57  
             // ignore
 58  0
         } catch (Throwable e) {
 59  0
             throw new IllegalArgumentException("Invalid URI, no Language registered for scheme : " + name, e);
 60  0
         }
 61  0
         if (type != null) {
 62  0
             if (LanguageResolver.class.isAssignableFrom(type)) {
 63  0
                 LanguageResolver resolver = (LanguageResolver)context.getInjector().newInstance(type);
 64  0
                 return resolver.resolveLanguage(name, context);
 65  
             } else {
 66  0
                 throw new IllegalArgumentException("Type is not a LanguageResolver implementation. Found: " + type.getName());
 67  
             }
 68  
         }
 69  0
         throw new NoSuchLanguageException(name);
 70  
     }
 71  
 }