JarScanner: Fix resource leak flagged by infer

This fixes resource leak with JarFile instance acquired by call to
ctor of JarScanner.

Bug: Issue 5198
Change-Id: I80feabcf9e0bfd683d1d8c47f3fd6c9be3ff65a6
This commit is contained in:
David Ostrovsky 2016-12-26 09:06:23 +01:00 committed by David Pursehouse
parent 3e82acac08
commit 944b51634a
3 changed files with 26 additions and 21 deletions

View File

@ -163,20 +163,21 @@ public class SwitchSecureStore extends SiteProgram {
private String getNewSecureStoreClassName(Path secureStore)
throws IOException {
JarScanner scanner = new JarScanner(secureStore);
List<String> newSecureStores =
scanner.findSubClassesOf(SecureStore.class);
if (newSecureStores.isEmpty()) {
throw new RuntimeException(String.format(
"Cannot find implementation of SecureStore interface in %s",
secureStore.toAbsolutePath()));
try (JarScanner scanner = new JarScanner(secureStore)) {
List<String> newSecureStores =
scanner.findSubClassesOf(SecureStore.class);
if (newSecureStores.isEmpty()) {
throw new RuntimeException(String.format(
"Cannot find implementation of SecureStore interface in %s",
secureStore.toAbsolutePath()));
}
if (newSecureStores.size() > 1) {
throw new RuntimeException(String.format(
"Found too many implementations of SecureStore:\n%s\nin %s", Joiner
.on("\n").join(newSecureStores), secureStore.toAbsolutePath()));
}
return Iterables.getOnlyElement(newSecureStores);
}
if (newSecureStores.size() > 1) {
throw new RuntimeException(String.format(
"Found too many implementations of SecureStore:\n%s\nin %s", Joiner
.on("\n").join(newSecureStores), secureStore.toAbsolutePath()));
}
return Iterables.getOnlyElement(newSecureStores);
}
private String getCurrentSecureStoreClassName(SitePaths sitePaths) {

View File

@ -310,13 +310,12 @@ public class BaseInit extends SiteProgram {
return null;
}
try {
Path secureStoreLib = Paths.get(secureStore);
if (!Files.exists(secureStoreLib)) {
throw new InvalidSecureStoreException(String.format(
"File %s doesn't exist", secureStore));
}
JarScanner scanner = new JarScanner(secureStoreLib);
Path secureStoreLib = Paths.get(secureStore);
if (!Files.exists(secureStoreLib)) {
throw new InvalidSecureStoreException(String.format(
"File %s doesn't exist", secureStore));
}
try (JarScanner scanner = new JarScanner(secureStoreLib)) {
List<String> secureStores =
scanner.findSubClassesOf(SecureStore.class);
if (secureStores.isEmpty()) {

View File

@ -53,7 +53,7 @@ import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
public class JarScanner implements PluginContentScanner {
public class JarScanner implements PluginContentScanner, AutoCloseable {
private static final int SKIP_ALL = ClassReader.SKIP_CODE
| ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES;
private final JarFile jarFile;
@ -131,6 +131,11 @@ public class JarScanner implements PluginContentScanner {
return findSubClassesOf(superClass.getName());
}
@Override
public void close() throws IOException {
jarFile.close();
}
private List<String> findSubClassesOf(String superClass) throws IOException {
String name = superClass.replace('.', '/');