diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2021-04-07 21:40:33 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2021-04-07 21:40:33 +0000 |
commit | b6aee1ef6d3e92a28ffd4b5c03e677b63b43747f (patch) | |
tree | ad9c7b312976c4ed113a7f3b5b4757bfe1b3eee6 /poi-ooxml-lite-agent/src | |
parent | 6458acb931a0cc17b2d5ed205a1b3fbbb78b9193 (diff) | |
download | poi-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/src')
-rw-r--r-- | poi-ooxml-lite-agent/src/main/java/org/apache/poi/ooxml/lite/OOXMLLiteAgent.java | 140 | ||||
-rw-r--r-- | poi-ooxml-lite-agent/src/main/java9/module-info.class | bin | 0 -> 268 bytes | |||
-rw-r--r-- | poi-ooxml-lite-agent/src/main/java9/module-info.java | 23 |
3 files changed, 163 insertions, 0 deletions
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 Binary files differnew file mode 100644 index 0000000000..4623f875fc --- /dev/null +++ b/poi-ooxml-lite-agent/src/main/java9/module-info.class 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 |