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

liquibase.change.ChangeFactory Maven / Gradle / Ivy

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

import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.servicelocator.ServiceLocator;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Factory class for constructing the correct liquibase.change.Change implementation based on the tag name.
 * It is currently implemented by a static array of Change implementations, although that may change in
 * later revisions.  The best way to get an instance of ChangeFactory is off the Liquibase.getChangeFactory() method.
 *
 * @see liquibase.change.Change
 */
public class ChangeFactory {

    private static ChangeFactory instance;

    private Map>> registry = new ConcurrentHashMap>>();

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

            for (Class clazz : classes) {
                //noinspection unchecked
                register(clazz);
            }

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

    }

    /**
     * Return singleton SqlGeneratorFactory
     */
    public static synchronized ChangeFactory getInstance() {
        if (instance == null) {
             instance = new ChangeFactory();
        }
        return instance;
    }

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


    public void register(Class changeClass) {
        try {
            String name = changeClass.newInstance().getChangeMetaData().getName();
            if (registry.get(name) == null) {
                registry.put(name, new TreeSet>(new Comparator>() {
                    public int compare(Class o1, Class o2) {
                        try {
                            return -1 * new Integer(o1.newInstance().getChangeMetaData().getPriority()).compareTo(o2.newInstance().getChangeMetaData().getPriority());
                        } catch (Exception e) {
                            throw new UnexpectedLiquibaseException(e);
                        }
                    }
                }));
            }
            registry.get(name).add(changeClass);
        } catch (Exception e) {
            throw new UnexpectedLiquibaseException(e);
        }
    }

    public void unregister(String name) {
        registry.remove(name);
    }

    public Map>> getRegistry() {
        return registry;
    }

    public Change create(String name) {
        SortedSet> classes = registry.get(name);

        if (classes == null) {
            return null;
        }

        try {
            return classes.iterator().next().newInstance();
        } catch (Exception e) {
            throw new UnexpectedLiquibaseException(e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy