Skip to content

Commit 11ac90f

Browse files
committed
Fixed ConcurrentModificationException in initialisation
1 parent c39895f commit 11ac90f

1 file changed

Lines changed: 15 additions & 3 deletions

File tree

  • FROST-Server.Core/src/main/java/de/fraunhofer/iosb/ilt/frostserver/json/deserialize

FROST-Server.Core/src/main/java/de/fraunhofer/iosb/ilt/frostserver/json/deserialize/JsonReader.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ public class JsonReader {
7070
* @return The cached or created object mapper.
7171
*/
7272
private static ObjectMapper getObjectMapper(ModelRegistry modelRegistry, boolean isAdmin) {
73+
ObjectMapper mapper;
74+
if (isAdmin) {
75+
mapper = mappersAdmin.get(modelRegistry);
76+
} else {
77+
mapper = mappers.get(modelRegistry);
78+
}
79+
if (mapper == null) {
80+
// computeIfAbsent is not thread-safe, and we don't want this method to be synchronised.
81+
mapper = initObjectMapper(modelRegistry, isAdmin);
82+
}
83+
return mapper;
84+
}
85+
86+
private static synchronized ObjectMapper initObjectMapper(ModelRegistry modelRegistry, boolean isAdmin) {
7387
if (isAdmin) {
7488
return mappersAdmin.computeIfAbsent(modelRegistry, mr -> createObjectMapper(mr, isAdmin));
7589
} else {
@@ -84,9 +98,7 @@ private static ObjectMapper getObjectMapper(ModelRegistry modelRegistry, boolean
8498
* mapper for.
8599
* @return The created object mapper.
86100
*/
87-
private static ObjectMapper createObjectMapper(ModelRegistry modelRegistry, boolean isAdmin) {
88-
// ToDo: Allow extensions to add deserializers
89-
101+
private static synchronized ObjectMapper createObjectMapper(ModelRegistry modelRegistry, boolean isAdmin) {
90102
GeoJsonDeserializier geoJsonDeserializier = new GeoJsonDeserializier();
91103
for (String encodingType : GeoJsonDeserializier.ENCODINGS) {
92104
CustomDeserializationManager.getInstance().registerDeserializer(encodingType, geoJsonDeserializier);

0 commit comments

Comments
 (0)