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
This commit is contained in:
Jason Tedor 2016-06-21 11:14:48 -04:00 committed by GitHub
parent 70482d1e39
commit 7b68d44ddf
2 changed files with 9 additions and 20 deletions

View File

@ -19,16 +19,11 @@
package org.elasticsearch; 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.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.io.stream.StreamOutput;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.jar.JarInputStream; import java.util.jar.JarInputStream;
import java.util.jar.Manifest; import java.util.jar.Manifest;
@ -47,9 +42,9 @@ public class Build {
final String date; final String date;
final boolean isSnapshot; final boolean isSnapshot;
Path path = getElasticsearchCodebase(); final URL url = getElasticsearchCodebase();
if (path.toString().endsWith(".jar")) { if (url.toString().endsWith(".jar")) {
try (JarInputStream jar = new JarInputStream(Files.newInputStream(path))) { try (JarInputStream jar = new JarInputStream(url.openStream())) {
Manifest manifest = jar.getManifest(); Manifest manifest = jar.getManifest();
shortHash = manifest.getMainAttributes().getValue("Change"); shortHash = manifest.getMainAttributes().getValue("Change");
date = manifest.getMainAttributes().getValue("Build-Date"); date = manifest.getMainAttributes().getValue("Build-Date");
@ -80,14 +75,8 @@ public class Build {
/** /**
* Returns path to elasticsearch codebase path * Returns path to elasticsearch codebase path
*/ */
@SuppressForbidden(reason = "looks up path of elasticsearch.jar directly") static URL getElasticsearchCodebase() {
static Path getElasticsearchCodebase() { return Build.class.getProtectionDomain().getCodeSource().getLocation();
URL url = Build.class.getProtectionDomain().getCodeSource().getLocation();
try {
return PathUtils.get(url.toURI());
} catch (URISyntaxException bogus) {
throw new RuntimeException(bogus);
}
} }
private String shortHash; private String shortHash;

View File

@ -22,16 +22,16 @@ package org.elasticsearch;
import org.elasticsearch.test.ESTestCase; import org.elasticsearch.test.ESTestCase;
import java.io.IOException; import java.io.IOException;
import java.nio.file.AccessMode; import java.io.InputStream;
import java.nio.file.Path; import java.net.URL;
public class BuildTests extends ESTestCase { public class BuildTests extends ESTestCase {
/** Asking for the jar metadata should not throw exception in tests, no matter how configured */ /** Asking for the jar metadata should not throw exception in tests, no matter how configured */
public void testJarMetadata() throws IOException { public void testJarMetadata() throws IOException {
Path path = Build.getElasticsearchCodebase(); URL url = Build.getElasticsearchCodebase();
// throws exception if does not exist, or we cannot access it // 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 // these should never be null
assertNotNull(Build.CURRENT.date()); assertNotNull(Build.CURRENT.date());
assertNotNull(Build.CURRENT.shortHash()); assertNotNull(Build.CURRENT.shortHash());