From 7b68d44ddf2e6522d2b2aa1efead9ccc8b4ef224 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Tue, 21 Jun 2016 11:14:48 -0400 Subject: [PATCH] Read Elasticsearch manifest via URL This commit modifies reading the Elasticsearch jar manifest via the URL instead of converting the URL to an NIO path for increased portability. Relates #18999 --- .../main/java/org/elasticsearch/Build.java | 21 +++++-------------- .../java/org/elasticsearch/BuildTests.java | 8 +++---- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/Build.java b/core/src/main/java/org/elasticsearch/Build.java index f844e3b4040..25da5f28166 100644 --- a/core/src/main/java/org/elasticsearch/Build.java +++ b/core/src/main/java/org/elasticsearch/Build.java @@ -19,16 +19,11 @@ package org.elasticsearch; -import org.elasticsearch.common.SuppressForbidden; -import org.elasticsearch.common.io.PathUtils; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import java.io.IOException; -import java.net.URISyntaxException; import java.net.URL; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.jar.JarInputStream; import java.util.jar.Manifest; @@ -47,9 +42,9 @@ public class Build { final String date; final boolean isSnapshot; - Path path = getElasticsearchCodebase(); - if (path.toString().endsWith(".jar")) { - try (JarInputStream jar = new JarInputStream(Files.newInputStream(path))) { + final URL url = getElasticsearchCodebase(); + if (url.toString().endsWith(".jar")) { + try (JarInputStream jar = new JarInputStream(url.openStream())) { Manifest manifest = jar.getManifest(); shortHash = manifest.getMainAttributes().getValue("Change"); date = manifest.getMainAttributes().getValue("Build-Date"); @@ -80,14 +75,8 @@ public class Build { /** * Returns path to elasticsearch codebase path */ - @SuppressForbidden(reason = "looks up path of elasticsearch.jar directly") - static Path getElasticsearchCodebase() { - URL url = Build.class.getProtectionDomain().getCodeSource().getLocation(); - try { - return PathUtils.get(url.toURI()); - } catch (URISyntaxException bogus) { - throw new RuntimeException(bogus); - } + static URL getElasticsearchCodebase() { + return Build.class.getProtectionDomain().getCodeSource().getLocation(); } private String shortHash; diff --git a/core/src/test/java/org/elasticsearch/BuildTests.java b/core/src/test/java/org/elasticsearch/BuildTests.java index f02d2f27e82..7bff83262b3 100644 --- a/core/src/test/java/org/elasticsearch/BuildTests.java +++ b/core/src/test/java/org/elasticsearch/BuildTests.java @@ -22,16 +22,16 @@ package org.elasticsearch; import org.elasticsearch.test.ESTestCase; import java.io.IOException; -import java.nio.file.AccessMode; -import java.nio.file.Path; +import java.io.InputStream; +import java.net.URL; public class BuildTests extends ESTestCase { /** Asking for the jar metadata should not throw exception in tests, no matter how configured */ public void testJarMetadata() throws IOException { - Path path = Build.getElasticsearchCodebase(); + URL url = Build.getElasticsearchCodebase(); // throws exception if does not exist, or we cannot access it - path.getFileSystem().provider().checkAccess(path, AccessMode.READ); + try (InputStream ignored = url.openStream()) {} // these should never be null assertNotNull(Build.CURRENT.date()); assertNotNull(Build.CURRENT.shortHash());