From 21f8d180e4e5697b615988648f53a520f8b02127 Mon Sep 17 00:00:00 2001 From: Jonathan Percival Date: Fri, 17 Sep 2021 18:48:57 -0600 Subject: [PATCH] Cache cds-service definitions --- .../cqf/dstu3/servlet/CdsHooksServlet.java | 12 ++++++++- .../opencds/cqf/r4/helpers/LibraryHelper.java | 27 +------------------ .../cqf/r4/servlet/CdsHooksServlet.java | 12 ++++++++- 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/dstu3/src/main/java/org/opencds/cqf/dstu3/servlet/CdsHooksServlet.java b/dstu3/src/main/java/org/opencds/cqf/dstu3/servlet/CdsHooksServlet.java index 628cd471a..37fe35f73 100644 --- a/dstu3/src/main/java/org/opencds/cqf/dstu3/servlet/CdsHooksServlet.java +++ b/dstu3/src/main/java/org/opencds/cqf/dstu3/servlet/CdsHooksServlet.java @@ -78,6 +78,8 @@ public class CdsHooksServlet extends HttpServlet { private LibraryHelper libraryHelper; + private JsonArray services; + @SuppressWarnings("unchecked") @Override public void init() { @@ -277,8 +279,16 @@ private void printStackTrack(Exception e, HttpServletResponse response) throws I response.getWriter().println(exceptionAsString); } + private synchronized JsonArray getServicesArray() { + if (this.services == null) { + this.services = getServices().get("services").getAsJsonArray(); + } + + return this.services; + } + private JsonObject getService(String service) { - JsonArray services = getServices().get("services").getAsJsonArray(); + JsonArray services = getServicesArray(); List ids = new ArrayList<>(); for (JsonElement element : services) { if (element.isJsonObject() && element.getAsJsonObject().has("id")) { diff --git a/r4/src/main/java/org/opencds/cqf/r4/helpers/LibraryHelper.java b/r4/src/main/java/org/opencds/cqf/r4/helpers/LibraryHelper.java index 971142be6..5dcdd0217 100644 --- a/r4/src/main/java/org/opencds/cqf/r4/helpers/LibraryHelper.java +++ b/r4/src/main/java/org/opencds/cqf/r4/helpers/LibraryHelper.java @@ -72,31 +72,6 @@ public org.opencds.cqf.cql.engine.execution.LibraryLoader createLibraryLoader( TranslatingLibraryLoader translatingLibraryLoader = new TranslatingLibraryLoader(modelManager, contentProviders, translatorOptions); - return new CacheAwareLibraryLoaderDecorator(translatingLibraryLoader, libraryCache) { - @Override - protected Boolean translatorOptionsMatch(Library library) { - return true; - } - }; - } - - @Override - public org.opencds.cqf.cql.engine.execution.LibraryLoader createLibraryLoader( - org.cqframework.cql.cql2elm.LibrarySourceProvider provider) { - ModelManager modelManager = new CacheAwareModelManager(this.modelCache); - LibraryManager libraryManager = new LibraryManager(modelManager); - libraryManager.getLibrarySourceLoader().clearProviders(); - - libraryManager.getLibrarySourceLoader().registerProvider(provider); - - TranslatingLibraryLoader translatingLibraryLoader = new TranslatingLibraryLoader(modelManager, null, - translatorOptions); - - return new CacheAwareLibraryLoaderDecorator(translatingLibraryLoader, libraryCache) { - @Override - protected Boolean translatorOptionsMatch(Library library) { - return true; - } - }; + return new CacheAwareLibraryLoaderDecorator(translatingLibraryLoader, libraryCache); } } diff --git a/r4/src/main/java/org/opencds/cqf/r4/servlet/CdsHooksServlet.java b/r4/src/main/java/org/opencds/cqf/r4/servlet/CdsHooksServlet.java index 8f0bd4143..31258fd49 100644 --- a/r4/src/main/java/org/opencds/cqf/r4/servlet/CdsHooksServlet.java +++ b/r4/src/main/java/org/opencds/cqf/r4/servlet/CdsHooksServlet.java @@ -78,6 +78,8 @@ public class CdsHooksServlet extends HttpServlet { private LibraryHelper libraryHelper; + private JsonArray services; + @SuppressWarnings("unchecked") @Override public void init() { @@ -284,7 +286,7 @@ private void printStackTrack(Exception e, HttpServletResponse response) throws I } private JsonObject getService(String service) { - JsonArray services = getServices().get("services").getAsJsonArray(); + JsonArray services = getServicesArray(); List ids = new ArrayList<>(); for (JsonElement element : services) { if (element.isJsonObject() && element.getAsJsonObject().has("id")) { @@ -298,6 +300,14 @@ private JsonObject getService(String service) { "Cannot resolve service: " + service + "\nAvailable services: " + ids.toString()); } + private synchronized JsonArray getServicesArray() { + if (this.services == null) { + this.services = getServices().get("services").getAsJsonArray(); + } + + return this.services; + } + private JsonObject getServices() { DiscoveryResolutionR4 discoveryResolutionR4 = new DiscoveryResolutionR4( FhirContext.forCached(FhirVersionEnum.R4).newRestfulGenericClient(HapiProperties.getServerAddress()));