Fix wrong Guice wiring

BanCommit and SuggestParentCandidates Guice beans were using assisted
inject pattern.  The factories were bound in request scope module even
though both beans have nothing to do with request scope.

Fix it by dropping unnecessary assisted inject bindings and move the
beans into singleton scope.

Change-Id: Ia792fb0db60e594456034c1517468a309393f5b5
This commit is contained in:
David Ostrovsky
2014-07-09 23:55:11 +02:00
committed by David Pursehouse
parent 17aa3fe3ee
commit e6d550e559
5 changed files with 35 additions and 46 deletions

View File

@@ -18,13 +18,11 @@ import static com.google.inject.Scopes.SINGLETON;
import com.google.gerrit.server.IdentifiedUser; import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.RequestCleanup; import com.google.gerrit.server.RequestCleanup;
import com.google.gerrit.server.git.BanCommit;
import com.google.gerrit.server.git.MergeOp; import com.google.gerrit.server.git.MergeOp;
import com.google.gerrit.server.git.SubmoduleOp; import com.google.gerrit.server.git.SubmoduleOp;
import com.google.gerrit.server.project.ChangeControl; import com.google.gerrit.server.project.ChangeControl;
import com.google.gerrit.server.project.PerRequestProjectControlCache; import com.google.gerrit.server.project.PerRequestProjectControlCache;
import com.google.gerrit.server.project.ProjectControl; import com.google.gerrit.server.project.ProjectControl;
import com.google.gerrit.server.project.SuggestParentCandidates;
import com.google.inject.servlet.RequestScoped; import com.google.inject.servlet.RequestScoped;
/** Bindings for {@link RequestScoped} entities. */ /** Bindings for {@link RequestScoped} entities. */
@@ -41,11 +39,5 @@ public class GerritRequestModule extends FactoryModule {
factory(SubmoduleOp.Factory.class); factory(SubmoduleOp.Factory.class);
factory(MergeOp.Factory.class); factory(MergeOp.Factory.class);
// Not really per-request, but dammit, I don't know where else to
// easily park this stuff.
//
factory(SuggestParentCandidates.Factory.class);
factory(BanCommit.Factory.class);
} }
} }

View File

