[bug-64045] close resources if we throw exceptions

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1872264 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2020-01-02 20:45:31 +00:00
parent 781ef0c754
commit 22266e209a
2 changed files with 50 additions and 10 deletions

View File

@ -36,9 +36,7 @@ import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.util.Internal;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.*;
import org.apache.poi.xddf.usermodel.chart.XDDFChart;
import org.apache.poi.xssf.usermodel.XSSFRelation;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
@ -57,24 +55,49 @@ public class POIXMLDocumentPart {
private PackagePart packagePart;
private POIXMLDocumentPart parent;
private Map<String, RelationPart> relations = new LinkedHashMap<>();
private boolean isCommited = false;
private boolean isCommitted = false;
/**
* to check whether embedded part is already committed
*
* @return return true if embedded part is committed
* @deprecated use @link{#isCommitted()}
*/
@Removal(version = "5.0.0")
@Deprecated
public boolean isCommited() {
return isCommited;
return isCommitted();
}
/**
* to check whether embedded part is already committed
*
* @return return true if embedded part is committed
* @since 4.1.2
*/
public boolean isCommitted() {
return isCommitted;
}
/**
* setter method to set embedded part is committed
*
* @param isCommited boolean value
* @param isCommitted boolean value
* @deprecated use @link{#setCommitted(isCommitted)}
*/
public void setCommited(boolean isCommited) {
this.isCommited = isCommited;
@Removal(version = "5.0.0")
@Deprecated
public void setCommited(boolean isCommitted) {
this.isCommitted = isCommitted;
}
/**
* setter method to set embedded part is committed
*
* @param isCommitted boolean value
*/
public void setCommitted(boolean isCommitted) {
this.isCommitted = isCommitted;
}
/**
@ -453,7 +476,7 @@ public class POIXMLDocumentPart {
*/
protected final void onSave(Set<PackagePart> alreadySaved) throws IOException {
//if part is already committed then return
if (this.isCommited) {
if (this.isCommitted) {
return;
}
@ -740,6 +763,7 @@ public class POIXMLDocumentPart {
if (coreRel != null) {
PackagePart pp = pkg.getPart(coreRel);
if (pp == null) {
closeQuietly(pp);
throw new POIXMLException("OOXML file structure broken/invalid - core document '" + coreRel.getTargetURI() + "' not found.");
}
return pp;
@ -747,9 +771,25 @@ public class POIXMLDocumentPart {
coreRel = pkg.getRelationshipsByType(PackageRelationshipTypes.STRICT_CORE_DOCUMENT).getRelationship(0);
if (coreRel != null) {
IOUtils.closeQuietly(pkg);
throw new POIXMLException("Strict OOXML isn't currently supported, please see bug #57699");
}
throw new POIXMLException("OOXML file structure broken/invalid - no core document found!");
}
private static void closeQuietly(final PackagePart closeable) {
// no need to log a NullPointerException here
if(closeable == null) {
return;
}
try {
closeable.close();
} catch ( Exception exc ) {
logger.log( POILogger.ERROR, "Unable to close resource: " + exc,
exc );
}
}
}

View File

@ -922,7 +922,7 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
private void setWorksheetPartCommitted() throws InvalidFormatException {
for (RelationPart part : getRelationParts()) {
if (POIXMLDocument.PACK_OBJECT_REL_TYPE.equals(part.getRelationship().getRelationshipType())) {
part.getDocumentPart().setCommited(true);
part.getDocumentPart().setCommitted(true);
break;
}
}