Use Guice to setup the FileTypeRegistery singleton

This avoids the static singleton.

Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce
2009-07-27 21:40:44 -07:00
parent 9bd9165b57
commit caaf64a38c
7 changed files with 43 additions and 56 deletions

View File

@@ -47,8 +47,6 @@ import java.security.SecureRandom;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -68,18 +66,13 @@ public class CatServlet extends HttpServlet {
private static final MimeType ZIP = new MimeType("application/zip");
private final GerritServer server;
private final SecureRandom rng;
private FileTypeRegistry registry;
private final FileTypeRegistry registry;
@Inject
CatServlet(final GerritServer gs) {
CatServlet(final GerritServer gs, final FileTypeRegistry ftr) {
server = gs;
rng = new SecureRandom();
}
@Override
public void init(final ServletConfig config) throws ServletException {
super.init(config);
registry = FileTypeRegistry.getInstance();
registry = ftr;
}
@Override

View File

@@ -14,8 +14,8 @@
package com.google.gerrit.server;
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.MimeException;
import eu.medsea.mimeutil.MimeType;
@@ -35,21 +35,19 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
@Singleton
public class FileTypeRegistry {
private static final String KEY_SAFE = "safe";
private static final String SECTION_MIMETYPE = "mimetype";
private static final Logger log =
LoggerFactory.getLogger(FileTypeRegistry.class);
private static final FileTypeRegistry INSTANCE = new FileTypeRegistry();
/** Get the global registry. */
public static FileTypeRegistry getInstance() {
return INSTANCE;
}
private final GerritServer server;
private MimeUtil2 mimeUtil;
private FileTypeRegistry() {
@Inject
FileTypeRegistry(final GerritServer gs) {
server = gs;
mimeUtil = new MimeUtil2();
register("eu.medsea.mimeutil.detector.ExtensionMimeDetector");
register("eu.medsea.mimeutil.detector.MagicMimeMimeDetector");
@@ -133,34 +131,16 @@ public class FileTypeRegistry {
return false;
}
final RepositoryConfig cfg = getGerritConfig();
if (cfg != null) {
final boolean any = isSafe(cfg, "*/*", false);
final boolean genericMedia = isSafe(cfg, type.getMediaType() + "/*", any);
return isSafe(cfg, type.toString(), genericMedia);
}
// Assume we cannot send the content inline.
//
return false;
final RepositoryConfig cfg = server.getGerritConfig();
final boolean any = isSafe(cfg, "*/*", false);
final boolean genericMedia = isSafe(cfg, type.getMediaType() + "/*", any);
return isSafe(cfg, type.toString(), genericMedia);
}
private static boolean isSafe(RepositoryConfig cfg, String type, boolean def) {
return cfg.getBoolean(SECTION_MIMETYPE, type, KEY_SAFE, def);
}
private static RepositoryConfig getGerritConfig() {
try {
return GerritServer.getInstance().getGerritConfig();
} catch (OrmException e) {
log.warn("Cannot obtain GerritServer", e);
return null;
} catch (XsrfException e) {
log.warn("Cannot obtain GerritServer", e);
return null;
}
}
private static boolean isUnknownType(Collection<MimeType> mimeTypes) {
if (mimeTypes.isEmpty()) {
return true;

View File

@@ -90,6 +90,7 @@ public class GerritServletConfig extends GuiceServletContextListener {
bind(GerritServer.class).toInstance(gs);
bind(ContactStore.class).toInstance(EncryptedContactStore.create(gs));
bind(OpenIdServiceImpl.class).toInstance(new OpenIdServiceImpl(gs));
bind(FileTypeRegistry.class).toInstance(new FileTypeRegistry(gs));
} catch (OrmException e) {
addError(e);
} catch (XsrfException e) {

View File

@@ -40,6 +40,7 @@ import com.google.gerrit.client.rpc.NoSuchAccountException;
import com.google.gerrit.client.rpc.NoSuchEntityException;
import com.google.gerrit.server.BaseServiceImplementation;
import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.FileTypeRegistry;
import com.google.gerrit.server.GerritJsonServlet;
import com.google.gerrit.server.GerritServer;
import com.google.gerrit.server.mail.AbandonedSender;
@@ -65,9 +66,11 @@ import java.util.Set;
class PatchDetailServiceImpl extends BaseServiceImplementation implements
PatchDetailService {
private final Logger log = LoggerFactory.getLogger(getClass());
private final FileTypeRegistry registry;
PatchDetailServiceImpl(final GerritServer gs) {
PatchDetailServiceImpl(final GerritServer gs, final FileTypeRegistry ftr) {
super(gs);
registry = ftr;
}
public void patchScript(final Patch.Key patchKey, final PatchSet.Id psa,
@@ -77,7 +80,8 @@ class PatchDetailServiceImpl extends BaseServiceImplementation implements
callback.onFailure(new NoSuchEntityException());
return;
}
run(callback, new PatchScriptAction(server, patchKey, psa, psb, s));
run(callback,
new PatchScriptAction(server, registry, patchKey, psa, psb, s));
}
public void patchComments(final Patch.Key patchKey, final PatchSet.Id psa,
@@ -109,7 +113,8 @@ class PatchDetailServiceImpl extends BaseServiceImplementation implements
if (comment.getKey().get() == null) {
final PatchLineComment nc =
new PatchLineComment(new PatchLineComment.Key(patch.getKey(),
ChangeUtil.messageUUID(db)), comment.getLine(), me, comment.getParentUuid());
ChangeUtil.messageUUID(db)), comment.getLine(), me, comment
.getParentUuid());
nc.setSide(comment.getSide());
nc.setMessage(comment.getMessage());
db.patchComments().insert(Collections.singleton(nc));
@@ -183,16 +188,17 @@ class PatchDetailServiceImpl extends BaseServiceImplementation implements
/**
* Update the reviewed status for the file by user @code{account}
*/
public void setReviewedByCurrentUser(final Key patchKey, final boolean reviewed,
AsyncCallback<VoidResult> callback) {
public void setReviewedByCurrentUser(final Key patchKey,
final boolean reviewed, AsyncCallback<VoidResult> callback) {
run(callback, new Action<VoidResult>() {
public VoidResult run(ReviewDb db) throws OrmException {
Account.Id account = Common.getAccountId();
AccountPatchReview.Key key = new AccountPatchReview.Key(patchKey, account);
AccountPatchReview.Key key =
new AccountPatchReview.Key(patchKey, account);
AccountPatchReview apr = db.accountPatchReviews().get(key);
if (apr == null && reviewed) {
db.accountPatchReviews().
insert(Collections.singleton(new AccountPatchReview(patchKey, account)));
db.accountPatchReviews().insert(
Collections.singleton(new AccountPatchReview(patchKey, account)));
} else if (apr != null && !reviewed) {
db.accountPatchReviews().delete(Collections.singleton(apr));
}

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.server.patch;
import com.google.gerrit.server.FileTypeRegistry;
import com.google.gerrit.server.GerritJsonServlet;
import com.google.gerrit.server.GerritServer;
import com.google.inject.Inject;
@@ -23,13 +24,16 @@ import com.google.inject.Singleton;
@SuppressWarnings("serial")
@Singleton
public class PatchDetailServiceSrv extends GerritJsonServlet {
private final FileTypeRegistry registry;
@Inject
PatchDetailServiceSrv(final GerritServer gs) {
PatchDetailServiceSrv(final GerritServer gs, final FileTypeRegistry ftr) {
super(gs);
registry = ftr;
}
@Override
protected Object createServiceHandle() throws Exception {
return new PatchDetailServiceImpl(server);
return new PatchDetailServiceImpl(server, registry);
}
}

View File

@@ -31,6 +31,7 @@ import com.google.gerrit.client.reviewdb.ReviewDb;
import com.google.gerrit.client.rpc.Common;
import com.google.gerrit.client.rpc.CorruptEntityException;
import com.google.gerrit.client.rpc.NoSuchEntityException;
import com.google.gerrit.server.FileTypeRegistry;
import com.google.gerrit.server.GerritServer;
import com.google.gerrit.server.BaseServiceImplementation.Action;
import com.google.gerrit.server.BaseServiceImplementation.Failure;
@@ -56,6 +57,7 @@ class PatchScriptAction implements Action<PatchScript> {
LoggerFactory.getLogger(PatchScriptAction.class);
private final GerritServer server;
private final FileTypeRegistry registry;
private final Patch.Key patchKey;
private final PatchSet.Id psa;
private final PatchSet.Id psb;
@@ -69,10 +71,11 @@ class PatchScriptAction implements Action<PatchScript> {
private Project.NameKey projectKey;
private Repository git;
PatchScriptAction(final GerritServer gs, final Patch.Key patchKey,
final PatchSet.Id psa, final PatchSet.Id psb,
PatchScriptAction(final GerritServer gs, final FileTypeRegistry ftr,
final Patch.Key patchKey, final PatchSet.Id psa, final PatchSet.Id psb,
final PatchScriptSettings settings) {
this.server = gs;
this.registry = ftr;
this.patchKey = patchKey;
this.psa = psa;
this.psb = psb;
@@ -167,7 +170,7 @@ class PatchScriptAction implements Action<PatchScript> {
else
throw new Failure(new NoSuchEntityException());
final PatchScriptBuilder b = new PatchScriptBuilder();
final PatchScriptBuilder b = new PatchScriptBuilder(registry);
b.setRepository(git);
b.setPatch(patch);
b.setSettings(s);

View File

@@ -78,11 +78,11 @@ class PatchScriptBuilder {
private List<Edit> edits;
private final FileTypeRegistry registry;
PatchScriptBuilder() {
PatchScriptBuilder(final FileTypeRegistry ftr) {
header = new ArrayList<String>();
a = new Side();
b = new Side();
registry = FileTypeRegistry.getInstance();
registry = ftr;
}
void setRepository(final Repository r) {