diff --git a/dspace-api/src/main/java/org/dspace/app/util/MetadataExposure.java b/dspace-api/src/main/java/org/dspace/app/util/MetadataExposure.java index ed562bee66..1b7b4c6838 100644 --- a/dspace-api/src/main/java/org/dspace/app/util/MetadataExposure.java +++ b/dspace-api/src/main/java/org/dspace/app/util/MetadataExposure.java @@ -103,7 +103,11 @@ public class MetadataExposure return false; // for schema.element, just check schema->elementSet - init(); + if (!isInitialized()) + { + init(); + } + if (qualifier == null) { Set elts = hiddenElementSets.get(schema); @@ -121,10 +125,15 @@ public class MetadataExposure } } - // load maps from configuration unless it's already done. - private static void init() + private static boolean isInitialized() { - if (hiddenElementSets == null) + return hiddenElementSets != null; + } + + // load maps from configuration unless it's already done. + private static synchronized void init() + { + if (!isInitialized()) { hiddenElementSets = new HashMap>(); hiddenElementMaps = new HashMap>>(); diff --git a/dspace-api/src/main/java/org/dspace/content/MetadataField.java b/dspace-api/src/main/java/org/dspace/content/MetadataField.java index ba28deb7e1..2ca579ad78 100644 --- a/dspace-api/src/main/java/org/dspace/content/MetadataField.java +++ b/dspace-api/src/main/java/org/dspace/content/MetadataField.java @@ -620,7 +620,10 @@ public class MetadataField public static MetadataField find(Context context, int id) throws SQLException { - initCache(context); + if (!isCacheInitialized()) + { + initCache(context); + } // 'sanity check' first. Integer iid = new Integer(id); @@ -636,41 +639,40 @@ public class MetadataField id2field = null; } - // load caches if necessary - private static void initCache(Context context) throws SQLException + private static boolean isCacheInitialized() { - if (id2field != null) - return; - - synchronized (MetadataField.class) + return id2field != null; + } + + // load caches if necessary + private static synchronized void initCache(Context context) throws SQLException + { + if (!isCacheInitialized()) { - if (id2field == null) + HashMap new_id2field = new HashMap(); + log.info("Loading MetadataField elements into cache."); + + // Grab rows from DB + TableRowIterator tri = DatabaseManager.queryTable(context,"MetadataFieldRegistry", + "SELECT * from MetadataFieldRegistry"); + + try { - HashMap new_id2field = new HashMap(); - log.info("Loading MetadataField elements into cache."); - - // Grab rows from DB - TableRowIterator tri = DatabaseManager.queryTable(context,"MetadataFieldRegistry", - "SELECT * from MetadataFieldRegistry"); - - try + while (tri.hasNext()) { - while (tri.hasNext()) - { - TableRow row = tri.next(); - int fieldID = row.getIntColumn("metadata_field_id"); - new_id2field.put(new Integer(fieldID), new MetadataField(row)); - } + TableRow row = tri.next(); + int fieldID = row.getIntColumn("metadata_field_id"); + new_id2field.put(new Integer(fieldID), new MetadataField(row)); } - finally - { - // close the TableRowIterator to free up resources - if (tri != null) - tri.close(); - } - - id2field = new_id2field; } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } + + id2field = new_id2field; } } diff --git a/dspace-api/src/main/java/org/dspace/content/MetadataSchema.java b/dspace-api/src/main/java/org/dspace/content/MetadataSchema.java index 9c50f0d034..df4325cc9a 100644 --- a/dspace-api/src/main/java/org/dspace/content/MetadataSchema.java +++ b/dspace-api/src/main/java/org/dspace/content/MetadataSchema.java @@ -536,7 +536,11 @@ public class MetadataSchema public static MetadataSchema find(Context context, int id) throws SQLException { - initCache(context); + if (!isCacheInitialized()) + { + initCache(context); + } + Integer iid = new Integer(id); // sanity check @@ -563,7 +567,10 @@ public class MetadataSchema if (shortName == null) return null; - initCache(context); + if (!isCacheInitialized()) + { + initCache(context); + } if (!name2schema.containsKey(shortName)) return null; @@ -578,44 +585,43 @@ public class MetadataSchema name2schema = null; } - // load caches if necessary - private static void initCache(Context context) throws SQLException + private static boolean isCacheInitialized() { - if (id2schema != null && name2schema != null) - return; + return (id2schema != null && name2schema != null); + } - synchronized (MetadataSchema.class) + // load caches if necessary + private static synchronized void initCache(Context context) throws SQLException + { + if (!isCacheInitialized()) { - if (id2schema == null && name2schema == null) + log.info("Loading schema cache for fast finds"); + HashMap new_id2schema = new HashMap(); + HashMap new_name2schema = new HashMap(); + + TableRowIterator tri = DatabaseManager.queryTable(context,"MetadataSchemaRegistry", + "SELECT * from MetadataSchemaRegistry"); + + try { - log.info("Loading schema cache for fast finds"); - HashMap new_id2schema = new HashMap(); - HashMap new_name2schema = new HashMap(); - - TableRowIterator tri = DatabaseManager.queryTable(context,"MetadataSchemaRegistry", - "SELECT * from MetadataSchemaRegistry"); - - try + while (tri.hasNext()) { - while (tri.hasNext()) - { - TableRow row = tri.next(); + TableRow row = tri.next(); - MetadataSchema s = new MetadataSchema(row); - new_id2schema.put(new Integer(s.schemaID), s); - new_name2schema.put(s.name, s); - } + MetadataSchema s = new MetadataSchema(row); + new_id2schema.put(new Integer(s.schemaID), s); + new_name2schema.put(s.name, s); } - finally - { - // close the TableRowIterator to free up resources - if (tri != null) - tri.close(); - } - - id2schema = new_id2schema; - name2schema = new_name2schema; } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } + + id2schema = new_id2schema; + name2schema = new_name2schema; } } } diff --git a/dspace-api/src/main/java/org/dspace/content/authority/DCInputAuthority.java b/dspace-api/src/main/java/org/dspace/content/authority/DCInputAuthority.java index dadae5b829..8e6d23d71a 100644 --- a/dspace-api/src/main/java/org/dspace/content/authority/DCInputAuthority.java +++ b/dspace-api/src/main/java/org/dspace/content/authority/DCInputAuthority.java @@ -98,7 +98,10 @@ public class DCInputAuthority extends SelfNamedPlugin implements ChoiceAuthority List names = new ArrayList(); Iterator pi = dci.getPairsNameIterator(); while (pi.hasNext()) + { names.add((String)pi.next()); + } + pluginNames = names.toArray(new String[names.size()]); log.debug("Got plugin names = "+Arrays.deepToString(pluginNames)); } diff --git a/dspace-api/src/main/java/org/dspace/search/DSQuery.java b/dspace-api/src/main/java/org/dspace/search/DSQuery.java index d7d1f39810..ab8e22b183 100644 --- a/dspace-api/src/main/java/org/dspace/search/DSQuery.java +++ b/dspace-api/src/main/java/org/dspace/search/DSQuery.java @@ -408,7 +408,7 @@ public class DSQuery /** * Close any IndexSearcher that is currently open. */ - public static void close() + public static synchronized void close() { if (searcher != null) { diff --git a/dspace-api/src/main/java/org/dspace/sort/SortOption.java b/dspace-api/src/main/java/org/dspace/sort/SortOption.java index b26f2ca8ad..f4b3602cb4 100644 --- a/dspace-api/src/main/java/org/dspace/sort/SortOption.java +++ b/dspace-api/src/main/java/org/dspace/sort/SortOption.java @@ -44,6 +44,7 @@ import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.log4j.Logger; import org.dspace.core.ConfigurationManager; /** @@ -54,6 +55,8 @@ import org.dspace.core.ConfigurationManager; */ public class SortOption { + private static final Logger log = Logger.getLogger(SortOption.class); + public static final String ASCENDING = "ASC"; public static final String DESCENDING = "DESC"; @@ -77,8 +80,27 @@ public class SortOption /** the sort options available for this index */ private static Set sortOptionsSet = null; - private static Map sortOptionsMap = null; - + static { + try + { + Set newSortOptionsSet = new HashSet(); + int idx = 1; + String option; + + while ( ((option = ConfigurationManager.getProperty("webui.itemlist.sort-option." + idx))) != null) + { + SortOption so = new SortOption(idx, option); + newSortOptionsSet.add(so); + idx++; + } + + SortOption.sortOptionsSet = newSortOptionsSet; + } + catch (SortException se) + { + log.fatal("Unable to load SortOptions", se); + } + } /** * Construct a new SortOption object with the given parameters * @@ -297,31 +319,6 @@ public class SortOption return false; } - /** - * @return a map of the configured sort options - */ - public static Map getSortOptionsMap() throws SortException - { - if (SortOption.sortOptionsMap != null) - return SortOption.sortOptionsMap; - - synchronized (SortOption.class) - { - if (SortOption.sortOptionsMap == null) - { - Map newSortOptionsMap = new HashMap(); - for (SortOption so : SortOption.getSortOptions()) - { - newSortOptionsMap.put(new Integer(so.getNumber()), so); - } - - SortOption.sortOptionsMap = newSortOptionsMap; - } - } - - return SortOption.sortOptionsMap; - } - /** * Return all the configured sort options * @return @@ -329,26 +326,9 @@ public class SortOption */ public static Set getSortOptions() throws SortException { - if (SortOption.sortOptionsSet != null) - return SortOption.sortOptionsSet; - - synchronized (SortOption.class) + if (SortOption.sortOptionsSet == null) { - if (SortOption.sortOptionsSet == null) - { - Set newSortOptionsSet = new HashSet(); - int idx = 1; - String option; - - while ( ((option = ConfigurationManager.getProperty("webui.itemlist.sort-option." + idx))) != null) - { - SortOption so = new SortOption(idx, option); - newSortOptionsSet.add(so); - idx++; - } - - SortOption.sortOptionsSet = newSortOptionsSet; - } + throw new SortException("Sort options not loaded"); } return SortOption.sortOptionsSet;