[DS-707] Stream clean up

git-svn-id: http://scm.dspace.org/svn/repo/dspace/trunk@5641 9c30dcfa-912a-0410-8fc2-9e0234be79fd
This commit is contained in:
Graham Triggs
2010-10-26 09:01:47 +00:00
parent a95a254bd2
commit 1d9d92c619
14 changed files with 186 additions and 81 deletions

View File

@@ -115,8 +115,19 @@ public class ItemArchive {
ItemArchive itarch = new ItemArchive();
itarch.dir = dir;
itarch.dirname = dir.getName();
InputStream is = new FileInputStream(new File(dir, DUBLIN_CORE_XML));
itarch.dtomList = MetadataUtilities.loadDublinCore(getDocumentBuilder(), is);
InputStream is = null;
try
{
is = new FileInputStream(new File(dir, DUBLIN_CORE_XML));
itarch.dtomList = MetadataUtilities.loadDublinCore(getDocumentBuilder(), is);
}
finally
{
if (is != null)
{
is.close();
}
}
ItemUpdate.pr("Loaded metadata with " + itarch.dtomList.size() + " fields");
if (itemField == null)

View File

@@ -303,10 +303,19 @@ public class METSExport
}
// Write the METS file
FileOutputStream out = new FileOutputStream(aipDir.toString()
+ java.io.File.separator + "mets.xml");
writeMETS(context, item, out, false);
out.close();
FileOutputStream out = null;
try
{
out = new FileOutputStream(aipDir.toString() + java.io.File.separator + "mets.xml");
writeMETS(context, item, out, false);
}
finally
{
if (out != null)
{
out.close();
}
}
// Write bitstreams
Bundle[] bundles = item.getBundles();

View File

@@ -37,6 +37,7 @@
*/
package org.dspace.app.util;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;
@@ -350,17 +351,30 @@ public class Util {
{
Properties constants = new Properties();
InputStream cis = null;
try
{
InputStream cis = Util.class.getResourceAsStream
("/META-INF/maven/org.dspace/dspace-api/pom.properties");
cis = Util.class.getResourceAsStream("/META-INF/maven/org.dspace/dspace-api/pom.properties");
constants.load(cis);
}
catch(Exception e)
{
log.error(e.getMessage(),e);
}
finally
{
if (cis != null)
{
try
{
cis.close();
}
catch (IOException e)
{
log.error("Unable to close input stream", e);
}
}
}
sourceVersion = constants.getProperty("version", "none");
}

View File

@@ -579,13 +579,11 @@ public final class BitstreamInfoDAO extends DAOSupport
{
ids.add(Integer.valueOf(rs.getInt(1)));
}
}
catch (SQLException e)
{
LOG.error("get item bitstreams " + e.getMessage(), e);
throw new RuntimeException(
"get item bitstreams. " + e.getMessage(), e);
throw new RuntimeException("get item bitstreams. " + e.getMessage(), e);
}
finally

View File

@@ -253,8 +253,7 @@ public class ChecksumHistoryDAO extends DAOSupport
try
{
update = conn
.prepareStatement("DELETE FROM checksum_history WHERE process_end_date<? AND result=?");
update = conn.prepareStatement("DELETE FROM checksum_history WHERE process_end_date<? AND result=?");
update.setTimestamp(1, new Timestamp(retentionDate.getTime()));
update.setString(2, result);
return update.executeUpdate();
@@ -285,8 +284,8 @@ public class ChecksumHistoryDAO extends DAOSupport
int count = 0;
for (Map.Entry<String, Long> interest : interests.entrySet())
{
count += deleteHistoryByDateAndCode(new Date(now
- interest.getValue().longValue()), interest.getKey(), conn);
count += deleteHistoryByDateAndCode(new Date(now - interest.getValue().longValue()),
interest.getKey(), conn);
conn.commit();
}
return count;

View File

@@ -343,11 +343,20 @@ public class RoleCrosswalk
String tempDirectory = ConfigurationManager.getProperty("upload.temp.dir");
File tempFile = File.createTempFile("RoleCrosswalkIngest" + dso.hashCode(), null, new File(tempDirectory));
tempFile.deleteOnExit();
FileOutputStream fileOutStream = new FileOutputStream(tempFile);
XMLOutputter writer = new XMLOutputter();
writer.output(root, fileOutStream);
fileOutStream.close();
FileOutputStream fileOutStream = null;
try
{
fileOutStream = new FileOutputStream(tempFile);
XMLOutputter writer = new XMLOutputter();
writer.output(root, fileOutStream);
}
finally
{
if (fileOutStream != null)
{
fileOutStream.close();
}
}
//Actually call the ingester
try

View File

@@ -238,6 +238,7 @@ public abstract class AbstractMETSDisseminator
PackageParameters params, File pkgFile)
throws PackageValidationException, CrosswalkException, AuthorizeException, SQLException, IOException
{
FileOutputStream outStream = null;
try
{
//Make sure our package file exists
@@ -247,7 +248,7 @@ public abstract class AbstractMETSDisseminator
}
//Open up an output stream to write to package file
FileOutputStream outStream = new FileOutputStream(pkgFile);
outStream = new FileOutputStream(pkgFile);
// Generate a true manifest-only "package", no external files/data & no need to zip up
if (params != null && params.getBooleanProperty("manifestOnly", false))
@@ -261,9 +262,6 @@ public abstract class AbstractMETSDisseminator
// make a Zip-based package
writeZipPackage(context, dso, params, outStream);
}//end if/else
//Close stream / stop writing to file
outStream.close();
}//end try
catch (MetsException e)
{
@@ -272,6 +270,14 @@ public abstract class AbstractMETSDisseminator
log.error("METS error: ",e);
throw new PackageValidationException(e);
}
finally
{
//Close stream / stop writing to file
if (outStream != null)
{
outStream.close();
}
}
}

