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

liquibase.database.ConnectionServiceFactory Maven / Gradle / Ivy

The newest version!
package liquibase.database;

import liquibase.Scope;
import liquibase.exception.DatabaseException;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.servicelocator.PrioritizedService;

import java.sql.Driver;
import java.util.*;

public class ConnectionServiceFactory {
    private static ConnectionServiceFactory instance;

    private final List databaseConnections = new ArrayList<>();

    public static synchronized void reset() {
        instance = new ConnectionServiceFactory();
    }

    public static synchronized ConnectionServiceFactory getInstance() {
        if (instance == null) {
            instance = new ConnectionServiceFactory();
        }

        return instance;
    }

    public DatabaseConnection create(String url, Driver driverObject, Properties driverProperties)
               throws DatabaseException {
        DatabaseConnection databaseConnection = getDatabaseConnection(url);
        try {
            databaseConnection.open(url, driverObject, driverProperties);
        }
        catch (Exception sqle) {
            throw new DatabaseException(sqle);
        }
        return databaseConnection;
    }

    /**
     * @deprecated use {@link #getDatabaseConnection(String)}
     */
    public DatabaseConnection getDatabaseConnection() {
        return getDatabaseConnection(null);
    }

    public DatabaseConnection getDatabaseConnection(String url) {
        final SortedSet sortedConnections = new TreeSet<>(
                (o1, o2) -> -1 * Integer.compare(o1.getPriority(), o2.getPriority()));

        databaseConnections.stream().filter(c -> c.supports(url))
                .forEach(sortedConnections::add);

        try {
            DatabaseConnection exampleService = sortedConnections.iterator().next();
            Class aClass = exampleService.getClass();
            DatabaseConnection connection;
            try {
                aClass.getConstructor();
                connection = aClass.getConstructor().newInstance();
            } catch (NoSuchMethodException e) {
                // must have been manually added to the registry and so already configured.
                connection = exampleService;
            }

            return connection;
        } catch (Exception e) {
            throw new UnexpectedLiquibaseException(e);
        }
    }

    private ConnectionServiceFactory() {
        List connections;
        try {
            connections = Scope.getCurrentScope().getServiceLocator().findInstances(DatabaseConnection.class);

            for (DatabaseConnection connection : connections) {
                register(connection.getClass().getConstructor().newInstance());
            }
        } catch (Exception e) {
            throw new UnexpectedLiquibaseException(e);
        }
    }

    public void register(DatabaseConnection databaseConnection) {
        databaseConnections.add(databaseConnection);
        databaseConnections.sort(PrioritizedService.COMPARATOR);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy