From: PJ Fanning Date: Thu, 2 Jan 2020 20:45:31 +0000 (+0000) Subject: [bug-64045] close resources if we throw exceptions X-Git-Tag: REL_4_1_2~54 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=22266e209a8f13a1ee88bafecb80e005303b165d;p=poi.git [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 --- diff --git a/src/ooxml/java/org/apache/poi/ooxml/POIXMLDocumentPart.java b/src/ooxml/java/org/apache/poi/ooxml/POIXMLDocumentPart.java index c8e1a277a4..8412b276d6 100644 --- a/src/ooxml/java/org/apache/poi/ooxml/POIXMLDocumentPart.java +++ b/src/ooxml/java/org/apache/poi/ooxml/POIXMLDocumentPart.java @@ -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 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 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 ); + } + } + } diff --git a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java index e074bf7268..8b93f41eaf 100644 --- a/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java +++ b/src/ooxml/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java @@ -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; } }