This is an automated email from the ASF dual-hosted git repository. dblevins pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/tomee-patch-plugin.git
commit 0a1e6449a7fd8c2ab2f3913156641334ccd77f18 Author: David Blevins <[email protected]> AuthorDate: Fri Mar 26 00:39:17 2021 -0700 Ability to replace both resources and jars Initial use case is actually to restore mojarra and eclipselink with completely unmodified versions --- .../patch/core/ReplacementNotFoundException.java | 23 +++++++++++++ .../org/apache/tomee/patch/core/Replacements.java | 34 +++++++++++++++++++ .../apache/tomee/patch/core/Transformation.java | 39 +++++++++++++++++----- .../org/apache/tomee/patch/plugin/PatchMojo.java | 5 +-- 4 files changed, 90 insertions(+), 11 deletions(-) diff --git a/tomee-patch-core/src/main/java/org/apache/tomee/patch/core/ReplacementNotFoundException.java b/tomee-patch-core/src/main/java/org/apache/tomee/patch/core/ReplacementNotFoundException.java new file mode 100644 index 0000000..97c8ab9 --- /dev/null +++ b/tomee-patch-core/src/main/java/org/apache/tomee/patch/core/ReplacementNotFoundException.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. + */ +package org.apache.tomee.patch.core; + +public class ReplacementNotFoundException extends RuntimeException { + public ReplacementNotFoundException(final String type, final String entry, final String absolutePath) { + super(String.format("Replacement %s %s not found at %s", entry, type, absolutePath)); + } +} diff --git a/tomee-patch-core/src/main/java/org/apache/tomee/patch/core/Replacements.java b/tomee-patch-core/src/main/java/org/apache/tomee/patch/core/Replacements.java new file mode 100644 index 0000000..e8d080d --- /dev/null +++ b/tomee-patch-core/src/main/java/org/apache/tomee/patch/core/Replacements.java @@ -0,0 +1,34 @@ +/* + * 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.tomee.patch.core; + +import java.util.HashMap; +import java.util.Map; + +public class Replacements { + + private Map<String, String> jars = new HashMap<>(); + private Map<String, String> resources = new HashMap<>(); + + public Map<String, String> getJars() { + return jars; + } + + public Map<String, String> getResources() { + return resources; + } +} diff --git a/tomee-patch-core/src/main/java/org/apache/tomee/patch/core/Transformation.java b/tomee-patch-core/src/main/java/org/apache/tomee/patch/core/Transformation.java index 8312aca..6df5961 100644 --- a/tomee-patch-core/src/main/java/org/apache/tomee/patch/core/Transformation.java +++ b/tomee-patch-core/src/main/java/org/apache/tomee/patch/core/Transformation.java @@ -21,6 +21,7 @@ import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes; import org.tomitribe.swizzle.stream.StreamBuilder; import org.tomitribe.util.IO; +import org.tomitribe.util.Mvn; import java.io.File; import java.io.IOException; @@ -28,10 +29,8 @@ import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import java.util.stream.Collectors; import java.util.zip.ZipEntry; @@ -44,18 +43,18 @@ public class Transformation { private final List<Clazz> classes = new ArrayList<Clazz>(); private final Log log; - private final Map<String, String> replacements; + private final Replacements replacements; public Transformation() { this.log = new NullLog(); - this.replacements = Collections.EMPTY_MAP; + this.replacements = new Replacements(); } - public Transformation(final List<Clazz> classes, final Map<String, String> replacements, final Log log) { + public Transformation(final List<Clazz> classes, final Replacements replacements, final Log log) { this.classes.addAll(classes); this.log = log; - this.replacements = replacements == null ? Collections.EMPTY_MAP : replacements; + this.replacements = replacements == null ? new Replacements() : replacements; } public static File transform(final File jar) throws IOException { @@ -75,6 +74,24 @@ public class Transformation { } private void scanJar(final String name, final InputStream inputStream, final OutputStream outputStream) throws IOException { + { + final String jar = new File(name).getName(); + final String replacement = replacements.getJars().get(jar); + if (replacement != null) { + final File file = Mvn.mvn(replacement); + if (!file.exists()) { + throw new ReplacementNotFoundException("jar", jar, file.getAbsolutePath()); + } + log.info(String.format("Replaced %s", name)); + IO.copy(inputStream, new OutputStream() { + @Override + public void write(final int b) throws IOException { + } + }); + IO.copy(file, outputStream); + } + } + final Jar oldJar = Jar.enter(name); final Jar jar = Jar.current(); try { @@ -218,10 +235,14 @@ public class Transformation { { final String name = new File(path).getName(); - final String replacement = replacements.get(name); + final String replacement = replacements.getResources().get(name); if (replacement != null) { - log.debug(String.format("Replaced %s with %s", path, replacement)); - inputStream = IO.read(new File(replacement)); + log.info(String.format("Replaced %s", path)); + final File file = new File(replacement); + if (!file.exists()) { + throw new ReplacementNotFoundException("resource", path, file.getAbsolutePath()); + } + inputStream = IO.read(file); } } diff --git a/tomee-patch-plugin/src/main/java/org/apache/tomee/patch/plugin/PatchMojo.java b/tomee-patch-plugin/src/main/java/org/apache/tomee/patch/plugin/PatchMojo.java index 8cceff3..f6c41b4 100644 --- a/tomee-patch-plugin/src/main/java/org/apache/tomee/patch/plugin/PatchMojo.java +++ b/tomee-patch-plugin/src/main/java/org/apache/tomee/patch/plugin/PatchMojo.java @@ -31,6 +31,7 @@ import org.apache.maven.toolchain.Toolchain; import org.apache.maven.toolchain.ToolchainManager; import org.apache.tomee.patch.core.Clazz; import org.apache.tomee.patch.core.Is; +import org.apache.tomee.patch.core.Replacements; import org.apache.tomee.patch.core.Transformation; import org.apache.tomee.patch.core.ZipToTar; import org.codehaus.plexus.compiler.Compiler; @@ -94,7 +95,7 @@ public class PatchMojo extends AbstractMojo { private Map<String, String> jdkToolchain; @Parameter - private Map<String, String> replacements; + private Replacements replace; @Parameter(defaultValue = "false") private Boolean createTarGz; @@ -187,7 +188,7 @@ public class PatchMojo extends AbstractMojo { final List<Clazz> clazzes = classes(); - final Transformation transformation = new Transformation(clazzes, replacements, new MavenLog(getLog())); + final Transformation transformation = new Transformation(clazzes, replace, new MavenLog(getLog())); for (final Artifact artifact : artifacts) { final File file = artifact.getFile(); getLog().debug("Patching " + file.getAbsolutePath());
