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

liquibase.serializer.SnapshotSerializerFactory Maven / Gradle / Ivy

There is a newer version: 4.30.0
Show newest version
package liquibase.serializer;

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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SnapshotSerializerFactory {
    private static SnapshotSerializerFactory instance;

    private Map serializers = new HashMap();


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

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

        return instance;
    }

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

            for (Class clazz : classes) {
                register((SnapshotSerializer) clazz.getConstructor().newInstance());
            }
        } catch (Exception e) {
            throw new UnexpectedLiquibaseException(e);
        }

    }

    public Map getSerializers() {
        return serializers;
    }

    public SnapshotSerializer getSerializer(String fileNameOrExtension) {
        fileNameOrExtension = fileNameOrExtension.replaceAll(".*\\.", ""); //just need the extension
        SnapshotSerializer snapshotSerializer = serializers.get(fileNameOrExtension);
        if (snapshotSerializer == null) {
            throw new RuntimeException("No serializer associated with the filename or extension '" + fileNameOrExtension + "'");
        }
        return snapshotSerializer;
    }

    public void register(SnapshotSerializer snapshotSerializer) {
        for (String extension : snapshotSerializer.getValidFileExtensions()) {
            serializers.put(extension, snapshotSerializer);
        }
    }

    public void unregister(SnapshotSerializer snapshotSerializer) {
        List> entrysToRemove = new ArrayList>();
        for (Map.Entry entry : serializers.entrySet()) {
            if (entry.getValue().equals(snapshotSerializer)) {
                entrysToRemove.add(entry);
            }
        }

        for (Map.Entry entry : entrysToRemove) {
            serializers.remove(entry.getKey());
        }

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy