From 013de36da7433e0a33666decce598b415656418b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20M=C3=A9riouma-Caron?= Date: Wed, 21 Oct 2015 17:54:18 -0400 Subject: [PATCH 1/2] Fix for #33 : GsonDateTypeAdapter isn't thread safe --- .../arcbees/vcs/util/GsonDateTypeAdapter.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/vcs-utils/src/main/java/com/arcbees/vcs/util/GsonDateTypeAdapter.java b/vcs-utils/src/main/java/com/arcbees/vcs/util/GsonDateTypeAdapter.java index 1d85eee..84f09e5 100644 --- a/vcs-utils/src/main/java/com/arcbees/vcs/util/GsonDateTypeAdapter.java +++ b/vcs-utils/src/main/java/com/arcbees/vcs/util/GsonDateTypeAdapter.java @@ -20,6 +20,7 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.Collections; import java.util.Date; import java.util.List; @@ -37,16 +38,19 @@ public class GsonDateTypeAdapter implements JsonDeserializer { "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX"); private static final DateFormat CUSTOM_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssXXX"); private static final List dateFormats = - Lists.newArrayList(DATE_TIME_LONG_FORMAT_ALT2, DATE_TIME_LONG_FORMAT, DATE_TIME_FORMAT, CUSTOM_DATE_FORMAT, - DATE_TIME_LONG_FORMAT_ALT); + Collections.synchronizedList(Lists.newArrayList( + DATE_TIME_LONG_FORMAT_ALT2, DATE_TIME_LONG_FORMAT, DATE_TIME_FORMAT, + CUSTOM_DATE_FORMAT, DATE_TIME_LONG_FORMAT_ALT)); @Override public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { String value = json.getAsJsonPrimitive().getAsString(); - for (DateFormat dateFormat : dateFormats) { - try { - return dateFormat.parse(value); - } catch (ParseException e) { + synchronized (dateFormats) { + for (DateFormat dateFormat : dateFormats) { + try { + return dateFormat.parse(value); + } catch (ParseException e) { + } } } From 7becf623de78adaf1bab04da3faf093552876655 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20M=C3=A9riouma-Caron?= Date: Wed, 21 Oct 2015 18:54:53 -0400 Subject: [PATCH 2/2] Make list Immutable instead of synchronized --- .../java/com/arcbees/vcs/util/GsonDateTypeAdapter.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/vcs-utils/src/main/java/com/arcbees/vcs/util/GsonDateTypeAdapter.java b/vcs-utils/src/main/java/com/arcbees/vcs/util/GsonDateTypeAdapter.java index 84f09e5..6a234d8 100644 --- a/vcs-utils/src/main/java/com/arcbees/vcs/util/GsonDateTypeAdapter.java +++ b/vcs-utils/src/main/java/com/arcbees/vcs/util/GsonDateTypeAdapter.java @@ -20,11 +20,10 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.Collections; import java.util.Date; import java.util.List; -import com.google.common.collect.Lists; +import com.google.common.collect.ImmutableList; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; @@ -38,9 +37,8 @@ public class GsonDateTypeAdapter implements JsonDeserializer { "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX"); private static final DateFormat CUSTOM_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssXXX"); private static final List dateFormats = - Collections.synchronizedList(Lists.newArrayList( - DATE_TIME_LONG_FORMAT_ALT2, DATE_TIME_LONG_FORMAT, DATE_TIME_FORMAT, - CUSTOM_DATE_FORMAT, DATE_TIME_LONG_FORMAT_ALT)); + ImmutableList.of(DATE_TIME_LONG_FORMAT_ALT2, DATE_TIME_LONG_FORMAT, DATE_TIME_FORMAT, + CUSTOM_DATE_FORMAT, DATE_TIME_LONG_FORMAT_ALT); @Override public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {