From: Andreas Beeker Date: Sun, 16 Aug 2020 23:18:19 +0000 (+0000) Subject: #64036 - Replace reflection calls in factories for Java 9+ X-Git-Tag: before_ooxml_3rd_edition~140 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b57da2dadafa52b0e1f93845d01e572f118c23d2;p=poi.git #64036 - Replace reflection calls in factories for Java 9+ ImageRenderer implementation are now loaded via ServiceLoader fixed the ServiceLoader.load invocations to pass a sensible ClassLoader as OSGi preparation git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1880909 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/extractor/ExtractorFactory.java b/src/java/org/apache/poi/extractor/ExtractorFactory.java index bdeb740e20..ccab16a213 100644 --- a/src/java/org/apache/poi/extractor/ExtractorFactory.java +++ b/src/java/org/apache/poi/extractor/ExtractorFactory.java @@ -81,7 +81,8 @@ public final class ExtractorFactory { private ExtractorFactory() { - ServiceLoader.load(ExtractorProvider.class).forEach(provider::add); + ClassLoader cl = ExtractorFactory.class.getClassLoader(); + ServiceLoader.load(ExtractorProvider.class, cl).forEach(provider::add); } /** diff --git a/src/java/org/apache/poi/sl/draw/ImageRenderer.java b/src/java/org/apache/poi/sl/draw/ImageRenderer.java index 0289fce7bf..5051d4593b 100644 --- a/src/java/org/apache/poi/sl/draw/ImageRenderer.java +++ b/src/java/org/apache/poi/sl/draw/ImageRenderer.java @@ -31,7 +31,8 @@ import org.apache.poi.util.Dimension2DDouble; /** * Classes can implement this interfaces to support other formats, for - * example, use Apache Batik to render WMF, PICT can be rendered using Apple QuickTime API for Java: + * example, use Apache Batik to render WMF (since POI 4.0, there's an internal WMF/EMF/EMF+ renderer in POI), + * PICT can be rendered using Apple QuickTime API for Java: * *
  * 
@@ -132,11 +133,11 @@ public interface ImageRenderer {
     /**
      * @param dim the dimension in pixels of the returned image
      * @return the image as buffered image or null if image could not be loaded
-     * 
+     *
      * @since POI 3.15-beta2
      */
     BufferedImage getImage(Dimension2D dim);
-    
+
     /**
      * Render picture data into the supplied graphics
      *
diff --git a/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java b/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java
index 200120b211..8155fccb8d 100644
--- a/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java
+++ b/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java
@@ -48,7 +48,8 @@ public final class SlideShowFactory {
     private final List> provider = new ArrayList<>();
 
     private SlideShowFactory() {
-        ServiceLoader.load(SlideShowProvider.class).forEach(provider::add);
+        ClassLoader cl = SlideShowFactory.class.getClassLoader();
+        ServiceLoader.load(SlideShowProvider.class, cl).forEach(provider::add);
     }
 
     /**
diff --git a/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java b/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
index e755b09f5e..965666904c 100644
--- a/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
+++ b/src/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
@@ -54,7 +54,8 @@ public final class WorkbookFactory {
     private final List provider = new ArrayList<>();
 
     private WorkbookFactory() {
-        ServiceLoader.load(WorkbookProvider.class).forEach(provider::add);
+        ClassLoader cl = WorkbookFactory.class.getClassLoader();
+        ServiceLoader.load(WorkbookProvider.class, cl).forEach(provider::add);
     }
 
 
diff --git a/src/multimodule/ooxml/java9/module-info.class b/src/multimodule/ooxml/java9/module-info.class
index 517b8bbe2c..fd59cc5611 100644
Binary files a/src/multimodule/ooxml/java9/module-info.class and b/src/multimodule/ooxml/java9/module-info.class differ
diff --git a/src/multimodule/ooxml/java9/module-info.java b/src/multimodule/ooxml/java9/module-info.java
index 66928ffa24..35533ce078 100644
--- a/src/multimodule/ooxml/java9/module-info.java
+++ b/src/multimodule/ooxml/java9/module-info.java
@@ -31,6 +31,7 @@ module org.apache.poi.ooxml {
     provides org.apache.poi.extractor.ExtractorProvider with org.apache.poi.ooxml.extractor.POIXMLExtractorFactory;
     provides org.apache.poi.ss.usermodel.WorkbookProvider with org.apache.poi.xssf.usermodel.XSSFWorkbookFactory;
     provides org.apache.poi.sl.usermodel.SlideShowProvider with org.apache.poi.xslf.usermodel.XSLFSlideShowFactory;
+    provides org.apache.poi.sl.draw.ImageRenderer with org.apache.poi.xslf.draw.SVGImageRenderer;
 
     exports org.apache.poi.xwpf.extractor;
     exports org.apache.poi.xwpf.usermodel;
diff --git a/src/multimodule/ooxml/test9/module-info.class b/src/multimodule/ooxml/test9/module-info.class
index be87166834..67874cc31c 100644
Binary files a/src/multimodule/ooxml/test9/module-info.class and b/src/multimodule/ooxml/test9/module-info.class differ
diff --git a/src/multimodule/ooxml/test9/module-info.java b/src/multimodule/ooxml/test9/module-info.java
index b41ae2b9b6..6ae5b6cd84 100644
--- a/src/multimodule/ooxml/test9/module-info.java
+++ b/src/multimodule/ooxml/test9/module-info.java
@@ -31,6 +31,7 @@ module org.apache.poi.ooxml {
     provides org.apache.poi.extractor.ExtractorProvider with org.apache.poi.ooxml.extractor.POIXMLExtractorFactory;
     provides org.apache.poi.ss.usermodel.WorkbookProvider with org.apache.poi.xssf.usermodel.XSSFWorkbookFactory;
     provides org.apache.poi.sl.usermodel.SlideShowProvider with org.apache.poi.xslf.usermodel.XSLFSlideShowFactory;
+    provides org.apache.poi.sl.draw.ImageRenderer with org.apache.poi.xslf.draw.SVGImageRenderer;
 
     exports org.apache.poi.xwpf.extractor;
     exports org.apache.poi.xwpf.usermodel;
diff --git a/src/multimodule/poi/java9/module-info.class b/src/multimodule/poi/java9/module-info.class
index ab0e08c358..c0a0ef367a 100644
Binary files a/src/multimodule/poi/java9/module-info.class and b/src/multimodule/poi/java9/module-info.class differ
diff --git a/src/multimodule/poi/java9/module-info.java b/src/multimodule/poi/java9/module-info.java
index ea3f394035..a34988980b 100644
--- a/src/multimodule/poi/java9/module-info.java
+++ b/src/multimodule/poi/java9/module-info.java
@@ -27,14 +27,15 @@ module org.apache.poi.poi {
     /* needed for CleanerUtil */
     requires jdk.unsupported;
 
+    /* for JPMS / OSGi interaction see https://blog.osgi.org/2013/02/javautilserviceloader-in-osgi.html */
     uses org.apache.poi.extractor.ExtractorProvider;
     uses org.apache.poi.ss.usermodel.WorkbookProvider;
     uses org.apache.poi.sl.usermodel.SlideShowProvider;
+    uses org.apache.poi.sl.draw.ImageRenderer;
 
-
-    provides org.apache.poi.ss.usermodel.WorkbookProvider with org.apache.poi.hssf.usermodel.HSSFWorkbookFactory;
     provides org.apache.poi.extractor.ExtractorProvider with org.apache.poi.extractor.MainExtractorFactory;
-
+    provides org.apache.poi.ss.usermodel.WorkbookProvider with org.apache.poi.hssf.usermodel.HSSFWorkbookFactory;
+    provides org.apache.poi.sl.draw.ImageRenderer with org.apache.poi.sl.draw.BitmapImageRenderer;
 
     exports org.apache.poi;
     exports org.apache.poi.common;
diff --git a/src/multimodule/poi/test9/module-info.class b/src/multimodule/poi/test9/module-info.class
index 2b43c43fa8..d3bfb0afd1 100644
Binary files a/src/multimodule/poi/test9/module-info.class and b/src/multimodule/poi/test9/module-info.class differ
diff --git a/src/multimodule/poi/test9/module-info.java b/src/multimodule/poi/test9/module-info.java
index 9e5bc0a110..79a6a70c2e 100644
--- a/src/multimodule/poi/test9/module-info.java
+++ b/src/multimodule/poi/test9/module-info.java
@@ -27,12 +27,15 @@ module org.apache.poi.poi {
     /* needed for CleanerUtil */
     requires jdk.unsupported;
 
+    /* for JPMS / OSGi interaction see https://blog.osgi.org/2013/02/javautilserviceloader-in-osgi.html */
     uses org.apache.poi.extractor.ExtractorProvider;
     uses org.apache.poi.ss.usermodel.WorkbookProvider;
     uses org.apache.poi.sl.usermodel.SlideShowProvider;
+    uses org.apache.poi.sl.draw.ImageRenderer;
 
-    provides org.apache.poi.ss.usermodel.WorkbookProvider with org.apache.poi.hssf.usermodel.HSSFWorkbookFactory;
     provides org.apache.poi.extractor.ExtractorProvider with org.apache.poi.extractor.MainExtractorFactory;
+    provides org.apache.poi.ss.usermodel.WorkbookProvider with org.apache.poi.hssf.usermodel.HSSFWorkbookFactory;
+    provides org.apache.poi.sl.draw.ImageRenderer with org.apache.poi.sl.draw.BitmapImageRenderer;
 
     exports org.apache.poi;
     exports org.apache.poi.common;
diff --git a/src/multimodule/scratchpad/java9/module-info.class b/src/multimodule/scratchpad/java9/module-info.class
index 8697888d28..a32c3eac27 100644
Binary files a/src/multimodule/scratchpad/java9/module-info.class and b/src/multimodule/scratchpad/java9/module-info.class differ
diff --git a/src/multimodule/scratchpad/java9/module-info.java b/src/multimodule/scratchpad/java9/module-info.java
index b936858c70..658ef8652b 100644
--- a/src/multimodule/scratchpad/java9/module-info.java
+++ b/src/multimodule/scratchpad/java9/module-info.java
@@ -22,6 +22,7 @@ module org.apache.poi.scratchpad {
 
     provides org.apache.poi.extractor.ExtractorProvider with org.apache.poi.extractor.ole2.OLE2ScratchpadExtractorFactory;
     provides org.apache.poi.sl.usermodel.SlideShowProvider with org.apache.poi.hslf.usermodel.HSLFSlideShowFactory;
+    provides org.apache.poi.sl.draw.ImageRenderer with org.apache.poi.hwmf.draw.HwmfImageRenderer, org.apache.poi.hemf.draw.HemfImageRenderer;
 
     exports org.apache.poi.hmef;
     exports org.apache.poi.hmef.dev;
diff --git a/src/multimodule/scratchpad/test9/module-info.class b/src/multimodule/scratchpad/test9/module-info.class
index 4ee1e56a65..cfda7bb495 100644
Binary files a/src/multimodule/scratchpad/test9/module-info.class and b/src/multimodule/scratchpad/test9/module-info.class differ
diff --git a/src/multimodule/scratchpad/test9/module-info.java b/src/multimodule/scratchpad/test9/module-info.java
index cb0691b9a1..9d406fd36e 100644
--- a/src/multimodule/scratchpad/test9/module-info.java
+++ b/src/multimodule/scratchpad/test9/module-info.java
@@ -22,6 +22,7 @@ module org.apache.poi.scratchpad {
 
     provides org.apache.poi.extractor.ExtractorProvider with org.apache.poi.extractor.ole2.OLE2ScratchpadExtractorFactory;
     provides org.apache.poi.sl.usermodel.SlideShowProvider with org.apache.poi.hslf.usermodel.HSLFSlideShowFactory;
+    provides org.apache.poi.sl.draw.ImageRenderer with org.apache.poi.hwmf.draw.HwmfImageRenderer, org.apache.poi.hemf.draw.HemfImageRenderer;
 
     exports org.apache.poi.hmef;
     exports org.apache.poi.hmef.dev;
diff --git a/src/resources/devtools/forbidden-signatures.txt b/src/resources/devtools/forbidden-signatures.txt
index facc47035f..dbe56c1022 100644
--- a/src/resources/devtools/forbidden-signatures.txt
+++ b/src/resources/devtools/forbidden-signatures.txt
@@ -136,4 +136,7 @@ java.lang.String#toString()
 #java.util.Hashtable
 
 @defaultMessage DatatypeConverter is not available in Java 9+ without adding add-opens - use java.util.Base64
-javax.xml.bind.DatatypeConverter
\ No newline at end of file
+javax.xml.bind.DatatypeConverter
+
+@defaultMessage don't rely on the threads ContextClassLoader - provide the classloader via load(Class, Classloader)
+java.util.ServiceLoader#load(java.lang.Class)
\ No newline at end of file
diff --git a/src/resources/main/META-INF/services/org.apache.poi.sl.draw.ImageRenderer b/src/resources/main/META-INF/services/org.apache.poi.sl.draw.ImageRenderer
new file mode 100644
index 0000000000..a911eb6cb4
--- /dev/null
+++ b/src/resources/main/META-INF/services/org.apache.poi.sl.draw.ImageRenderer
@@ -0,0 +1,18 @@
+# ====================================================================
+#  Licensed to the Apache Software Foundation (ASF) under one or more
+#  contributor license agreements.  See the NOTICE file distributed with
+#  this work for additional information regarding copyright ownership.
+#  The ASF licenses this file to You under the Apache License, Version 2.0
+#  (the "License"); you may not use this file except in compliance with
+#  the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+# ====================================================================
+
+org.apache.poi.sl.draw.BitmapImageRenderer
\ No newline at end of file
diff --git a/src/resources/ooxml/META-INF/services/org.apache.poi.sl.draw.ImageRenderer b/src/resources/ooxml/META-INF/services/org.apache.poi.sl.draw.ImageRenderer
new file mode 100644
index 0000000000..f5f7bb9556
--- /dev/null
+++ b/src/resources/ooxml/META-INF/services/org.apache.poi.sl.draw.ImageRenderer
@@ -0,0 +1,18 @@
+# ====================================================================
+#  Licensed to the Apache Software Foundation (ASF) under one or more
+#  contributor license agreements.  See the NOTICE file distributed with
+#  this work for additional information regarding copyright ownership.
+#  The ASF licenses this file to You under the Apache License, Version 2.0
+#  (the "License"); you may not use this file except in compliance with
+#  the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+# ====================================================================
+
+org.apache.poi.xslf.draw.SVGImageRenderer
\ No newline at end of file
diff --git a/src/resources/scratchpad/META-INF/services/org.apache.poi.sl.draw.ImageRenderer b/src/resources/scratchpad/META-INF/services/org.apache.poi.sl.draw.ImageRenderer
new file mode 100644
index 0000000000..c3149c3aa9
--- /dev/null
+++ b/src/resources/scratchpad/META-INF/services/org.apache.poi.sl.draw.ImageRenderer
@@ -0,0 +1,19 @@
+# ====================================================================
+#  Licensed to the Apache Software Foundation (ASF) under one or more
+#  contributor license agreements.  See the NOTICE file distributed with
+#  this work for additional information regarding copyright ownership.
+#  The ASF licenses this file to You under the Apache License, Version 2.0
+#  (the "License"); you may not use this file except in compliance with
+#  the License.  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+# ====================================================================
+
+org.apache.poi.hwmf.draw.HwmfImageRenderer
+org.apache.poi.hemf.draw.HemfImageRenderer