Use try-with-resources statements

- instead of finally blocks
- in cases of missing try-finally

Change-Id: I94f481a33d8e6a3180c436245d6e95e4d525280c
This commit is contained in:
Urs Wolfer
2015-06-11 21:44:59 +02:00
parent b17ceb8587
commit 5e90c63dea
112 changed files with 727 additions and 1332 deletions

View File

@@ -127,8 +127,7 @@ public class SshKeyCacheImpl implements SshKeyCache {
@Override
public Iterable<SshKeyCacheEntry> load(String username) throws Exception {
final ReviewDb db = schema.open();
try {
try (ReviewDb db = schema.open()) {
final AccountExternalId.Key key =
new AccountExternalId.Key(SCHEME_USERNAME, username);
final AccountExternalId user = db.accountExternalIds().get(key);
@@ -147,8 +146,6 @@ public class SshKeyCacheImpl implements SshKeyCache {
return NO_KEYS;
}
return Collections.unmodifiableList(kl);
} finally {
db.close();
}
}

View File

@@ -92,10 +92,22 @@ public class LsUserRefs extends SshCommand {
IdentifiedUser user = userFactory.create(userAccount.getId());
ProjectControl userProjectControl = projectControl.forUser(user);
Repository repo;
try {
repo = repoManager.openRepository(userProjectControl.getProject()
.getNameKey());
try (Repository repo = repoManager.openRepository(
userProjectControl.getProject().getNameKey())) {
try {
Map<String, Ref> refsMap =
new VisibleRefFilter(tagCache, changeCache, repo, userProjectControl,
db, true).filter(repo.getRefDatabase().getRefs(ALL), false);
for (final String ref : refsMap.keySet()) {
if (!onlyRefsHeads || ref.startsWith(RefNames.REFS_HEADS)) {
stdout.println(ref);
}
}
} catch (IOException e) {
throw new Failure(1, "fatal: Error reading refs: '"
+ projectControl.getProject().getNameKey(), e);
}
} catch (RepositoryNotFoundException e) {
throw new UnloggedFailure("fatal: '"
+ projectControl.getProject().getNameKey() + "': not a git archive");
@@ -103,22 +115,5 @@ public class LsUserRefs extends SshCommand {
throw new UnloggedFailure("fatal: Error opening: '"
+ projectControl.getProject().getNameKey());
}
try {
Map<String, Ref> refsMap =
new VisibleRefFilter(tagCache, changeCache, repo, userProjectControl,
db, true).filter(repo.getRefDatabase().getRefs(ALL), false);
for (final String ref : refsMap.keySet()) {
if (!onlyRefsHeads || ref.startsWith(RefNames.REFS_HEADS)) {
stdout.println(ref);
}
}
} catch (IOException e) {
throw new Failure(1, "fatal: Error reading refs: '"
+ projectControl.getProject().getNameKey(), e);
} finally {
repo.close();
}
}
}

View File

@@ -171,11 +171,10 @@ public class UploadArchive extends AbstractGitCommand {
throw new Failure(5, "fatal: cannot perform upload-archive operation");
}
try {
// The archive is sent in DATA sideband channel
SideBandOutputStream sidebandOut =
try (SideBandOutputStream sidebandOut =
new SideBandOutputStream(SideBandOutputStream.CH_DATA,
SideBandOutputStream.MAX_BUF, out);
SideBandOutputStream.MAX_BUF, out)) {
new ArchiveCommand(repo)
.setFormat(f.name())
.setFormatOptions(getFormatOptions(f))
@@ -185,18 +184,17 @@ public class UploadArchive extends AbstractGitCommand {
.setOutputStream(sidebandOut)
.call();
sidebandOut.flush();
sidebandOut.close();
} catch (GitAPIException e) {
throw new Failure(7, "fatal: git api exception, " + e);
}
} catch (Failure f) {
// Report the error in ERROR sideband channel
SideBandOutputStream sidebandError =
try (SideBandOutputStream sidebandError =
new SideBandOutputStream(SideBandOutputStream.CH_ERROR,
SideBandOutputStream.MAX_BUF, out);
sidebandError.write(f.getMessage().getBytes(UTF_8));
sidebandError.flush();
sidebandError.close();
SideBandOutputStream.MAX_BUF, out)) {
sidebandError.write(f.getMessage().getBytes(UTF_8));
sidebandError.flush();
}
throw f;
} finally {
// In any case, cleanly close the packetOut channel