Skip to content

Commit 7b68d44

Browse files
authored
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
1 parent 70482d1 commit 7b68d44

File tree

2 files changed

+9
-20
lines changed

2 files changed

+9
-20
lines changed

core/src/main/java/org/elasticsearch/Build.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,11 @@
1919

2020
package org.elasticsearch;
2121

22-
import org.elasticsearch.common.SuppressForbidden;
23-
import org.elasticsearch.common.io.PathUtils;
2422
import org.elasticsearch.common.io.stream.StreamInput;
2523
import org.elasticsearch.common.io.stream.StreamOutput;
2624

2725
import java.io.IOException;
28-
import java.net.URISyntaxException;
2926
import java.net.URL;
30-
import java.nio.file.Files;
31-
import java.nio.file.Path;
3227
import java.util.jar.JarInputStream;
3328
import java.util.jar.Manifest;
3429

@@ -47,9 +42,9 @@ public class Build {
4742
final String date;
4843
final boolean isSnapshot;
4944

50-
Path path = getElasticsearchCodebase();
51-
if (path.toString().endsWith(".jar")) {
52-
try (JarInputStream jar = new JarInputStream(Files.newInputStream(path))) {
45+
final URL url = getElasticsearchCodebase();
46+
if (url.toString().endsWith(".jar")) {
47+
try (JarInputStream jar = new JarInputStream(url.openStream())) {
5348
Manifest manifest = jar.getManifest();
5449
shortHash = manifest.getMainAttributes().getValue("Change");
5550
date = manifest.getMainAttributes().getValue("Build-Date");
@@ -80,14 +75,8 @@ public class Build {
8075
/**
8176
* Returns path to elasticsearch codebase path
8277
*/
83-
@SuppressForbidden(reason = "looks up path of elasticsearch.jar directly")
84-
static Path getElasticsearchCodebase() {
85-
URL url = Build.class.getProtectionDomain().getCodeSource().getLocation();
86-
try {
87-
return PathUtils.get(url.toURI());
88-
} catch (URISyntaxException bogus) {
89-
throw new RuntimeException(bogus);
90-
}
78+
static URL getElasticsearchCodebase() {
79+
return Build.class.getProtectionDomain().getCodeSource().getLocation();
9180
}
9281

9382
private String shortHash;

core/src/test/java/org/elasticsearch/BuildTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222
import org.elasticsearch.test.ESTestCase;
2323

2424
import java.io.IOException;
25-
import java.nio.file.AccessMode;
26-
import java.nio.file.Path;
25+
import java.io.InputStream;
26+
import java.net.URL;
2727

2828
public class BuildTests extends ESTestCase {
2929

3030
/** Asking for the jar metadata should not throw exception in tests, no matter how configured */
3131
public void testJarMetadata() throws IOException {
32-
Path path = Build.getElasticsearchCodebase();
32+
URL url = Build.getElasticsearchCodebase();
3333
// throws exception if does not exist, or we cannot access it
34-
path.getFileSystem().provider().checkAccess(path, AccessMode.READ);
34+
try (InputStream ignored = url.openStream()) {}
3535
// these should never be null
3636
assertNotNull(Build.CURRENT.date());
3737
assertNotNull(Build.CURRENT.shortHash());

0 commit comments

Comments
 (0)