Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 5 additions & 16 deletions core/src/main/java/org/elasticsearch/Build.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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");
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions core/src/test/java/org/elasticsearch/BuildTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
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());
Expand Down