[DS-707] Fix multithreading issues

git-svn-id: http://scm.dspace.org/svn/repo/dspace/trunk@5481 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Graham Triggs
2010-10-20 14:05:34 +00:00
parent b19e893557
commit b67fa3fbed
6 changed files with 113 additions and 113 deletions

View File

@@ -103,7 +103,11 @@ public class MetadataExposure
return false; return false;
// for schema.element, just check schema->elementSet // for schema.element, just check schema->elementSet
init(); if (!isInitialized())
{
init();
}
if (qualifier == null) if (qualifier == null)
{ {
Set<String> elts = hiddenElementSets.get(schema); Set<String> elts = hiddenElementSets.get(schema);
@@ -121,10 +125,15 @@ public class MetadataExposure
} }
} }
// load maps from configuration unless it's already done. private static boolean isInitialized()
private static void init()
{ {
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<String,Set<String>>(); hiddenElementSets = new HashMap<String,Set<String>>();
hiddenElementMaps = new HashMap<String,Map<String,Set<String>>>(); hiddenElementMaps = new HashMap<String,Map<String,Set<String>>>();

View File

@@ -620,7 +620,10 @@ public class MetadataField
public static MetadataField find(Context context, int id) public static MetadataField find(Context context, int id)
throws SQLException throws SQLException
{ {
initCache(context); if (!isCacheInitialized())
{
initCache(context);
}
// 'sanity check' first. // 'sanity check' first.
Integer iid = new Integer(id); Integer iid = new Integer(id);
@@ -636,41 +639,40 @@ public class MetadataField
id2field = null; id2field = null;
} }
// load caches if necessary private static boolean isCacheInitialized()
private static void initCache(Context context) throws SQLException
{ {
if (id2field != null) return id2field != null;
return; }
synchronized (MetadataField.class) // 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(); while (tri.hasNext())
log.info("Loading MetadataField elements into cache.");
// Grab rows from DB
TableRowIterator tri = DatabaseManager.queryTable(context,"MetadataFieldRegistry",
"SELECT * from MetadataFieldRegistry");
try
{ {
while (tri.hasNext()) TableRow row = tri.next();
{ int fieldID = row.getIntColumn("metadata_field_id");
TableRow row = tri.next(); new_id2field.put(new Integer(fieldID), new MetadataField(row));
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;
} }
} }

View File

@@ -536,7 +536,11 @@ public class MetadataSchema
public static MetadataSchema find(Context context, int id) public static MetadataSchema find(Context context, int id)
throws SQLException throws SQLException
{ {
initCache(context); if (!isCacheInitialized())
{
initCache(context);
}
Integer iid = new Integer(id); Integer iid = new Integer(id);
// sanity check // sanity check
@@ -563,7 +567,10 @@ public class MetadataSchema
if (shortName == null) if (shortName == null)
return null; return null;
initCache(context); if (!isCacheInitialized())
{
initCache(context);
}
if (!name2schema.containsKey(shortName)) if (!name2schema.containsKey(shortName))
return null; return null;
@@ -578,44 +585,43 @@ public class MetadataSchema
name2schema = null; name2schema = null;
} }
// load caches if necessary private static boolean isCacheInitialized()
private static void initCache(Context context) throws SQLException
{ {
if (id2schema != null && name2schema != null) return (id2schema != null && name2schema != null);
return; }
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"); while (tri.hasNext())
HashMap new_id2schema = new HashMap();
HashMap new_name2schema = new HashMap();
TableRowIterator tri = DatabaseManager.queryTable(context,"MetadataSchemaRegistry",
"SELECT * from MetadataSchemaRegistry");
try
{ {
while (tri.hasNext()) TableRow row = tri.next();
{
TableRow row = tri.next();
MetadataSchema s = new MetadataSchema(row); MetadataSchema s = new MetadataSchema(row);
new_id2schema.put(new Integer(s.schemaID), s); new_id2schema.put(new Integer(s.schemaID), s);
new_name2schema.put(s.name, 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;
} }
} }
} }

View File

@@ -98,7 +98,10 @@ public class DCInputAuthority extends SelfNamedPlugin implements ChoiceAuthority
List<String> names = new ArrayList<String>(); List<String> names = new ArrayList<String>();
Iterator pi = dci.getPairsNameIterator(); Iterator pi = dci.getPairsNameIterator();
while (pi.hasNext()) while (pi.hasNext())
{
names.add((String)pi.next()); names.add((String)pi.next());
}
pluginNames = names.toArray(new String[names.size()]); pluginNames = names.toArray(new String[names.size()]);
log.debug("Got plugin names = "+Arrays.deepToString(pluginNames)); log.debug("Got plugin names = "+Arrays.deepToString(pluginNames));
} }

View File

@@ -408,7 +408,7 @@ public class DSQuery
/** /**
* Close any IndexSearcher that is currently open. * Close any IndexSearcher that is currently open.
*/ */
public static void close() public static synchronized void close()
{ {
if (searcher != null) if (searcher != null)
{ {

View File

@@ -44,6 +44,7 @@ import java.util.StringTokenizer;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import org.dspace.core.ConfigurationManager; import org.dspace.core.ConfigurationManager;
/** /**
@@ -54,6 +55,8 @@ import org.dspace.core.ConfigurationManager;
*/ */
public class SortOption public class SortOption
{ {
private static final Logger log = Logger.getLogger(SortOption.class);
public static final String ASCENDING = "ASC"; public static final String ASCENDING = "ASC";
public static final String DESCENDING = "DESC"; public static final String DESCENDING = "DESC";
@@ -77,8 +80,27 @@ public class SortOption
/** the sort options available for this index */ /** the sort options available for this index */
private static Set<SortOption> sortOptionsSet = null; private static Set<SortOption> sortOptionsSet = null;
private static Map<Integer, SortOption> sortOptionsMap = null; static {
try
{
Set<SortOption> newSortOptionsSet = new HashSet<SortOption>();
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 * Construct a new SortOption object with the given parameters
* *
@@ -297,31 +319,6 @@ public class SortOption
return false; return false;
} }
/**
* @return a map of the configured sort options
*/
public static Map<Integer, SortOption> getSortOptionsMap() throws SortException
{
if (SortOption.sortOptionsMap != null)
return SortOption.sortOptionsMap;
synchronized (SortOption.class)
{
if (SortOption.sortOptionsMap == null)
{
Map<Integer, SortOption> newSortOptionsMap = new HashMap<Integer, SortOption>();
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 all the configured sort options
* @return * @return
@@ -329,26 +326,9 @@ public class SortOption
*/ */
public static Set<SortOption> getSortOptions() throws SortException public static Set<SortOption> getSortOptions() throws SortException
{ {
if (SortOption.sortOptionsSet != null) if (SortOption.sortOptionsSet == null)
return SortOption.sortOptionsSet;
synchronized (SortOption.class)
{ {
if (SortOption.sortOptionsSet == null) throw new SortException("Sort options not loaded");
{
Set<SortOption> newSortOptionsSet = new HashSet<SortOption>();
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;
}
} }
return SortOption.sortOptionsSet; return SortOption.sortOptionsSet;