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.Scope;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.servicelocator.PrioritizedService;
import liquibase.servicelocator.ServiceLocator;

import java.util.*;

public class SnapshotSerializerFactory {
    private static SnapshotSerializerFactory instance;

    private Map> serializers = new HashMap<>();

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

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

        return instance;
    }

    private SnapshotSerializerFactory() {
        try {
            for (SnapshotSerializer serializer : Scope.getCurrentScope().getServiceLocator().findInstances(SnapshotSerializer.class)) {
                register(serializer);
            }
        } catch (Exception e) {
            throw new UnexpectedLiquibaseException(e);
        }
    }

    public Map> getSerializers() {
        return serializers;
    }

    public List getSerializers(String fileNameOrExtension) {
        fileNameOrExtension = fileNameOrExtension.replaceAll(".*\\.", ""); //just need the extension
        List snapshotSerializers = serializers.get(fileNameOrExtension);
        if (snapshotSerializers == null) {
            return Collections.emptyList();
        }
        return snapshotSerializers;
    }

    public SnapshotSerializer getSerializer(String fileNameOrExtension) {
        List snapshotSerializers = getSerializers(fileNameOrExtension);
        if (snapshotSerializers.isEmpty()) {
            throw new RuntimeException("No serializers associated with the filename or extension '" + fileNameOrExtension + "'");
        }
        return snapshotSerializers.get(0);
    }

    public void register(SnapshotSerializer snapshotSerializer) {
        for (String extension : snapshotSerializer.getValidFileExtensions()) {
            List snapshotSerializers = serializers.get(extension);
            if (snapshotSerializers == null) {
                snapshotSerializers = new ArrayList<>();
                serializers.put(extension, snapshotSerializers);
            }
            snapshotSerializers.add(snapshotSerializer);
            Collections.sort(snapshotSerializers, PrioritizedService.COMPARATOR);
        }
    }

    public void unregister(SnapshotSerializer snapshotSerializer) {
        for (Iterator>> entryIterator = serializers.entrySet().iterator(); entryIterator.hasNext();) {
            Map.Entry> entry = entryIterator.next();
            List snapshotSerializers = entry.getValue();
            for (Iterator iterator = snapshotSerializers.iterator(); iterator.hasNext();) {
                SnapshotSerializer value = iterator.next();
                if (value.equals(snapshotSerializer)) {
                    iterator.remove();
                }
            }
            if (snapshotSerializers.isEmpty()) {
                entryIterator.remove();
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy