From 11ac90f9540989fc3c472493ce176accb5dce6a6 Mon Sep 17 00:00:00 2001 From: Hylke van der Schaaf Date: Wed, 10 Jan 2024 09:23:13 +0100 Subject: [PATCH] Fixed ConcurrentModificationException in initialisation --- .../json/deserialize/JsonReader.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/FROST-Server.Core/src/main/java/de/fraunhofer/iosb/ilt/frostserver/json/deserialize/JsonReader.java b/FROST-Server.Core/src/main/java/de/fraunhofer/iosb/ilt/frostserver/json/deserialize/JsonReader.java index 47d4e1222..220ed7276 100644 --- a/FROST-Server.Core/src/main/java/de/fraunhofer/iosb/ilt/frostserver/json/deserialize/JsonReader.java +++ b/FROST-Server.Core/src/main/java/de/fraunhofer/iosb/ilt/frostserver/json/deserialize/JsonReader.java @@ -70,6 +70,20 @@ public class JsonReader { * @return The cached or created object mapper. */ private static ObjectMapper getObjectMapper(ModelRegistry modelRegistry, boolean isAdmin) { + ObjectMapper mapper; + if (isAdmin) { + mapper = mappersAdmin.get(modelRegistry); + } else { + mapper = mappers.get(modelRegistry); + } + if (mapper == null) { + // computeIfAbsent is not thread-safe, and we don't want this method to be synchronised. + mapper = initObjectMapper(modelRegistry, isAdmin); + } + return mapper; + } + + private static synchronized ObjectMapper initObjectMapper(ModelRegistry modelRegistry, boolean isAdmin) { if (isAdmin) { return mappersAdmin.computeIfAbsent(modelRegistry, mr -> createObjectMapper(mr, isAdmin)); } else { @@ -84,9 +98,7 @@ private static ObjectMapper getObjectMapper(ModelRegistry modelRegistry, boolean * mapper for. * @return The created object mapper. */ - private static ObjectMapper createObjectMapper(ModelRegistry modelRegistry, boolean isAdmin) { - // ToDo: Allow extensions to add deserializers - + private static synchronized ObjectMapper createObjectMapper(ModelRegistry modelRegistry, boolean isAdmin) { GeoJsonDeserializier geoJsonDeserializier = new GeoJsonDeserializier(); for (String encodingType : GeoJsonDeserializier.ENCODINGS) { CustomDeserializationManager.getInstance().registerDeserializer(encodingType, geoJsonDeserializier);