use of liquibase.resource.ResourceAccessor in project liquibase by liquibase.
the class LoadDataChange method getCSVReader.
public CSVReader getCSVReader() throws IOException {
ResourceAccessor resourceAccessor = getResourceAccessor();
if (resourceAccessor == null) {
throw new UnexpectedLiquibaseException("No file resourceAccessor specified for " + getFile());
}
InputStream stream = StreamUtil.openStream(file, isRelativeToChangelogFile(), getChangeSet(), resourceAccessor);
if (stream == null) {
return null;
}
Reader streamReader;
if (getEncoding() == null) {
streamReader = new UtfBomAwareReader(stream);
} else {
streamReader = new UtfBomAwareReader(stream, getEncoding());
}
char quotchar;
if (StringUtils.trimToEmpty(this.quotchar).length() == 0) {
// hope this is impossible to have a field surrounded with non ascii char 0x01
quotchar = '\1';
} else {
quotchar = this.quotchar.charAt(0);
}
if (separator == null) {
separator = liquibase.util.csv.CSVReader.DEFAULT_SEPARATOR + "";
}
return new CSVReader(streamReader, separator.charAt(0), quotchar);
}
use of liquibase.resource.ResourceAccessor in project liquibase by liquibase.
the class LiquibaseServletListener method executeUpdate.
/**
* Executes the Liquibase update.
*/
private void executeUpdate(ServletContext servletContext, InitialContext ic) throws NamingException, SQLException, LiquibaseException {
setDataSource((String) servletValueContainer.getValue(LIQUIBASE_DATASOURCE));
if (getDataSource() == null) {
throw new RuntimeException("Cannot run Liquibase, " + LIQUIBASE_DATASOURCE + " is not set");
}
setChangeLogFile((String) servletValueContainer.getValue(LIQUIBASE_CHANGELOG));
if (getChangeLogFile() == null) {
throw new RuntimeException("Cannot run Liquibase, " + LIQUIBASE_CHANGELOG + " is not set");
}
setContexts((String) servletValueContainer.getValue(LIQUIBASE_CONTEXTS));
setLabels((String) servletValueContainer.getValue(LIQUIBASE_LABELS));
this.defaultSchema = StringUtils.trimToNull((String) servletValueContainer.getValue(LIQUIBASE_SCHEMA_DEFAULT));
Connection connection = null;
Database database = null;
try {
DataSource dataSource = (DataSource) ic.lookup(this.dataSourceName);
connection = dataSource.getConnection();
Thread currentThread = Thread.currentThread();
ClassLoader contextClassLoader = currentThread.getContextClassLoader();
ResourceAccessor threadClFO = new ClassLoaderResourceAccessor(contextClassLoader);
ResourceAccessor clFO = new ClassLoaderResourceAccessor();
ResourceAccessor fsFO = new FileSystemResourceAccessor();
database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
database.setDefaultSchemaName(getDefaultSchema());
Liquibase liquibase = new Liquibase(getChangeLogFile(), new CompositeResourceAccessor(clFO, fsFO, threadClFO), database);
@SuppressWarnings("unchecked") Enumeration<String> initParameters = servletContext.getInitParameterNames();
while (initParameters.hasMoreElements()) {
String name = initParameters.nextElement().trim();
if (name.startsWith(LIQUIBASE_PARAMETER + ".")) {
liquibase.setChangeLogParameter(name.substring(LIQUIBASE_PARAMETER.length() + 1), servletValueContainer.getValue(name));
}
}
liquibase.update(new Contexts(getContexts()), new LabelExpression(getLabels()));
} finally {
if (database != null) {
database.close();
} else if (connection != null) {
connection.close();
}
}
}
use of liquibase.resource.ResourceAccessor in project liquibase by liquibase.
the class BaseLiquibaseTask method execute.
@Override
public final void execute() throws BuildException {
super.execute();
log("Starting Liquibase.", Project.MSG_INFO);
classLoader = getProject().createClassLoader(classpath);
classLoader.setParent(this.getClass().getClassLoader());
classLoader.setThreadContextLoader();
validateParameters();
Database database = null;
try {
ResourceAccessor resourceAccessor = createResourceAccessor(classLoader);
database = createDatabaseFromType(databaseType);
liquibase = new Liquibase(getChangeLogFile(), resourceAccessor, database);
if (changeLogParameters != null) {
changeLogParameters.applyParameters(liquibase);
}
if (isPromptOnNonLocalDatabase() && !liquibase.isSafeToRunUpdate() && UIFactory.getInstance().getFacade().promptForNonLocalDatabase(liquibase.getDatabase())) {
log("User chose not to run task against a non-local database.", Project.MSG_INFO);
return;
}
if (shouldRun()) {
executeWithLiquibaseClassloader();
}
} catch (LiquibaseException e) {
throw new BuildException("Unable to initialize Liquibase. " + e.toString(), e);
} finally {
closeDatabase(database);
classLoader.resetThreadContextLoader();
classLoader.cleanup();
classLoader = null;
}
}
use of liquibase.resource.ResourceAccessor in project liquibase by liquibase.
the class ConvertCommand method run.
@Override
protected CommandResult run() throws Exception {
List<ResourceAccessor> openers = new ArrayList<ResourceAccessor>();
openers.add(new FileSystemResourceAccessor());
openers.add(new ClassLoaderResourceAccessor());
if (classpath != null) {
openers.add(new FileSystemResourceAccessor(classpath));
}
ResourceAccessor resourceAccessor = new CompositeResourceAccessor(openers);
ChangeLogParser sourceParser = ChangeLogParserFactory.getInstance().getParser(src, resourceAccessor);
ChangeLogSerializer outSerializer = ChangeLogSerializerFactory.getInstance().getSerializer(out);
DatabaseChangeLog changeLog = sourceParser.parse(src, new ChangeLogParameters(), resourceAccessor);
File outFile = new File(out);
if (!outFile.exists()) {
outFile.getParentFile().mkdirs();
}
FileOutputStream outputStream = new FileOutputStream(outFile);
try {
outSerializer.write(changeLog.getChangeSets(), outputStream);
} finally {
outputStream.flush();
outputStream.close();
}
return new CommandResult("Converted successfully");
}
use of liquibase.resource.ResourceAccessor in project liquibase by liquibase.
the class ExtServiceLocatorTest method setup.
@Before
public void setup() throws Exception {
ResourceAccessor resourceAccessor = new ClassLoaderResourceAccessor();
serviceLocator = ServiceLocator.getInstance();
serviceLocator.setResourceAccessor(resourceAccessor);
}
Aggregations