You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OOXMLLiteAgent.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ooxml.lite;
  16. import static net.bytebuddy.matcher.ElementMatchers.named;
  17. import java.io.IOException;
  18. import java.lang.instrument.ClassFileTransformer;
  19. import java.lang.instrument.Instrumentation;
  20. import java.nio.charset.StandardCharsets;
  21. import java.nio.file.Files;
  22. import java.nio.file.Path;
  23. import java.nio.file.Paths;
  24. import java.nio.file.StandardOpenOption;
  25. import java.security.ProtectionDomain;
  26. import java.util.HashSet;
  27. import java.util.Set;
  28. import java.util.regex.Pattern;
  29. import java.util.stream.Stream;
  30. import net.bytebuddy.agent.builder.AgentBuilder;
  31. import net.bytebuddy.implementation.MethodDelegation;
  32. import net.bytebuddy.implementation.SuperMethodCall;
  33. import net.bytebuddy.matcher.ElementMatchers;
  34. import org.apache.xmlbeans.impl.schema.SchemaTypeSystemImpl;
  35. /**
  36. * OOXMLLiteAgent is the replacement for the former OOXMLLite, because in Java 12
  37. * it isn't possible to access the privates :) of the ClassLoader
  38. */
  39. public class OOXMLLiteAgent {
  40. public static void premain(String agentArgs, Instrumentation inst) throws IOException {
  41. String[] args = (agentArgs == null ? "" : agentArgs).split("\\|", 2);
  42. String logBase = args.length >= 1 ? args[0] : "ooxml-lite-report";
  43. XsbLogger.load(logBase+".xsb");
  44. ClazzLogger log = new ClazzLogger();
  45. log.load(logBase+".clazz");
  46. log.setPattern(args.length >= 2 ? args[1] : ".*/schemas/.*");
  47. inst.addTransformer(log);
  48. new AgentBuilder.Default()
  49. // .with(AgentBuilder.Listener.StreamWriting.toSystemOut())
  50. .type(named("org.apache.xmlbeans.impl.schema.XsbReader"))
  51. .transform((builder, type, cl, m) ->
  52. builder
  53. .constructor(ElementMatchers.any())
  54. .intercept(MethodDelegation.to(XsbLogger.class).andThen(SuperMethodCall.INSTANCE))
  55. )
  56. .installOn(inst);
  57. }
  58. /**
  59. * This logger intercepts the loading of XmlBeans .xsb
  60. *
  61. * when ran in the ant junitlauncher, it's not possible to have the interceptor methods as
  62. * instance method of ClazzLogger. the junit test will fail ... though it works ok in IntelliJ
  63. * probably because of classpath vs. modulepath instantiation
  64. */
  65. public static class XsbLogger {
  66. private static Path logPath;
  67. private static final Set<Integer> hashes = new HashSet<>();
  68. static void load(String path) throws IOException {
  69. logPath = Paths.get(path);
  70. if (Files.exists(logPath)) {
  71. try (Stream<String> stream = Files.lines(logPath)) {
  72. stream.forEach((s) -> hashes.add(s.hashCode()));
  73. }
  74. }
  75. }
  76. // SchemaTypeSystemImpl.XsbReader::new is delegated to here - method name doesn't matter
  77. public static void loadXsb(SchemaTypeSystemImpl parent, String handle) {
  78. write(logPath, handle, hashes);
  79. }
  80. public static void loadXsb(SchemaTypeSystemImpl parent, String handle, int filetype) {
  81. loadXsb(parent, handle);
  82. }
  83. }
  84. /**
  85. * This logger is used to log the used XmlBeans classes
  86. */
  87. public static class ClazzLogger implements ClassFileTransformer {
  88. Path logPath;
  89. Pattern includes;
  90. final Set<Integer> hashes = new HashSet<>();
  91. void setPattern(String regex) {
  92. includes = Pattern.compile(regex);
  93. }
  94. void load(String path) throws IOException {
  95. this.logPath = Paths.get(path);
  96. if (Files.exists(this.logPath)) {
  97. try (Stream<String> stream = Files.lines(this.logPath)) {
  98. stream.forEach((s) -> hashes.add(s.hashCode()));
  99. }
  100. }
  101. }
  102. public byte[] transform(ClassLoader loader, String className, Class redefiningClass, ProtectionDomain domain, byte[] bytes) {
  103. if (logPath != null && className != null && includes.matcher(className).find()) {
  104. write(logPath, className, hashes);
  105. }
  106. return bytes;
  107. }
  108. }
  109. static void write(Path path, String item, Set<Integer> hashes) {
  110. if (!hashes.contains(item.hashCode())) {
  111. try {
  112. // TODO: check if this is atomic ... as transform() is probably called synchronized, it doesn't matter anyway
  113. Files.write(path, (item+"\n").getBytes(StandardCharsets.ISO_8859_1), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
  114. hashes.add(item.hashCode());
  115. } catch (IOException ignored) {
  116. }
  117. }
  118. }
  119. }