StaticModule: Lazily initialize zip FileSystem

Java's zip FileSystem is rather memory-hungry, as it maintains the
full central directory in memory, among other internal state. In the
headless server case, we never actually need to serve a UI from the
war, so it's not necessary to even load the zip FileSystem.

Maintain a synchronized map of zip FileSystems in GerritLauncher,
since each one still needs to be a singleton, but we don't want to
eagerly load it just because we wanted to look up the war location.

Change-Id: Id186964b2a197a8a0e874f86c72c21e06fc1388f
This commit is contained in:
Dave Borowitz
2015-11-16 15:41:00 -05:00
parent 982c80de15
commit f3ff4db9c3
2 changed files with 167 additions and 129 deletions

View File

@@ -38,7 +38,9 @@ import java.security.CodeSource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.jar.Attributes;
@@ -300,9 +302,10 @@ public final class GerritLauncher {
}
private static volatile File myArchive;
private static volatile FileSystem myArchiveFs;
private static volatile File myHome;
private static final Map<Path, FileSystem> zipFileSystems = new HashMap<>();
/**
* Locate the JAR/WAR file we were launched from.
*
@@ -319,19 +322,24 @@ public final class GerritLauncher {
return result;
}
result = locateMyArchive();
myArchiveFs = FileSystems.newFileSystem(
URI.create("jar:" + result.toPath().toUri()),
Collections.<String, String> emptyMap());
myArchive = result;
}
}
return result;
}
public static FileSystem getDistributionArchiveFileSystem()
throws FileNotFoundException, IOException {
getDistributionArchive();
return myArchiveFs;
public static synchronized FileSystem getZipFileSystem(Path zip)
throws IOException {
// FileSystems canonicalizes the path, so we should too.
zip = zip.toRealPath();
FileSystem zipFs = zipFileSystems.get(zip);
if (zipFs == null) {
zipFs = FileSystems.newFileSystem(
URI.create("jar:" + zip.toUri()),
Collections.<String, String> emptyMap());
zipFileSystems.put(zip, zipFs);
}
return zipFs;
}
private static File locateMyArchive() throws FileNotFoundException {