From 237d55b3e39f78161d55f411bad380b1acff38d6 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Fri, 15 May 2020 07:36:27 +0000 Subject: [PATCH] [bug-64441] synchronize code that initialises WorkbookFactory git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1877776 13f79535-47bb-0310-9956-ffa450edef68 --- .../hssf/usermodel/HSSFWorkbookFactory.java | 4 +++ .../poi/ss/usermodel/WorkbookFactory.java | 30 ++++++++++++++++--- .../xssf/usermodel/XSSFWorkbookFactory.java | 5 +++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java index 6b041d6a4d..1d826e4c10 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java @@ -33,6 +33,10 @@ import org.apache.poi.util.Internal; public class HSSFWorkbookFactory extends WorkbookFactory { static { + init(); + } + + public static void init() { WorkbookFactory.createHssfFromScratch = HSSFWorkbookFactory::createWorkbook; WorkbookFactory.createHssfByNode = HSSFWorkbookFactory::createWorkbook; } diff --git a/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java b/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java index dac35db033..450d21232c 100644 --- a/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java +++ b/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java @@ -53,6 +53,8 @@ public abstract class WorkbookFactory { Workbook apply(T t, U u) throws IOException; } + private static Object hssfLock = new Object(); + private static Object xssfLock = new Object(); protected static CreateWorkbook0 createHssfFromScratch; protected static CreateWorkbook1 createHssfByNode; @@ -333,20 +335,40 @@ public abstract class WorkbookFactory { private static void initXssf() throws IOException { if (createXssfFromScratch == null) { - initFactory("org.apache.poi.xssf.usermodel.XSSFWorkbookFactory", "poi-ooxml-*.jar"); + synchronized (xssfLock) { + if (createXssfFromScratch == null) { + String factoryClass = "org.apache.poi.xssf.usermodel.XSSFWorkbookFactory"; + Class cls = initFactory(factoryClass, "poi-ooxml-*.jar"); + try { + cls.getMethod("init").invoke(null); + } catch (Exception e) { + throw new IOException(factoryClass+" failed to init."); + } + } + } } } private static void initHssf() throws IOException { if (createHssfFromScratch == null) { // HSSF is part of the main jar, so this shouldn't fail ... - initFactory("org.apache.poi.hssf.usermodel.HSSFWorkbookFactory", "poi-*.jar"); + synchronized (hssfLock) { + if (createHssfFromScratch == null) { + String factoryClass = "org.apache.poi.hssf.usermodel.HSSFWorkbookFactory"; + Class cls = initFactory(factoryClass, "poi-*.jar"); + try { + cls.getMethod("init").invoke(null); + } catch (Exception e) { + throw new IOException(factoryClass+" failed to init."); + } + } + } } } - private static void initFactory(String factoryClass, String jar) throws IOException { + private static Class initFactory(String factoryClass, String jar) throws IOException { try { - Class.forName(factoryClass, true, WorkbookFactory.class.getClassLoader()); + return Class.forName(factoryClass, true, WorkbookFactory.class.getClassLoader()); } catch (ClassNotFoundException e) { throw new IOException(factoryClass+" not found - check if " + jar + " is on the classpath."); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbookFactory.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbookFactory.java index a997307cab..78fc0805e2 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbookFactory.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbookFactory.java @@ -31,13 +31,16 @@ import org.apache.poi.ss.usermodel.WorkbookFactory; public class XSSFWorkbookFactory extends WorkbookFactory { static { + init(); + } + + public static void init() { WorkbookFactory.createXssfFromScratch = XSSFWorkbookFactory::createWorkbook; WorkbookFactory.createXssfByStream = XSSFWorkbookFactory::createWorkbook; WorkbookFactory.createXssfByPackage = o -> XSSFWorkbookFactory.createWorkbook((OPCPackage)o); WorkbookFactory.createXssfByFile = XSSFWorkbookFactory::createWorkbook; } - /** * Create a new empty Workbook * -- 2.39.5