Fix JarManifests util

This commit is contained in:
Jason Penilla 2021-11-24 15:45:51 -08:00 committed by MiniDigger | Martin
parent 2e347b629d
commit e6fe6effee

View file

@ -6,105 +6,44 @@ Subject: [PATCH] Add Git information to version command/on startup
diff --git a/src/main/java/io/papermc/paper/util/JarManifests.java b/src/main/java/io/papermc/paper/util/JarManifests.java diff --git a/src/main/java/io/papermc/paper/util/JarManifests.java b/src/main/java/io/papermc/paper/util/JarManifests.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..02ad04c41b9c3ec68b1dcdc22c538340d8d3455b index 0000000000000000000000000000000000000000..cfd5fa044f58c8a5493f8efc08280b22c401a1da
--- /dev/null --- /dev/null
+++ b/src/main/java/io/papermc/paper/util/JarManifests.java +++ b/src/main/java/io/papermc/paper/util/JarManifests.java
@@ -0,0 +1,93 @@ @@ -0,0 +1,32 @@
+/*
+Copyright (c) 2012-2017, 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 io.papermc.paper.util; +package io.papermc.paper.util;
+ +
+import java.io.IOException; +import java.io.IOException;
+import java.io.InputStream; +import java.io.InputStream;
+import java.net.URL; +import java.util.Collections;
+import java.util.*; +import java.util.Map;
+import java.util.WeakHashMap;
+import java.util.jar.Manifest; +import java.util.jar.Manifest;
+import org.jetbrains.annotations.NotNull; +import org.checkerframework.checker.nullness.qual.Nullable;
+import org.jetbrains.annotations.ApiStatus;
+ +
+/** +@ApiStatus.Internal
+ * Modified version of jcabi-manifests
+ *
+ */
+public final class JarManifests { +public final class JarManifests {
+ private JarManifests() {
+ }
+ +
+ public static final JarManifests JAR_MANIFESTS = new JarManifests(); + private static final Map<ClassLoader, Manifest> MANIFESTS = Collections.synchronizedMap(new WeakHashMap<>());
+ public static final Map<String, String> MANIFEST_MAP;
+ +
+ static { + public static Manifest manifest(final ClassLoader loader) {
+ try { + return MANIFESTS.computeIfAbsent(loader, classLoader -> {
+ MANIFEST_MAP = Collections.unmodifiableMap(JAR_MANIFESTS.getManifestMap()); + final @Nullable InputStream stream = classLoader.getResourceAsStream("META-INF/MANIFEST.MF");
+ if (stream == null) {
+ throw new IllegalArgumentException("Provided ClassLoader does not have a manifest file in the correct location!");
+ }
+ try (stream) {
+ return new Manifest(stream);
+ } catch (final IOException ex) { + } catch (final IOException ex) {
+ throw new RuntimeException(ex); + throw new RuntimeException("Failed to read manifest.");
+ } + }
+ } + });
+
+ private JarManifests() {}
+
+ // Based on:
+ // https://github.com/jcabi/jcabi-manifests/blob/c4e1dd22bb6099769b8d279ebe3737e5b638b278/src/main/java/com/jcabi/manifests/ClasspathMfs.java#L49-L58
+
+ /**
+ * Get collection containing all META-INF/MANIFEST.MF streams
+ *
+ * @throws IOException if unable to get resources from classloader
+ */
+ public @NotNull Collection<@NotNull InputStream> getManifestStreams() throws IOException {
+ final Enumeration<URL> resources = getClass().getClassLoader().getResources("META-INF/MANIFEST.MF");
+ final Collection<InputStream> streams = new ArrayList<>(1);
+ while (resources.hasMoreElements()) {
+ streams.add(resources.nextElement().openStream());
+ }
+ return streams;
+ }
+
+ // Based on:
+ // https://github.com/jcabi/jcabi-manifests/blob/c4e1dd22bb6099769b8d279ebe3737e5b638b278/src/main/java/com/jcabi/manifests/Manifests.java#L209-L225
+ // https://github.com/jcabi/jcabi-manifests/blob/c4e1dd22bb6099769b8d279ebe3737e5b638b278/src/main/java/com/jcabi/manifests/Manifests.java#L381-L388
+
+ /**
+ * Get map containing entries from all manifests
+ *
+ * @throws IOException if unable to get manifest streams
+ */
+ public @NotNull Map<@NotNull String, @NotNull String> getManifestMap() throws IOException {
+ final HashMap<String, String> attribs = new HashMap<>();
+ for (final InputStream stream : getManifestStreams()) {
+ for (final Map.Entry<Object, Object> attr: new Manifest(stream).getMainAttributes().entrySet()) {
+ attribs.put(attr.getKey().toString(), attr.getValue().toString());
+ }
+ }
+ return attribs;
+ } + }
+} +}
diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java diff --git a/src/main/java/org/bukkit/Bukkit.java b/src/main/java/org/bukkit/Bukkit.java
index 0521781a48d326c0a4a01b920188e9ce00b51ef0..ca64f6c0b3f89231a7d8a2bb08d732270831bd2d 100644 index 0521781a48d326c0a4a01b920188e9ce00b51ef0..7868a54eec71f7b31d7fabf3288eb2dc62649e92 100644
--- a/src/main/java/org/bukkit/Bukkit.java --- a/src/main/java/org/bukkit/Bukkit.java
+++ b/src/main/java/org/bukkit/Bukkit.java +++ b/src/main/java/org/bukkit/Bukkit.java
@@ -51,6 +51,7 @@ import org.bukkit.util.CachedServerIcon; @@ -51,6 +51,7 @@ import org.bukkit.util.CachedServerIcon;
@ -130,10 +69,10 @@ index 0521781a48d326c0a4a01b920188e9ce00b51ef0..ca64f6c0b3f89231a7d8a2bb08d73227
+ */ + */
+ @NotNull + @NotNull
+ public static String getVersionMessage() { + public static String getVersionMessage() {
+ Map<String, String> attributes = JarManifests.MANIFEST_MAP; + final var manifest = JarManifests.manifest(Bukkit.getServer().getClass().getClassLoader());
+ @NotNull String gitBranch = attributes.get("Git-Branch"); + final String gitBranch = manifest.getMainAttributes().getValue("Git-Branch");
+ @NotNull String gitCommit = attributes.get("Git-Commit"); + final String gitCommit = manifest.getMainAttributes().getValue("Git-Commit");
+ @NotNull String branchMsg = " on " + gitBranch; + String branchMsg = " on " + gitBranch;
+ if ("master".equals(gitBranch) || "main".equals(gitBranch)) { + if ("master".equals(gitBranch) || "main".equals(gitBranch)) {
+ branchMsg = ""; // Don't show branch on main/master + branchMsg = ""; // Don't show branch on main/master
+ } + }