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

liquibase.structure.core.DatabaseObjectFactory Maven / Gradle / Ivy

There is a newer version: 3.6.2.5.inovus
Show newest version
package liquibase.structure.core;

import liquibase.changelog.column.LiquibaseColumn;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.logging.LogService;
import liquibase.logging.LogType;
import liquibase.servicelocator.ServiceLocator;
import liquibase.structure.DatabaseObject;
import liquibase.util.StringUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class DatabaseObjectFactory {

    private static DatabaseObjectFactory instance;
    private Set> standardTypes;

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

    private DatabaseObjectFactory() {
    }

    public Set> parseTypes(String typesString) {
        if (StringUtils.trimToNull(typesString) == null) {
            return getStandardTypes();
        } else {
            Set> returnSet = new HashSet<>();

            Set typesToInclude = new HashSet<>(Arrays.asList(typesString.toLowerCase().split("\\s*,\\s*")));
            Set typesNotFound = new HashSet<>(typesToInclude);

            Class[] classes = ServiceLocator.getInstance().findClasses(DatabaseObject.class);
            for (Class clazz : classes) {
                if (typesToInclude.contains(clazz.getSimpleName().toLowerCase())
                        || typesToInclude.contains(clazz.getSimpleName().toLowerCase()+"s")
                        || typesToInclude.contains(clazz.getSimpleName().toLowerCase()+"es") //like indexes
                        ) {
                    returnSet.add(clazz);
                    typesNotFound.remove(clazz.getSimpleName().toLowerCase());
                    typesNotFound.remove(clazz.getSimpleName().toLowerCase()+"s");
                    typesNotFound.remove(clazz.getSimpleName().toLowerCase()+"es");
                }
            }
            if (!typesNotFound.isEmpty()) {
                throw new UnexpectedLiquibaseException("Unknown snapshot type(s) "+StringUtils.join(typesNotFound, ", "));
            }
            return returnSet;
        }
    }

    public Set> getStandardTypes() {
        if (standardTypes == null) {
            Set> set = new HashSet<>();

            Class[] classes = ServiceLocator.getInstance().findClasses(DatabaseObject.class);
            for (Class clazz : classes) {
                try {
                    if (!clazz.equals(LiquibaseColumn.class) && clazz.newInstance().snapshotByDefault()) {
                        set.add(clazz);
                    }
                } catch (Exception e) {
                    LogService.getLog(getClass()).info(LogType.LOG, "Cannot construct "+clazz.getName()+" to determine if it should be included in the snapshot by default");
                }
            }

            standardTypes = set;
        }
        return standardTypes;
    }

    public void reset() {
        this.standardTypes = null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy