All Downloads are FREE. Search and download functionalities are using the official Maven repository.

liquibase.integration.commandline.CommandLineResourceAccessor Maven / Gradle / Ivy

There is a newer version: 4.30.0
Show newest version
package liquibase.integration.commandline;

import liquibase.resource.ResourceAccessor;
import liquibase.util.StringUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

/**
 * Implementation of liquibase.FileOpener for the command line app.
 *
 * @see liquibase.resource.ResourceAccessor
 */
public class CommandLineResourceAccessor implements ResourceAccessor {
    private ClassLoader loader;

    public CommandLineResourceAccessor(ClassLoader loader) {
        this.loader = loader;
    }

    @Override
    public InputStream getResourceAsStream(String file) throws IOException {
        URL resource = loader.getResource(file);
        if (resource == null) {
            throw new IOException(file + " could not be found");
        }
        return resource.openStream();
    }

    @Override
    public Enumeration getResources(String packageName) throws IOException {
        return loader.getResources(packageName);
    }

    @Override
    public ClassLoader toClassLoader() {
        return loader;
    }

    @Override
    public String toString() {
        String description;
        if (loader instanceof URLClassLoader) {
            List urls = new ArrayList();
            for (URL url : ((URLClassLoader) loader).getURLs()) {
                urls.add(url.toExternalForm());
            }
            description = StringUtils.join(urls, ",");
        } else {
            description = loader.getClass().getName();
        }
        return getClass().getName()+"("+ description +")";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy