Fix syncronization issue with the marker-api

This commit is contained in:
Blue (Lukas Rieger) 2021-06-17 22:56:29 +02:00
parent c31945444d
commit 470a5cbad1
No known key found for this signature in database
GPG Key ID: 904C4995F9E1F800

View File

@ -92,63 +92,67 @@ public synchronized void load() throws IOException {
} }
private synchronized void load(boolean overwriteChanges) throws IOException { private synchronized void load(boolean overwriteChanges) throws IOException {
Set<String> externallyRemovedSets = new HashSet<>(markerSets.keySet()); synchronized (MarkerAPIImpl.class) {
Set<String> externallyRemovedSets = new HashSet<>(markerSets.keySet());
if (markerFile.exists() && markerFile.isFile()) { if (markerFile.exists() && markerFile.isFile()) {
GsonConfigurationLoader loader = GsonConfigurationLoader.builder().file(markerFile).build(); GsonConfigurationLoader loader = GsonConfigurationLoader.builder().file(markerFile).build();
ConfigurationNode node = loader.load(); ConfigurationNode node = loader.load();
for (ConfigurationNode markerSetNode : node.node("markerSets").childrenList()) { for (ConfigurationNode markerSetNode : node.node("markerSets").childrenList()) {
String setId = markerSetNode.node("id").getString(); String setId = markerSetNode.node("id").getString();
if (setId == null) { if (setId == null) {
Logger.global.logDebug("Marker-API: Failed to load a markerset: No id defined!"); Logger.global.logDebug("Marker-API: Failed to load a markerset: No id defined!");
continue; continue;
} }
externallyRemovedSets.remove(setId); externallyRemovedSets.remove(setId);
if (!overwriteChanges && removedMarkerSets.contains(setId)) continue; if (!overwriteChanges && removedMarkerSets.contains(setId)) continue;
MarkerSetImpl set = markerSets.get(setId); MarkerSetImpl set = markerSets.get(setId);
try { try {
if (set == null) { if (set == null) {
set = new MarkerSetImpl(setId); set = new MarkerSetImpl(setId);
set.load(api, markerSetNode, true); set.load(api, markerSetNode, true);
} else { } else {
set.load(api, markerSetNode, overwriteChanges); set.load(api, markerSetNode, overwriteChanges);
}
markerSets.put(setId, set);
} catch (MarkerFileFormatException ex) {
Logger.global.logDebug("Marker-API: Failed to load marker-set '" + setId + ": " + ex);
} }
markerSets.put(setId, set);
} catch (MarkerFileFormatException ex) {
Logger.global.logDebug("Marker-API: Failed to load marker-set '" + setId + ": " + ex);
} }
} }
}
if (overwriteChanges) {
if (overwriteChanges) { for (String setId : externallyRemovedSets) {
for (String setId : externallyRemovedSets) { markerSets.remove(setId);
markerSets.remove(setId); }
removedMarkerSets.clear();
} }
removedMarkerSets.clear();
} }
} }
@Override @Override
public synchronized void save() throws IOException { public synchronized void save() throws IOException {
load(false); synchronized (MarkerAPIImpl.class) {
load(false);
FileUtils.createFile(markerFile); FileUtils.createFile(markerFile);
GsonConfigurationLoader loader = GsonConfigurationLoader.builder().file(markerFile).build(); GsonConfigurationLoader loader = GsonConfigurationLoader.builder().file(markerFile).build();
ConfigurationNode node = loader.createNode(); ConfigurationNode node = loader.createNode();
for (MarkerSetImpl set : markerSets.values()) { for (MarkerSetImpl set : markerSets.values()) {
set.save(node.node("markerSets").appendListNode()); set.save(node.node("markerSets").appendListNode());
}
loader.save(node);
removedMarkerSets.clear();
} }
loader.save(node);
removedMarkerSets.clear();
} }
} }