[ASTERIXDB-3405][EXT] Avoid ConcurrentModificationException in DatasourceFactoryProvider

- user model changes: no
- storage format changes: no
- interface changes: no

Details:

Change-Id: Ic9dd2358ad093d3d59f838e85bc746d795638bb0
Reviewed-on: https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/18310
Integration-Tests: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
Reviewed-by: Ali Alsuliman <ali.al.solaiman@gmail.com>
Reviewed-by: Murtadha Hubail <mhubail@apache.org>
diff --git a/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/DatasourceFactoryProvider.java b/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/DatasourceFactoryProvider.java
index b0aded6..f35dce4 100644
--- a/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/DatasourceFactoryProvider.java
+++ b/asterixdb/asterix-external-data/src/main/java/org/apache/asterix/external/provider/DatasourceFactoryProvider.java
@@ -116,9 +116,7 @@
             throw new NotImplementedException();
         }
 
-        if (factories == null) {
-            initFactories();
-        }
+        initFactories();
 
         if (factories.containsKey(adaptorName)) {
             Map<String, Class<?>> formatClassMap = factories.get(adaptorName);
@@ -134,7 +132,10 @@
         }
     }
 
-    protected static void initFactories() throws AsterixException {
+    protected static synchronized void initFactories() throws AsterixException {
+        if (factories != null) {
+            return;
+        }
         factories = new HashMap<>();
         ClassLoader cl = ParserFactoryProvider.class.getClassLoader();
         final Charset encoding = StandardCharsets.UTF_8;