Coverage Report - us.daveread.basicquery.DynamicClassLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
DynamicClassLoader
92%
34/37
100%
7/7
0
 
 1  
 package us.daveread.basicquery;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.IOException;
 5  
 import java.net.MalformedURLException;
 6  
 import java.net.URL;
 7  
 import java.net.URLClassLoader;
 8  
 import java.util.ArrayList;
 9  
 import java.util.HashMap;
 10  
 import java.util.List;
 11  
 import java.util.Map;
 12  
 
 13  
 import org.apache.log4j.Logger;
 14  
 
 15  
 /**
 16  
  * <p>
 17  
  * Title: Dynamic class loader
 18  
  * </p>
 19  
  * <p>
 20  
  * Description: Dynamically load classes
 21  
  * </p>
 22  
  * <p>
 23  
  * Copyright: Copyright (c) 2004-2014, David Read
 24  
  * </p>
 25  
  * <p>
 26  
  * This program is free software; you can redistribute it and/or modify it under
 27  
  * the terms of the GNU General Public License as published by the Free Software
 28  
  * Foundation; either version 2 of the License, or (at your option) any later
 29  
  * version.
 30  
  * </p>
 31  
  * <p>
 32  
  * This program is distributed in the hope that it will be useful, but WITHOUT
 33  
  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 34  
  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 35  
  * details.
 36  
  * </p>
 37  
  * <p>
 38  
  * You should have received a copy of the GNU General Public License along with
 39  
  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 40  
  * Place, Suite 330, Boston, MA 02111-1307 USA
 41  
  * </p>
 42  
  * <p>
 43  
  * </p>
 44  
  * 
 45  
  * @author David Read
 46  
  */
 47  
 
 48  
 public class DynamicClassLoader extends ClassLoader {
 49  
   /**
 50  
    * Logger
 51  
    */
 52  1
   private static final Logger LOGGER = Logger
 53  
       .getLogger(DynamicClassLoader.class);
 54  
 
 55  
   /**
 56  
    * Cache of loaded classes
 57  
    */
 58  
   private Map<String, Class<?>> cachedClasses;
 59  
 
 60  
   /**
 61  
    * URL class loader instance
 62  
    */
 63  
   private URLClassLoader classLoader;
 64  
 
 65  
   /**
 66  
    * Create a DynamicClassLoader instance from a list of file paths
 67  
    * 
 68  
    * @param archives
 69  
    *          A list of file paths
 70  
    */
 71  3
   public DynamicClassLoader(List<File> archives) {
 72  3
     cachedClasses = new HashMap<String, Class<?>>();
 73  3
     setupClassLoader(archives);
 74  3
   }
 75  
 
 76  
   /**
 77  
    * Add the files to the class loader
 78  
    * 
 79  
    * @param archives
 80  
    *          The list of file paths to class libraries
 81  
    */
 82  
   public void setupClassLoader(List<File> archives) {
 83  
     List<URL> urls;
 84  
     String fileName;
 85  
 
 86  3
     urls = new ArrayList<URL>();
 87  9
     for (int a = 0; a < archives.size(); ++a) {
 88  
       try {
 89  6
         fileName = (archives.get(a)).getCanonicalPath();
 90  3
         if (fileName.indexOf(":") > 0) {
 91  3
           fileName = fileName.substring(fileName.indexOf(":") + 1);
 92  
         }
 93  3
         fileName = fileName.replace('\\', '/');
 94  
 
 95  3
         LOGGER.info("Add to classpath " + fileName);
 96  3
         urls.add(new URL("jar:file:" + fileName + "!/"));
 97  0
       } catch (MalformedURLException mal) {
 98  0
         LOGGER.error("Error with archive URL", mal);
 99  3
       } catch (IOException io) {
 100  3
         LOGGER.error("Failure loading archive", io);
 101  3
       }
 102  
 
 103  6
       classLoader = new URLClassLoader(urls.toArray(new URL[0]));
 104  
     }
 105  3
   }
 106  
 
 107  
   @Override
 108  
   public synchronized Class<?> loadClass(String className) throws
 109  
       ClassNotFoundException {
 110  1
     return loadClass(className, false);
 111  
   }
 112  
 
 113  
   @Override
 114  
   public synchronized Class<?> loadClass(String className, boolean resolveIt)
 115  
       throws
 116  
       ClassNotFoundException {
 117  
     Class<?> loadedClass;
 118  
 
 119  3
     LOGGER.info("Load class [" + className + "]");
 120  
 
 121  3
     loadedClass = cachedClasses.get(className);
 122  
 
 123  3
     if (loadedClass == null) {
 124  
       try {
 125  3
         loadedClass = super.findSystemClass(className);
 126  1
       } catch (ClassNotFoundException exc) {
 127  
         // Ignore, try and load it below
 128  1
         if (LOGGER.isDebugEnabled()) {
 129  1
           LOGGER.debug(
 130  
               "Class not found on system classpath, look in local files", exc);
 131  
         }
 132  2
       }
 133  
     }
 134  
 
 135  3
     if (loadedClass == null) {
 136  1
       loadedClass = classLoader.loadClass(className);
 137  
     }
 138  
 
 139  2
     if (loadedClass == null) {
 140  0
       throw new ClassNotFoundException(className);
 141  
     }
 142  
 
 143  2
     if (resolveIt) {
 144  1
       resolveClass(loadedClass);
 145  
     }
 146  
 
 147  2
     cachedClasses.put(className, loadedClass);
 148  
 
 149  2
     return loadedClass;
 150  
   }
 151  
 }