aboutsummaryrefslogtreecommitdiffstats
path: root/poi-ooxml-lite-agent
diff options
context:
space:
mode:
authorAndreas Beeker <kiwiwings@apache.org>2021-04-07 21:40:33 +0000
committerAndreas Beeker <kiwiwings@apache.org>2021-04-07 21:40:33 +0000
commitb6aee1ef6d3e92a28ffd4b5c03e677b63b43747f (patch)
treead9c7b312976c4ed113a7f3b5b4757bfe1b3eee6 /poi-ooxml-lite-agent
parent6458acb931a0cc17b2d5ed205a1b3fbbb78b9193 (diff)
downloadpoi-b6aee1ef6d3e92a28ffd4b5c03e677b63b43747f.tar.gz
poi-b6aee1ef6d3e92a28ffd4b5c03e677b63b43747f.zip
65206 - Migrate ant / maven to gradle build
compile / jar / test of mrJars don't include ants build.xml anymore rename directories to match project and maven artifact names refactor artifacts - so each project has one artifact replace static references in hssf/dev tests with junit5 constructs, which had problems in parallel tests increase gradle heap to 4gb because of OOM - maybe less would also work git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1888488 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi-ooxml-lite-agent')
-rw-r--r--poi-ooxml-lite-agent/build.gradle89
-rw-r--r--poi-ooxml-lite-agent/src/main/java/org/apache/poi/ooxml/lite/OOXMLLiteAgent.java140
-rw-r--r--poi-ooxml-lite-agent/src/main/java9/module-info.classbin0 -> 268 bytes
-rw-r--r--poi-ooxml-lite-agent/src/main/java9/module-info.java23
4 files changed, 252 insertions, 0 deletions
diff --git a/poi-ooxml-lite-agent/build.gradle b/poi-ooxml-lite-agent/build.gradle
new file mode 100644
index 0000000000..7b9204c8f3
--- /dev/null
+++ b/poi-ooxml-lite-agent/build.gradle
@@ -0,0 +1,89 @@
+/* ====================================================================
+ 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.
+==================================================================== */
+
+plugins {
+ id 'java'
+ id 'maven-publish'
+ id 'java-library'
+}
+
+final String JAVA9_SRC = 'src/main/java9'
+final String JAVA9_OUT = "${buildDir}/classes/java9/main/"
+final String VERSIONS9 = 'META-INF/versions/9'
+
+sourceSets {
+ main {
+ if (JavaVersion.current() != JavaVersion.VERSION_1_8) {
+ output.dir(JAVA9_OUT, builtBy: 'cacheJava9')
+ }
+ }
+}
+
+dependencies {
+ api 'net.bytebuddy:byte-buddy:1.10.19'
+ api 'net.bytebuddy:byte-buddy-agent:1.10.19'
+ api "org.apache.xmlbeans:xmlbeans:${xmlbeansVersion}"
+}
+
+final MODULE_NAME = 'org.apache.poi.ooxml_lite'
+
+java {
+ sourceCompatibility = JavaVersion.VERSION_1_8
+ targetCompatibility = JavaVersion.VERSION_1_8
+}
+
+task compileJava9(type: JavaCompile) {
+ dependsOn 'compileJava'
+
+ sourceCompatibility = 9
+ targetCompatibility = 9
+ destinationDirectory = file(JAVA9_OUT + VERSIONS9)
+ source = file(JAVA9_SRC)
+ classpath = files()
+ doFirst {
+ options.compilerArgs = [
+ '--patch-module', "${MODULE_NAME}=${sourceSets.main.output.classesDirs.asPath}",
+ '--module-path', sourceSets.main.compileClasspath.asPath
+ ]
+ }
+}
+
+task cacheJava9(type: Copy) {
+ dependsOn 'compileJava9'
+
+ from(file(JAVA9_OUT + VERSIONS9))
+ into(JAVA9_SRC)
+}
+
+jar {
+ destinationDirectory = file("../build/dist/maven/${project.archivesBaseName}")
+
+ if (JavaVersion.current() == JavaVersion.VERSION_1_8) {
+ into('META-INF/versions/9') {
+ from JAVA9_SRC include '*.class'
+ }
+ }
+
+ manifest {
+ attributes (
+ 'Automatic-Module-Name' : MODULE_NAME,
+ 'Multi-Release': 'true',
+ 'Premain-Class' : 'org.apache.poi.ooxml.lite.OOXMLLiteAgent'
+ )
+ }
+}
+
diff --git a/poi-ooxml-lite-agent/src/main/java/org/apache/poi/ooxml/lite/OOXMLLiteAgent.java b/poi-ooxml-lite-agent/src/main/java/org/apache/poi/ooxml/lite/OOXMLLiteAgent.java
new file mode 100644
index 0000000000..64cec25ac6
--- /dev/null
+++ b/poi-ooxml-lite-agent/src/main/java/org/apache/poi/ooxml/lite/OOXMLLiteAgent.java
@@ -0,0 +1,140 @@
+/* ====================================================================
+ 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.
+==================================================================== */
+
+package org.apache.poi.ooxml.lite;
+
+import static net.bytebuddy.matcher.ElementMatchers.named;
+
+import java.io.IOException;
+import java.lang.instrument.ClassFileTransformer;
+import java.lang.instrument.Instrumentation;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardOpenOption;
+import java.security.ProtectionDomain;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
+
+import net.bytebuddy.agent.builder.AgentBuilder;
+import net.bytebuddy.implementation.MethodDelegation;
+import net.bytebuddy.implementation.SuperMethodCall;
+import net.bytebuddy.matcher.ElementMatchers;
+import org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl;
+
+/**
+ * OOXMLLiteAgent is the replacement for the former OOXMLLite, because in Java 12
+ * it isn't possible to access the privates :) of the ClassLoader
+ */
+public class OOXMLLiteAgent {
+
+ public static void premain(String agentArgs, Instrumentation inst) throws IOException {
+ String[] args = (agentArgs == null ? "" : agentArgs).split("\\|", 2);
+ String logBase = args.length >= 1 ? args[0] : "ooxml-lite-report";
+
+ XsbLogger.load(logBase+".xsb");
+
+ ClazzLogger log = new ClazzLogger();
+ log.load(logBase+".clazz");
+ log.setPattern(args.length >= 2 ? args[1] : ".*/schemas/.*");
+ inst.addTransformer(log);
+
+ new AgentBuilder.Default()
+ // .with(AgentBuilder.Listener.StreamWriting.toSystemOut())
+ .type(named("org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl$XsbReader"))
+ .transform((builder, type, cl, m) ->
+ builder
+ .constructor(ElementMatchers.any())
+ .intercept(MethodDelegation.to(XsbLogger.class).andThen(SuperMethodCall.INSTANCE))
+ )
+ .installOn(inst);
+ }
+
+ /**
+ * This logger intercepts the loading of XmlBeans .xsb
+ *
+ * when ran in the ant junitlauncher, it's not possible to have the interceptor methods as
+ * instance method of ClazzLogger. the junit test will fail ... though it works ok in IntelliJ
+ * probably because of classpath vs. modulepath instantiation
+ */
+ public static class XsbLogger {
+ private static Path logPath;
+ private static final Set<Integer> hashes = new HashSet<>();
+
+ static void load(String path) throws IOException {
+ logPath = Paths.get(path);
+ if (Files.exists(logPath)) {
+ try (Stream<String> stream = Files.lines(logPath)) {
+ stream.forEach((s) -> hashes.add(s.hashCode()));
+ }
+ }
+ }
+
+ // SchemaTypeSystemImpl.XsbReader::new is delegated to here - method name doesn't matter
+ public static void loadXsb(SchemaTypeSystemImpl parent, String handle) {
+ write(logPath, handle, hashes);
+ }
+
+ public static void loadXsb(SchemaTypeSystemImpl parent, String handle, int filetype) {
+ loadXsb(parent, handle);
+ }
+ }
+
+ /**
+ * This logger is used to log the used XmlBeans classes
+ */
+ public static class ClazzLogger implements ClassFileTransformer {
+ Path logPath;
+ Pattern includes;
+ final Set<Integer> hashes = new HashSet<>();
+
+ void setPattern(String regex) {
+ includes = Pattern.compile(regex);
+ }
+
+ void load(String path) throws IOException {
+ this.logPath = Paths.get(path);
+ if (Files.exists(this.logPath)) {
+ try (Stream<String> stream = Files.lines(this.logPath)) {
+ stream.forEach((s) -> hashes.add(s.hashCode()));
+ }
+ }
+ }
+
+ public byte[] transform(ClassLoader loader, String className, Class redefiningClass, ProtectionDomain domain, byte[] bytes) {
+ if (logPath != null && className != null && includes.matcher(className).find()) {
+ write(logPath, className, hashes);
+ }
+ return bytes;
+ }
+ }
+
+
+ static void write(Path path, String item, Set<Integer> hashes) {
+ if (!hashes.contains(item.hashCode())) {
+ try {
+ // TODO: check if this is atomic ... as transform() is probably called synchronized, it doesn't matter anyway
+ Files.write(path, (item+"\n").getBytes(StandardCharsets.ISO_8859_1), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
+ hashes.add(item.hashCode());
+ } catch (IOException ignored) {
+ }
+ }
+ }
+}
diff --git a/poi-ooxml-lite-agent/src/main/java9/module-info.class b/poi-ooxml-lite-agent/src/main/java9/module-info.class
new file mode 100644
index 0000000000..4623f875fc
--- /dev/null
+++ b/poi-ooxml-lite-agent/src/main/java9/module-info.class
Binary files differ
diff --git a/poi-ooxml-lite-agent/src/main/java9/module-info.java b/poi-ooxml-lite-agent/src/main/java9/module-info.java
new file mode 100644
index 0000000000..85184ca2b2
--- /dev/null
+++ b/poi-ooxml-lite-agent/src/main/java9/module-info.java
@@ -0,0 +1,23 @@
+/* ====================================================================
+ 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.
+==================================================================== */
+
+module org.apache.poi.ooxml_lite {
+ requires java.instrument;
+ requires static net.bytebuddy.agent;
+
+ exports org.apache.poi.ooxml.lite;
+} \ No newline at end of file