Fix warning "close() called when useCnt is already zero"

WatchConfig was closing the repository twice in getProjectWatches
method. Using a try-with-resources, the method was closing the
repository and the WatchConfig which was also closing the repository.

WatchConfig was keeping a reference on the repository passed to the
load method and closing it in the close method.

VersionedMetaData default implementation of load does not keep a
reference on the repository. There is no added value of keeping a
reference on the repository, it's not reused anyway.

Remove repository reference and no longer implement AutoCloseable.

Also rename open method to read to not leave the impression that
something needs to be close and to follow convention of other
VersionedMetaData classes.

Bug: Issue 4290
Change-Id: Ibc347b1837a6ba21f325210359ce5f10b0a3e69c
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/account/WatchConfig.java b/gerrit-server/src/main/java/com/google/gerrit/server/account/WatchConfig.java
index fb9f1a3..c3d28ca 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/account/WatchConfig.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/account/WatchConfig.java
@@ -92,7 +92,7 @@
  * Unknown notify types are ignored and removed on save.
  */
 public class WatchConfig extends VersionedMetaData
-    implements AutoCloseable, ValidationError.Sink {
+    implements ValidationError.Sink {
   @Singleton
   public static class Accessor {
     private final GitRepositoryManager repoManager;
@@ -114,8 +114,8 @@
 
     public Map<ProjectWatchKey, Set<NotifyType>> getProjectWatches(
         Account.Id accountId) throws IOException, ConfigInvalidException {
-      try (Repository git = repoManager.openRepository(allUsersName);
-          WatchConfig watchConfig = new WatchConfig(accountId)) {
+      try (Repository git = repoManager.openRepository(allUsersName)) {
+        WatchConfig watchConfig = new WatchConfig(accountId);
         watchConfig.load(git);
         return watchConfig.getProjectWatches();
       }
@@ -123,39 +123,38 @@
 
     public void upsertProjectWatches(Account.Id accountId,
         Map<ProjectWatchKey, Set<NotifyType>> newProjectWatches)
-            throws IOException, ConfigInvalidException {
-      try (WatchConfig watchConfig = open(accountId)) {
-        Map<ProjectWatchKey, Set<NotifyType>> projectWatches =
-            watchConfig.getProjectWatches();
-        projectWatches.putAll(newProjectWatches);
-        commit(watchConfig);
-      }
+        throws IOException, ConfigInvalidException {
+      WatchConfig watchConfig = read(accountId);
+      Map<ProjectWatchKey, Set<NotifyType>> projectWatches =
+          watchConfig.getProjectWatches();
+      projectWatches.putAll(newProjectWatches);
+      commit(watchConfig);
     }
 
     public void deleteProjectWatches(Account.Id accountId,
         Collection<ProjectWatchKey> projectWatchKeys)
             throws IOException, ConfigInvalidException {
-      try (WatchConfig watchConfig = open(accountId)) {
-        Map<ProjectWatchKey, Set<NotifyType>> projectWatches =
-            watchConfig.getProjectWatches();
-        boolean commit = false;
-        for (ProjectWatchKey key : projectWatchKeys) {
-          if (projectWatches.remove(key) != null) {
-            commit = true;
-          }
+      WatchConfig watchConfig = read(accountId);
+      Map<ProjectWatchKey, Set<NotifyType>> projectWatches =
+          watchConfig.getProjectWatches();
+      boolean commit = false;
+      for (ProjectWatchKey key : projectWatchKeys) {
+        if (projectWatches.remove(key) != null) {
+          commit = true;
         }
-        if (commit) {
-          commit(watchConfig);
-        }
+      }
+      if (commit) {
+        commit(watchConfig);
       }
     }
 
-    private WatchConfig open(Account.Id accountId)
+    private WatchConfig read(Account.Id accountId)
         throws IOException, ConfigInvalidException {
-      Repository git = repoManager.openRepository(allUsersName);
-      WatchConfig watchConfig = new WatchConfig(accountId);
-      watchConfig.load(git);
-      return watchConfig;
+      try (Repository git = repoManager.openRepository(allUsersName)) {
+        WatchConfig watchConfig = new WatchConfig(accountId);
+        watchConfig.load(git);
+        return watchConfig;
+      }
     }
 
     private void commit(WatchConfig watchConfig)
@@ -186,7 +185,6 @@
   private final Account.Id accountId;
   private final String ref;
 
-  private Repository git;
   private Map<ProjectWatchKey, Set<NotifyType>> projectWatches;
   private List<ValidationError> validationErrors;
 
@@ -201,13 +199,6 @@
   }
 
   @Override
-  public void load(Repository git) throws IOException, ConfigInvalidException {
-    checkState(this.git == null);
-    this.git = git;
-    super.load(git);
-  }
-
-  @Override
   protected void onLoad() throws IOException, ConfigInvalidException {
     Config cfg = readConfig(WATCH_CONFIG);
     projectWatches = parse(accountId, cfg, this);
@@ -282,13 +273,6 @@
     return true;
   }
 
-  @Override
-  public void close() {
-    if (git != null) {
-      git.close();
-    }
-  }
-
   private void checkLoaded() {
     checkState(projectWatches != null, "project watches not loaded yet");
   }
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/git/validators/CommitValidators.java b/gerrit-server/src/main/java/com/google/gerrit/server/git/validators/CommitValidators.java
index e7b19aa..d4956ab 100644
--- a/gerrit-server/src/main/java/com/google/gerrit/server/git/validators/CommitValidators.java
+++ b/gerrit-server/src/main/java/com/google/gerrit/server/git/validators/CommitValidators.java
@@ -374,7 +374,6 @@
         Account.Id accountId = Account.Id.fromRef(refControl.getRefName());
         if (accountId != null) {
           try {
-            @SuppressWarnings("resource")
             WatchConfig wc = new WatchConfig(accountId);
             wc.load(repo, receiveEvent.command.getNewId());
             if (!wc.getValidationErrors().isEmpty()) {