liquibase.parser.ChangeLogParserFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liquibase-core Show documentation
Show all versions of liquibase-core Show documentation
Liquibase is a tool for managing and executing database changes.
package liquibase.parser;
import liquibase.Scope;
import liquibase.exception.LiquibaseException;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.exception.UnknownChangelogFormatException;
import liquibase.resource.ResourceAccessor;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ChangeLogParserFactory {
private static ChangeLogParserFactory instance;
private final List parsers = new ArrayList<>();
public static synchronized void reset() {
instance = new ChangeLogParserFactory();
}
public static synchronized ChangeLogParserFactory getInstance() {
if (instance == null) {
instance = new ChangeLogParserFactory();
}
return instance;
}
/**
* Set the instance used by this singleton. Used primarily for testing.
*/
public static synchronized void setInstance(ChangeLogParserFactory instance) {
ChangeLogParserFactory.instance = instance;
}
private ChangeLogParserFactory() {
try {
List parser = Scope.getCurrentScope().getServiceLocator().findInstances(ChangeLogParser.class);
register(parser);
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
}
}
public List getParsers() {
return new ArrayList<>(parsers);
}
public ChangeLogParser getParser(String fileNameOrExtension, ResourceAccessor resourceAccessor) throws LiquibaseException {
for (ChangeLogParser parser : parsers) {
if (parser.supports(fileNameOrExtension, resourceAccessor)) {
Scope.getCurrentScope().getLog(ChangeLogParserFactory.class).fine(String.format("Matched file '%s' to parser '%s'",
fileNameOrExtension, parser.getClass().getSimpleName()));
return parser;
}
}
throw new UnknownChangelogFormatException("Cannot find parser that supports " + fileNameOrExtension);
}
public void register(ChangeLogParser changeLogParsers) {
register(Collections.singletonList(changeLogParsers));
}
private void register(List changeLogParsers) {
parsers.addAll(changeLogParsers);
parsers.sort(ChangeLogParser.COMPARATOR);
}
public void unregister(ChangeLogParser changeLogParser) {
parsers.remove(changeLogParser);
}
public void unregisterAllParsers() {
parsers.clear();
}
}