From fad6cae019d3dbca9c143855d77f4d2b11c6df3b Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 15 Oct 2024 16:13:17 +0000 Subject: [PATCH] try to make ExcelAntWorkbookUtilFactory thread safe git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1921338 13f79535-47bb-0310-9956-ffa450edef68 --- .../util/ExcelAntWorkbookUtilFactory.java | 20 +++++++++---------- .../poi/xwpf/usermodel/VerticalAlign.java | 7 +++++-- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/poi-excelant/src/main/java/org/apache/poi/ss/excelant/util/ExcelAntWorkbookUtilFactory.java b/poi-excelant/src/main/java/org/apache/poi/ss/excelant/util/ExcelAntWorkbookUtilFactory.java index 55c4896705..7838a5d6eb 100644 --- a/poi-excelant/src/main/java/org/apache/poi/ss/excelant/util/ExcelAntWorkbookUtilFactory.java +++ b/poi-excelant/src/main/java/org/apache/poi/ss/excelant/util/ExcelAntWorkbookUtilFactory.java @@ -27,7 +27,7 @@ import java.util.Map; */ public final class ExcelAntWorkbookUtilFactory { - private static Map workbookUtilMap; + private static final Map workbookUtilMap = new HashMap<>(); private ExcelAntWorkbookUtilFactory() { } @@ -41,16 +41,14 @@ public final class ExcelAntWorkbookUtilFactory { * a freshly instantiated one if none did exist before. */ public static ExcelAntWorkbookUtil getInstance(String fileName) { - if(workbookUtilMap == null) { - workbookUtilMap = new HashMap<>(); + synchronized (workbookUtilMap) { + if(workbookUtilMap.containsKey(fileName)) { + return workbookUtilMap.get(fileName); + } + + ExcelAntWorkbookUtil wbu = new ExcelAntWorkbookUtil(fileName); + workbookUtilMap.put(fileName, wbu); + return wbu; } - - if(workbookUtilMap.containsKey(fileName)) { - return workbookUtilMap.get(fileName); - } - - ExcelAntWorkbookUtil wbu = new ExcelAntWorkbookUtil(fileName); - workbookUtilMap.put(fileName, wbu); - return wbu; } } diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/VerticalAlign.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/VerticalAlign.java index 8e6df981b4..16a84cf26e 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/VerticalAlign.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/VerticalAlign.java @@ -16,6 +16,7 @@ ==================================================================== */ package org.apache.poi.xwpf.usermodel; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -45,12 +46,14 @@ public enum VerticalAlign { */ SUBSCRIPT(3); - private static final Map imap = new HashMap<>(); + private static final Map imap; static { + final Map tempMap = new HashMap<>(); for (VerticalAlign p : values()) { - imap.put(p.getValue(), p); + tempMap.put(p.getValue(), p); } + imap = Collections.unmodifiableMap(tempMap); } private final int value; -- 2.39.5