Search in sources :

Example 1 with FileSystemResource

use of org.flywaydb.core.internal.resource.filesystem.FileSystemResource in project flyway by flyway.

the class FileSystemScanner method scanForResources.

/**
 * Scans the FileSystem for resources under the specified location, starting with the specified prefix and ending with
 * the specified suffix.
 *
 * @param location The location in the filesystem to start searching. Subdirectories are also searched.
 * @return The resources that were found.
 */
public Collection<LoadableResource> scanForResources(Location location) {
    String path = location.getRootPath();
    LOG.debug("Scanning for filesystem resources at '" + path + "'");
    File dir = new File(path);
    if (!dir.exists()) {
        if (throwOnMissingLocations) {
            throw new FlywayException("Failed to find filesystem location:" + path + ".");
        }
        LOG.error("Skipping filesystem location:" + path + " (not found).");
        return Collections.emptyList();
    }
    if (!dir.canRead()) {
        if (throwOnMissingLocations) {
            throw new FlywayException("Failed to find filesystem location:" + path + " (not readable).");
        }
        LOG.error("Skipping filesystem location:" + path + " (not readable).");
        return Collections.emptyList();
    }
    if (!dir.isDirectory()) {
        if (throwOnMissingLocations) {
            throw new FlywayException("Failed to find filesystem location:" + path + " (not a directory).");
        }
        LOG.error("Skipping filesystem location:" + path + " (not a directory).");
        return Collections.emptyList();
    }
    Set<LoadableResource> resources = new TreeSet<>();
    for (String resourceName : findResourceNamesFromFileSystem(path, new File(path))) {
        boolean detectEncodingForThisResource = detectEncoding;
        if (location.matchesPath(resourceName)) {
            Charset encoding = defaultEncoding;
            String encodingBlurb = "";
            if (new File(resourceName + ".conf").exists()) {
                LoadableResource metadataResource = new FileSystemResource(location, resourceName + ".conf", defaultEncoding, false);
                SqlScriptMetadata metadata = SqlScriptMetadata.fromResource(metadataResource, null);
                if (metadata.encoding() != null) {
                    encoding = Charset.forName(metadata.encoding());
                    detectEncodingForThisResource = false;
                    encodingBlurb = " (with overriding encoding " + encoding + ")";
                }
            }
            resources.add(new FileSystemResource(location, resourceName, encoding, detectEncodingForThisResource, stream));
            LOG.debug("Found filesystem resource: " + resourceName + encodingBlurb);
        }
    }
    return resources;
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) TreeSet(java.util.TreeSet) SqlScriptMetadata(org.flywaydb.core.internal.sqlscript.SqlScriptMetadata) LoadableResource(org.flywaydb.core.api.resource.LoadableResource) Charset(java.nio.charset.Charset) FileSystemResource(org.flywaydb.core.internal.resource.filesystem.FileSystemResource) File(java.io.File)

Aggregations

File (java.io.File)1 Charset (java.nio.charset.Charset)1 TreeSet (java.util.TreeSet)1 FlywayException (org.flywaydb.core.api.FlywayException)1 LoadableResource (org.flywaydb.core.api.resource.LoadableResource)1 FileSystemResource (org.flywaydb.core.internal.resource.filesystem.FileSystemResource)1 SqlScriptMetadata (org.flywaydb.core.internal.sqlscript.SqlScriptMetadata)1