Coverage Report - org.webslinger.lang.ClassUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
ClassUtil
11%
3/28
25%
1/4
0
 
 1  
 package org.webslinger.lang;
 2  
 
 3  
 import java.net.URL;
 4  
 import java.util.ArrayList;
 5  
 import java.util.Enumeration;
 6  
 import java.util.List;
 7  
 import java.util.Properties;
 8  
 
 9  0
 public final class ClassUtil {
 10  1
     public static final Object NULL = new Object();
 11  
 
 12  
     public static Object loadImplementation(String label, String[] impls, ClassLoader loader) {
 13  2
         for (String impl: impls) {
 14  
             try {
 15  2
                 return Class.forName(impl, true, loader).newInstance();
 16  0
             } catch (Throwable t) {
 17  
                 // ignore
 18  
             }
 19  
         }
 20  0
         throw new UnsupportedOperationException(label);
 21  
     }
 22  
 
 23  
     public static List loadImplementationsProperties(String label, String propName, Class clz) {
 24  0
         ArrayList<Class> list = new ArrayList<Class>();
 25  0
         loadImplementationsPropertiesInternal(list, label, propName, clz.getClassLoader());
 26  0
         loadImplementationsPropertiesInternal(list, label, propName, Thread.currentThread().getContextClassLoader());
 27  0
         return list;
 28  
     }
 29  
 
 30  
     private static void loadImplementationsPropertiesInternal(List<Class> list, String label, String propName, ClassLoader loader) {
 31  0
         if (loader == null) return;
 32  
         try {
 33  0
             Enumeration<URL> props = loader.getResources(propName);
 34  0
             while (props.hasMoreElements()) {
 35  0
                 URL url = props.nextElement();
 36  0
                 Properties properties = new Properties();
 37  
                 try {
 38  0
                     properties.load(url.openConnection().getInputStream());
 39  0
                     for (int i = 0; ; i++) {
 40  0
                         String impl = properties.getProperty("impl." + i);
 41  0
                         if (impl == null) break;
 42  
                         try {
 43  0
                             list.add(Class.forName(impl, true, loader));
 44  0
                         } catch (Throwable t) {
 45  
                             // ignore
 46  0
                         }
 47  
                     }
 48  0
                 } catch (Throwable t) {
 49  
                     // ignore
 50  0
                 }
 51  0
             }
 52  0
         } catch (Throwable t) {
 53  
             // ignore
 54  0
         }
 55  0
     }
 56  
 }
 57