Use assisted injection to create the PushOp instances
This avoids needing to pass through data from the ReplicationConfig or the PushReplication, instead we setup a child injector that has bindings for the handful of items to be passed in that is unique to the configuration instance, and leave it at at that. Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
@@ -16,6 +16,7 @@ package com.google.gerrit.git;
|
||||
|
||||
import com.google.gerrit.server.GerritServer;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.assistedinject.Assisted;
|
||||
|
||||
import com.jcraft.jsch.JSchException;
|
||||
|
||||
@@ -50,26 +51,31 @@ import java.util.Set;
|
||||
* take that lock to ensure they are working with a current view of the object.
|
||||
*/
|
||||
class PushOp implements Runnable {
|
||||
interface Factory {
|
||||
PushOp create(String d, URIish u);
|
||||
}
|
||||
|
||||
private static final Logger log = PushReplication.log;
|
||||
static final String MIRROR_ALL = "..all..";
|
||||
|
||||
@Inject
|
||||
private GerritServer server;
|
||||
private final GerritServer server;
|
||||
private final PushReplication.ReplicationConfig pool;
|
||||
private final RemoteConfig config;
|
||||
|
||||
private final Set<String> delta = new HashSet<String>();
|
||||
private final PushReplication.ReplicationConfig pool;
|
||||
private final String projectName;
|
||||
private final RemoteConfig config;
|
||||
private final URIish uri;
|
||||
private boolean mirror;
|
||||
|
||||
private Repository db;
|
||||
|
||||
PushOp(final PushReplication.ReplicationConfig p, final String d,
|
||||
final RemoteConfig c, final URIish u) {
|
||||
@Inject
|
||||
PushOp(final GerritServer gs, final PushReplication.ReplicationConfig p,
|
||||
final RemoteConfig c, @Assisted final String d, @Assisted final URIish u) {
|
||||
server = gs;
|
||||
pool = p;
|
||||
projectName = d;
|
||||
config = c;
|
||||
projectName = d;
|
||||
uri = u;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,8 +16,10 @@ package com.google.gerrit.git;
|
||||
|
||||
import com.google.gerrit.client.reviewdb.Project;
|
||||
import com.google.gerrit.server.config.SitePath;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.assistedinject.FactoryProvider;
|
||||
|
||||
import com.jcraft.jsch.Session;
|
||||
|
||||
@@ -140,7 +142,7 @@ public class PushReplication implements ReplicationQueue {
|
||||
c.addPushRefSpec(spec);
|
||||
}
|
||||
|
||||
r.add(new ReplicationConfig(c, cfg));
|
||||
r.add(new ReplicationConfig(injector, c, cfg));
|
||||
}
|
||||
return Collections.unmodifiableList(r);
|
||||
}
|
||||
@@ -162,19 +164,32 @@ public class PushReplication implements ReplicationQueue {
|
||||
return result;
|
||||
}
|
||||
|
||||
class ReplicationConfig {
|
||||
static class ReplicationConfig {
|
||||
private final RemoteConfig remote;
|
||||
private final int delay;
|
||||
private final WorkQueue.Executor pool;
|
||||
private final Map<URIish, PushOp> pending = new HashMap<URIish, PushOp>();
|
||||
private final PushOp.Factory opFactory;
|
||||
|
||||
ReplicationConfig(final RemoteConfig rc, final Config cfg) {
|
||||
ReplicationConfig(final Injector injector, final RemoteConfig rc,
|
||||
final Config cfg) {
|
||||
remote = rc;
|
||||
delay = Math.max(0, getInt(rc, cfg, "replicationdelay", 15));
|
||||
|
||||
final int poolSize = Math.max(0, getInt(rc, cfg, "threads", 1));
|
||||
final String poolName = "ReplicateTo-" + rc.getName();
|
||||
pool = WorkQueue.createQueue(poolSize, poolName);
|
||||
|
||||
opFactory = injector.createChildInjector(new AbstractModule() {
|
||||
@Override
|
||||
protected void configure() {
|
||||
bind(PushReplication.ReplicationConfig.class).toInstance(
|
||||
ReplicationConfig.this);
|
||||
bind(RemoteConfig.class).toInstance(remote);
|
||||
bind(PushOp.Factory.class).toProvider(
|
||||
FactoryProvider.newFactory(PushOp.Factory.class, PushOp.class));
|
||||
}
|
||||
}).getInstance(PushOp.Factory.class);
|
||||
}
|
||||
|
||||
private int getInt(final RemoteConfig rc, final Config cfg,
|
||||
@@ -187,8 +202,7 @@ public class PushReplication implements ReplicationQueue {
|
||||
synchronized (pending) {
|
||||
PushOp e = pending.get(uri);
|
||||
if (e == null) {
|
||||
e = new PushOp(this, project.get(), remote, uri);
|
||||
injector.injectMembers(e);
|
||||
e = opFactory.create(project.get(), uri);
|
||||
pool.schedule(e, delay, TimeUnit.SECONDS);
|
||||
pending.put(uri, e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user