diff --git a/dspace-api/src/main/java/org/dspace/content/AuthorityVirtualMetadataServiceImpl.java b/dspace-api/src/main/java/org/dspace/content/AuthorityVirtualMetadataServiceImpl.java index f129bfe64b9..4c9b5089200 100644 --- a/dspace-api/src/main/java/org/dspace/content/AuthorityVirtualMetadataServiceImpl.java +++ b/dspace-api/src/main/java/org/dspace/content/AuthorityVirtualMetadataServiceImpl.java @@ -47,20 +47,29 @@ public class AuthorityVirtualMetadataServiceImpl implements AuthorityVirtualMeta @Autowired protected MetadataAuthorityService metadataAuthorityService; + /** + * Map of virtual field names that are configured for authority control and exist in metadata registry + */ private HashMap validVirtualFieldNames; + /** + * Map of authority virtual metadata configurations, see virtual-metadata.xml + */ private Map> authorityVirtualMaps; private static final Logger log = LogManager.getLogger(); - public void init() { - validVirtualFieldNames = new HashMap<>(); - authorityVirtualMaps = new HashMap<>(); - Context context = null; - // Obtain new context and initialise lists and maps - try { - context = new Context(Context.Mode.READ_ONLY); + /** + * Initialize hashmaps for field / configuration lookups. This is only executed the first time a lookup + * is attempted, to keep database load down for data retrieval that typically will not change between + * service restarts. + */ + public void initMaps() throws SQLException { + if (validVirtualFieldNames == null && authorityVirtualMaps == null) { + validVirtualFieldNames = new HashMap<>(); + authorityVirtualMaps = new HashMap<>(); + Context context = new Context(Context.Mode.READ_ONLY); // Get field maps configured in virtual metadata spring configuration - authorityVirtualMaps = authorityVirtualMetadataPopulator.getMap(); + authorityVirtualMaps = authorityVirtualMetadataPopulator.getMap(); // Iterate map of maps, just to check each virtual field name (key of 2nd-level map) exists // and populate the virtual field names list so we can look it up later without further database calls for (String configuredAuthorityField : authorityVirtualMaps.keySet()) { @@ -71,13 +80,6 @@ public void init() { } } } - } catch (SQLException e) { - log.error("Could not obtain context" + e.getMessage(), e); - throw new RuntimeException(e); - } finally { - if (context != null) { - context.close(); - } } } @@ -93,6 +95,16 @@ public void init() { */ public List getAuthorityVirtualMetadata(Item item, List dbMetadataValues) { List authorityMetadataValues = new LinkedList<>(); + // If maps are not initialized, do it now + if (authorityVirtualMaps == null || validVirtualFieldNames == null) { + try { + initMaps(); + } catch (SQLException e) { + log.error("Error initializing authority virtual metadata configuration", e); + return authorityMetadataValues; + } + } + // Make a map of counts - we want to calc a real 'place' Map fieldCounts = new HashMap<>(); for (MetadataValue mv : dbMetadataValues) { diff --git a/dspace/config/spring/api/core-services.xml b/dspace/config/spring/api/core-services.xml index 6a748da08fb..c56fc0771ed 100644 --- a/dspace/config/spring/api/core-services.xml +++ b/dspace/config/spring/api/core-services.xml @@ -57,7 +57,7 @@ - +