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
This commit is contained in:
David Ostrovsky
2016-11-10 08:02:58 +01:00
committed by David Ostrovsky
parent 3f771978e0
commit 5a09d99add
3 changed files with 40 additions and 60 deletions

View File

@@ -18,44 +18,32 @@ import com.google.common.cache.Cache;
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");
}
}

View File

@@ -18,44 +18,31 @@ import com.google.common.cache.Cache;
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");
}
}

View File

@@ -21,6 +21,7 @@ import static java.nio.file.Files.isReadable;
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 @@ public class StaticModule extends ServletModule {
@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 @@ public class StaticModule extends ServletModule {
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;