View File

@@ -156,9 +156,19 @@ public class PDFPackager
Item myitem = wi.getItem();
original = myitem.createBundle("ORIGINAL");
InputStream fileStream = new FileInputStream(pkgFile);
bs = original.createBitstream(fileStream);
fileStream.close();
InputStream fileStream = null;
try
{
fileStream = new FileInputStream(pkgFile);
bs = original.createBitstream(fileStream);
}
finally
{
if (fileStream != null)
{
fileStream.close();
}
}
bs.setName("package.pdf");
setFormatToMIMEType(context, bs, "application/pdf");

View File

@@ -123,12 +123,21 @@ public class RoleDisseminator implements PackageDisseminator
{
boolean emitPasswords = params.containsKey("passwords");
//open file stream for writing
FileOutputStream fileOut = new FileOutputStream(pkgFile);
writeToStream(context, object, fileOut, emitPasswords);
//close file stream & save
fileOut.close();
FileOutputStream fileOut = null;
try
{
//open file stream for writing
fileOut = new FileOutputStream(pkgFile);
writeToStream(context, object, fileOut, emitPasswords);
}
finally
{
//close file stream & save
if (fileOut != null)
{
fileOut.close();
}
}
}
/**

View File

@@ -315,7 +315,7 @@ public class DatabaseManager
throw sqle;
}
}
/**
* Return an iterator with the results of executing statement. The table
* parameter indicates the type of result. If table is null, the column
@@ -1036,8 +1036,8 @@ public class DatabaseManager
public static void loadSql(Reader r) throws SQLException, IOException
{
BufferedReader reader = new BufferedReader(r);
StringBuffer sql = new StringBuffer();
String SQL = null;
StringBuilder sqlBuilder = new StringBuilder();
String sql = null;
String line = null;
@@ -1057,8 +1057,7 @@ public class DatabaseManager
// Look for comments
int commentStart = line.indexOf("--");
String input = (commentStart != -1) ? line.substring(0,
commentStart) : line;
String input = (commentStart != -1) ? line.substring(0, commentStart) : line;
// Empty line, skip
if (input.trim().equals(""))
@@ -1067,11 +1066,11 @@ public class DatabaseManager
}
// Put it on the SQL buffer
sql.append(input.replace(';', ' ')); // remove all semicolons
sqlBuilder.append(input.replace(';', ' ')); // remove all semicolons
// from sql file!
// Add a space
sql.append(" ");
sqlBuilder.append(" ");
// More to come?
// Look for quotes
@@ -1109,18 +1108,17 @@ public class DatabaseManager
continue;
}
sql = sqlBuilder.toString();
if (log.isDebugEnabled())
{
log.debug("Running database query \"" + sql + "\"");
}
SQL = sql.toString();
try
{
// Use execute, not executeQuery (which expects results) or
// executeUpdate
statement.execute(SQL);
statement.execute(sql);
}
catch (SQLWarning sqlw)
{
@@ -1137,17 +1135,16 @@ public class DatabaseManager
// These are Postgres-isms:
// There's no easy way to check if a table exists before
// creating it, so we always drop tables, then create them
boolean isDrop = ((SQL != null) && (sqlmessage != null)
&& (SQL.toUpperCase().startsWith("DROP")) && (sqlmessage
.indexOf("does not exist") != -1));
boolean isDrop = ((sql != null) && (sqlmessage != null)
&& (sql.toUpperCase().startsWith("DROP"))
&& (sqlmessage.indexOf("does not exist") != -1));
// Creating a view causes a bogus warning
boolean isNoResults = ((SQL != null)
boolean isNoResults = ((sql != null)
&& (sqlmessage != null)
&& ((SQL.toUpperCase().startsWith("CREATE VIEW")) || (SQL
.toUpperCase()
.startsWith("CREATE FUNCTION"))) && (sqlmessage
.indexOf("No results were returned") != -1));
&& (sql.toUpperCase().startsWith("CREATE VIEW")
|| sql.toUpperCase().startsWith("CREATE FUNCTION"))
&& (sqlmessage.indexOf("No results were returned") != -1));
// If the messages are bogus, give them a low priority
if (isDrop || isNoResults)
@@ -1168,8 +1165,8 @@ public class DatabaseManager
}
// Reset SQL buffer
sql = new StringBuffer();
SQL = null;
sqlBuilder = new StringBuilder();
sql = null;
}
}
finally

View File

@@ -48,6 +48,8 @@ import org.dspace.core.Context;
import org.dspace.handle.HandleManager;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.*;
@@ -83,22 +85,25 @@ public class SearchUtils {
.convertProperties(ConfigurationManager.getProperties());
try {
File config = new File(props.getProperty("dspace.dir")
+ "/config/dspace-solr-search.cfg");
File config = new File(props.getProperty("dspace.dir") + "/config/dspace-solr-search.cfg");
if (config.exists()) {
props.combine(new ExtendedProperties(config.getAbsolutePath()));
} else {
ExtendedProperties defaults = new ExtendedProperties();
defaults
.load(SolrServiceImpl.class
.getResourceAsStream("dspace-solr-search.cfg"));
props.combine(defaults);
InputStream is = null;
try {
is = SolrServiceImpl.class.getResourceAsStream("dspace-solr-search.cfg");
ExtendedProperties defaults = new ExtendedProperties();
defaults.load(is);
props.combine(defaults);
} finally {
if (is != null) {
is.close();
}
}
}
log.debug("combined configuration");
}
catch (Exception e) {
} catch (IOException e) {
log.error(e.getMessage(), e);
}

View File

@@ -175,20 +175,34 @@ public class CollectionDepositor extends Depositor
String fn = swordService.getFilename(context, deposit, true);
FileInputStream fis = new FileInputStream(deposit.getFile());
Bitstream bitstream = swordBundle.createBitstream(fis);
bitstream.setName(fn);
bitstream.setDescription("SWORD deposit package");
Bitstream bitstream;
FileInputStream fis = null;
try
{
fis = new FileInputStream(deposit.getFile());
bitstream = swordBundle.createBitstream(fis);
}
finally
{
if (fis != null)
{
fis.close();
}
}
BitstreamFormat bf = BitstreamFormat.findByMIMEType(context, deposit.getContentType());
if (bf != null)
{
bitstream.setFormat(bf);
}
bitstream.setName(fn);
bitstream.setDescription("SWORD deposit package");
bitstream.update();
swordBundle.update();
item.update();
BitstreamFormat bf = BitstreamFormat.findByMIMEType(context, deposit.getContentType());
if (bf != null)
{
bitstream.setFormat(bf);
}
bitstream.update();
swordBundle.update();
item.update();
swordService.message("Original package stored as " + fn + ", in item bundle " + swordBundle);

View File

@@ -132,8 +132,20 @@ public class ItemDepositor extends Depositor
String fn = swordService.getFilename(context, deposit, true);
FileInputStream fis = new FileInputStream(deposit.getFile());
Bitstream bitstream = swordBundle.createBitstream(fis);
Bitstream bitstream;
FileInputStream fis = null;
try
{
fis = new FileInputStream(deposit.getFile());
bitstream = swordBundle.createBitstream(fis);
}
finally
{
if (fis != null)
{
fis.close();
}
}
bitstream.setName(fn);
bitstream.setDescription("Original file deposited via SWORD");

View File

@@ -102,9 +102,21 @@ public class SimpleFileIngester implements SWORDIngester
original = item.createBundle("ORIGINAL");
}
File depositFile = deposit.getFile();
FileInputStream fis = new FileInputStream(depositFile);
Bitstream bs = original.createBitstream(fis);
Bitstream bs;
FileInputStream fis = null;
try
{
fis = new FileInputStream(deposit.getFile());
bs = original.createBitstream(fis);
}
finally
{
if (fis != null)
{
fis.close();
}
}
String fn = swordService.getFilename(context, deposit, false);
bs.setName(fn);