Coverage Report - liquibase.resource.FileSystemResourceAccessor
 
Classes in this File Line Coverage Branch Coverage Complexity
FileSystemResourceAccessor
60%
20/33
46%
12/26
3.5
FileSystemResourceAccessor$1
100%
3/3
N/A
3.5
 
 1  
 package liquibase.resource;
 2  
 
 3  
 import java.io.File;
 4  
 import java.io.FileInputStream;
 5  
 import java.io.IOException;
 6  
 import java.io.InputStream;
 7  
 import java.net.MalformedURLException;
 8  
 import java.net.URL;
 9  
 import java.net.URLClassLoader;
 10  
 import java.util.ArrayList;
 11  
 import java.util.Enumeration;
 12  
 import java.util.Iterator;
 13  
 import java.util.List;
 14  
 import java.util.Vector;
 15  
 
 16  
 /**
 17  
  * A FileOpener implementation which finds Files in the File System.
 18  
  * <p/>
 19  
  * FileSystemFileOpeners can use a BaseDirectory to determine where relative paths should be resolved from.
 20  
  * 
 21  
  * @author <a href="mailto:csuml@yahoo.co.uk>Paul Keeble</a>
 22  
  */
 23  
 public class FileSystemResourceAccessor implements ResourceAccessor {
 24  
     String baseDirectory;
 25  
 
 26  
     /**
 27  
      * Creates using a Base directory of null, all files will be resolved exactly as they are given.
 28  
      */
 29  0
     public FileSystemResourceAccessor() {
 30  0
         baseDirectory = null;
 31  0
     }
 32  
 
 33  
     /**
 34  
      * Creates using a supplied base directory.
 35  
      * 
 36  
      * @param base
 37  
      *            The path to use to resolve relative paths
 38  
      */
 39  5
     public FileSystemResourceAccessor(String base) {
 40  5
         if (new File(base).isFile())
 41  1
             throw new IllegalArgumentException("base must be a directory");
 42  4
         baseDirectory = base;
 43  4
     }
 44  
 
 45  
     /**
 46  
      * Opens a stream on a file, resolving to the baseDirectory if the file is relative.
 47  
      */
 48  
     @Override
 49  
     public InputStream getResourceAsStream(String file) throws IOException {
 50  1
         File absoluteFile = new File(file);
 51  1
         File relativeFile = (baseDirectory == null) ? new File(file) : new File(baseDirectory, file);
 52  
 
 53  1
         if (absoluteFile.exists() && absoluteFile.isFile() && absoluteFile.isAbsolute()) {
 54  0
             return new FileInputStream(absoluteFile);
 55  1
         } else if (relativeFile.exists() && relativeFile.isFile()) {
 56  1
             return new FileInputStream(relativeFile);
 57  
         } else {
 58  0
             return null;
 59  
 
 60  
         }
 61  
     }
 62  
 
 63  
     @Override
 64  
     public Enumeration<URL> getResources(String packageName) throws IOException {
 65  2
         String directoryPath = (new File(packageName).isAbsolute() || baseDirectory == null) ? packageName
 66  
                 : baseDirectory + File.separator + packageName;
 67  
 
 68  2
         File directoryFile = new File(directoryPath);
 69  2
         if (!directoryFile.exists()) {
 70  0
             return new Vector<URL>().elements();
 71  
         }
 72  2
         File[] files = directoryFile.listFiles();
 73  
 
 74  2
         List<URL> results = new ArrayList<URL>();
 75  
 
 76  14
         for (File f : files) {
 77  12
             if (!f.isDirectory())
 78  12
                 results.add(f.toURI().toURL());
 79  
         }
 80  
 
 81  2
         final Iterator<URL> it = results.iterator();
 82  14
         return new Enumeration<URL>() {
 83  
 
 84  
             @Override
 85  
             public boolean hasMoreElements() {
 86  14
                 return it.hasNext();
 87  
             }
 88  
 
 89  
             @Override
 90  
             public URL nextElement() {
 91  12
                 return it.next();
 92  
             }
 93  
         };
 94  
     }
 95  
 
 96  
     @Override
 97  
     public ClassLoader toClassLoader() {
 98  
         try {
 99  0
             return new URLClassLoader(new URL[] { new URL("file://" + baseDirectory) });
 100  0
         } catch (MalformedURLException e) {
 101  0
             throw new RuntimeException(e);
 102  
         }
 103  
     }
 104  
 
 105  
     @Override
 106  
     public String toString() {
 107  0
         String dir = baseDirectory;
 108  0
         if (dir == null) {
 109  0
             dir = new File(".").getAbsolutePath();
 110  
         }
 111  0
         return getClass().getName() + "(" + dir + ")";
 112  
     }
 113  
 
 114  
 }