Split generation of FS backend data directory to own class

This will allow to iterate over backends and get the corresponding
data directory without instantiating the LocalLargeFileRepository
instance.

Change-Id: If00ef2c96681a4e702fd7957a7ca0dbf5dae6dad
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LfsFsDataDirectoryManager.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LfsFsDataDirectoryManager.java
new file mode 100644
index 0000000..d8e6366
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LfsFsDataDirectoryManager.java
@@ -0,0 +1,68 @@
+// Copyright (C) 2019 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.lfs.fs;
+
+import com.google.common.base.Strings;
+import com.google.gerrit.extensions.annotations.PluginData;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import com.googlesource.gerrit.plugins.lfs.LfsBackend;
+import com.googlesource.gerrit.plugins.lfs.LfsConfigurationFactory;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+@Singleton
+public class LfsFsDataDirectoryManager {
+  private static final String KEY_DIRECTORY = "directory";
+
+  private final LfsConfigurationFactory configFactory;
+  private final Path defaultDataDir;
+
+  @Inject
+  LfsFsDataDirectoryManager(
+      LfsConfigurationFactory configFactory, @PluginData Path defaultDataDir) {
+    this.configFactory = configFactory;
+    this.defaultDataDir = defaultDataDir;
+  }
+
+  public Path ensureForBackend(LfsBackend backend) throws IOException {
+    return getForBackend(backend, true);
+  }
+
+  public Path getForBackend(LfsBackend backend, boolean ensure) throws IOException {
+    String dataDir =
+        configFactory.getGlobalConfig().getString(backend.type.name(), backend.name, KEY_DIRECTORY);
+    if (Strings.isNullOrEmpty(dataDir)) {
+      return defaultDataDir;
+    }
+
+    if (ensure) {
+      // note that the following method not only creates missing
+      // directory/directories but throws exception when path
+      // exists and points to file
+      Path ensured = Files.createDirectories(Paths.get(dataDir));
+
+      // we should at least make sure that directory is readable
+      if (!Files.isReadable(ensured)) {
+        throw new IOException("Path '" + ensured.toAbsolutePath() + "' cannot be accessed");
+      }
+
+      return ensured;
+    }
+    return Paths.get(dataDir);
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LocalLargeFileRepository.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LocalLargeFileRepository.java
index f67e0bb..069d163 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LocalLargeFileRepository.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LocalLargeFileRepository.java
@@ -17,20 +17,14 @@
 import static org.eclipse.jgit.lfs.lib.Constants.DOWNLOAD;
 import static org.eclipse.jgit.lfs.lib.Constants.UPLOAD;
 
-import com.google.common.base.Strings;
 import com.google.gerrit.extensions.annotations.PluginCanonicalWebUrl;
-import com.google.gerrit.extensions.annotations.PluginData;
 import com.google.inject.Inject;
 import com.google.inject.assistedinject.Assisted;
 import com.googlesource.gerrit.plugins.lfs.LfsBackend;
 import com.googlesource.gerrit.plugins.lfs.LfsConfigurationFactory;
-import com.googlesource.gerrit.plugins.lfs.LfsGlobalConfig;
 import com.googlesource.gerrit.plugins.lfs.auth.AuthInfo;
 import com.googlesource.gerrit.plugins.lfs.auth.ExpiringAction;
 import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
 import java.time.Instant;
 import org.eclipse.jgit.lfs.lib.AnyLongObjectId;
 import org.eclipse.jgit.lfs.server.Response;
@@ -50,15 +44,13 @@
 
   @Inject
   LocalLargeFileRepository(
+      LfsFsDataDirectoryManager dataDirManager,
       LfsConfigurationFactory configFactory,
       LfsFsRequestAuthorizer authorizer,
       @PluginCanonicalWebUrl String url,
-      @PluginData Path defaultDataDir,
       @Assisted LfsBackend backend)
       throws IOException {
-    super(
-        getContentUrl(url, backend),
-        getOrCreateDataDir(configFactory.getGlobalConfig(), backend, defaultDataDir));
+    super(getContentUrl(url, backend), dataDirManager.ensureForBackend(backend));
     this.authorizer = authorizer;
     this.servletUrlPattern = "/" + getContentPath(backend) + "*";
     this.expiresIn =
@@ -99,24 +91,4 @@
   private static String getContentPath(LfsBackend backend) {
     return String.format(CONTENT_PATH_TEMPLATE, backend.name());
   }
-
-  private static Path getOrCreateDataDir(
-      LfsGlobalConfig config, LfsBackend backendConfig, Path defaultDataDir) throws IOException {
-    String dataDir = config.getString(backendConfig.type.name(), backendConfig.name, "directory");
-    if (Strings.isNullOrEmpty(dataDir)) {
-      return defaultDataDir;
-    }
-
-    // note that the following method not only creates missing
-    // directory/directories but throws exception when path
-    // exists and points to file
-    Path ensured = Files.createDirectories(Paths.get(dataDir));
-
-    // we should at least make sure that directory is readable
-    if (!Files.isReadable(ensured)) {
-      throw new IOException("Path '" + ensured.toAbsolutePath() + "' cannot be accessed");
-    }
-
-    return ensured;
-  }
 }