
com.ecyshor.cassmig.files.ModuleFileLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cassandra-migration Show documentation
Show all versions of cassandra-migration Show documentation
Java schema migration library for Cassandra
package com.ecyshor.cassmig.files;
import com.ecyshor.cassmig.exception.ExternalMigrationException;
import com.ecyshor.cassmig.model.ModuleLoadingConfig;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.*;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Collection;
import java.util.List;
import static org.apache.commons.io.filefilter.FileFileFilter.FILE;
public class ModuleFileLoader implements FileLoader {
private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ModuleFileLoader.class);
public List loadFiles(ModuleLoadingConfig config){
URL resource = Thread.currentThread().getContextClassLoader().getResource(config.getPath());
if (resource != null) {
File file = getFileFromURL(resource);
Collection files = FileUtils.listFiles(file, new AndFileFilter(Lists.asList(CanReadFileFilter.CAN_READ,
new SuffixFileFilter(".cql"), new IOFileFilter[] {FILE})), FalseFileFilter.FALSE);
return Lists.newArrayList(Iterables.transform(files, new Function() {
@Nullable public InputStream apply(@Nullable File file) {
try {
assert file != null;
return new FileInputStream(file);
} catch (FileNotFoundException e) {
String error = "Failure to open migration file. File not found for file " + file.getName();
LOGGER.error(error, e);
throw new RuntimeException(error);
}
}
}));
}
throw new IllegalArgumentException("The configured folder " + config.getPath() + " for the migration is not correct.");
}
private File getFileFromURL(URL resource) {
try {
return new File(resource.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
} catch (IllegalArgumentException ex) {
throw new ExternalMigrationException(ex.getMessage());
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy