diff --git a/dspace-api/src/main/java/org/dspace/app/mets/METSExport.java b/dspace-api/src/main/java/org/dspace/app/mets/METSExport.java index bbdd968715..ed1dc08fc2 100644 --- a/dspace-api/src/main/java/org/dspace/app/mets/METSExport.java +++ b/dspace-api/src/main/java/org/dspace/app/mets/METSExport.java @@ -242,9 +242,18 @@ public class METSExport + File.separator + "config" + File.separator + "dc2mods.cfg"; // Read it in - InputStream is = new FileInputStream(configFile); - dcToMODS = new Properties(); - dcToMODS.load(is); + InputStream is = null; + try + { + is = new FileInputStream(configFile); + dcToMODS = new Properties(); + dcToMODS.load(is); + } + finally + { + if (is != null) + try { is.close(); } catch (IOException ioe) { } + } } /** diff --git a/dspace-api/src/main/java/org/dspace/app/statistics/ReportGenerator.java b/dspace-api/src/main/java/org/dspace/app/statistics/ReportGenerator.java index 06b58b313c..c80fc87841 100644 --- a/dspace-api/src/main/java/org/dspace/app/statistics/ReportGenerator.java +++ b/dspace-api/src/main/java/org/dspace/app/statistics/ReportGenerator.java @@ -562,30 +562,41 @@ public class ReportGenerator { FileReader fr = null; BufferedReader br = null; - - // read in the map file, printing a warning if none is found - String record = null; - try - { - fr = new FileReader(map); - br = new BufferedReader(fr); - } - catch (IOException e) - { - System.err.println("Failed to read map file: log file actions will be displayed without translation"); - return; - } - - // loop through the map file and read in the values - while ((record = br.readLine()) != null) + + try { - Matcher matchReal = real.matcher(record); - - // if the line is real then read it in - if (matchReal.matches()) + // read in the map file, printing a warning if none is found + String record = null; + try { - actionMap.put(matchReal.group(1).trim(), matchReal.group(2).trim()); + fr = new FileReader(map); + br = new BufferedReader(fr); } + catch (IOException e) + { + System.err.println("Failed to read map file: log file actions will be displayed without translation"); + return; + } + + // loop through the map file and read in the values + while ((record = br.readLine()) != null) + { + Matcher matchReal = real.matcher(record); + + // if the line is real then read it in + if (matchReal.matches()) + { + actionMap.put(matchReal.group(1).trim(), matchReal.group(2).trim()); + } + } + } + finally + { + if (br != null) + try { br.close(); } catch (IOException ioe) { } + + if (fr != null) + try { fr.close(); } catch (IOException ioe) { } } } diff --git a/dspace-api/src/main/java/org/dspace/authenticate/X509Authentication.java b/dspace-api/src/main/java/org/dspace/authenticate/X509Authentication.java index 2d69474058..541c629121 100755 --- a/dspace-api/src/main/java/org/dspace/authenticate/X509Authentication.java +++ b/dspace-api/src/main/java/org/dspace/authenticate/X509Authentication.java @@ -140,12 +140,13 @@ public class X509Authentication // First look for keystore full of trusted certs. if (keystorePath != null) { + FileInputStream fis = null; if (keystorePassword == null) keystorePassword = ""; try { KeyStore ks = KeyStore.getInstance("JKS"); - ks.load(new FileInputStream(keystorePath), - keystorePassword.toCharArray()); + fis = new FileInputStream(keystorePath); + ks.load(fis, keystorePassword.toCharArray()); caCertKeyStore = ks; } catch (IOException e) @@ -158,14 +159,22 @@ public class X509Authentication log.error("X509Authentication: Failed to extract CA keystore, file="+ keystorePath+", error="+e.toString()); } + finally + { + if (fis != null) + try { fis.close(); } catch (IOException ioe) { } + } } // Second, try getting public key out of CA cert, if that's configured. if (caCertPath != null) { + InputStream is = null; + FileInputStream fis = null; try { - InputStream is = new BufferedInputStream(new FileInputStream(caCertPath)); + fis = new FileInputStream(caCertPath); + is = new BufferedInputStream(fis); X509Certificate cert = (X509Certificate) CertificateFactory .getInstance("X.509").generateCertificate(is); if (cert != null) @@ -181,6 +190,14 @@ public class X509Authentication log.error("X509Authentication: Failed to extract CA cert, file="+ caCertPath+", error="+e.toString()); } + finally + { + if (is != null) + try { is.close(); } catch (IOException ioe) { } + + if (fis != null) + try { fis.close(); } catch (IOException ioe) { } + } } } diff --git a/dspace-api/src/main/java/org/dspace/authorize/AuthorizeManager.java b/dspace-api/src/main/java/org/dspace/authorize/AuthorizeManager.java index f5a4435f5b..c83f9d57c0 100644 --- a/dspace-api/src/main/java/org/dspace/authorize/AuthorizeManager.java +++ b/dspace-api/src/main/java/org/dspace/authorize/AuthorizeManager.java @@ -424,24 +424,31 @@ public class AuthorizeManager List policies = new ArrayList(); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // first check the cache (FIXME: is this right?) - ResourcePolicy cachepolicy = (ResourcePolicy) c.fromCache( - ResourcePolicy.class, row.getIntColumn("policy_id")); - - if (cachepolicy != null) + while (tri.hasNext()) { - policies.add(cachepolicy); - } - else - { - policies.add(new ResourcePolicy(c, row)); + TableRow row = tri.next(); + + // first check the cache (FIXME: is this right?) + ResourcePolicy cachepolicy = (ResourcePolicy) c.fromCache( + ResourcePolicy.class, row.getIntColumn("policy_id")); + + if (cachepolicy != null) + { + policies.add(cachepolicy); + } + else + { + policies.add(new ResourcePolicy(c, row)); + } } } - tri.close(); + finally + { + if (tri != null) + tri.close(); + } return policies; } @@ -463,24 +470,31 @@ public class AuthorizeManager List policies = new ArrayList(); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // first check the cache (FIXME: is this right?) - ResourcePolicy cachepolicy = (ResourcePolicy) c.fromCache( - ResourcePolicy.class, row.getIntColumn("policy_id")); - - if (cachepolicy != null) + while (tri.hasNext()) { - policies.add(cachepolicy); - } - else - { - policies.add(new ResourcePolicy(c, row)); + TableRow row = tri.next(); + + // first check the cache (FIXME: is this right?) + ResourcePolicy cachepolicy = (ResourcePolicy) c.fromCache( + ResourcePolicy.class, row.getIntColumn("policy_id")); + + if (cachepolicy != null) + { + policies.add(cachepolicy); + } + else + { + policies.add(new ResourcePolicy(c, row)); + } } } - tri.close(); + finally + { + if (tri != null) + tri.close(); + } return policies; } @@ -507,24 +521,31 @@ public class AuthorizeManager List policies = new ArrayList(); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // first check the cache (FIXME: is this right?) - ResourcePolicy cachepolicy = (ResourcePolicy) c.fromCache( - ResourcePolicy.class, row.getIntColumn("policy_id")); - - if (cachepolicy != null) + while (tri.hasNext()) { - policies.add(cachepolicy); - } - else - { - policies.add(new ResourcePolicy(c, row)); + TableRow row = tri.next(); + + // first check the cache (FIXME: is this right?) + ResourcePolicy cachepolicy = (ResourcePolicy) c.fromCache( + ResourcePolicy.class, row.getIntColumn("policy_id")); + + if (cachepolicy != null) + { + policies.add(cachepolicy); + } + else + { + policies.add(new ResourcePolicy(c, row)); + } } } - tri.close(); + finally + { + if (tri != null) + tri.close(); + } return policies; } @@ -697,37 +718,44 @@ public class AuthorizeManager TableRowIterator tri = DatabaseManager.queryTable(c, "resourcepolicy", "SELECT * FROM resourcepolicy WHERE resource_type_id= ? "+ "AND resource_id= ? AND action_id= ? ",o.getType(),o.getID(),actionID); - + List groups = new ArrayList(); - - while (tri.hasNext()) + try { - TableRow row = tri.next(); - // first check the cache (FIXME: is this right?) - ResourcePolicy cachepolicy = (ResourcePolicy) c.fromCache( - ResourcePolicy.class, row.getIntColumn("policy_id")); - - ResourcePolicy myPolicy = null; - - if (cachepolicy != null) + while (tri.hasNext()) { - myPolicy = cachepolicy; - } - else - { - myPolicy = new ResourcePolicy(c, row); - } + TableRow row = tri.next(); - // now do we have a group? - Group myGroup = myPolicy.getGroup(); + // first check the cache (FIXME: is this right?) + ResourcePolicy cachepolicy = (ResourcePolicy) c.fromCache( + ResourcePolicy.class, row.getIntColumn("policy_id")); - if (myGroup != null) - { - groups.add(myGroup); + ResourcePolicy myPolicy = null; + + if (cachepolicy != null) + { + myPolicy = cachepolicy; + } + else + { + myPolicy = new ResourcePolicy(c, row); + } + + // now do we have a group? + Group myGroup = myPolicy.getGroup(); + + if (myGroup != null) + { + groups.add(myGroup); + } } } - tri.close(); + finally + { + if (tri != null) + tri.close(); + } Group[] groupArray = new Group[groups.size()]; groupArray = groups.toArray(groupArray); diff --git a/dspace-api/src/main/java/org/dspace/browse/ItemCountDAOOracle.java b/dspace-api/src/main/java/org/dspace/browse/ItemCountDAOOracle.java index 2a791a1077..8c7eab7c9f 100644 --- a/dspace-api/src/main/java/org/dspace/browse/ItemCountDAOOracle.java +++ b/dspace-api/src/main/java/org/dspace/browse/ItemCountDAOOracle.java @@ -96,11 +96,12 @@ public class ItemCountDAOOracle implements ItemCountDAO public void collectionCount(Collection collection, int count) throws ItemCountException { - try + TableRowIterator tri = null; + try { // first find out if we have a record Object[] sparams = { new Integer(collection.getID()) }; - TableRowIterator tri = DatabaseManager.query(context, collectionSelect, sparams); + tri = DatabaseManager.query(context, collectionSelect, sparams); if (tri.hasNext()) { @@ -112,15 +113,18 @@ public class ItemCountDAOOracle implements ItemCountDAO Object[] params = { new Integer(collection.getID()), new Integer(count) }; DatabaseManager.updateQuery(context, collectionInsert, params); } - - tri.close(); } catch (SQLException e) { log.error("caught exception: ", e); throw new ItemCountException(e); } - } + finally + { + if (tri != null) + tri.close(); + } + } /** * Store the count of the given community @@ -132,11 +136,12 @@ public class ItemCountDAOOracle implements ItemCountDAO public void communityCount(Community community, int count) throws ItemCountException { - try + TableRowIterator tri = null; + try { // first find out if we have a record Object[] sparams = { new Integer(community.getID()) }; - TableRowIterator tri = DatabaseManager.query(context, communitySelect, sparams); + tri = DatabaseManager.query(context, communitySelect, sparams); if (tri.hasNext()) { @@ -148,15 +153,18 @@ public class ItemCountDAOOracle implements ItemCountDAO Object[] params = { new Integer(community.getID()), new Integer(count) }; DatabaseManager.updateQuery(context, communityInsert, params); } - - tri.close(); } catch (SQLException e) { log.error("caught exception: ", e); throw new ItemCountException(e); } - } + finally + { + if (tri != null) + tri.close(); + } + } /** * Set the dspace context to use @@ -268,10 +276,11 @@ public class ItemCountDAOOracle implements ItemCountDAO private int getCollectionCount(Collection collection) throws ItemCountException { - try + TableRowIterator tri = null; + try { Object[] params = { new Integer(collection.getID()) }; - TableRowIterator tri = DatabaseManager.query(context, collectionSelect, params); + tri = DatabaseManager.query(context, collectionSelect, params); if (!tri.hasNext()) { @@ -284,9 +293,7 @@ public class ItemCountDAOOracle implements ItemCountDAO { throw new ItemCountException("More than one count row in the database"); } - - tri.close(); - + return tr.getIntColumn("count"); } catch (SQLException e) @@ -294,7 +301,12 @@ public class ItemCountDAOOracle implements ItemCountDAO log.error("caught exception: ", e); throw new ItemCountException(e); } - } + finally + { + if (tri != null) + tri.close(); + } + } /** * get the count for the given community @@ -306,10 +318,11 @@ public class ItemCountDAOOracle implements ItemCountDAO private int getCommunityCount(Community community) throws ItemCountException { - try + TableRowIterator tri = null; + try { Object[] params = { new Integer(community.getID()) }; - TableRowIterator tri = DatabaseManager.query(context, communitySelect, params); + tri = DatabaseManager.query(context, communitySelect, params); if (!tri.hasNext()) { @@ -322,9 +335,7 @@ public class ItemCountDAOOracle implements ItemCountDAO { throw new ItemCountException("More than one count row in the database"); } - - tri.close(); - + return tr.getIntColumn("count"); } catch (SQLException e) @@ -332,5 +343,10 @@ public class ItemCountDAOOracle implements ItemCountDAO log.error("caught exception: ", e); throw new ItemCountException(e); } - } + finally + { + if (tri != null) + tri.close(); + } + } } diff --git a/dspace-api/src/main/java/org/dspace/browse/ItemCountDAOPostgres.java b/dspace-api/src/main/java/org/dspace/browse/ItemCountDAOPostgres.java index aa08beeb20..cb3a0702c2 100644 --- a/dspace-api/src/main/java/org/dspace/browse/ItemCountDAOPostgres.java +++ b/dspace-api/src/main/java/org/dspace/browse/ItemCountDAOPostgres.java @@ -96,11 +96,12 @@ public class ItemCountDAOPostgres implements ItemCountDAO public void collectionCount(Collection collection, int count) throws ItemCountException { - try + TableRowIterator tri = null; + try { // first find out if we have a record Object[] sparams = { new Integer(collection.getID()) }; - TableRowIterator tri = DatabaseManager.query(context, collectionSelect, sparams); + tri = DatabaseManager.query(context, collectionSelect, sparams); if (tri.hasNext()) { @@ -112,15 +113,18 @@ public class ItemCountDAOPostgres implements ItemCountDAO Object[] params = { new Integer(collection.getID()), new Integer(count) }; DatabaseManager.updateQuery(context, collectionInsert, params); } - - tri.close(); } catch (SQLException e) { log.error("caught exception: ", e); throw new ItemCountException(e); } - } + finally + { + if (tri != null) + tri.close(); + } + } /** * Store the count of the given community @@ -132,11 +136,12 @@ public class ItemCountDAOPostgres implements ItemCountDAO public void communityCount(Community community, int count) throws ItemCountException { - try + TableRowIterator tri = null; + try { // first find out if we have a record Object[] sparams = { new Integer(community.getID()) }; - TableRowIterator tri = DatabaseManager.query(context, communitySelect, sparams); + tri = DatabaseManager.query(context, communitySelect, sparams); if (tri.hasNext()) { @@ -148,15 +153,18 @@ public class ItemCountDAOPostgres implements ItemCountDAO Object[] params = { new Integer(community.getID()), new Integer(count) }; DatabaseManager.updateQuery(context, communityInsert, params); } - - tri.close(); } catch (SQLException e) { log.error("caught exception: ", e); throw new ItemCountException(e); } - } + finally + { + if (tri != null) + tri.close(); + } + } /** * Set the dspace context to use @@ -268,10 +276,11 @@ public class ItemCountDAOPostgres implements ItemCountDAO private int getCollectionCount(Collection collection) throws ItemCountException { - try + TableRowIterator tri = null; + try { Object[] params = { new Integer(collection.getID()) }; - TableRowIterator tri = DatabaseManager.query(context, collectionSelect, params); + tri = DatabaseManager.query(context, collectionSelect, params); if (!tri.hasNext()) { @@ -284,9 +293,7 @@ public class ItemCountDAOPostgres implements ItemCountDAO { throw new ItemCountException("More than one count row in the database"); } - - tri.close(); - + return tr.getIntColumn("count"); } catch (SQLException e) @@ -294,7 +301,12 @@ public class ItemCountDAOPostgres implements ItemCountDAO log.error("caught exception: ", e); throw new ItemCountException(e); } - } + finally + { + if (tri != null) + tri.close(); + } + } /** * get the count for the given community @@ -306,10 +318,11 @@ public class ItemCountDAOPostgres implements ItemCountDAO private int getCommunityCount(Community community) throws ItemCountException { - try + TableRowIterator tri = null; + try { Object[] params = { new Integer(community.getID()) }; - TableRowIterator tri = DatabaseManager.query(context, communitySelect, params); + tri = DatabaseManager.query(context, communitySelect, params); if (!tri.hasNext()) { @@ -322,9 +335,7 @@ public class ItemCountDAOPostgres implements ItemCountDAO { throw new ItemCountException("More than one count row in the database"); } - - tri.close(); - + return tr.getIntColumn("count"); } catch (SQLException e) @@ -332,5 +343,10 @@ public class ItemCountDAOPostgres implements ItemCountDAO log.error("caught exception: ", e); throw new ItemCountException(e); } - } + finally + { + if (tri != null) + tri.close(); + } + } } diff --git a/dspace-api/src/main/java/org/dspace/checker/HandleDispatcher.java b/dspace-api/src/main/java/org/dspace/checker/HandleDispatcher.java index 22b6e44b8d..2db219a921 100644 --- a/dspace-api/src/main/java/org/dspace/checker/HandleDispatcher.java +++ b/dspace-api/src/main/java/org/dspace/checker/HandleDispatcher.java @@ -98,59 +98,62 @@ public class HandleDispatcher implements BitstreamDispatcher * @throws SQLException * if database access fails. */ - private void init() + private synchronized void init() { - Context context = null; - int dsoType = -1; - - int id = -1; - try + if (init == Boolean.FALSE) { - context = new Context(); - DSpaceObject dso = HandleManager.resolveToObject(context, handle); - id = dso.getID(); - dsoType = dso.getType(); - context.abort(); + Context context = null; + int dsoType = -1; - } - catch (SQLException e) - { - LOG.error("init error " + e.getMessage(), e); - throw new RuntimeException("init error" + e.getMessage(), e); - - } - finally - { - // Abort the context if it's still valid - if ((context != null) && context.isValid()) + int id = -1; + try { + context = new Context(); + DSpaceObject dso = HandleManager.resolveToObject(context, handle); + id = dso.getID(); + dsoType = dso.getType(); context.abort(); + } + catch (SQLException e) + { + LOG.error("init error " + e.getMessage(), e); + throw new RuntimeException("init error" + e.getMessage(), e); + + } + finally + { + // Abort the context if it's still valid + if ((context != null) && context.isValid()) + { + context.abort(); + } + } + + List ids = new ArrayList(); + + switch (dsoType) + { + case Constants.BITSTREAM: + ids.add(new Integer(id)); + break; + + case Constants.ITEM: + ids = bitstreamInfoDAO.getItemBitstreams(id); + break; + + case Constants.COLLECTION: + ids = bitstreamInfoDAO.getCollectionBitstreams(id); + break; + + case Constants.COMMUNITY: + ids = bitstreamInfoDAO.getCommunityBitstreams(id); + break; + } + + delegate = new ListDispatcher(ids); + init = Boolean.TRUE; } - - List ids = new ArrayList(); - - switch (dsoType) - { - case Constants.BITSTREAM: - ids.add(new Integer(id)); - break; - - case Constants.ITEM: - ids = bitstreamInfoDAO.getItemBitstreams(id); - break; - - case Constants.COLLECTION: - ids = bitstreamInfoDAO.getCollectionBitstreams(id); - break; - - case Constants.COMMUNITY: - ids = bitstreamInfoDAO.getCommunityBitstreams(id); - break; - } - - delegate = new ListDispatcher(ids); - init = Boolean.TRUE; } /** @@ -160,12 +163,9 @@ public class HandleDispatcher implements BitstreamDispatcher */ public int next() { - synchronized (init) + if (init == Boolean.FALSE) { - if (init == Boolean.FALSE) - { - init(); - } + init(); } return delegate.next(); diff --git a/dspace-api/src/main/java/org/dspace/content/Bitstream.java b/dspace-api/src/main/java/org/dspace/content/Bitstream.java index 79017bcf46..7973b1c9f1 100644 --- a/dspace-api/src/main/java/org/dspace/content/Bitstream.java +++ b/dspace-api/src/main/java/org/dspace/content/Bitstream.java @@ -581,27 +581,32 @@ public class Bitstream extends DSpaceObject // Build a list of Bundle objects List bundles = new ArrayList(); - - while (tri.hasNext()) + try { - TableRow r = tri.next(); - - // First check the cache - Bundle fromCache = (Bundle) bContext.fromCache(Bundle.class, r - .getIntColumn("bundle_id")); - - if (fromCache != null) + while (tri.hasNext()) { - bundles.add(fromCache); - } - else - { - bundles.add(new Bundle(bContext, r)); + TableRow r = tri.next(); + + // First check the cache + Bundle fromCache = (Bundle) bContext.fromCache(Bundle.class, r + .getIntColumn("bundle_id")); + + if (fromCache != null) + { + bundles.add(fromCache); + } + else + { + bundles.add(new Bundle(bContext, r)); + } } } - - // close the TableRowIterator to free up resources - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } Bundle[] bundleArray = new Bundle[bundles.size()]; bundleArray = (Bundle[]) bundles.toArray(bundleArray); diff --git a/dspace-api/src/main/java/org/dspace/content/BitstreamFormat.java b/dspace-api/src/main/java/org/dspace/content/BitstreamFormat.java index 25563e4274..353681a18a 100644 --- a/dspace-api/src/main/java/org/dspace/content/BitstreamFormat.java +++ b/dspace-api/src/main/java/org/dspace/content/BitstreamFormat.java @@ -114,12 +114,19 @@ public class BitstreamFormat "SELECT * FROM fileextension WHERE bitstream_format_id= ? ", getID()); - while (tri.hasNext()) + try { - extensions.add(tri.next().getStringColumn("extension")); + while (tri.hasNext()) + { + extensions.add(tri.next().getStringColumn("extension")); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - // close the TableRowIterator to free up resources - tri.close(); // Cache ourselves context.cache(this, row.getIntColumn("bitstream_format_id")); @@ -298,26 +305,33 @@ public class BitstreamFormat TableRowIterator tri = DatabaseManager.queryTable(context, "bitstreamformatregistry", "SELECT * FROM bitstreamformatregistry ORDER BY bitstream_format_id"); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // From cache? - BitstreamFormat fromCache = (BitstreamFormat) context.fromCache( - BitstreamFormat.class, row - .getIntColumn("bitstream_format_id")); - - if (fromCache != null) + while (tri.hasNext()) { - formats.add(fromCache); - } - else - { - formats.add(new BitstreamFormat(context, row)); + TableRow row = tri.next(); + + // From cache? + BitstreamFormat fromCache = (BitstreamFormat) context.fromCache( + BitstreamFormat.class, row + .getIntColumn("bitstream_format_id")); + + if (fromCache != null) + { + formats.add(fromCache); + } + else + { + formats.add(new BitstreamFormat(context, row)); + } } } - // close the TableRowIterator to free up resources - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } // Return the formats as an array BitstreamFormat[] formatArray = new BitstreamFormat[formats.size()]; @@ -349,26 +363,33 @@ public class BitstreamFormat TableRowIterator tri = DatabaseManager.queryTable(context, "bitstreamformatregistry", myQuery); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // From cache? - BitstreamFormat fromCache = (BitstreamFormat) context.fromCache( - BitstreamFormat.class, row - .getIntColumn("bitstream_format_id")); - - if (fromCache != null) + while (tri.hasNext()) { - formats.add(fromCache); - } - else - { - formats.add(new BitstreamFormat(context, row)); + TableRow row = tri.next(); + + // From cache? + BitstreamFormat fromCache = (BitstreamFormat) context.fromCache( + BitstreamFormat.class, row + .getIntColumn("bitstream_format_id")); + + if (fromCache != null) + { + formats.add(fromCache); + } + else + { + formats.add(new BitstreamFormat(context, row)); + } } } - // close the TableRowIterator to free up resources - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } // Return the formats as an array BitstreamFormat[] formatArray = new BitstreamFormat[formats.size()]; diff --git a/dspace-api/src/main/java/org/dspace/content/Bundle.java b/dspace-api/src/main/java/org/dspace/content/Bundle.java index f741d5709a..0082764dfc 100644 --- a/dspace-api/src/main/java/org/dspace/content/Bundle.java +++ b/dspace-api/src/main/java/org/dspace/content/Bundle.java @@ -110,26 +110,33 @@ public class Bundle extends DSpaceObject + "bundle2bitstream.bitstream_id=bitstream.bitstream_id AND " + "bundle2bitstream.bundle_id= ? ", bundleRow.getIntColumn("bundle_id")); - - while (tri.hasNext()) + + try { - TableRow r = (TableRow) tri.next(); - - // First check the cache - Bitstream fromCache = (Bitstream) context.fromCache( - Bitstream.class, r.getIntColumn("bitstream_id")); - - if (fromCache != null) + while (tri.hasNext()) { - bitstreams.add(fromCache); - } - else - { - bitstreams.add(new Bitstream(ourContext, r)); + TableRow r = (TableRow) tri.next(); + + // First check the cache + Bitstream fromCache = (Bitstream) context.fromCache( + Bitstream.class, r.getIntColumn("bitstream_id")); + + if (fromCache != null) + { + bitstreams.add(fromCache); + } + else + { + bitstreams.add(new Bitstream(ourContext, r)); + } } } - // close the TableRowIterator to free up resources - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } // Cache ourselves context.cache(this, row.getIntColumn("bundle_id")); @@ -331,26 +338,33 @@ public class Bundle extends DSpaceObject "item2bundle.item_id=item.item_id AND " + "item2bundle.bundle_id= ? ", bundleRow.getIntColumn("bundle_id")); - - while (tri.hasNext()) + + try { - TableRow r = (TableRow) tri.next(); - - // Used cached copy if there is one - Item fromCache = (Item) ourContext.fromCache(Item.class, r - .getIntColumn("item_id")); - - if (fromCache != null) + while (tri.hasNext()) { - items.add(fromCache); - } - else - { - items.add(new Item(ourContext, r)); + TableRow r = (TableRow) tri.next(); + + // Used cached copy if there is one + Item fromCache = (Item) ourContext.fromCache(Item.class, r + .getIntColumn("item_id")); + + if (fromCache != null) + { + items.add(fromCache); + } + else + { + items.add(new Item(ourContext, r)); + } } } - // close the TableRowIterator to free up resources - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } Item[] itemArray = new Item[items.size()]; itemArray = (Item[]) items.toArray(itemArray); @@ -503,13 +517,20 @@ public class Bundle extends DSpaceObject "SELECT * FROM bundle2bitstream WHERE bitstream_id= ? ", b.getID()); - if (!tri.hasNext()) + try { - // The bitstream is an orphan, delete it - b.delete(); + if (!tri.hasNext()) + { + // The bitstream is an orphan, delete it + b.delete(); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - // close the TableRowIterator to free up resources - tri.close(); } /** diff --git a/dspace-api/src/main/java/org/dspace/content/Collection.java b/dspace-api/src/main/java/org/dspace/content/Collection.java index 5403ed680b..cdb670d2ea 100644 --- a/dspace-api/src/main/java/org/dspace/content/Collection.java +++ b/dspace-api/src/main/java/org/dspace/content/Collection.java @@ -289,25 +289,32 @@ public class Collection extends DSpaceObject List collections = new ArrayList(); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // First check the cache - Collection fromCache = (Collection) context.fromCache( - Collection.class, row.getIntColumn("collection_id")); - - if (fromCache != null) + while (tri.hasNext()) { - collections.add(fromCache); - } - else - { - collections.add(new Collection(context, row)); + TableRow row = tri.next(); + + // First check the cache + Collection fromCache = (Collection) context.fromCache( + Collection.class, row.getIntColumn("collection_id")); + + if (fromCache != null) + { + collections.add(fromCache); + } + else + { + collections.add(new Collection(context, row)); + } } } - // close the TableRowIterator to free up resources - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } Collection[] collectionArray = new Collection[collections.size()]; collectionArray = (Collection[]) collections.toArray(collectionArray); @@ -924,24 +931,31 @@ public class Collection extends DSpaceObject "SELECT * FROM collection2item WHERE item_id= ? ", item.getID()); - if (!tri.hasNext()) + try { - //make the right to remove the item explicit because the implicit - // relation - //has been removed. This only has to concern the currentUser - // because - //he started the removal process and he will end it too. - //also add right to remove from the item to remove it's bundles. - AuthorizeManager.addPolicy(ourContext, item, Constants.DELETE, - ourContext.getCurrentUser()); - AuthorizeManager.addPolicy(ourContext, item, Constants.REMOVE, - ourContext.getCurrentUser()); + if (!tri.hasNext()) + { + //make the right to remove the item explicit because the implicit + // relation + //has been removed. This only has to concern the currentUser + // because + //he started the removal process and he will end it too. + //also add right to remove from the item to remove it's bundles. + AuthorizeManager.addPolicy(ourContext, item, Constants.DELETE, + ourContext.getCurrentUser()); + AuthorizeManager.addPolicy(ourContext, item, Constants.REMOVE, + ourContext.getCurrentUser()); - // Orphan; delete it - item.delete(); + // Orphan; delete it + item.delete(); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - // close the TableRowIterator to free up resources - tri.close(); } /** @@ -1181,31 +1195,38 @@ public class Collection extends DSpaceObject // Build a list of Community objects List communities = new ArrayList(); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // First check the cache - Community owner = (Community) ourContext.fromCache(Community.class, - row.getIntColumn("community_id")); - - if (owner == null) + while (tri.hasNext()) { - owner = new Community(ourContext, row); - } + TableRow row = tri.next(); - communities.add(owner); + // First check the cache + Community owner = (Community) ourContext.fromCache(Community.class, + row.getIntColumn("community_id")); - // now add any parent communities - Community[] parents = owner.getAllParents(); + if (owner == null) + { + owner = new Community(ourContext, row); + } - for (int i = 0; i < parents.length; i++) - { - communities.add(parents[i]); + communities.add(owner); + + // now add any parent communities + Community[] parents = owner.getAllParents(); + + for (int i = 0; i < parents.length; i++) + { + communities.add(parents[i]); + } } } - // close the TableRowIterator to free up resources - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } Community[] communityArray = new Community[communities.size()]; communityArray = (Community[]) communities.toArray(communityArray); @@ -1316,20 +1337,39 @@ public class Collection extends DSpaceObject public int countItems() throws SQLException { - String query = "SELECT count(*) FROM collection2item, item WHERE " - + "collection2item.collection_id = ? " - + "AND collection2item.item_id = item.item_id " - + "AND in_archive ='1' AND item.withdrawn='0' "; + int itemcount = 0; + PreparedStatement statement = null; + ResultSet rs = null; - PreparedStatement statement = ourContext.getDBConnection().prepareStatement(query); - statement.setInt(1,getID()); - - ResultSet rs = statement.executeQuery(); - - rs.next(); - int itemcount = rs.getInt(1); + try + { + String query = "SELECT count(*) FROM collection2item, item WHERE " + + "collection2item.collection_id = ? " + + "AND collection2item.item_id = item.item_id " + + "AND in_archive ='1' AND item.withdrawn='0' "; - statement.close(); + statement = ourContext.getDBConnection().prepareStatement(query); + statement.setInt(1,getID()); + + rs = statement.executeQuery(); + if (rs != null) + { + rs.next(); + itemcount = rs.getInt(1); + } + } + finally + { + if (rs != null) + { + try { rs.close(); } catch (SQLException sqle) { } + } + + if (statement != null) + { + try { statement.close(); } catch (SQLException sqle) { } + } + } return itemcount; } diff --git a/dspace-api/src/main/java/org/dspace/content/Community.java b/dspace-api/src/main/java/org/dspace/content/Community.java index 6ae3ccc3a6..b3edb43752 100644 --- a/dspace-api/src/main/java/org/dspace/content/Community.java +++ b/dspace-api/src/main/java/org/dspace/content/Community.java @@ -237,25 +237,32 @@ public class Community extends DSpaceObject List communities = new ArrayList(); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // First check the cache - Community fromCache = (Community) context.fromCache( - Community.class, row.getIntColumn("community_id")); - - if (fromCache != null) + while (tri.hasNext()) { - communities.add(fromCache); - } - else - { - communities.add(new Community(context, row)); + TableRow row = tri.next(); + + // First check the cache + Community fromCache = (Community) context.fromCache( + Community.class, row.getIntColumn("community_id")); + + if (fromCache != null) + { + communities.add(fromCache); + } + else + { + communities.add(new Community(context, row)); + } } } - // close the TableRowIterator to free up resources - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } Community[] communityArray = new Community[communities.size()]; communityArray = (Community[]) communities.toArray(communityArray); @@ -283,25 +290,32 @@ public class Community extends DSpaceObject List topCommunities = new ArrayList(); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // First check the cache - Community fromCache = (Community) context.fromCache( - Community.class, row.getIntColumn("community_id")); - - if (fromCache != null) + while (tri.hasNext()) { - topCommunities.add(fromCache); - } - else - { - topCommunities.add(new Community(context, row)); + TableRow row = tri.next(); + + // First check the cache + Community fromCache = (Community) context.fromCache( + Community.class, row.getIntColumn("community_id")); + + if (fromCache != null) + { + topCommunities.add(fromCache); + } + else + { + topCommunities.add(new Community(context, row)); + } } } - // close the TableRowIterator to free up resources - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } Community[] communityArray = new Community[topCommunities.size()]; communityArray = (Community[]) topCommunities.toArray(communityArray); @@ -514,25 +528,32 @@ public class Community extends DSpaceObject getID()); // Make Collection objects - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // First check the cache - Collection fromCache = (Collection) ourContext.fromCache( - Collection.class, row.getIntColumn("collection_id")); - - if (fromCache != null) + while (tri.hasNext()) { - collections.add(fromCache); - } - else - { - collections.add(new Collection(ourContext, row)); + TableRow row = tri.next(); + + // First check the cache + Collection fromCache = (Collection) ourContext.fromCache( + Collection.class, row.getIntColumn("collection_id")); + + if (fromCache != null) + { + collections.add(fromCache); + } + else + { + collections.add(new Collection(ourContext, row)); + } } } - // close the TableRowIterator to free up resources - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } // Put them in an array Collection[] collectionArray = new Collection[collections.size()]; @@ -562,25 +583,32 @@ public class Community extends DSpaceObject // Make Community objects - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // First check the cache - Community fromCache = (Community) ourContext.fromCache( - Community.class, row.getIntColumn("community_id")); - - if (fromCache != null) + while (tri.hasNext()) { - subcommunities.add(fromCache); - } - else - { - subcommunities.add(new Community(ourContext, row)); + TableRow row = tri.next(); + + // First check the cache + Community fromCache = (Community) ourContext.fromCache( + Community.class, row.getIntColumn("community_id")); + + if (fromCache != null) + { + subcommunities.add(fromCache); + } + else + { + subcommunities.add(new Community(ourContext, row)); + } } } - // close the TableRowIterator to free up resources - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } // Put them in an array Community[] communityArray = new Community[subcommunities.size()]; @@ -608,25 +636,32 @@ public class Community extends DSpaceObject getID()); // Make Community object - if (tri.hasNext()) + try { - TableRow row = tri.next(); - - // First check the cache - Community fromCache = (Community) ourContext.fromCache( - Community.class, row.getIntColumn("community_id")); - - if (fromCache != null) + if (tri.hasNext()) { - parentCommunity = fromCache; - } - else - { - parentCommunity = new Community(ourContext, row); + TableRow row = tri.next(); + + // First check the cache + Community fromCache = (Community) ourContext.fromCache( + Community.class, row.getIntColumn("community_id")); + + if (fromCache != null) + { + parentCommunity = fromCache; + } + else + { + parentCommunity = new Community(ourContext, row); + } } } - // close the TableRowIterator to free up resources - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } return parentCommunity; } @@ -693,22 +728,29 @@ public class Community extends DSpaceObject "community2collection", "SELECT * FROM community2collection WHERE " + "community_id= ? AND collection_id= ? ",getID(),c.getID()); - - if (!tri.hasNext()) + + try { - // No existing mapping, so add one - TableRow mappingRow = DatabaseManager.create(ourContext, - "community2collection"); + if (!tri.hasNext()) + { + // No existing mapping, so add one + TableRow mappingRow = DatabaseManager.create(ourContext, + "community2collection"); - mappingRow.setColumn("community_id", getID()); - mappingRow.setColumn("collection_id", c.getID()); + mappingRow.setColumn("community_id", getID()); + mappingRow.setColumn("collection_id", c.getID()); - ourContext.addEvent(new Event(Event.ADD, Constants.COMMUNITY, getID(), Constants.COLLECTION, c.getID(), c.getHandle())); + ourContext.addEvent(new Event(Event.ADD, Constants.COMMUNITY, getID(), Constants.COLLECTION, c.getID(), c.getHandle())); - DatabaseManager.update(ourContext, mappingRow); + DatabaseManager.update(ourContext, mappingRow); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - // close the TableRowIterator to free up resources - tri.close(); } /** @@ -748,22 +790,29 @@ public class Community extends DSpaceObject "community2community", "SELECT * FROM community2community WHERE parent_comm_id= ? "+ "AND child_comm_id= ? ",getID(), c.getID()); - - if (!tri.hasNext()) + + try { - // No existing mapping, so add one - TableRow mappingRow = DatabaseManager.create(ourContext, - "community2community"); + if (!tri.hasNext()) + { + // No existing mapping, so add one + TableRow mappingRow = DatabaseManager.create(ourContext, + "community2community"); - mappingRow.setColumn("parent_comm_id", getID()); - mappingRow.setColumn("child_comm_id", c.getID()); + mappingRow.setColumn("parent_comm_id", getID()); + mappingRow.setColumn("child_comm_id", c.getID()); - ourContext.addEvent(new Event(Event.ADD, Constants.COMMUNITY, getID(), Constants.COMMUNITY, c.getID(), c.getHandle())); + ourContext.addEvent(new Event(Event.ADD, Constants.COMMUNITY, getID(), Constants.COMMUNITY, c.getID(), c.getHandle())); - DatabaseManager.update(ourContext, mappingRow); + DatabaseManager.update(ourContext, mappingRow); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - // close the TableRowIterator to free up resources - tri.close(); } /** @@ -793,25 +842,32 @@ public class Community extends DSpaceObject "SELECT * FROM community2collection WHERE collection_id= ? ", c.getID()); - if (!tri.hasNext()) + try { - //make the right to remove the collection explicit because the - // implicit relation - //has been removed. This only has to concern the currentUser - // because - //he started the removal process and he will end it too. - //also add right to remove from the collection to remove it's - // items. - AuthorizeManager.addPolicy(ourContext, c, Constants.DELETE, - ourContext.getCurrentUser()); - AuthorizeManager.addPolicy(ourContext, c, Constants.REMOVE, - ourContext.getCurrentUser()); + if (!tri.hasNext()) + { + //make the right to remove the collection explicit because the + // implicit relation + //has been removed. This only has to concern the currentUser + // because + //he started the removal process and he will end it too. + //also add right to remove from the collection to remove it's + // items. + AuthorizeManager.addPolicy(ourContext, c, Constants.DELETE, + ourContext.getCurrentUser()); + AuthorizeManager.addPolicy(ourContext, c, Constants.REMOVE, + ourContext.getCurrentUser()); - // Orphan; delete it - c.delete(); + // Orphan; delete it + c.delete(); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - // close the TableRowIterator to free up resources - tri.close(); } /** @@ -841,25 +897,32 @@ public class Community extends DSpaceObject "SELECT * FROM community2community WHERE child_comm_id= ? ", c.getID()); - if (!tri.hasNext()) + try { - //make the right to remove the sub explicit because the implicit - // relation - //has been removed. This only has to concern the currentUser - // because - //he started the removal process and he will end it too. - //also add right to remove from the subcommunity to remove it's - // children. - AuthorizeManager.addPolicy(ourContext, c, Constants.DELETE, - ourContext.getCurrentUser()); - AuthorizeManager.addPolicy(ourContext, c, Constants.REMOVE, - ourContext.getCurrentUser()); + if (!tri.hasNext()) + { + //make the right to remove the sub explicit because the implicit + // relation + //has been removed. This only has to concern the currentUser + // because + //he started the removal process and he will end it too. + //also add right to remove from the subcommunity to remove it's + // children. + AuthorizeManager.addPolicy(ourContext, c, Constants.DELETE, + ourContext.getCurrentUser()); + AuthorizeManager.addPolicy(ourContext, c, Constants.REMOVE, + ourContext.getCurrentUser()); - // Orphan; delete it - c.delete(); + // Orphan; delete it + c.delete(); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - // close the TableRowIterator to free up resources - tri.close(); } /** diff --git a/dspace-api/src/main/java/org/dspace/content/FormatIdentifier.java b/dspace-api/src/main/java/org/dspace/content/FormatIdentifier.java index 362e74c143..827940f144 100644 --- a/dspace-api/src/main/java/org/dspace/content/FormatIdentifier.java +++ b/dspace-api/src/main/java/org/dspace/content/FormatIdentifier.java @@ -105,17 +105,24 @@ public class FormatIdentifier extension); BitstreamFormat retFormat = null; - if (tri.hasNext()) + try { - // Return first match - retFormat = new BitstreamFormat(context, tri.next()); + if (tri.hasNext()) + { + // Return first match + retFormat = new BitstreamFormat(context, tri.next()); + } + else + { + retFormat = null; + } } - else + finally { - retFormat = null; + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - // close the TableRowIterator to free up resources - tri.close(); return retFormat; } } diff --git a/dspace-api/src/main/java/org/dspace/content/Item.java b/dspace-api/src/main/java/org/dspace/content/Item.java index c5406649f3..3de55d1e15 100644 --- a/dspace-api/src/main/java/org/dspace/content/Item.java +++ b/dspace-api/src/main/java/org/dspace/content/Item.java @@ -143,39 +143,46 @@ public class Item extends DSpaceObject // Get Dublin Core metadata TableRowIterator tri = retrieveMetadata(); - while (tri.hasNext()) + try { - TableRow resultRow = tri.next(); - - // Get the associated metadata field and schema information - int fieldID = resultRow.getIntColumn("metadata_field_id"); - MetadataField field = MetadataField.find(context, fieldID); - - if (field == null) + while (tri.hasNext()) { - log.error("Loading item - cannot found metadata field " - + fieldID); + TableRow resultRow = tri.next(); + + // Get the associated metadata field and schema information + int fieldID = resultRow.getIntColumn("metadata_field_id"); + MetadataField field = MetadataField.find(context, fieldID); + + if (field == null) + { + log.error("Loading item - cannot found metadata field " + + fieldID); + } + else + { + MetadataSchema schema = MetadataSchema.find( + context, field.getSchemaID()); + + // Make a DCValue object + DCValue dcv = new DCValue(); + dcv.element = field.getElement(); + dcv.qualifier = field.getQualifier(); + dcv.value = resultRow.getStringColumn("text_value"); + dcv.language = resultRow.getStringColumn("text_lang"); + //dcv.namespace = schema.getNamespace(); + dcv.schema = schema.getName(); + + // Add it to the list + dublinCore.add(dcv); + } } - else - { - MetadataSchema schema = MetadataSchema.find( - context, field.getSchemaID()); - - // Make a DCValue object - DCValue dcv = new DCValue(); - dcv.element = field.getElement(); - dcv.qualifier = field.getQualifier(); - dcv.value = resultRow.getStringColumn("text_value"); - dcv.language = resultRow.getStringColumn("text_lang"); - //dcv.namespace = schema.getNamespace(); - dcv.schema = schema.getName(); - - // Add it to the list - dublinCore.add(dcv); } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - // close the TableRowIterator to free up resources - tri.close(); // Get our Handle if any handle = HandleManager.findHandle(context, this); @@ -918,25 +925,32 @@ public class Item extends DSpaceObject "collection2item.item_id= ? ", itemRow.getIntColumn("item_id")); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // First check the cache - Collection fromCache = (Collection) ourContext.fromCache( - Collection.class, row.getIntColumn("collection_id")); - - if (fromCache != null) + while (tri.hasNext()) { - collections.add(fromCache); - } - else - { - collections.add(new Collection(ourContext, row)); + TableRow row = tri.next(); + + // First check the cache + Collection fromCache = (Collection) ourContext.fromCache( + Collection.class, row.getIntColumn("collection_id")); + + if (fromCache != null) + { + collections.add(fromCache); + } + else + { + collections.add(new Collection(ourContext, row)); + } } } - // close the TableRowIterator to free up resources - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } Collection[] collectionArray = new Collection[collections.size()]; collectionArray = (Collection[]) collections.toArray(collectionArray); @@ -963,31 +977,38 @@ public class Item extends DSpaceObject "AND community2item.item_id= ? ", itemRow.getIntColumn("item_id")); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // First check the cache - Community owner = (Community) ourContext.fromCache(Community.class, - row.getIntColumn("community_id")); - - if (owner == null) + while (tri.hasNext()) { - owner = new Community(ourContext, row); - } + TableRow row = tri.next(); - communities.add(owner); + // First check the cache + Community owner = (Community) ourContext.fromCache(Community.class, + row.getIntColumn("community_id")); - // now add any parent communities - Community[] parents = owner.getAllParents(); + if (owner == null) + { + owner = new Community(ourContext, row); + } - for (int i = 0; i < parents.length; i++) - { - communities.add(parents[i]); + communities.add(owner); + + // now add any parent communities + Community[] parents = owner.getAllParents(); + + for (int i = 0; i < parents.length; i++) + { + communities.add(parents[i]); + } } } - // close the TableRowIterator to free up resources - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } Community[] communityArray = new Community[communities.size()]; communityArray = (Community[]) communities.toArray(communityArray); @@ -1012,25 +1033,32 @@ public class Item extends DSpaceObject "item2bundle.item_id= ? ", itemRow.getIntColumn("item_id")); - while (tri.hasNext()) - { - TableRow r = tri.next(); + try + { + while (tri.hasNext()) + { + TableRow r = tri.next(); - // First check the cache - Bundle fromCache = (Bundle) ourContext.fromCache(Bundle.class, - r.getIntColumn("bundle_id")); + // First check the cache + Bundle fromCache = (Bundle) ourContext.fromCache(Bundle.class, + r.getIntColumn("bundle_id")); - if (fromCache != null) - { - bundles.add(fromCache); - } - else - { - bundles.add(new Bundle(ourContext, r)); - } - } - // close the TableRowIterator to free up resources - tri.close(); + if (fromCache != null) + { + bundles.add(fromCache); + } + else + { + bundles.add(new Bundle(ourContext, r)); + } + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } } Bundle[] bundleArray = new Bundle[bundles.size()]; @@ -1184,25 +1212,32 @@ public class Item extends DSpaceObject "SELECT * FROM item2bundle WHERE bundle_id= ? ", b.getID()); - if (!tri.hasNext()) + try { - //make the right to remove the bundle explicit because the implicit - // relation - //has been removed. This only has to concern the currentUser - // because - //he started the removal process and he will end it too. - //also add right to remove from the bundle to remove it's - // bitstreams. - AuthorizeManager.addPolicy(ourContext, b, Constants.DELETE, - ourContext.getCurrentUser()); - AuthorizeManager.addPolicy(ourContext, b, Constants.REMOVE, - ourContext.getCurrentUser()); + if (!tri.hasNext()) + { + //make the right to remove the bundle explicit because the implicit + // relation + //has been removed. This only has to concern the currentUser + // because + //he started the removal process and he will end it too. + //also add right to remove from the bundle to remove it's + // bitstreams. + AuthorizeManager.addPolicy(ourContext, b, Constants.DELETE, + ourContext.getCurrentUser()); + AuthorizeManager.addPolicy(ourContext, b, Constants.REMOVE, + ourContext.getCurrentUser()); - // The bundle is an orphan, delete it - b.delete(); + // The bundle is an orphan, delete it + b.delete(); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - // close the TableRowIterator to free up resources - tri.close(); } /** 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 06606ec257..933a61d7b4 100644 --- a/dspace-api/src/main/java/org/dspace/content/MetadataField.java +++ b/dspace-api/src/main/java/org/dspace/content/MetadataField.java @@ -320,15 +320,20 @@ public class MetadataField schemaID, element, qualifier); } - TableRow row = null; - if (tri.hasNext()) + try { - row = tri.next(); + if (tri.hasNext()) + { + row = tri.next(); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - - // close the TableRowIterator to free up resources - tri.close(); if (row == null) { @@ -354,14 +359,21 @@ public class MetadataField // Get all the metadatafieldregistry rows TableRowIterator tri = DatabaseManager.queryTable(context, "MetadataFieldRegistry", "SELECT mfr.* FROM MetadataFieldRegistry mfr, MetadataSchemaRegistry msr where mfr.metadata_schema_id= msr.metadata_schema_id ORDER BY msr.short_id, mfr.element, mfr.qualifier"); - - // Make into DC Type objects - while (tri.hasNext()) + + try { - fields.add(new MetadataField(tri.next())); + // Make into DC Type objects + while (tri.hasNext()) + { + fields.add(new MetadataField(tri.next())); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - // close the TableRowIterator to free up resources - tri.close(); // Convert list into an array MetadataField[] typeArray = new MetadataField[fields.size()]; @@ -386,13 +398,20 @@ public class MetadataField "SELECT * FROM MetadataFieldRegistry WHERE metadata_schema_id= ? " + " ORDER BY element, qualifier", schemaID); - // Make into DC Type objects - while (tri.hasNext()) + try { - fields.add(new MetadataField(tri.next())); + // Make into DC Type objects + while (tri.hasNext()) + { + fields.add(new MetadataField(tri.next())); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - // close the TableRowIterator to free up resources - tri.close(); // Convert list into an array MetadataField[] typeArray = new MetadataField[fields.size()]; @@ -509,41 +528,60 @@ public class MetadataField String qualifier) throws IOException, SQLException, AuthorizeException { - Connection con = context.getDBConnection(); - TableRow reg = DatabaseManager.row("MetadataFieldRegistry"); - - String qualifierClause = ""; - - if (qualifier == null) - { - qualifierClause = "and qualifier is null"; - } - else - { - qualifierClause = "and qualifier = ?"; - } - - String query = "SELECT COUNT(*) FROM " + reg.getTable() - + " WHERE metadata_schema_id= ? " - + " and metadata_field_id != ? " - + " and element= ? " + qualifierClause; - - PreparedStatement statement = con.prepareStatement(query); - statement.setInt(1,schemaID); - statement.setInt(2,fieldID); - statement.setString(3,element); - - if (qualifier != null) - { - statement.setString(4,qualifier); - } - - ResultSet rs = statement.executeQuery(); - int count = 0; - if (rs.next()) + Connection con = null; + PreparedStatement statement = null; + ResultSet rs = null; + + try { - count = rs.getInt(1); + con = context.getDBConnection(); + TableRow reg = DatabaseManager.row("MetadataFieldRegistry"); + + String qualifierClause = ""; + + if (qualifier == null) + { + qualifierClause = "and qualifier is null"; + } + else + { + qualifierClause = "and qualifier = ?"; + } + + String query = "SELECT COUNT(*) FROM " + reg.getTable() + + " WHERE metadata_schema_id= ? " + + " and metadata_field_id != ? " + + " and element= ? " + qualifierClause; + + statement = con.prepareStatement(query); + statement.setInt(1,schemaID); + statement.setInt(2,fieldID); + statement.setString(3,element); + + if (qualifier != null) + { + statement.setString(4,qualifier); + } + + rs = statement.executeQuery(); + + if (rs.next()) + { + count = rs.getInt(1); + } + } + finally + { + if (rs != null) + { + try { rs.close(); } catch (SQLException sqle) { } + } + + if (statement != null) + { + try { statement.close(); } catch (SQLException sqle) { } + } } return (count == 0); @@ -604,21 +642,36 @@ public class MetadataField { if (id2field != null) return; - id2field = new HashMap(); - log.info("Loading MetadataField elements into cache."); - // Grab rows from DB - TableRowIterator tri = DatabaseManager.queryTable(context,"MetadataFieldRegistry", - "SELECT * from MetadataFieldRegistry"); - - while (tri.hasNext()) + synchronized (MetadataField.class) { - TableRow row = tri.next(); - int fieldID = row.getIntColumn("metadata_field_id"); - id2field.put(new Integer(fieldID), new MetadataField(row)); - } + if (id2field == null) + { + HashMap new_id2field = new HashMap(); + log.info("Loading MetadataField elements into cache."); - // close the TableRowIterator to free up resources - tri.close(); + // 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"); + 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; + } + } } } 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 2f04cd8b1f..4e4c4f59cc 100644 --- a/dspace-api/src/main/java/org/dspace/content/MetadataSchema.java +++ b/dspace-api/src/main/java/org/dspace/content/MetadataSchema.java @@ -262,13 +262,19 @@ public class MetadataSchema namespace); TableRow row = null; - if (tri.hasNext()) + try { - row = tri.next(); + if (tri.hasNext()) + { + row = tri.next(); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - - // close the TableRowIterator to free up resources - tri.close(); if (row == null) { @@ -360,13 +366,20 @@ public class MetadataSchema TableRowIterator tri = DatabaseManager.queryTable(context, "MetadataSchemaRegistry", "SELECT * FROM MetadataSchemaRegistry ORDER BY metadata_schema_id"); - // Make into DC Type objects - while (tri.hasNext()) + try { - schemas.add(new MetadataSchema(tri.next())); + // Make into DC Type objects + while (tri.hasNext()) + { + schemas.add(new MetadataSchema(tri.next())); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - // close the TableRowIterator to free up resources - tri.close(); // Convert list into an array MetadataSchema[] typeArray = new MetadataSchema[schemas.size()]; @@ -385,22 +398,41 @@ public class MetadataSchema private boolean uniqueNamespace(Context context, String namespace) throws SQLException { - Connection con = context.getDBConnection(); - TableRow reg = DatabaseManager.row("MetadataSchemaRegistry"); - - String query = "SELECT COUNT(*) FROM " + reg.getTable() + " " + - "WHERE metadata_schema_id != ? " + - "AND namespace= ? "; - PreparedStatement statement = con.prepareStatement(query); - statement.setInt(1,schemaID); - statement.setString(2,namespace); - - ResultSet rs = statement.executeQuery(); - int count = 0; - if (rs.next()) + Connection con = context.getDBConnection(); + PreparedStatement statement = null; + ResultSet rs = null; + + try { - count = rs.getInt(1); + TableRow reg = DatabaseManager.row("MetadataSchemaRegistry"); + + String query = "SELECT COUNT(*) FROM " + reg.getTable() + " " + + "WHERE metadata_schema_id != ? " + + "AND namespace= ? "; + + statement = con.prepareStatement(query); + statement.setInt(1,schemaID); + statement.setString(2,namespace); + + rs = statement.executeQuery(); + + if (rs.next()) + { + count = rs.getInt(1); + } + } + finally + { + if (rs != null) + { + try { rs.close(); } catch (SQLException sqle) { } + } + + if (statement != null) + { + try { statement.close(); } catch (SQLException sqle) { } + } } return (count == 0); @@ -417,23 +449,41 @@ public class MetadataSchema private boolean uniqueShortName(Context context, String name) throws SQLException { - Connection con = context.getDBConnection(); - TableRow reg = DatabaseManager.row("MetadataSchemaRegistry"); - - String query = "SELECT COUNT(*) FROM " + reg.getTable() + " " + - "WHERE metadata_schema_id != ? " + - "AND short_id = ? "; - - PreparedStatement statement = con.prepareStatement(query); - statement.setInt(1,schemaID); - statement.setString(2,name); - - ResultSet rs = statement.executeQuery(); - int count = 0; - if (rs.next()) + Connection con = context.getDBConnection(); + PreparedStatement statement = null; + ResultSet rs = null; + + try { - count = rs.getInt(1); + TableRow reg = DatabaseManager.row("MetadataSchemaRegistry"); + + String query = "SELECT COUNT(*) FROM " + reg.getTable() + " " + + "WHERE metadata_schema_id != ? " + + "AND short_id = ? "; + + statement = con.prepareStatement(query); + statement.setInt(1,schemaID); + statement.setString(2,name); + + rs = statement.executeQuery(); + + if (rs.next()) + { + count = rs.getInt(1); + } + } + finally + { + if (rs != null) + { + try { rs.close(); } catch (SQLException sqle) { } + } + + if (statement != null) + { + try { statement.close(); } catch (SQLException sqle) { } + } } return (count == 0); @@ -501,21 +551,38 @@ public class MetadataSchema if (id2schema != null && name2schema != null) return; - log.info("Loading schema cache for fast finds"); - id2schema = new HashMap(); - name2schema = new HashMap(); - - TableRowIterator tri = DatabaseManager.queryTable(context,"MetadataSchemaRegistry", - "SELECT * from MetadataSchemaRegistry"); - while (tri.hasNext()) + synchronized (MetadataSchema.class) { - TableRow row = tri.next(); + if (id2schema == null && name2schema == null) + { + log.info("Loading schema cache for fast finds"); + HashMap new_id2schema = new HashMap(); + HashMap new_name2schema = new HashMap(); - MetadataSchema s = new MetadataSchema(row); - id2schema.put(new Integer(s.schemaID), s); - name2schema.put(s.name, s); + TableRowIterator tri = DatabaseManager.queryTable(context,"MetadataSchemaRegistry", + "SELECT * from MetadataSchemaRegistry"); + + try + { + while (tri.hasNext()) + { + TableRow row = tri.next(); + + 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; + } } - // close the TableRowIterator to free up resources - tri.close(); } } diff --git a/dspace-api/src/main/java/org/dspace/content/MetadataValue.java b/dspace-api/src/main/java/org/dspace/content/MetadataValue.java index 2d54f220bd..59c8f715f5 100644 --- a/dspace-api/src/main/java/org/dspace/content/MetadataValue.java +++ b/dspace-api/src/main/java/org/dspace/content/MetadataValue.java @@ -278,13 +278,19 @@ public class MetadataValue valueId); TableRow row = null; - if (tri.hasNext()) + try { - row = tri.next(); + if (tri.hasNext()) + { + row = tri.next(); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - - // close the TableRowIterator to free up resources - tri.close(); if (row == null) { @@ -316,14 +322,20 @@ public class MetadataValue TableRow row = null; java.util.Collection ret = new ArrayList(); - while (tri.hasNext()) + try { - row = tri.next(); - ret.add(new MetadataValue(row)); + while (tri.hasNext()) + { + row = tri.next(); + ret.add(new MetadataValue(row)); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - - // close the TableRowIterator to free up resources - tri.close(); return ret; } diff --git a/dspace-api/src/main/java/org/dspace/content/SupervisedItem.java b/dspace-api/src/main/java/org/dspace/content/SupervisedItem.java index 9d86f5dcdb..429aaaff57 100644 --- a/dspace-api/src/main/java/org/dspace/content/SupervisedItem.java +++ b/dspace-api/src/main/java/org/dspace/content/SupervisedItem.java @@ -121,16 +121,23 @@ public class SupervisedItem extends WorkspaceItem TableRowIterator tri = DatabaseManager.queryTable(context, "workspaceitem", query); - - while (tri.hasNext()) + + try { - TableRow row = tri.next(); - SupervisedItem si = new SupervisedItem(context, row); - - sItems.add(si); + while (tri.hasNext()) + { + TableRow row = tri.next(); + SupervisedItem si = new SupervisedItem(context, row); + + sItems.add(si); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - - tri.close(); SupervisedItem[] siArray = new SupervisedItem[sItems.size()]; siArray = (SupervisedItem[]) sItems.toArray(siArray); @@ -159,16 +166,23 @@ public class SupervisedItem extends WorkspaceItem "ORDER BY epersongroup.name"; TableRowIterator tri = DatabaseManager.queryTable(c,"epersongroup",query, wi); - - while (tri.hasNext()) + + try { - TableRow row = tri.next(); - Group group = Group.find(c,row.getIntColumn("eperson_group_id")); - - groupList.add(group); + while (tri.hasNext()) + { + TableRow row = tri.next(); + Group group = Group.find(c,row.getIntColumn("eperson_group_id")); + + groupList.add(group); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - - tri.close(); Group[] groupArray = new Group[groupList.size()]; groupArray = (Group[]) groupList.toArray(groupArray); @@ -201,17 +215,24 @@ public class SupervisedItem extends WorkspaceItem TableRowIterator tri = DatabaseManager.queryTable(ourContext, "epersongroup", query, this.getID()); - - while (tri.hasNext()) + + try { - TableRow row = tri.next(); - Group group = Group.find(ourContext, - row.getIntColumn("eperson_group_id")); - - groupList.add(group); + while (tri.hasNext()) + { + TableRow row = tri.next(); + Group group = Group.find(ourContext, + row.getIntColumn("eperson_group_id")); + + groupList.add(group); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - - tri.close(); Group[] groupArray = new Group[groupList.size()]; groupArray = (Group[]) groupList.toArray(groupArray); @@ -244,15 +265,22 @@ public class SupervisedItem extends WorkspaceItem TableRowIterator tri = DatabaseManager.queryTable(context, "workspaceitem", query,ep.getID()); - - while (tri.hasNext()) + + try { - TableRow row = tri.next(); - SupervisedItem si = new SupervisedItem(context, row); - sItems.add(si); + while (tri.hasNext()) + { + TableRow row = tri.next(); + SupervisedItem si = new SupervisedItem(context, row); + sItems.add(si); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - - tri.close(); SupervisedItem[] siArray = new SupervisedItem[sItems.size()]; siArray = (SupervisedItem[]) sItems.toArray(siArray); diff --git a/dspace-api/src/main/java/org/dspace/content/WorkspaceItem.java b/dspace-api/src/main/java/org/dspace/content/WorkspaceItem.java index 6de88f2b8d..f201e00c2e 100644 --- a/dspace-api/src/main/java/org/dspace/content/WorkspaceItem.java +++ b/dspace-api/src/main/java/org/dspace/content/WorkspaceItem.java @@ -312,23 +312,30 @@ public class WorkspaceItem implements InProgressSubmission "ORDER BY workspaceitem.workspace_item_id", ep.getID()); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // Check the cache - WorkspaceItem wi = (WorkspaceItem) context.fromCache( - WorkspaceItem.class, row.getIntColumn("workspace_item_id")); - - if (wi == null) + while (tri.hasNext()) { - wi = new WorkspaceItem(context, row); - } + TableRow row = tri.next(); - wsItems.add(wi); + // Check the cache + WorkspaceItem wi = (WorkspaceItem) context.fromCache( + WorkspaceItem.class, row.getIntColumn("workspace_item_id")); + + if (wi == null) + { + wi = new WorkspaceItem(context, row); + } + + wsItems.add(wi); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - // close the TableRowIterator to free up resources - tri.close(); WorkspaceItem[] wsArray = new WorkspaceItem[wsItems.size()]; wsArray = (WorkspaceItem[]) wsItems.toArray(wsArray); @@ -356,24 +363,31 @@ public class WorkspaceItem implements InProgressSubmission "workspaceitem.collection_id= ? ", c.getID()); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // Check the cache - WorkspaceItem wi = (WorkspaceItem) context.fromCache( - WorkspaceItem.class, row.getIntColumn("workspace_item_id")); - - // not in cache? turn row into workspaceitem - if (wi == null) + while (tri.hasNext()) { - wi = new WorkspaceItem(context, row); - } + TableRow row = tri.next(); - wsItems.add(wi); + // Check the cache + WorkspaceItem wi = (WorkspaceItem) context.fromCache( + WorkspaceItem.class, row.getIntColumn("workspace_item_id")); + + // not in cache? turn row into workspaceitem + if (wi == null) + { + wi = new WorkspaceItem(context, row); + } + + wsItems.add(wi); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - // close the TableRowIterator to free up resources - tri.close(); WorkspaceItem[] wsArray = new WorkspaceItem[wsItems.size()]; wsArray = (WorkspaceItem[]) wsItems.toArray(wsArray); @@ -397,24 +411,31 @@ public class WorkspaceItem implements InProgressSubmission "workspaceitem", query); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // Check the cache - WorkspaceItem wi = (WorkspaceItem) context.fromCache( - WorkspaceItem.class, row.getIntColumn("workspace_item_id")); - - // not in cache? turn row into workspaceitem - if (wi == null) + while (tri.hasNext()) { - wi = new WorkspaceItem(context, row); + TableRow row = tri.next(); + + // Check the cache + WorkspaceItem wi = (WorkspaceItem) context.fromCache( + WorkspaceItem.class, row.getIntColumn("workspace_item_id")); + + // not in cache? turn row into workspaceitem + if (wi == null) + { + wi = new WorkspaceItem(context, row); + } + + wsItems.add(wi); } - - wsItems.add(wi); } - - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } WorkspaceItem[] wsArray = new WorkspaceItem[wsItems.size()]; wsArray = (WorkspaceItem[]) wsItems.toArray(wsArray); diff --git a/dspace-api/src/main/java/org/dspace/content/crosswalk/MODSDisseminationCrosswalk.java b/dspace-api/src/main/java/org/dspace/content/crosswalk/MODSDisseminationCrosswalk.java index a656eac505..ea93483566 100644 --- a/dspace-api/src/main/java/org/dspace/content/crosswalk/MODSDisseminationCrosswalk.java +++ b/dspace-api/src/main/java/org/dspace/content/crosswalk/MODSDisseminationCrosswalk.java @@ -260,9 +260,11 @@ public class MODSDisseminationCrosswalk extends SelfNamedPlugin File.separator + "config" + File.separator; File propsFile = new File(parent, propsFilename); Properties modsConfig = new Properties(); + FileInputStream pfs = null; try { - modsConfig.load(new FileInputStream(propsFile)); + pfs = new FileInputStream(propsFile); + modsConfig.load(pfs); } catch (IOException e) { @@ -270,6 +272,12 @@ public class MODSDisseminationCrosswalk extends SelfNamedPlugin throw new CrosswalkInternalException("MODS crosswalk cannot "+ "open config file: "+e.toString()); } + finally + { + if (pfs != null) + try { pfs.close(); } catch (IOException ioe) { } + } + modsMap = new HashMap(); Enumeration pe = modsConfig.propertyNames(); while (pe.hasMoreElements()) diff --git a/dspace-api/src/main/java/org/dspace/content/crosswalk/QDCCrosswalk.java b/dspace-api/src/main/java/org/dspace/content/crosswalk/QDCCrosswalk.java index 9159e62b4e..198e6ee0e3 100644 --- a/dspace-api/src/main/java/org/dspace/content/crosswalk/QDCCrosswalk.java +++ b/dspace-api/src/main/java/org/dspace/content/crosswalk/QDCCrosswalk.java @@ -280,7 +280,17 @@ public class QDCCrosswalk extends SelfNamedPlugin File.separator + "config" + File.separator; File propsFile = new File(parent, propsFilename); Properties qdcProps = new Properties(); - qdcProps.load(new FileInputStream(propsFile)); + FileInputStream pfs = null; + try + { + pfs = new FileInputStream(propsFile); + qdcProps.load(pfs); + } + finally + { + if (pfs != null) + try { pfs.close(); } catch (IOException ioe) { } + } // grovel properties to initialize qdc->element and element->qdc maps. // evaluate the XML fragment with a wrapper including namespaces. diff --git a/dspace-api/src/main/java/org/dspace/content/crosswalk/XHTMLHeadDisseminationCrosswalk.java b/dspace-api/src/main/java/org/dspace/content/crosswalk/XHTMLHeadDisseminationCrosswalk.java index 7bd9baf71e..6437b2d715 100644 --- a/dspace-api/src/main/java/org/dspace/content/crosswalk/XHTMLHeadDisseminationCrosswalk.java +++ b/dspace-api/src/main/java/org/dspace/content/crosswalk/XHTMLHeadDisseminationCrosswalk.java @@ -120,7 +120,16 @@ public class XHTMLHeadDisseminationCrosswalk extends SelfNamedPlugin implements // Read in configuration Properties crosswalkProps = new Properties(); - crosswalkProps.load(new FileInputStream(config)); + FileInputStream fis = new FileInputStream(config); + try + { + crosswalkProps.load(fis); + } + finally + { + if (fis != null) + try { fis.close(); } catch (IOException ioe) { } + } Enumeration e = crosswalkProps.keys(); while (e.hasMoreElements()) diff --git a/dspace-api/src/main/java/org/dspace/core/ConfigurationManager.java b/dspace-api/src/main/java/org/dspace/core/ConfigurationManager.java index 84fb8b8240..dc175aeb8e 100644 --- a/dspace-api/src/main/java/org/dspace/core/ConfigurationManager.java +++ b/dspace-api/src/main/java/org/dspace/core/ConfigurationManager.java @@ -177,9 +177,12 @@ public class ConfigurationManager { // Load in default license + FileReader fr = null; + BufferedReader br = null; try { - BufferedReader br = new BufferedReader(new FileReader(licenseFile)); + fr = new FileReader(licenseFile); + br = new BufferedReader(fr); String lineIn; license = ""; while ((lineIn = br.readLine()) != null) @@ -195,6 +198,15 @@ public class ConfigurationManager // configuration we can't do anything System.exit(1); } + finally + { + if (br != null) + try { br.close(); } catch (IOException ioe) { } + + if (fr != null) + try { fr.close(); } catch (IOException ioe) { } + } + return license; } @@ -480,7 +492,8 @@ public class ConfigurationManager protected static File getConfigurationFile() { // in case it hasn't been done yet. - loadConfig(null); + if (loadedFile == null) + loadConfig(null); return loadedFile; } @@ -494,7 +507,7 @@ public class ConfigurationManager * The dspace.cfg configuration file to use, or * null to try default locations */ - public static void loadConfig(String configFile) + public static synchronized void loadConfig(String configFile) { if (properties != null) @@ -505,6 +518,7 @@ public class ConfigurationManager URL url = null; + InputStream is = null; try { String configProperty = null; @@ -556,7 +570,8 @@ public class ConfigurationManager else { properties = new Properties(); - properties.load(url.openStream()); + is = url.openStream(); + properties.load(is); // walk values, interpolating any embedded references. for (Enumeration pe = properties.propertyNames(); pe.hasMoreElements(); ) @@ -577,16 +592,25 @@ public class ConfigurationManager // configuration we can't do anything throw new RuntimeException("Cannot load configuration: " + url, e); } - + finally + { + if (is != null) + try { is.close(); } catch (IOException ioe) { } + } + // Load in default license File licenseFile = new File(getProperty("dspace.dir") + File.separator + "config" + File.separator + "default.license"); + + FileInputStream fir = null; + InputStreamReader ir = null; + BufferedReader br = null; try { - FileInputStream fir = new FileInputStream(licenseFile); - InputStreamReader ir = new InputStreamReader(fir, "UTF-8"); - BufferedReader br = new BufferedReader(ir); + fir = new FileInputStream(licenseFile); + ir = new InputStreamReader(fir, "UTF-8"); + br = new BufferedReader(ir); String lineIn; license = ""; @@ -605,7 +629,18 @@ public class ConfigurationManager // FIXME: Maybe something more graceful here, but with the // configuration we can't do anything throw new RuntimeException("Cannot load license: " + licenseFile.toString(),e); - } + } + finally + { + if (br != null) + try { br.close(); } catch (IOException ioe) { } + + if (ir != null) + try { ir.close(); } catch (IOException ioe) { } + + if (fir != null) + try { fir.close(); } catch (IOException ioe) { } + } diff --git a/dspace-api/src/main/java/org/dspace/core/LogManager.java b/dspace-api/src/main/java/org/dspace/core/LogManager.java index 95e317a0fa..b5a9ac0dff 100644 --- a/dspace-api/src/main/java/org/dspace/core/LogManager.java +++ b/dspace-api/src/main/java/org/dspace/core/LogManager.java @@ -84,9 +84,8 @@ public class LogManager contextExtraInfo = "no_context"; } - String result = new String(email + ":" + contextExtraInfo + ":" - + action + ":" + extrainfo); - - return result; + StringBuilder result = new StringBuilder(); + result.append(email).append(":").append(contextExtraInfo).append(":").append(action).append(":").append(extrainfo); + return result.toString(); } } diff --git a/dspace-api/src/main/java/org/dspace/core/PluginManager.java b/dspace-api/src/main/java/org/dspace/core/PluginManager.java index 5b71e30e66..377286f2b1 100755 --- a/dspace-api/src/main/java/org/dspace/core/PluginManager.java +++ b/dspace-api/src/main/java/org/dspace/core/PluginManager.java @@ -564,6 +564,9 @@ public class PluginManager public static void checkConfiguration() throws IOException { + FileReader fr = null; + BufferedReader cr = null; + /* XXX TODO: (maybe) test that implementation class is really a * subclass or impl of the plugin "interface" */ @@ -574,51 +577,63 @@ public class PluginManager Map namedKey = new HashMap(); Map selfnamedKey = new HashMap(); Map reusableKey = new HashMap(); + HashMap keyMap = new HashMap(); // 1. First pass -- grovel the actual config file to check for // duplicate keys, since Properties class hides them from us. // Also build lists of each type of key, check for misspellings. File config = ConfigurationManager.getConfigurationFile(); - BufferedReader cr = new BufferedReader(new FileReader(config)); - String line = null; - boolean continued = false; - HashMap keyMap = new HashMap(); - Pattern keyPattern = Pattern.compile("([^\\s\\=\\:]+)"); - while ((line = cr.readLine()) != null) + try { - line = line.trim(); - if (line.startsWith("!") || line.startsWith("#")) - continued = false; - else + fr = new FileReader(config); + cr = new BufferedReader(fr); + String line = null; + boolean continued = false; + Pattern keyPattern = Pattern.compile("([^\\s\\=\\:]+)"); + while ((line = cr.readLine()) != null) { - if (!continued && line.startsWith("plugin.")) + line = line.trim(); + if (line.startsWith("!") || line.startsWith("#")) + continued = false; + else { - Matcher km = keyPattern.matcher(line); - if (km.find()) + if (!continued && line.startsWith("plugin.")) { - String key = line.substring(0, km.end(1)); - if (keyMap.containsKey(key)) - log.error("Duplicate key \""+key+"\" in DSpace configuration file="+config.toString()); - else - keyMap.put(key, key); + Matcher km = keyPattern.matcher(line); + if (km.find()) + { + String key = line.substring(0, km.end(1)); + if (keyMap.containsKey(key)) + log.error("Duplicate key \""+key+"\" in DSpace configuration file="+config.toString()); + else + keyMap.put(key, key); - if (key.startsWith(SINGLE_PREFIX)) - singleKey.put(key.substring(SINGLE_PREFIX.length()), key); - else if (key.startsWith(SEQUENCE_PREFIX)) - sequenceKey.put(key.substring(SEQUENCE_PREFIX.length()), key); - else if (key.startsWith(NAMED_PREFIX)) - namedKey.put(key.substring(NAMED_PREFIX.length()), key); - else if (key.startsWith(SELFNAMED_PREFIX)) - selfnamedKey.put(key.substring(SELFNAMED_PREFIX.length()), key); - else if (key.startsWith(REUSABLE_PREFIX)) - reusableKey.put(key.substring(REUSABLE_PREFIX.length()), key); - else - log.error("Key with unknown prefix \""+key+"\" in DSpace configuration file="+config.toString()); + if (key.startsWith(SINGLE_PREFIX)) + singleKey.put(key.substring(SINGLE_PREFIX.length()), key); + else if (key.startsWith(SEQUENCE_PREFIX)) + sequenceKey.put(key.substring(SEQUENCE_PREFIX.length()), key); + else if (key.startsWith(NAMED_PREFIX)) + namedKey.put(key.substring(NAMED_PREFIX.length()), key); + else if (key.startsWith(SELFNAMED_PREFIX)) + selfnamedKey.put(key.substring(SELFNAMED_PREFIX.length()), key); + else if (key.startsWith(REUSABLE_PREFIX)) + reusableKey.put(key.substring(REUSABLE_PREFIX.length()), key); + else + log.error("Key with unknown prefix \""+key+"\" in DSpace configuration file="+config.toString()); + } } + continued = line.length() > 0 && line.charAt(line.length()-1) == '\\'; } - continued = line.length() > 0 && line.charAt(line.length()-1) == '\\'; } } + finally + { + if (cr != null) + try { cr.close(); } catch (IOException ioe) { } + + if (fr != null) + try { fr.close(); } catch (IOException ioe) { } + } // 1.1 Sanity check, make sure keyMap == set of keys from Configuration Enumeration pne = ConfigurationManager.propertyNames(); diff --git a/dspace-api/src/main/java/org/dspace/eperson/EPerson.java b/dspace-api/src/main/java/org/dspace/eperson/EPerson.java index ab44efff68..0d7f7a884b 100644 --- a/dspace-api/src/main/java/org/dspace/eperson/EPerson.java +++ b/dspace-api/src/main/java/org/dspace/eperson/EPerson.java @@ -908,36 +908,57 @@ public class EPerson extends DSpaceObject "SELECT * from item where submitter_id= ? ", getID()); - if (tri.hasNext()) + try { - tableList.add("item"); + if (tri.hasNext()) + { + tableList.add("item"); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - tri.close(); - // check for eperson in workflowitem table tri = DatabaseManager.query(myContext, "SELECT * from workflowitem where owner= ? ", getID()); - if (tri.hasNext()) + try { - tableList.add("workflowitem"); + if (tri.hasNext()) + { + tableList.add("workflowitem"); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - tri.close(); - // check for eperson in tasklistitem table tri = DatabaseManager.query(myContext, "SELECT * from tasklistitem where eperson_id= ? ", getID()); - if (tri.hasNext()) + try { - tableList.add("tasklistitem"); + if (tri.hasNext()) + { + tableList.add("tasklistitem"); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - - tri.close(); // the list of tables can be used to construct an error message // explaining to the user why the eperson cannot be deleted. diff --git a/dspace-api/src/main/java/org/dspace/eperson/Group.java b/dspace-api/src/main/java/org/dspace/eperson/Group.java index 201cb7aec0..0191f78fb8 100644 --- a/dspace-api/src/main/java/org/dspace/eperson/Group.java +++ b/dspace-api/src/main/java/org/dspace/eperson/Group.java @@ -141,26 +141,33 @@ public class Group extends DSpaceObject "epersongroup2eperson.eperson_group_id= ?", myRow.getIntColumn("eperson_group_id")); - while (tri.hasNext()) + try { - TableRow r = (TableRow) tri.next(); - - // First check the cache - EPerson fromCache = (EPerson) myContext.fromCache( - EPerson.class, r.getIntColumn("eperson_id")); - - if (fromCache != null) + while (tri.hasNext()) { - epeople.add(fromCache); - } - else - { - epeople.add(new EPerson(myContext, r)); + TableRow r = (TableRow) tri.next(); + + // First check the cache + EPerson fromCache = (EPerson) myContext.fromCache( + EPerson.class, r.getIntColumn("eperson_id")); + + if (fromCache != null) + { + epeople.add(fromCache); + } + else + { + epeople.add(new EPerson(myContext, r)); + } } } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } - tri.close(); - // now get Group objects tri = DatabaseManager.queryTable(myContext,"epersongroup", "SELECT epersongroup.* FROM epersongroup, group2group WHERE " + @@ -168,25 +175,32 @@ public class Group extends DSpaceObject "group2group.parent_id= ? ", myRow.getIntColumn("eperson_group_id")); - while (tri.hasNext()) + try { - TableRow r = (TableRow) tri.next(); - - // First check the cache - Group fromCache = (Group) myContext.fromCache(Group.class, - r.getIntColumn("eperson_group_id")); - - if (fromCache != null) + while (tri.hasNext()) { - groups.add(fromCache); - } - else - { - groups.add(new Group(myContext, r)); + TableRow r = (TableRow) tri.next(); + + // First check the cache + Group fromCache = (Group) myContext.fromCache(Group.class, + r.getIntColumn("eperson_group_id")); + + if (fromCache != null) + { + groups.add(fromCache); + } + else + { + groups.add(new Group(myContext, r)); + } } } - - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } } catch (Exception e) @@ -449,16 +463,23 @@ public class Group extends DSpaceObject Set groupIDs = new HashSet(); - while (tri.hasNext()) + try { - TableRow row = tri.next(); + while (tri.hasNext()) + { + TableRow row = tri.next(); - int childID = row.getIntColumn("eperson_group_id"); + int childID = row.getIntColumn("eperson_group_id"); - groupIDs.add(new Integer(childID)); + groupIDs.add(new Integer(childID)); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - - tri.close(); // Also need to get all "Special Groups" user is a member of! // Otherwise, you're ignoring the user's membership to these groups! @@ -501,16 +522,23 @@ public class Group extends DSpaceObject "SELECT * FROM group2groupcache WHERE " + groupQuery, parameters); - while (tri.hasNext()) + try { - TableRow row = tri.next(); + while (tri.hasNext()) + { + TableRow row = tri.next(); - int parentID = row.getIntColumn("parent_id"); + int parentID = row.getIntColumn("parent_id"); - groupIDs.add(new Integer(parentID)); + groupIDs.add(new Integer(parentID)); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - - tri.close(); return groupIDs; } @@ -570,16 +598,23 @@ public class Group extends DSpaceObject Set groupIDs = new HashSet(); - while (tri.hasNext()) + try { - TableRow row = tri.next(); + while (tri.hasNext()) + { + TableRow row = tri.next(); - int childID = row.getIntColumn("child_id"); + int childID = row.getIntColumn("child_id"); - groupIDs.add(new Integer(childID)); + groupIDs.add(new Integer(childID)); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - - tri.close(); // now we have all the groups (including this one) // it is time to find all the EPeople who belong to those groups @@ -612,16 +647,23 @@ public class Group extends DSpaceObject "SELECT * FROM epersongroup2eperson WHERE " + epersonQuery, parameters); - while (tri.hasNext()) + try { - TableRow row = tri.next(); + while (tri.hasNext()) + { + TableRow row = tri.next(); - int epersonID = row.getIntColumn("eperson_id"); - - epeopleIDs.add(new Integer(epersonID)); + int epersonID = row.getIntColumn("eperson_id"); + + epeopleIDs.add(new Integer(epersonID)); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - - tri.close(); return epeopleIDs; } @@ -1135,32 +1177,39 @@ public class Group extends DSpaceObject Map> parents = new HashMap>(); - while (tri.hasNext()) + try { - TableRow row = (TableRow) tri.next(); - - Integer parentID = new Integer(row.getIntColumn("parent_id")); - Integer childID = new Integer(row.getIntColumn("child_id")); - - // if parent doesn't have an entry, create one - if (!parents.containsKey(parentID)) + while (tri.hasNext()) { - Set children = new HashSet(); + TableRow row = (TableRow) tri.next(); - // add child id to the list - children.add(childID); - parents.put(parentID, children); - } - else - { - // parent has an entry, now add the child to the parent's record - // of children - Set children = parents.get(parentID); - children.add(childID); + Integer parentID = new Integer(row.getIntColumn("parent_id")); + Integer childID = new Integer(row.getIntColumn("child_id")); + + // if parent doesn't have an entry, create one + if (!parents.containsKey(parentID)) + { + Set children = new HashSet(); + + // add child id to the list + children.add(childID); + parents.put(parentID, children); + } + else + { + // parent has an entry, now add the child to the parent's record + // of children + Set children = parents.get(parentID); + children.add(childID); + } } } - - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } // now parents is a hash of all of the IDs of groups that are parents // and each hash entry is a hash of all of the IDs of children of those diff --git a/dspace-api/src/main/java/org/dspace/eperson/Subscribe.java b/dspace-api/src/main/java/org/dspace/eperson/Subscribe.java index e6e333e8bc..144d1f949b 100644 --- a/dspace-api/src/main/java/org/dspace/eperson/Subscribe.java +++ b/dspace-api/src/main/java/org/dspace/eperson/Subscribe.java @@ -112,20 +112,27 @@ public class Subscribe " AND collection_id= ? ", eperson.getID(),collection.getID()); - if (!r.hasNext()) + try { - // Not subscribed, so add them - TableRow row = DatabaseManager.create(context, "subscription"); - row.setColumn("eperson_id", eperson.getID()); - row.setColumn("collection_id", collection.getID()); - DatabaseManager.update(context, row); + if (!r.hasNext()) + { + // Not subscribed, so add them + TableRow row = DatabaseManager.create(context, "subscription"); + row.setColumn("eperson_id", eperson.getID()); + row.setColumn("collection_id", collection.getID()); + DatabaseManager.update(context, row); - log.info(LogManager.getHeader(context, "subscribe", - "eperson_id=" + eperson.getID() + ",collection_id=" - + collection.getID())); + log.info(LogManager.getHeader(context, "subscribe", + "eperson_id=" + eperson.getID() + ",collection_id=" + + collection.getID())); + } + } + finally + { + // close the TableRowIterator to free up resources + if (r != null) + r.close(); } - - r.close(); } else { @@ -198,15 +205,22 @@ public class Subscribe List collections = new ArrayList(); - while (tri.hasNext()) + try { - TableRow row = tri.next(); + while (tri.hasNext()) + { + TableRow row = tri.next(); - collections.add(Collection.find(context, row - .getIntColumn("collection_id"))); + collections.add(Collection.find(context, row + .getIntColumn("collection_id"))); + } + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); } - - tri.close(); Collection[] collArray = new Collection[collections.size()]; @@ -231,11 +245,17 @@ public class Subscribe "SELECT * FROM subscription WHERE eperson_id= ? " + "AND collection_id= ? ", eperson.getID(),collection.getID()); - - boolean result = tri.hasNext(); - tri.close(); - - return result; + + try + { + return tri.hasNext(); + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } } /** @@ -265,42 +285,49 @@ public class Subscribe EPerson currentEPerson = null; List collections = null; // List of Collections - // Go through the list collating subscriptions for each e-person - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // Does this row relate to the same e-person as the last? - if ((currentEPerson == null) - || (row.getIntColumn("eperson_id") != currentEPerson - .getID())) + // Go through the list collating subscriptions for each e-person + while (tri.hasNext()) { - // New e-person. Send mail for previous e-person - if (currentEPerson != null) - { + TableRow row = tri.next(); - try + // Does this row relate to the same e-person as the last? + if ((currentEPerson == null) + || (row.getIntColumn("eperson_id") != currentEPerson + .getID())) + { + // New e-person. Send mail for previous e-person + if (currentEPerson != null) { - sendEmail(context, currentEPerson, collections, test); - } - catch (MessagingException me) - { - log.error("Failed to send subscription to eperson_id=" - + currentEPerson.getID()); - log.error(me); + + try + { + sendEmail(context, currentEPerson, collections, test); + } + catch (MessagingException me) + { + log.error("Failed to send subscription to eperson_id=" + + currentEPerson.getID()); + log.error(me); + } } + + currentEPerson = EPerson.find(context, row + .getIntColumn("eperson_id")); + collections = new ArrayList(); } - currentEPerson = EPerson.find(context, row - .getIntColumn("eperson_id")); - collections = new ArrayList(); + collections.add(Collection.find(context, row + .getIntColumn("collection_id"))); } - - collections.add(Collection.find(context, row - .getIntColumn("collection_id"))); } - - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } // Process the last person if (currentEPerson != null) diff --git a/dspace-api/src/main/java/org/dspace/eperson/Supervisor.java b/dspace-api/src/main/java/org/dspace/eperson/Supervisor.java index afd9c65815..66adabce6d 100644 --- a/dspace-api/src/main/java/org/dspace/eperson/Supervisor.java +++ b/dspace-api/src/main/java/org/dspace/eperson/Supervisor.java @@ -108,10 +108,17 @@ public class Supervisor { TableRowIterator tri = DatabaseManager.queryTable(context, "epersongroup2workspaceitem", query,groupID,wsItemID); - - boolean result = tri.hasNext(); - tri.close(); - return result; + + try + { + return tri.hasNext(); + } + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } } /** diff --git a/dspace-api/src/main/java/org/dspace/handle/HandleManager.java b/dspace-api/src/main/java/org/dspace/handle/HandleManager.java index 0231af4c15..2ff23544e9 100644 --- a/dspace-api/src/main/java/org/dspace/handle/HandleManager.java +++ b/dspace-api/src/main/java/org/dspace/handle/HandleManager.java @@ -327,13 +327,20 @@ public class HandleManager TableRowIterator iterator = DatabaseManager.queryTable(context, null, sql, prefix+"%"); List results = new ArrayList(); - while (iterator.hasNext()) + try { - TableRow row = (TableRow) iterator.next(); - results.add(row.getStringColumn("handle")); + while (iterator.hasNext()) + { + TableRow row = (TableRow) iterator.next(); + results.add(row.getStringColumn("handle")); + } + } + finally + { + // close the TableRowIterator to free up resources + if (iterator != null) + iterator.close(); } - - iterator.close(); return results; } diff --git a/dspace-api/src/main/java/org/dspace/search/Harvest.java b/dspace-api/src/main/java/org/dspace/search/Harvest.java index a81a4de65b..757d4bb38d 100644 --- a/dspace-api/src/main/java/org/dspace/search/Harvest.java +++ b/dspace-api/src/main/java/org/dspace/search/Harvest.java @@ -228,43 +228,51 @@ public class Harvest List infoObjects = new LinkedList(); int index = 0; - // Process results of query into HarvestedItemInfo objects - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - /* - * This conditional ensures that we only process items within any - * constraints specified by 'offset' and 'limit' parameters. - */ - if ((index >= offset) - && ((limit == 0) || (index < (offset + limit)))) + // Process results of query into HarvestedItemInfo objects + while (tri.hasNext()) { - HarvestedItemInfo itemInfo = new HarvestedItemInfo(); - - itemInfo.context = context; - itemInfo.handle = row.getStringColumn("handle"); - itemInfo.itemID = row.getIntColumn("resource_id"); - itemInfo.datestamp = row.getDateColumn("last_modified"); - itemInfo.withdrawn = row.getBooleanColumn("withdrawn"); + TableRow row = tri.next(); - if (collections) + /* + * This conditional ensures that we only process items within any + * constraints specified by 'offset' and 'limit' parameters. + */ + if ((index >= offset) + && ((limit == 0) || (index < (offset + limit)))) { - fillCollections(context, itemInfo); + HarvestedItemInfo itemInfo = new HarvestedItemInfo(); + + itemInfo.context = context; + itemInfo.handle = row.getStringColumn("handle"); + itemInfo.itemID = row.getIntColumn("resource_id"); + itemInfo.datestamp = row.getDateColumn("last_modified"); + itemInfo.withdrawn = row.getBooleanColumn("withdrawn"); + + if (collections) + { + fillCollections(context, itemInfo); + } + + if (items) + { + // Get the item + itemInfo.item = Item.find(context, itemInfo.itemID); + } + + infoObjects.add(itemInfo); } - if (items) - { - // Get the item - itemInfo.item = Item.find(context, itemInfo.itemID); - } - - infoObjects.add(itemInfo); + index++; } - - index++; } - tri.close(); + finally + { + // close the TableRowIterator to free up resources + if (tri != null) + tri.close(); + } return infoObjects; } diff --git a/dspace-api/src/main/java/org/dspace/sort/OrderFormat.java b/dspace-api/src/main/java/org/dspace/sort/OrderFormat.java index 06d8e3e684..d2a702418c 100644 --- a/dspace-api/src/main/java/org/dspace/sort/OrderFormat.java +++ b/dspace-api/src/main/java/org/dspace/sort/OrderFormat.java @@ -107,24 +107,24 @@ public class OrderFormat { return delegate.makeSortString(value, language); } - } - - // No delegates found, so apply defaults - if (type.equalsIgnoreCase(OrderFormat.AUTHOR) && authorDelegate != null) - { - return authorDelegate.makeSortString(value, language); + + // No delegates found, so apply defaults + if (type.equalsIgnoreCase(OrderFormat.AUTHOR) && authorDelegate != null) + { + return authorDelegate.makeSortString(value, language); + } + + if (type.equalsIgnoreCase(OrderFormat.TITLE) && titleDelegate != null) + { + return titleDelegate.makeSortString(value, language); + } + + if (type.equalsIgnoreCase(OrderFormat.TEXT) && textDelegate != null) + { + return textDelegate.makeSortString(value, language); + } } - if (type.equalsIgnoreCase(OrderFormat.TITLE) && titleDelegate != null) - { - return titleDelegate.makeSortString(value, language); - } - - if (type.equalsIgnoreCase(OrderFormat.TEXT) && textDelegate != null) - { - return textDelegate.makeSortString(value, language); - } - return value; } 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 2033dab581..b26f2ca8ad 100644 --- a/dspace-api/src/main/java/org/dspace/sort/SortOption.java +++ b/dspace-api/src/main/java/org/dspace/sort/SortOption.java @@ -304,16 +304,21 @@ public class SortOption { if (SortOption.sortOptionsMap != null) return SortOption.sortOptionsMap; - - SortOption.sortOptionsMap = new HashMap(); - synchronized (SortOption.sortOptionsMap) + + synchronized (SortOption.class) { - for (SortOption so : SortOption.getSortOptions()) + if (SortOption.sortOptionsMap == null) { - SortOption.sortOptionsMap.put(new Integer(so.getNumber()), so); + Map newSortOptionsMap = new HashMap(); + for (SortOption so : SortOption.getSortOptions()) + { + newSortOptionsMap.put(new Integer(so.getNumber()), so); + } + + SortOption.sortOptionsMap = newSortOptionsMap; } } - + return SortOption.sortOptionsMap; } @@ -327,17 +332,22 @@ public class SortOption if (SortOption.sortOptionsSet != null) return SortOption.sortOptionsSet; - SortOption.sortOptionsSet = new HashSet(); - synchronized (SortOption.sortOptionsSet) + synchronized (SortOption.class) { - int idx = 1; - String option; - - while ( ((option = ConfigurationManager.getProperty("webui.itemlist.sort-option." + idx))) != null) + if (SortOption.sortOptionsSet == null) { - SortOption so = new SortOption(idx, option); - SortOption.sortOptionsSet.add(so); - idx++; + 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; } } diff --git a/dspace-api/src/main/java/org/dspace/storage/bitstore/BitstreamStorageManager.java b/dspace-api/src/main/java/org/dspace/storage/bitstore/BitstreamStorageManager.java index 69361b4be2..4b2435f181 100644 --- a/dspace-api/src/main/java/org/dspace/storage/bitstore/BitstreamStorageManager.java +++ b/dspace-api/src/main/java/org/dspace/storage/bitstore/BitstreamStorageManager.java @@ -352,9 +352,13 @@ public class BitstreamStorageManager bitstream.setColumn("size_bytes", file.length()); - bitstream.setColumn("checksum", Utils.toHex(dis.getMessageDigest() - .digest())); - bitstream.setColumn("checksum_algorithm", "MD5"); + if (dis != null) + { + bitstream.setColumn("checksum", Utils.toHex(dis.getMessageDigest() + .digest())); + bitstream.setColumn("checksum_algorithm", "MD5"); + } + bitstream.setColumn("deleted", false); DatabaseManager.update(context, bitstream); diff --git a/dspace-api/src/main/java/org/dspace/storage/rdbms/DatabaseManager.java b/dspace-api/src/main/java/org/dspace/storage/rdbms/DatabaseManager.java index a01b954932..6a2e02b4fe 100644 --- a/dspace-api/src/main/java/org/dspace/storage/rdbms/DatabaseManager.java +++ b/dspace-api/src/main/java/org/dspace/storage/rdbms/DatabaseManager.java @@ -276,10 +276,19 @@ public class DatabaseManager public static TableRow querySingle(Context context, String query, Object... parameters) throws SQLException { - TableRowIterator iterator = query(context, query, parameters); + TableRow retRow = null; + TableRowIterator iterator = null; + try + { + iterator = query(context, query, parameters); + retRow = (!iterator.hasNext()) ? null : iterator.next(); + } + finally + { + if (iterator != null) + iterator.close(); + } - TableRow retRow = (!iterator.hasNext()) ? null : iterator.next(); - iterator.close(); return (retRow); } @@ -304,10 +313,18 @@ public class DatabaseManager public static TableRow querySingleTable(Context context, String table, String query, Object... parameters) throws SQLException { + TableRow retRow = null; TableRowIterator iterator = queryTable(context, canonicalize(table), query, parameters); - TableRow retRow = (!iterator.hasNext()) ? null : iterator.next(); - iterator.close(); + try + { + retRow = (!iterator.hasNext()) ? null : iterator.next(); + } + finally + { + if (iterator != null) + iterator.close(); + } return (retRow); } @@ -564,26 +581,44 @@ public class DatabaseManager public static void insert(Context context, TableRow row) throws SQLException { + int newID = -1; String table = canonicalize(row.getTable()); + Statement statement = null; + ResultSet rs = null; - // Get an ID (primary key) for this row by using the "getnextid" - // SQL function in Postgres, or directly with sequences in Oracle - String myQuery = "SELECT getnextid('" + table + "') AS result"; - - if ("oracle".equals(ConfigurationManager.getProperty("db.name"))) + try { - myQuery = "SELECT " + table + "_seq" + ".nextval FROM dual"; + // Get an ID (primary key) for this row by using the "getnextid" + // SQL function in Postgres, or directly with sequences in Oracle + String myQuery = "SELECT getnextid('" + table + "') AS result"; + + if ("oracle".equals(ConfigurationManager.getProperty("db.name"))) + { + myQuery = "SELECT " + table + "_seq" + ".nextval FROM dual"; + } + + statement = context.getDBConnection().createStatement(); + rs = statement.executeQuery(myQuery); + + rs.next(); + + newID = rs.getInt(1); + } + finally + { + if (rs != null) + { + try { rs.close(); } catch (SQLException sqle) { } + } + + if (statement != null) + { + try { statement.close(); } catch (SQLException sqle) { } + } } - Statement statement = context.getDBConnection().createStatement(); - ResultSet rs = statement.executeQuery(myQuery); - - rs.next(); - - int newID = rs.getInt(1); - rs.close(); - - statement.close(); + if (newID < 0) + throw new SQLException("Unable to retrieve sequence ID"); // Set the ID in the table row object row.setColumn(getPrimaryKeyColumn(table), newID); @@ -1354,7 +1389,9 @@ public class DatabaseManager private static Map retrieveColumnInfo(String table) throws SQLException { Connection connection = null; - + ResultSet pkcolumns = null; + ResultSet columns = null; + try { String schema = ConfigurationManager.getProperty("db.schema"); @@ -1367,13 +1404,13 @@ public class DatabaseManager String tname = (table.length() >= max) ? table .substring(0, max - 1) : table; - ResultSet pkcolumns = metadata.getPrimaryKeys(null, schema, tname); + pkcolumns = metadata.getPrimaryKeys(null, schema, tname); Set pks = new HashSet(); while (pkcolumns.next()) pks.add(pkcolumns.getString(4)); - ResultSet columns = metadata.getColumns(null, schema, tname, null); + columns = metadata.getColumns(null, schema, tname, null); while (columns.next()) { @@ -1394,9 +1431,19 @@ public class DatabaseManager } finally { + if (pkcolumns != null) + { + try { pkcolumns.close(); } catch (SQLException sqle) { } + } + + if (columns != null) + { + try { columns.close(); } catch (SQLException sqle) { } + } + if (connection != null) { - connection.close(); + try { connection.close(); } catch (SQLException sqle) { } } } } diff --git a/dspace-api/src/main/java/org/dspace/storage/rdbms/TableRowIterator.java b/dspace-api/src/main/java/org/dspace/storage/rdbms/TableRowIterator.java index 4c0d0634cb..93e6313651 100644 --- a/dspace-api/src/main/java/org/dspace/storage/rdbms/TableRowIterator.java +++ b/dspace-api/src/main/java/org/dspace/storage/rdbms/TableRowIterator.java @@ -108,7 +108,7 @@ public class TableRowIterator /** * Finalize -- this method is called when this object is GC-ed. */ - public void finalize() + protected void finalize() { close(); } diff --git a/dspace-api/src/main/java/org/dspace/workflow/WorkflowItem.java b/dspace-api/src/main/java/org/dspace/workflow/WorkflowItem.java index 049ba576ae..1c186ec131 100644 --- a/dspace-api/src/main/java/org/dspace/workflow/WorkflowItem.java +++ b/dspace-api/src/main/java/org/dspace/workflow/WorkflowItem.java @@ -171,15 +171,21 @@ public class WorkflowItem implements InProgressSubmission TableRowIterator tri = DatabaseManager.queryTable(c, "workflowitem", "SELECT * FROM workflowitem"); - // make a list of workflow items - while (tri.hasNext()) + try { - TableRow row = tri.next(); - WorkflowItem wi = new WorkflowItem(c, row); - wfItems.add(wi); + // make a list of workflow items + while (tri.hasNext()) + { + TableRow row = tri.next(); + WorkflowItem wi = new WorkflowItem(c, row); + wfItems.add(wi); + } + } + finally + { + if (tri != null) + tri.close(); } - - tri.close(); WorkflowItem[] wfArray = new WorkflowItem[wfItems.size()]; wfArray = (WorkflowItem[]) wfItems.toArray(wfArray); @@ -211,23 +217,29 @@ public class WorkflowItem implements InProgressSubmission "ORDER BY workflowitem.workflow_id", ep.getID()); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // Check the cache - WorkflowItem wi = (WorkflowItem) context.fromCache( - WorkflowItem.class, row.getIntColumn("workflow_id")); - - if (wi == null) + while (tri.hasNext()) { - wi = new WorkflowItem(context, row); - } + TableRow row = tri.next(); - wfItems.add(wi); + // Check the cache + WorkflowItem wi = (WorkflowItem) context.fromCache( + WorkflowItem.class, row.getIntColumn("workflow_id")); + + if (wi == null) + { + wi = new WorkflowItem(context, row); + } + + wfItems.add(wi); + } + } + finally + { + if (tri != null) + tri.close(); } - - tri.close(); WorkflowItem[] wfArray = new WorkflowItem[wfItems.size()]; wfArray = (WorkflowItem[]) wfItems.toArray(wfArray); @@ -255,24 +267,30 @@ public class WorkflowItem implements InProgressSubmission "workflowitem.collection_id= ? ", c.getID()); - while (tri.hasNext()) + try { - TableRow row = tri.next(); - - // Check the cache - WorkflowItem wi = (WorkflowItem) context.fromCache( - WorkflowItem.class, row.getIntColumn("workflow_id")); - - // not in cache? turn row into workflowitem - if (wi == null) + while (tri.hasNext()) { - wi = new WorkflowItem(context, row); - } + TableRow row = tri.next(); - wsItems.add(wi); + // Check the cache + WorkflowItem wi = (WorkflowItem) context.fromCache( + WorkflowItem.class, row.getIntColumn("workflow_id")); + + // not in cache? turn row into workflowitem + if (wi == null) + { + wi = new WorkflowItem(context, row); + } + + wsItems.add(wi); + } + } + finally + { + if (tri != null) + tri.close(); } - - tri.close(); WorkflowItem[] wsArray = new WorkflowItem[wsItems.size()]; wsArray = (WorkflowItem[]) wsItems.toArray(wsArray); diff --git a/dspace-api/src/main/java/org/dspace/workflow/WorkflowManager.java b/dspace-api/src/main/java/org/dspace/workflow/WorkflowManager.java index a3459d87cb..f9ac1a450b 100644 --- a/dspace-api/src/main/java/org/dspace/workflow/WorkflowManager.java +++ b/dspace-api/src/main/java/org/dspace/workflow/WorkflowManager.java @@ -237,12 +237,18 @@ public class WorkflowManager TableRowIterator tri = DatabaseManager.queryTable(c, "workflowitem", myquery,e.getID()); - while (tri.hasNext()) + try { - mylist.add(new WorkflowItem(c, tri.next())); + while (tri.hasNext()) + { + mylist.add(new WorkflowItem(c, tri.next())); + } + } + finally + { + if (tri != null) + tri.close(); } - - tri.close(); return mylist; } @@ -265,12 +271,18 @@ public class WorkflowManager TableRowIterator tri = DatabaseManager .queryTable(c, "workflowitem", myquery, e.getID()); - while (tri.hasNext()) + try { - mylist.add(new WorkflowItem(c, tri.next())); + while (tri.hasNext()) + { + mylist.add(new WorkflowItem(c, tri.next())); + } + } + finally + { + if (tri != null) + tri.close(); } - - tri.close(); return mylist; } diff --git a/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/FeedServlet.java b/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/FeedServlet.java index b101c25173..ed7809ede1 100644 --- a/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/FeedServlet.java +++ b/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/FeedServlet.java @@ -190,7 +190,7 @@ public class FeedServlet extends DSpaceServlet //as long as this is not a site wide feed, //attempt to retrieve the Collection or Community object - if(!handle.equals(SITE_FEED_KEY)) + if(handle != null && !handle.equals(SITE_FEED_KEY)) { // Determine if handle is a valid reference dso = HandleManager.resolveToObject(context, handle); @@ -371,7 +371,7 @@ public class FeedServlet extends DSpaceServlet : HandleManager.getCanonicalForm(dso.getHandle()); // put in container-level data - channel.setDescription(description.replaceAll("\\p{Cntrl}", "")); + channel.setDescription(description == null ? "" : description.replaceAll("\\p{Cntrl}", "")); channel.setLink(objectUrl); //build channel title by passing in type and title String channelTitle = MessageFormat.format(labels.getString(clazz + ".feed.title"), diff --git a/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/StatisticsServlet.java b/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/StatisticsServlet.java index 466597c9e4..2bf0b947d1 100644 --- a/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/StatisticsServlet.java +++ b/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/StatisticsServlet.java @@ -122,6 +122,7 @@ public class StatisticsServlet extends org.dspace.app.webui.servlet.DSpaceServle HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException, AuthorizeException { + StringBuffer report = new StringBuffer(); String date = (String) request.getParameter("date"); request.setAttribute("date", date); @@ -135,123 +136,135 @@ public class StatisticsServlet extends org.dspace.app.webui.servlet.DSpaceServle FileInputStream fir = null; InputStreamReader ir = null; BufferedReader br = null; - - List monthsList = new ArrayList(); - - Pattern monthly = Pattern.compile("report-([0-9][0-9][0-9][0-9]-[0-9]+)\\.html"); - Pattern general = Pattern.compile("report-general-([0-9]+-[0-9]+-[0-9]+)\\.html"); - - // FIXME: this whole thing is horribly inflexible and needs serious - // work; but as a basic proof of concept will suffice - - // if no date is passed then we want to get the most recent general - // report - if (date == null) + + try { - request.setAttribute("general", new Boolean(true)); - - SimpleDateFormat sdf = new SimpleDateFormat("yyyy'-'M'-'dd"); - Date mostRecentDate = null; - + List monthsList = new ArrayList(); + + Pattern monthly = Pattern.compile("report-([0-9][0-9][0-9][0-9]-[0-9]+)\\.html"); + Pattern general = Pattern.compile("report-general-([0-9]+-[0-9]+-[0-9]+)\\.html"); + + // FIXME: this whole thing is horribly inflexible and needs serious + // work; but as a basic proof of concept will suffice + + // if no date is passed then we want to get the most recent general + // report + if (date == null) + { + request.setAttribute("general", new Boolean(true)); + + SimpleDateFormat sdf = new SimpleDateFormat("yyyy'-'M'-'dd"); + Date mostRecentDate = null; + + for (int i = 0; i < reports.length; i++) + { + Matcher matchGeneral = general.matcher(reports[i].getName()); + if (matchGeneral.matches()) + { + Date parsedDate = null; + + try + { + parsedDate = sdf.parse(matchGeneral.group(1).trim()); + } + catch (ParseException e) + { + // FIXME: currently no error handling + } + + if (mostRecentDate == null) + { + mostRecentDate = parsedDate; + reportFile = reports[i]; + } + + if (parsedDate != null && parsedDate.compareTo(mostRecentDate) > 0) + { + mostRecentDate = parsedDate; + reportFile = reports[i]; + } + } + } + } + + // if a date is passed then we want to get the file for that month + if (date != null) + { + String desiredReport = "report-" + date + ".html"; + + for (int i = 0; i < reports.length; i++) + { + if (reports[i].getName().equals(desiredReport)) + { + reportFile = reports[i]; + } + } + } + + if (reportFile == null) + { + JSPManager.showJSP(request, response, "statistics/no-report.jsp"); + } + + // finally, build the list of report dates + SimpleDateFormat sdf = new SimpleDateFormat("yyyy'-'M"); for (int i = 0; i < reports.length; i++) { - Matcher matchGeneral = general.matcher(reports[i].getName()); - if (matchGeneral.matches()) + Matcher matchReport = monthly.matcher(reports[i].getName()); + if (matchReport.matches()) { Date parsedDate = null; - try + try { - parsedDate = sdf.parse(matchGeneral.group(1).trim()); + parsedDate = sdf.parse(matchReport.group(1).trim()); } catch (ParseException e) { // FIXME: currently no error handling } - - if (mostRecentDate == null) - { - mostRecentDate = parsedDate; - reportFile = reports[i]; - } - - if (parsedDate.compareTo(mostRecentDate) > 0) - { - mostRecentDate = parsedDate; - reportFile = reports[i]; - } + + monthsList.add(parsedDate); } } - } - - // if a date is passed then we want to get the file for that month - if (date != null) - { - String desiredReport = "report-" + date + ".html"; - - for (int i = 0; i < reports.length; i++) + + Date[] months = new Date[monthsList.size()]; + months = (Date[]) monthsList.toArray(months); + + Arrays.sort(months); + + request.setAttribute("months", months); + + try { - if (reports[i].getName().equals(desiredReport)) - { - reportFile = reports[i]; - } + fir = new FileInputStream(reportFile.getPath()); + ir = new InputStreamReader(fir, "UTF-8"); + br = new BufferedReader(ir); } - } - - if (reportFile == null) - { - JSPManager.showJSP(request, response, "statistics/no-report.jsp"); - } - - // finally, build the list of report dates - SimpleDateFormat sdf = new SimpleDateFormat("yyyy'-'M"); - for (int i = 0; i < reports.length; i++) - { - Matcher matchReport = monthly.matcher(reports[i].getName()); - if (matchReport.matches()) + catch (IOException e) { - Date parsedDate = null; - - try - { - parsedDate = sdf.parse(matchReport.group(1).trim()); - } - catch (ParseException e) - { - // FIXME: currently no error handling - } - - monthsList.add(parsedDate); + // FIXME: no error handing yet + throw new RuntimeException(e.getMessage(),e); + } + + // FIXME: there's got to be a better way of doing this + String line = null; + while ((line = br.readLine()) != null) + { + report.append(line); } } - - Date[] months = new Date[monthsList.size()]; - months = (Date[]) monthsList.toArray(months); - - Arrays.sort(months); - - request.setAttribute("months", months); - - try - { - fir = new FileInputStream(reportFile.getPath()); - ir = new InputStreamReader(fir, "UTF-8"); - br = new BufferedReader(ir); - } - catch (IOException e) + finally { - // FIXME: no error handing yet - throw new RuntimeException(e.getMessage(),e); - } - - // FIXME: there's got to be a better way of doing this - StringBuffer report = new StringBuffer(); - String line = null; - while ((line = br.readLine()) != null) - { - report.append(line); + if (br != null) + try { br.close(); } catch (IOException ioe) { } + + if (ir != null) + try { ir.close(); } catch (IOException ioe) { } + + if (fir != null) + try { fir.close(); } catch (IOException ioe) { } } - // set the report to be displayed request.setAttribute("report", report.toString()); diff --git a/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/SuggestServlet.java b/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/SuggestServlet.java index 942cc08852..e3e8710edd 100644 --- a/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/SuggestServlet.java +++ b/dspace-jspui/dspace-jspui-api/src/main/java/org/dspace/app/webui/servlet/SuggestServlet.java @@ -93,7 +93,7 @@ public class SuggestServlet extends DSpaceServlet if (item != null) { DCValue[] titleDC = item.getDC("title", null, Item.ANY); - if (titleDC != null || titleDC.length > 0) + if (titleDC != null && titleDC.length > 0) { title = titleDC[0].value; } diff --git a/dspace-oai/dspace-oai-api/src/main/java/org/dspace/app/oai/DIDLCrosswalk.java b/dspace-oai/dspace-oai-api/src/main/java/org/dspace/app/oai/DIDLCrosswalk.java index 735236fc29..f78a2fcc8b 100644 --- a/dspace-oai/dspace-oai-api/src/main/java/org/dspace/app/oai/DIDLCrosswalk.java +++ b/dspace-oai/dspace-oai-api/src/main/java/org/dspace/app/oai/DIDLCrosswalk.java @@ -231,8 +231,21 @@ public class DIDLCrosswalk extends Crosswalk byte[] buffer = new byte[intSize]; Context contextl= new Context(); - BufferedInputStream bis=new BufferedInputStream(BitstreamStorageManager.retrieve(contextl,bitstreams[k].getID())); - int size=bis.read(buffer); + InputStream is = BitstreamStorageManager.retrieve(contextl,bitstreams[k].getID()); + BufferedInputStream bis = new BufferedInputStream(is); + try + { + int size=bis.read(buffer); + } + finally + { + if (bis != null) + try { bis.close(); } catch (IOException ioe) { } + + if (is != null) + try { is.close(); } catch (IOException ioe) { } + } + contextl.complete(); String encoding = new String(Base64.encodeBase64(buffer), "ASCII");