Use Guice injection to pass GerritServer to HttpServlets

If an HttpServlet requires the GerritServer instance to do its work
(and most do) we can pass it through the constructor by way of Guice
rather than trying to obtain it during the init method.

Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2009-07-27 20:34:28 -07:00
parent ee6d0cf1d3
commit dd326e35bd
7 changed files with 86 additions and 71 deletions

View File

@@ -18,16 +18,14 @@ import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.reviewdb.Account;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.rpc.Common;
import com.google.gwtjsonrpc.server.XsrfException;
import com.google.gwtorm.client.OrmException;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
@@ -45,21 +43,13 @@ public class BecomeAnyAccountLoginServlet extends HttpServlet {
}
}
private boolean allowed;
private GerritServer server;
private final boolean allowed;
private final GerritServer server;
@Override
public void init(final ServletConfig config) throws ServletException {
super.init(config);
@Inject
BecomeAnyAccountLoginServlet(final GerritServer gs) {
server = gs;
allowed = isAllowed();
try {
server = GerritServer.getInstance();
} catch (OrmException e) {
throw new ServletException("Cannot load GerritServer", e);
} catch (XsrfException e) {
throw new ServletException("Cannot load GerritServer", e);
}
}
@Override

View File

@@ -24,8 +24,8 @@ import com.google.gerrit.client.reviewdb.PatchSet;
import com.google.gerrit.client.reviewdb.Project;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.rpc.Common;
import com.google.gwtjsonrpc.server.XsrfException;
import com.google.gwtorm.client.OrmException;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import eu.medsea.mimeutil.MimeType;
@@ -66,21 +66,19 @@ import javax.servlet.http.HttpServletResponse;
@Singleton
public class CatServlet extends HttpServlet {
private static final MimeType ZIP = new MimeType("application/zip");
private GerritServer server;
private SecureRandom rng;
private final GerritServer server;
private final SecureRandom rng;
private FileTypeRegistry registry;
@Inject
CatServlet(final GerritServer gs) {
server = gs;
rng = new SecureRandom();
}
@Override
public void init(final ServletConfig config) throws ServletException {
super.init(config);
try {
server = GerritServer.getInstance();
} catch (OrmException e) {
throw new ServletException("Cannot load GerritServer", e);
} catch (XsrfException e) {
throw new ServletException("Cannot load GerritServer", e);
}
rng = new SecureRandom();
registry = FileTypeRegistry.getInstance();
}

View File

@@ -48,6 +48,7 @@ import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.Transaction;
import com.google.gwtorm.jdbc.Database;
import com.google.gwtorm.jdbc.SimpleDataSource;
import com.google.inject.Singleton;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
@@ -95,6 +96,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;
/** Global server-side state for Gerrit. */
@Singleton
public class GerritServer {
private static final Logger log = LoggerFactory.getLogger(GerritServer.class);
private static DataSource datasource;

View File

@@ -15,16 +15,25 @@
package com.google.gerrit.server;
import com.google.gerrit.server.ssh.SshServlet;
import com.google.gwtjsonrpc.server.XsrfException;
import com.google.gwtorm.client.OrmException;
import com.google.inject.AbstractModule;
import com.google.inject.ConfigurationException;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import java.security.ProviderException;
import javax.servlet.ServletContextEvent;
/** Configures the web application environment for Gerrit Code Review. */
public class GerritServletConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(new ServletModule() {
private static Module createServletModule() {
return new ServletModule() {
@Override
protected void configureServlets() {
filter("/*").through(UrlRewriteFilter.class);
@@ -65,6 +74,48 @@ public class GerritServletConfig extends GuiceServletContextListener {
private void rpc(String name, Class<? extends GerritJsonServlet> clazz) {
serve("/gerrit/rpc/" + name).with(clazz);
}
});
};
}
private static Module createDatabaseModule() {
return new AbstractModule() {
@Override
protected void configure() {
try {
bind(GerritServer.class).toInstance(GerritServer.getInstance(true));
} catch (OrmException e) {
addError(e);
} catch (XsrfException e) {
addError(e);
}
}
};
}
private final Injector injector =
Guice.createInjector(createDatabaseModule(), createServletModule());
@Override
protected Injector getInjector() {
return injector;
}
@Override
public void contextInitialized(final ServletContextEvent event) {
super.contextInitialized(event);
}
@Override
public void contextDestroyed(final ServletContextEvent event) {
try {
final GerritServer gs = injector.getInstance(Key.get(GerritServer.class));
gs.closeDataSource();
} catch (ConfigurationException ce) {
// Assume it never started.
} catch (ProviderException ce) {
// Assume it never started.
}
super.contextDestroyed(event);
}
}

View File

@@ -18,8 +18,7 @@ import com.google.gerrit.client.data.GerritConfig;
import com.google.gerrit.client.reviewdb.Account;
import com.google.gerrit.client.rpc.Common;
import com.google.gwt.user.server.rpc.RPCServletUtils;
import com.google.gwtjsonrpc.server.XsrfException;
import com.google.gwtorm.client.OrmException;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.spearce.jgit.lib.Constants;
@@ -44,23 +43,20 @@ import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
@Singleton
public class HostPageServlet extends HttpServlet {
private GerritServer server;
private final GerritServer server;
private String canonicalUrl;
private boolean wantSSL;
private Document hostDoc;
@Inject
HostPageServlet(final GerritServer gs) {
server = gs;
}
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
try {
server = GerritServer.getInstance();
} catch (OrmException e) {
throw new ServletException("Cannot load GerritServer", e);
} catch (XsrfException e) {
throw new ServletException("Cannot load GerritServer", e);
}
final File sitePath = server.getSitePath();
canonicalUrl = server.getCanonicalURL();
wantSSL = canonicalUrl != null && canonicalUrl.startsWith("https:");

View File

@@ -15,8 +15,7 @@
package com.google.gerrit.server;
import com.google.gwt.user.server.rpc.RPCServletUtils;
import com.google.gwtjsonrpc.server.XsrfException;
import com.google.gwtorm.client.OrmException;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import org.spearce.jgit.util.NB;
@@ -29,8 +28,6 @@ import java.io.OutputStream;
import java.util.HashMap;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -90,22 +87,11 @@ public class StaticServlet extends HttpServlet {
return out.toByteArray();
}
private File staticBase;
private final File staticBase;
@Override
public void init(final ServletConfig config) throws ServletException {
super.init(config);
final GerritServer srv;
try {
srv = GerritServer.getInstance();
} catch (OrmException e) {
throw new ServletException("Cannot load GerritServer", e);
} catch (XsrfException e) {
throw new ServletException("Cannot load GerritServer", e);
}
final File p = srv.getSitePath();
@Inject
StaticServlet(final GerritServer gs) {
final File p = gs.getSitePath();
staticBase = p != null ? new File(p, "static") : null;
}

View File

@@ -20,7 +20,6 @@ import com.google.gerrit.client.reviewdb.Change;
import com.google.gerrit.client.reviewdb.RevId;
import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.rpc.Common;
import com.google.gwtjsonrpc.server.XsrfException;
import com.google.gwtorm.client.OrmException;
import com.google.inject.Singleton;
@@ -72,15 +71,8 @@ public class UrlRewriteFilter implements Filter {
private FilterConfig config;
public void init(final FilterConfig config) throws ServletException {
public void init(final FilterConfig config) {
this.config = config;
try {
GerritServer.getInstance();
} catch (OrmException e) {
throw new ServletException("Cannot initialize GerritServer", e);
} catch (XsrfException e) {
throw new ServletException("Cannot initialize GerritServer", e);
}
}
public void destroy() {