Coverage Report - org.liquibase.maven.plugins.MavenResourceAccessor
 
Classes in this File Line Coverage Branch Coverage Complexity
MavenResourceAccessor
0%
0/16
0%
0/4
1.333
 
 1  
 package org.liquibase.maven.plugins;
 2  
 
 3  
 import java.io.IOException;
 4  
 import java.io.InputStream;
 5  
 import java.net.URL;
 6  
 import java.net.URLClassLoader;
 7  
 import java.util.ArrayList;
 8  
 import java.util.Enumeration;
 9  
 import java.util.List;
 10  
 
 11  
 import liquibase.resource.ResourceAccessor;
 12  
 import liquibase.util.StringUtils;
 13  
 
 14  
 /**
 15  
  * Implementation of liquibase.FileOpener for Maven which will use a default or user specified {@link ClassLoader} to
 16  
  * load files/resources.
 17  
  * 
 18  
  * @see liquibase.resource.ResourceAccessor
 19  
  */
 20  
 public class MavenResourceAccessor implements ResourceAccessor {
 21  
 
 22  
     /**
 23  
      * The class loader to use to load files/resources from.
 24  
      */
 25  
     private ClassLoader _loader;
 26  
 
 27  
     /**
 28  
      * Creates a {@link liquibase.resource.ResourceAccessor} that uses the classloader for the class.
 29  
      */
 30  
     public MavenResourceAccessor() {
 31  0
         this(MavenResourceAccessor.class.getClassLoader());
 32  0
     }
 33  
 
 34  
     /**
 35  
      * Creates a {@link liquibase.resource.ResourceAccessor} that will use the specified {@link ClassLoader} to load
 36  
      * files.
 37  
      * 
 38  
      * @param cl
 39  
      *            The {@link ClassLoader} to use to load files/resources.
 40  
      */
 41  0
     public MavenResourceAccessor(ClassLoader cl) {
 42  0
         _loader = cl;
 43  0
     }
 44  
 
 45  
     public InputStream getResourceAsStream(String file) throws IOException {
 46  0
         return _loader.getResourceAsStream(file);
 47  
     }
 48  
 
 49  
     public Enumeration<URL> getResources(String packageName) throws IOException {
 50  0
         return _loader.getResources(packageName);
 51  
     }
 52  
 
 53  
     public ClassLoader toClassLoader() {
 54  0
         return _loader;
 55  
     }
 56  
 
 57  
     @Override
 58  
     public String toString() {
 59  
         String description;
 60  0
         if (_loader instanceof URLClassLoader) {
 61  0
             List<String> urls = new ArrayList<String>();
 62  0
             for (URL url : ((URLClassLoader) _loader).getURLs()) {
 63  0
                 urls.add(url.toExternalForm());
 64  
             }
 65  0
             description = StringUtils.join(urls, ",");
 66  0
         } else {
 67  0
             description = _loader.getClass().getName();
 68  
         }
 69  0
         return getClass().getName() + "(" + description + ")";
 70  
     }
 71  
 }