Search in sources :

Example 1 with ResourceAccessor

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);
}
Also used : ResourceAccessor(liquibase.resource.ResourceAccessor) CSVReader(liquibase.util.csv.CSVReader) EmptyLineAndCommentSkippingInputStream(liquibase.io.EmptyLineAndCommentSkippingInputStream) InputStream(java.io.InputStream) CSVReader(liquibase.util.csv.CSVReader) UtfBomAwareReader(liquibase.resource.UtfBomAwareReader) Reader(java.io.Reader) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) UtfBomAwareReader(liquibase.resource.UtfBomAwareReader)

Example 2 with ResourceAccessor

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();
        }
    }
}
Also used : CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ResourceAccessor(liquibase.resource.ResourceAccessor) Connection(java.sql.Connection) JdbcConnection(liquibase.database.jvm.JdbcConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) Contexts(liquibase.Contexts) DataSource(javax.sql.DataSource) Liquibase(liquibase.Liquibase) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) Database(liquibase.database.Database) LabelExpression(liquibase.LabelExpression) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor)

Example 3 with ResourceAccessor

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;
    }
}
Also used : Liquibase(liquibase.Liquibase) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ResourceAccessor(liquibase.resource.ResourceAccessor) Database(liquibase.database.Database) LiquibaseException(liquibase.exception.LiquibaseException) BuildException(org.apache.tools.ant.BuildException)

Example 4 with ResourceAccessor

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");
}
Also used : CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ResourceAccessor(liquibase.resource.ResourceAccessor) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ArrayList(java.util.ArrayList) ChangeLogSerializer(liquibase.serializer.ChangeLogSerializer) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) CommandResult(liquibase.command.CommandResult) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) ChangeLogParameters(liquibase.changelog.ChangeLogParameters) ChangeLogParser(liquibase.parser.ChangeLogParser) FileOutputStream(java.io.FileOutputStream) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) File(java.io.File)

Example 5 with ResourceAccessor

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);
}
Also used : ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ResourceAccessor(liquibase.resource.ResourceAccessor) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) Before(org.junit.Before)

Aggregations

ResourceAccessor (liquibase.resource.ResourceAccessor)12 CompositeResourceAccessor (liquibase.resource.CompositeResourceAccessor)7 FileSystemResourceAccessor (liquibase.resource.FileSystemResourceAccessor)7 Database (liquibase.database.Database)4 LiquibaseException (liquibase.exception.LiquibaseException)4 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)4 ClassLoaderResourceAccessor (liquibase.resource.ClassLoaderResourceAccessor)4 Liquibase (liquibase.Liquibase)3 File (java.io.File)2 Connection (java.sql.Connection)2 JdbcConnection (liquibase.database.jvm.JdbcConnection)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 URLClassLoader (java.net.URLClassLoader)1 ArrayList (java.util.ArrayList)1 DataSource (javax.sql.DataSource)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1