liquibase.serializer.SnapshotSerializerFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liquibase-core Show documentation
Show all versions of liquibase-core Show documentation
Liquibase is a tool for managing and executing database changes.
The newest version!
package liquibase.serializer;
import liquibase.Scope;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.servicelocator.PrioritizedService;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class SnapshotSerializerFactory {
private static SnapshotSerializerFactory instance;
private final Map> serializers = new ConcurrentHashMap<>();
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.computeIfAbsent(extension, k -> new ArrayList<>());
snapshotSerializers.add(snapshotSerializer);
snapshotSerializers.sort(PrioritizedService.COMPARATOR);
}
}
public void unregister(SnapshotSerializer snapshotSerializer) {
for (Iterator>> entryIterator = serializers.entrySet().iterator(); entryIterator.hasNext();) {
Map.Entry> entry = entryIterator.next();
List snapshotSerializers = entry.getValue();
snapshotSerializers.removeIf(value -> value.equals(snapshotSerializer));
if (snapshotSerializers.isEmpty()) {
entryIterator.remove();
}
}
}
}