Coverage Report - org.apache.camel.util.FactoryFinder
 
Classes in this File Line Coverage Branch Coverage Complexity
FactoryFinder
80% 
100% 
2.3
 
 1  
 /**
 2  
  *
 3  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 4  
  * contributor license agreements.  See the NOTICE file distributed with
 5  
  * this work for additional information regarding copyright ownership.
 6  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 7  
  * (the "License"); you may not use this file except in compliance with
 8  
  * the License.  You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 package org.apache.camel.util;
 19  
 
 20  
 import org.apache.camel.spi.Injector;
 21  
 
 22  
 import java.io.BufferedInputStream;
 23  
 import java.io.IOException;
 24  
 import java.io.InputStream;
 25  
 import java.util.Properties;
 26  
 import java.util.concurrent.ConcurrentHashMap;
 27  
 
 28  
 public class FactoryFinder {
 29  
     private final String path;
 30  45
     private final ConcurrentHashMap classMap = new ConcurrentHashMap();
 31  
 
 32  
     public FactoryFinder() {
 33  44
         this("META-INF/services/org/apache/camel/");
 34  44
     }
 35  
 
 36  45
     public FactoryFinder(String path) {
 37  45
         this.path = path;
 38  45
     }
 39  
 
 40  
     /**
 41  
      * Creates a new instance of the given key
 42  
      *
 43  
      * @param key is the key to add to the path to find a text file
 44  
      *            containing the factory name
 45  
      * @return a newly created instance
 46  
      */
 47  
     public Object newInstance(String key)
 48  
             throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException {
 49  44
         return newInstance(key, (String) null);
 50  
     }
 51  
 
 52  
     public Object newInstance(String key, String propertyPrefix)
 53  
             throws IllegalAccessException, InstantiationException, IOException, ClassNotFoundException {
 54  44
         Class clazz = findClass(key, propertyPrefix);
 55  0
         return clazz.newInstance();
 56  
     }
 57  
 
 58  
     public Object newInstance(String key, Injector injector) throws IOException, ClassNotFoundException {
 59  0
         return newInstance(key, injector, null);
 60  
     }
 61  
 
 62  
     public Object newInstance(String key, Injector injector, String propertyPrefix) throws IOException, ClassNotFoundException {
 63  0
         Class type = findClass(key, propertyPrefix);
 64  0
         return injector.newInstance(type);
 65  
     }
 66  
 
 67  
     public Class findClass(String key) throws ClassNotFoundException, IOException {
 68  67
         return findClass(key, null);
 69  
     }
 70  
 
 71  
     public Class findClass(String key, String propertyPrefix) throws ClassNotFoundException, IOException {
 72  111
         if (propertyPrefix == null) {
 73  111
             propertyPrefix = "";
 74  
         }
 75  
 
 76  111
         Class clazz = (Class) classMap.get(propertyPrefix + key);
 77  111
         if (clazz == null) {
 78  50
             clazz = newInstance(doFindFactoryProperies(key), propertyPrefix);
 79  6
             classMap.put(propertyPrefix + key, clazz);
 80  
         }
 81  67
         return clazz;
 82  
     }
 83  
 
 84  
     private Class newInstance(Properties properties, String propertyPrefix) throws ClassNotFoundException, IOException {
 85  
 
 86  6
         String className = properties.getProperty(propertyPrefix + "class");
 87  6
         if (className == null) {
 88  0
             throw new IOException("Expected property is missing: " + propertyPrefix + "class");
 89  
         }
 90  6
         Class clazz = null;
 91  6
         ClassLoader loader = Thread.currentThread().getContextClassLoader();
 92  6
         if (loader != null) {
 93  
             try {
 94  6
                 clazz = loader.loadClass(className);
 95  
             }
 96  0
             catch (ClassNotFoundException e) {
 97  
                 // ignore
 98  6
             }
 99  
         }
 100  6
         if (clazz == null) {
 101  0
             clazz = FactoryFinder.class.getClassLoader().loadClass(className);
 102  
         }
 103  6
         return clazz;
 104  
     }
 105  
 
 106  
     private Properties doFindFactoryProperies(String key) throws IOException {
 107  50
         String uri = path + key;
 108  
 
 109  
         // lets try the thread context class loader first
 110  50
         ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
 111  50
         if (classLoader == null) {
 112  0
             classLoader = getClass().getClassLoader();
 113  
         }
 114  50
         InputStream in = classLoader.getResourceAsStream(uri);
 115  50
         if (in == null) {
 116  44
             in = FactoryFinder.class.getClassLoader().getResourceAsStream(uri);
 117  44
             if (in == null) {
 118  44
                 throw new NoFactoryAvailableException(uri);
 119  
             }
 120  
         }
 121  
 
 122  
         // lets load the file
 123  6
         BufferedInputStream reader = null;
 124  
         try {
 125  6
             reader = new BufferedInputStream(in);
 126  6
             Properties properties = new Properties();
 127  6
             properties.load(reader);
 128  6
             return properties;
 129  
         }
 130  
         finally {
 131  0
             try {
 132  6
                 reader.close();
 133  
             }
 134  0
             catch (Exception e) {
 135  6
             }
 136  6
         }
 137  
     }
 138  
 }