PolyGerrit: Don't create development servlets in production code path

To satisfy Guice wiring, it's not necessary needed to create a dummy
provider that is not actually used. Use @Nullable instead to bind
to Provider that optionally returns null. This simplifies the code
in development servlets and avoid eager created (unused) development
instances in production code path.

Also rename the development servlets with Dev suffix to make the intent
more clear.

This is a preparation change to abstract the build system and support
Bazel in addition to Buck.

Change-Id: I9c2a7b61509de446e292af9cb0217d24547bbbf5
diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/BowerComponentsServlet.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/BowerComponentsDevServlet.java
similarity index 62%
rename from gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/BowerComponentsServlet.java
rename to gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/BowerComponentsDevServlet.java
index 3e0f833..c214d1f 100644
--- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/BowerComponentsServlet.java
+++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/BowerComponentsDevServlet.java
@@ -18,44 +18,32 @@
 import com.google.gerrit.launcher.GerritLauncher;
 
 import java.io.IOException;
-import java.nio.file.Files;
 import java.nio.file.Path;
+import java.util.Objects;
 
-class BowerComponentsServlet extends ResourceServlet {
+/* Bower component servlet only used in development mode */
+class BowerComponentsDevServlet extends ResourceServlet {
   private static final long serialVersionUID = 1L;
 
-  private final Path zip;
   private final Path bowerComponents;
 
-  BowerComponentsServlet(Cache<Path, Resource> cache, Path buckOut)
+  BowerComponentsDevServlet(Cache<Path, Resource> cache, Path buckOut)
       throws IOException {
     super(cache, true);
-    zip = getZipPath(buckOut);
-    if (zip == null || !Files.exists(zip)) {
-      bowerComponents = null;
-    } else {
-      bowerComponents = GerritLauncher
-          .newZipFileSystem(zip)
-          .getPath("/");
-    }
+    Objects.requireNonNull(buckOut);
+
+    Path zip = buckOut.resolve("gen")
+        .resolve("polygerrit-ui")
+        .resolve("polygerrit_components")
+        .resolve("polygerrit_components.bower_components.zip");
+
+    bowerComponents = GerritLauncher
+        .newZipFileSystem(zip)
+        .getPath("/");
   }
 
   @Override
   protected Path getResourcePath(String pathInfo) throws IOException {
-    if (bowerComponents == null) {
-      throw new IOException("No polymer components found: " + zip
-          + ". Run `buck build //polygerrit-ui:polygerrit_components`?");
-    }
     return bowerComponents.resolve(pathInfo);
   }
-
-  private static Path getZipPath(Path buckOut) {
-    if (buckOut == null) {
-      return null;
-    }
-    return buckOut.resolve("gen")
-        .resolve("polygerrit-ui")
-        .resolve("polygerrit_components")
-        .resolve("polygerrit_components.bower_components.zip");
-  }
 }
diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/FontsServlet.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/FontsDevServlet.java
similarity index 64%
rename from gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/FontsServlet.java
rename to gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/FontsDevServlet.java
index 3a8c8cb..9925c49 100644
--- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/FontsServlet.java
+++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/FontsDevServlet.java
@@ -18,44 +18,31 @@
 import com.google.gerrit.launcher.GerritLauncher;
 
 import java.io.IOException;
-import java.nio.file.Files;
 import java.nio.file.Path;
+import java.util.Objects;
 
-class FontsServlet extends ResourceServlet {
+/* Font servlet only used in development mode */
+class FontsDevServlet extends ResourceServlet {
   private static final long serialVersionUID = 1L;
 
-  private final Path zip;
   private final Path fonts;
 
-  FontsServlet(Cache<Path, Resource> cache, Path buckOut)
+  FontsDevServlet(Cache<Path, Resource> cache, Path buckOut)
       throws IOException {
     super(cache, true);
-    zip = getZipPath(buckOut);
-    if (zip == null || !Files.exists(zip)) {
-      fonts = null;
-    } else {
-      fonts = GerritLauncher
-          .newZipFileSystem(zip)
-          .getPath("/");
-    }
+    Objects.requireNonNull(buckOut);
+
+    Path zip = buckOut.resolve("gen")
+        .resolve("polygerrit-ui")
+        .resolve("fonts")
+        .resolve("fonts.zip");
+    fonts = GerritLauncher
+        .newZipFileSystem(zip)
+        .getPath("/");
   }
 
   @Override
   protected Path getResourcePath(String pathInfo) throws IOException {
-    if (fonts == null) {
-      throw new IOException("No fonts found: " + zip
-          + ". Run `buck build //polygerrit-ui:fonts`?");
-    }
     return fonts.resolve(pathInfo);
   }
-
-  private static Path getZipPath(Path buckOut) {
-    if (buckOut == null) {
-      return null;
-    }
-    return buckOut.resolve("gen")
-        .resolve("polygerrit-ui")
-        .resolve("fonts")
-        .resolve("fonts.zip");
-  }
 }
diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/StaticModule.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/StaticModule.java
index 31e337e..3770469 100644
--- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/StaticModule.java
+++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/raw/StaticModule.java
@@ -21,6 +21,7 @@
 
 import com.google.common.cache.Cache;
 import com.google.common.collect.ImmutableList;
+import com.google.gerrit.common.Nullable;
 import com.google.gerrit.extensions.client.UiType;
 import com.google.gerrit.httpd.XsrfCookieFilter;
 import com.google.gerrit.httpd.raw.ResourceServlet.Resource;
@@ -282,16 +283,20 @@
 
     @Provides
     @Singleton
-    BowerComponentsServlet getBowerComponentsServlet(
+    BowerComponentsDevServlet getBowerComponentsServlet(
         @Named(CACHE) Cache<Path, Resource> cache) throws IOException {
-      return new BowerComponentsServlet(cache, getPaths().buckOut);
+      return getPaths().isDev()
+          ? new BowerComponentsDevServlet(cache, getPaths().buckOut)
+          : null;
     }
 
     @Provides
     @Singleton
-    FontsServlet getFontsServlet(
+    FontsDevServlet getFontsServlet(
         @Named(CACHE) Cache<Path, Resource> cache) throws IOException {
-      return new FontsServlet(cache, getPaths().buckOut);
+      return getPaths().isDev()
+          ? new FontsDevServlet(cache, getPaths().buckOut)
+          : null;
     }
 
     private Path polyGerritBasePath() {
@@ -430,16 +435,16 @@
     private final Paths paths;
     private final HttpServlet polyGerritIndex;
     private final PolyGerritUiServlet polygerritUI;
-    private final BowerComponentsServlet bowerComponentServlet;
-    private final FontsServlet fontServlet;
+    private final BowerComponentsDevServlet bowerComponentServlet;
+    private final FontsDevServlet fontServlet;
 
     @Inject
     PolyGerritFilter(GerritOptions options,
         Paths paths,
         @Named(POLYGERRIT_INDEX_SERVLET) HttpServlet polyGerritIndex,
         PolyGerritUiServlet polygerritUI,
-        BowerComponentsServlet bowerComponentServlet,
-        FontsServlet fontServlet) {
+        @Nullable BowerComponentsDevServlet bowerComponentServlet,
+        @Nullable FontsDevServlet fontServlet) {
       this.paths = paths;
       this.options = options;
       this.polyGerritIndex = polyGerritIndex;