@@ -24,6 +24,7 @@ import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.project.ProjectControl; import com.google.gerrit.server.project.ProjectControl;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Provider; import com.google.inject.Provider;
import com.google.inject.Singleton;
import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException; import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException; import org.eclipse.jgit.errors.IncorrectObjectTypeException;
@@ -45,38 +46,36 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.TimeZone; import java.util.TimeZone;
@Singleton
public class BanCommit { public class BanCommit {
public interface Factory {
BanCommit create();
}
/** /**
* Loads a list of commits to reject from {@code refs/meta/reject-commits}. * Loads a list of commits to reject from {@code refs/meta/reject-commits}.
* *
* @param repo repository from which the rejected commits should be loaded * @param repo repository from which the rejected commits should be loaded
* @return NoteMap of commits to be rejected, null if there are none. * @return NoteMap of commits to be rejected, null if there are none.
* @throws IOException the map cannot be loaded. * @throws IOException the map cannot be loaded.
*/ */
public static NoteMap loadRejectCommitsMap(Repository repo) public static NoteMap loadRejectCommitsMap(Repository repo)
throws IOException { throws IOException {
try { try {
Ref ref = repo.getRef(RefNames.REFS_REJECT_COMMITS); Ref ref = repo.getRef(RefNames.REFS_REJECT_COMMITS);
if (ref == null) { if (ref == null) {
return NoteMap.newEmptyMap(); return NoteMap.newEmptyMap();
} }
RevWalk rw = new RevWalk(repo); RevWalk rw = new RevWalk(repo);
try { try {
RevCommit map = rw.parseCommit(ref.getObjectId()); RevCommit map = rw.parseCommit(ref.getObjectId());
return NoteMap.read(rw.getObjectReader(), map); return NoteMap.read(rw.getObjectReader(), map);
} finally { } finally {
rw.release(); rw.release();
} }
} catch (IOException badMap) { } catch (IOException badMap) {
throw new IOException("Cannot load " throw new IOException("Cannot load " + RefNames.REFS_REJECT_COMMITS,
+ RefNames.REFS_REJECT_COMMITS, badMap); badMap);
} }
} }
private final Provider<IdentifiedUser> currentUser; private final Provider<IdentifiedUser> currentUser;
private final GitRepositoryManager repoManager; private final GitRepositoryManager repoManager;
@@ -96,8 +95,8 @@ public class BanCommit {
public BanCommitResult ban(final ProjectControl projectControl, public BanCommitResult ban(final ProjectControl projectControl,
final List<ObjectId> commitsToBan, final String reason) final List<ObjectId> commitsToBan, final String reason)
throws PermissionDeniedException, IOException, throws PermissionDeniedException, IOException, InterruptedException,
InterruptedException, MergeException, ConcurrentRefUpdateException { MergeException, ConcurrentRefUpdateException {
if (!projectControl.isOwner()) { if (!projectControl.isOwner()) {
throw new PermissionDeniedException( throw new PermissionDeniedException(
"No project owner: not permitted to ban commits"); "No project owner: not permitted to ban commits");
@@ -124,8 +123,8 @@ public class BanCommit {
banCommitNotes.set(commitToBan, createNoteContent(reason, inserter)); banCommitNotes.set(commitToBan, createNoteContent(reason, inserter));
} }
inserter.flush(); inserter.flush();
NotesBranchUtil notesBranchUtil = notesBranchUtilFactory.create(project, NotesBranchUtil notesBranchUtil =
repo, inserter); notesBranchUtilFactory.create(project, repo, inserter);
NoteMap newlyCreated = NoteMap newlyCreated =
notesBranchUtil.commitNewNotes(banCommitNotes, REFS_REJECT_COMMITS, notesBranchUtil.commitNewNotes(banCommitNotes, REFS_REJECT_COMMITS,
createPersonIdent(), buildCommitMessage(commitsToBan, reason)); createPersonIdent(), buildCommitMessage(commitsToBan, reason));

View File

@@ -18,6 +18,7 @@ import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.config.AllProjectsName; import com.google.gerrit.server.config.AllProjectsName;
import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator; import java.util.Comparator;
@@ -25,11 +26,8 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
@Singleton
public class SuggestParentCandidates { public class SuggestParentCandidates {
public interface Factory {
SuggestParentCandidates create();
}
private final ProjectControl.Factory projectControlFactory; private final ProjectControl.Factory projectControlFactory;
private final ProjectCache projectCache; private final ProjectCache projectCache;
private final AllProjectsName allProject; private final AllProjectsName allProject;

View File

@@ -50,13 +50,13 @@ public class BanCommitCommand extends SshCommand {
private List<ObjectId> commitsToBan = new ArrayList<>(); private List<ObjectId> commitsToBan = new ArrayList<>();
@Inject @Inject
private BanCommit.Factory banCommitFactory; private BanCommit banCommit;
@Override @Override
protected void run() throws Failure { protected void run() throws Failure {
try { try {
final BanCommitResult result = final BanCommitResult result =
banCommitFactory.create().ban(projectControl, commitsToBan, reason); banCommit.ban(projectControl, commitsToBan, reason);
final List<ObjectId> newlyBannedCommits = final List<ObjectId> newlyBannedCommits =
result.getNewlyBannedCommits(); result.getNewlyBannedCommits();

View File

@@ -135,7 +135,7 @@ final class CreateProjectCommand extends SshCommand {
private GerritApi gApi; private GerritApi gApi;
@Inject @Inject
private SuggestParentCandidates.Factory suggestParentCandidatesFactory; private SuggestParentCandidates suggestParentCandidates;
@Override @Override
protected void run() throws UnloggedFailure { protected void run() throws UnloggedFailure {
@@ -176,7 +176,7 @@ final class CreateProjectCommand extends SshCommand {
gApi.projects().name(projectName).create(input); gApi.projects().name(projectName).create(input);
} else { } else {
List<Project.NameKey> parentCandidates = List<Project.NameKey> parentCandidates =
suggestParentCandidatesFactory.create().getNameKeys(); suggestParentCandidates.getNameKeys();
for (Project.NameKey parent : parentCandidates) { for (Project.NameKey parent : parentCandidates) {
stdout.print(parent + "\n"); stdout.print(parent + "\n");