package org.opencds.cqf.cql.engine.serializing;

import java.util.Iterator;
import java.util.ServiceLoader;

public class CqlLibraryReaderFactory {

    static ServiceLoader<CqlLibraryReaderProvider> loader = ServiceLoader
            .load(CqlLibraryReaderProvider.class);

    public static Iterator<CqlLibraryReaderProvider> providers(boolean refresh) {
        if (refresh) {
            loader.reload();
        }
        return loader.iterator();
    }

    public static CqlLibraryReader getReader(String contentType) {
        if (providers(false).hasNext()) {
            return providers(false).next().create(contentType);
        }
        throw new RuntimeException("No ElmLibraryReaderProviders found for " + contentType);
    }
}

@@ -286,16 +286,29 @@
  static 
+synchronized 
 Iterator
@@ -355,32 +355,32 @@
 lean refresh) %7B%0A
-
         if (refr
@@ -417,32 +417,33 @@
 ad();%0A        %7D%0A
+%0A
         return l
@@ -538,24 +538,66 @@
 ) %7B%0A        
+var providers = providers(false);%0A        
 if (provider
@@ -597,23 +597,16 @@
 roviders
-(false)
 .hasNext
@@ -639,23 +639,16 @@
 roviders
-(false)
 .next().
@@ -678,16 +678,17 @@
       %7D%0A
+%0A
         

Synchronize access to ServiceLoaders to prevent race conditions