All Downloads are FREE. Search and download functionalities are using the official Maven repository.

liquibase.snapshot.DatabaseSnapshotGeneratorFactory Maven / Gradle / Ivy

There is a newer version: 4.29.2
Show newest version
package liquibase.snapshot;

import liquibase.database.Database;
import liquibase.diff.DiffStatusListener;
import liquibase.exception.DatabaseException;
import liquibase.servicelocator.ServiceLocator;

import java.util.*;

public class DatabaseSnapshotGeneratorFactory {

    private static DatabaseSnapshotGeneratorFactory instance;

    private List registry = new ArrayList();

    private DatabaseSnapshotGeneratorFactory() {
        try {
            Class[] classes = ServiceLocator.getInstance().findClasses(DatabaseSnapshotGenerator.class);

            for (Class clazz : classes) {
                register(clazz.getConstructor().newInstance());
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public static DatabaseSnapshotGeneratorFactory getInstance() {
        if (instance == null) {
             instance = new DatabaseSnapshotGeneratorFactory();
        }
        return instance;
    }

    public DatabaseSnapshotGenerator getGenerator(Database database) {
        return getGenerators(database).iterator().next();
    }


    /**
     * Get generators supporting database, sorted from highest priority to
     * lowest.
     *
     * @param database
     * @return
     */
    public SortedSet getGenerators(final Database database) {
        SortedSet generators = new TreeSet(new Comparator() {
            public int compare(DatabaseSnapshotGenerator o1, DatabaseSnapshotGenerator o2) {
                return -1 * Integer.valueOf(o1.getPriority(database)).compareTo(o2.getPriority(database));
            }
        });

        for (DatabaseSnapshotGenerator generator : registry) {
            if (generator.supports(database)) {
                generators.add(generator);
            }
        }

        return generators;
    }

    /**
     * Get generator for database with highest priority.
     */
    public DatabaseSnapshot createSnapshot(Database database, String schema, Set listeners) throws DatabaseException {
        return getGenerator(database).createSnapshot(database, schema, listeners);
    }

    /**
     * Returns instances of all implemented database types.
     */
    public List getRegistry() {
        return registry;
    }

    public void register(DatabaseSnapshotGenerator snapshotGenerator) {
        registry.add(0, snapshotGenerator);
    }

    public static void resetAll() {
        instance = null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy