From db0e45146a8f4071c036a88c60fc37d4e10f0e57 Mon Sep 17 00:00:00 2001 From: Adel El-Beik <48713346+adelel1@users.noreply.github.com> Date: Tue, 21 Dec 2021 11:24:02 +0000 Subject: [PATCH] ENT-6494: Upgrade log4j to 2.17.0 (#6998) --- build.gradle | 2 +- .../common/logging/manifest/Manifests.java | 379 ++++++++++++++++++ .../net/corda/common/logging/CordaVersion.kt | 4 +- 3 files changed, 382 insertions(+), 3 deletions(-) create mode 100644 common/logging/src/main/java/net/corda/common/logging/manifest/Manifests.java diff --git a/build.gradle b/build.gradle index 6ae4ddd339..c183661636 100644 --- a/build.gradle +++ b/build.gradle @@ -68,7 +68,7 @@ buildscript { ext.servlet_version = '4.0.1' ext.assertj_version = '3.12.2' ext.slf4j_version = '1.7.26' - ext.log4j_version = '2.16.0' + ext.log4j_version = '2.17.0' ext.bouncycastle_version = constants.getProperty("bouncycastleVersion") ext.guava_version = constants.getProperty("guavaVersion") ext.caffeine_version = constants.getProperty("caffeineVersion") diff --git a/common/logging/src/main/java/net/corda/common/logging/manifest/Manifests.java b/common/logging/src/main/java/net/corda/common/logging/manifest/Manifests.java new file mode 100644 index 0000000000..efb9b98362 --- /dev/null +++ b/common/logging/src/main/java/net/corda/common/logging/manifest/Manifests.java @@ -0,0 +1,379 @@ +/** + * Copyright (c) 2012-2014, jcabi.com + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: 1) Redistributions of source code must retain the above + * copyright notice, this list of conditions and the following + * disclaimer. 2) Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. 3) Neither the name of the jcabi.com nor + * the names of its contributors may be used to endorse or promote + * products derived from this software without specific prior written + * permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT + * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.corda.common.logging.manifest; + +import com.jcabi.log.Logger; +import com.jcabi.manifests.ClasspathMfs; +import com.jcabi.manifests.MfMap; +import com.jcabi.manifests.Mfs; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.jar.Attributes; +import java.util.jar.Manifest; + +/** + * To a large effect this is a copy of {@code com.jcabi.manifests.Manifests} class + * with some fixes that are identified in the code with "FIX-UP" comment. + * + * Static reader of {@code META-INF/MANIFEST.MF} files. + * + * The class provides convenient methods to read + * all {@code MANIFEST.MF} files available in classpath + * and all attributes from them. + * + *
This mechanism may be very useful for transferring + * information from continuous integration environment to the production + * environment. For example, you want your site to show project version and + * the date of {@code WAR} file packaging. First, you configure + * {@code maven-war-plugin} to add this information to {@code MANIFEST.MF}: + * + *
<plugin> + * <artifactId>maven-war-plugin</artifactId> + * <configuration> + * <archive> + * <manifestEntries> + * <Foo-Version>${project.version}</Foo-Version> + * <Foo-Date>${maven.build.timestamp}</Foo-Date> + * </manifestEntries> + * </archive> + * </configuration> + * </plugin>+ * + *
{@code maven-war-plugin} will add these attributes to your + * {@code MANIFEST.MF} file and the + * project will be deployed to the production environment. Then, you can read + * these attributes where it's necessary (in one of your JAXB annotated objects, + * for example) and show to users: + * + *
import com.jcabi.manifests.Manifest; + * import java.text.SimpleDateFormat; + * import java.util.Date; + * import java.util.Locale; + * import javax.xml.bind.annotation.XmlElement; + * import javax.xml.bind.annotation.XmlRootElement; + * @XmlRootElement + * public final class Page { + * @XmlElement + * public String version() { + * return Manifests.read("Foo-Version"); + * } + * @XmlElement + * public Date date() { + * return new SimpleDateFormat("yyyy.MM.dd", Locale.ENGLISH).parse( + * Manifests.read("Foo-Date"); + * ); + * } + * }+ * + *
If you want to add more manifests to the collection, use + * its static instance: + * + *
Manifests.DEFAULT.append(new FilesMfs(new File("MANIFEST.MF")));+ * + *
You can also modify the map directly: + * + *
Manifests.DEFAULT.put("Hello", "world");+ * + *
The only dependency you need (check the latest version at + * jcabi-manifests): + * + *
<dependency> + * <groupId>com.jcabi</groupId> + * <artifactId>jcabi-manifests</artifactId> + * </dependency>+ * + * @author Yegor Bugayenko (yegor@tpc2.com) + * @version $Id$ + * @since 0.7 + * @see JAR Manifest + * @see Maven Archiver + * @see manifests.jcabi.com + * @see How to Read MANIFEST.MF Files + */ +@SuppressWarnings("PMD.TooManyMethods") +public final class Manifests implements MfMap { + + /** + * Default singleton. + */ + public static final MfMap DEFAULT = new Manifests(); + + /** + * Attributes retrieved. + */ + private final transient Map
If such a attribute doesn't exist {@link IllegalArgumentException} + * will be thrown. If you're not sure whether the attribute is present or + * not use {@link #exists(String)} beforehand. + * + *
The method is thread-safe.
+ *
+ * @param name Name of the attribute
+ * @return The value of the attribute retrieved
+ */
+ public static String read(final String name) {
+ if (name == null) {
+ throw new IllegalArgumentException("attribute can't be NULL");
+ }
+ if (name.isEmpty()) {
+ throw new IllegalArgumentException("attribute can't be empty");
+ }
+ if (!Manifests.exists(name)) {
+ throw new IllegalArgumentException(
+ Logger.format(
+ // @checkstyle LineLength (1 line)
+ "Attribute '%s' not found in MANIFEST.MF file(s) among %d other attribute(s): %[list]s",
+ name,
+ Manifests.DEFAULT.size(),
+ new TreeSet Use this method before {@link #read(String)} to check whether an
+ * attribute exists, in order to avoid a runtime exception.
+ *
+ * The method is thread-safe.
+ *
+ * @param name Name of the attribute to check
+ * @return Returns {@code TRUE} if it exists, {@code FALSE} otherwise
+ */
+ public static boolean exists(final String name) {
+ if (name == null) {
+ throw new IllegalArgumentException("attribute name can't be NULL");
+ }
+ if (name.isEmpty()) {
+ throw new IllegalArgumentException("attribute name can't be empty");
+ }
+ return Manifests.DEFAULT.containsKey(name);
+ }
+
+ /**
+ * Load attributes from input stream.
+ *
+ * Inside the method we catch {@code RuntimeException} (which may look
+ * suspicious) in order to protect our execution flow from expected (!)
+ * exceptions from {@link Manifest#getMainAttributes()}. For some reason,
+ * this JDK method doesn't throw checked exceptions if {@code MANIFEST.MF}
+ * file format is broken. Instead, it throws a runtime exception (an
+ * unchecked one), which we should catch in such an inconvenient way.
+ *
+ * @param stream Stream to load from
+ * @return The attributes loaded
+ * @throws IOException If some problem happens
+ * @since 0.8
+ */
+ @SuppressWarnings("PMD.AvoidCatchingGenericException")
+ private static Map