Register @Listen annotated classes in Http and Ssh modules

Current implementation of Web UI plugins allows auto register them using
@Listen annotation. Unfortunately listeners can only be bind in
system module.

This patch enables binding listeners in Http and Ssh modules.

Change-Id: I7569d0e743bd9e1c9bb8aded09a5740bb88c1cca
Signed-off-by: Dariusz Luksza <dariusz@luksza.org>
This commit is contained in:
Dariusz Luksza
2012-10-30 15:47:45 +01:00
parent cc53d2d83f
commit 9ec549cb65
5 changed files with 95 additions and 11 deletions

View File

@@ -14,22 +14,29 @@
package com.google.gerrit.sshd;
import static com.google.gerrit.server.plugins.AutoRegisterUtil.calculateBindAnnotation;
import com.google.common.base.Preconditions;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.gerrit.extensions.annotations.Export;
import com.google.gerrit.server.plugins.InvalidPluginException;
import com.google.gerrit.server.plugins.ModuleGenerator;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
import org.apache.sshd.server.Command;
import java.lang.annotation.Annotation;
import java.util.Map;
class SshAutoRegisterModuleGenerator
extends AbstractModule
implements ModuleGenerator {
private final Map<String, Class<Command>> commands = Maps.newHashMap();
private final Multimap<TypeLiteral<?>, Class<?>> listeners = LinkedListMultimap.create();
private CommandName command;
@Override
@@ -39,6 +46,16 @@ class SshAutoRegisterModuleGenerator
for (Map.Entry<String, Class<Command>> e : commands.entrySet()) {
bind(Commands.key(command, e.getKey())).to(e.getValue());
}
for (Map.Entry<TypeLiteral<?>, Class<?>> e : listeners.entries()) {
@SuppressWarnings("unchecked")
TypeLiteral<Object> type = (TypeLiteral<Object>) e.getKey();
@SuppressWarnings("unchecked")
Class<Object> impl = (Class<Object>) e.getValue();
Annotation n = calculateBindAnnotation(impl);
bind(type).annotatedWith(n).to(impl);
}
}
public void setPluginName(String name) {
@@ -66,6 +83,12 @@ class SshAutoRegisterModuleGenerator
}
}
@Override
public void listen(TypeLiteral<?> tl, Class<?> clazz) {
listeners.put(tl, clazz);
}
@Override
public Module create() throws InvalidPluginException {
Preconditions.checkState(command != null, "pluginName must be provided");