diff --git a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/plugins/HttpAutoRegisterModuleGenerator.java b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/plugins/HttpAutoRegisterModuleGenerator.java index 2d957f275f..d1c617f069 100644 --- a/gerrit-httpd/src/main/java/com/google/gerrit/httpd/plugins/HttpAutoRegisterModuleGenerator.java +++ b/gerrit-httpd/src/main/java/com/google/gerrit/httpd/plugins/HttpAutoRegisterModuleGenerator.java @@ -14,14 +14,20 @@ package com.google.gerrit.httpd.plugins; +import static com.google.gerrit.server.plugins.AutoRegisterUtil.calculateBindAnnotation; + +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.Module; import com.google.inject.Scopes; +import com.google.inject.TypeLiteral; import com.google.inject.servlet.ServletModule; +import java.lang.annotation.Annotation; import java.util.Map; import javax.servlet.http.HttpServlet; @@ -29,6 +35,7 @@ import javax.servlet.http.HttpServlet; class HttpAutoRegisterModuleGenerator extends ServletModule implements ModuleGenerator { private final Map> serve = Maps.newHashMap(); + private final Multimap, Class> listeners = LinkedListMultimap.create(); @Override protected void configureServlets() { @@ -36,6 +43,16 @@ class HttpAutoRegisterModuleGenerator extends ServletModule bind(e.getValue()).in(Scopes.SINGLETON); serve(e.getKey()).with(e.getValue()); } + for (Map.Entry, Class> e : listeners.entries()) { + @SuppressWarnings("unchecked") + TypeLiteral type = (TypeLiteral) e.getKey(); + + @SuppressWarnings("unchecked") + Class impl = (Class) e.getValue(); + + Annotation n = calculateBindAnnotation(impl); + bind(type).annotatedWith(n).to(impl); + } } @Override @@ -62,6 +79,11 @@ class HttpAutoRegisterModuleGenerator extends ServletModule } } + @Override + public void listen(TypeLiteral tl, Class clazz) { + listeners.put(tl, clazz); + } + @Override public Module create() throws InvalidPluginException { return this; diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/plugins/AutoRegisterModules.java b/gerrit-server/src/main/java/com/google/gerrit/server/plugins/AutoRegisterModules.java index eacdfa0ed1..c2c23a3a0d 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/plugins/AutoRegisterModules.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/plugins/AutoRegisterModules.java @@ -14,6 +14,7 @@ package com.google.gerrit.server.plugins; +import static com.google.gerrit.server.plugins.AutoRegisterUtil.calculateBindAnnotation; import static com.google.gerrit.server.plugins.PluginGuiceEnvironment.is; import com.google.common.collect.LinkedListMultimap; @@ -26,7 +27,6 @@ import com.google.inject.AbstractModule; import com.google.inject.Module; import com.google.inject.Scopes; import com.google.inject.TypeLiteral; -import com.google.inject.internal.UniqueAnnotations; import org.eclipse.jgit.util.IO; import org.objectweb.asm.AnnotationVisitor; @@ -116,16 +116,7 @@ class AutoRegisterModules { @SuppressWarnings("unchecked") Class impl = (Class) e.getValue(); - Annotation n = impl.getAnnotation(Export.class); - if (n == null) { - n = impl.getAnnotation(javax.inject.Named.class); - } - if (n == null) { - n = impl.getAnnotation(com.google.inject.name.Named.class); - } - if (n == null) { - n = UniqueAnnotations.create(); - } + Annotation n = calculateBindAnnotation(impl); bind(type).annotatedWith(n).to(impl); } } @@ -248,6 +239,8 @@ class AutoRegisterModules { if (env.hasDynamicSet(tl)) { sysSingletons.add(clazz); sysListen.put(tl, clazz); + httpGen.listen(tl, clazz); + sshGen.listen(tl, clazz); } else if (env.hasDynamicMap(tl)) { if (clazz.getAnnotation(Export.class) == null) { throw new InvalidPluginException(String.format( @@ -256,6 +249,8 @@ class AutoRegisterModules { } sysSingletons.add(clazz); sysListen.put(tl, clazz); + httpGen.listen(tl, clazz); + sshGen.listen(tl, clazz); } else { throw new InvalidPluginException(String.format( "Cannot register %s, server does not accept %s", diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/plugins/AutoRegisterUtil.java b/gerrit-server/src/main/java/com/google/gerrit/server/plugins/AutoRegisterUtil.java new file mode 100644 index 0000000000..3256d7f787 --- /dev/null +++ b/gerrit-server/src/main/java/com/google/gerrit/server/plugins/AutoRegisterUtil.java @@ -0,0 +1,41 @@ +// Copyright (C) 2012 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.gerrit.server.plugins; + +import com.google.gerrit.extensions.annotations.Export; +import com.google.inject.internal.UniqueAnnotations; + +import java.lang.annotation.Annotation; + +public final class AutoRegisterUtil { + + public static Annotation calculateBindAnnotation(Class impl) { + Annotation n = impl.getAnnotation(Export.class); + if (n == null) { + n = impl.getAnnotation(javax.inject.Named.class); + } + if (n == null) { + n = impl.getAnnotation(com.google.inject.name.Named.class); + } + if (n == null) { + n = UniqueAnnotations.create(); + } + return n; + } + + private AutoRegisterUtil() { + // hide default constructor + } +} diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/plugins/ModuleGenerator.java b/gerrit-server/src/main/java/com/google/gerrit/server/plugins/ModuleGenerator.java index 92e3b1dd6b..2011e9d198 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/plugins/ModuleGenerator.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/plugins/ModuleGenerator.java @@ -16,11 +16,14 @@ package com.google.gerrit.server.plugins; import com.google.gerrit.extensions.annotations.Export; import com.google.inject.Module; +import com.google.inject.TypeLiteral; public interface ModuleGenerator { void setPluginName(String name); void export(Export export, Class type) throws InvalidPluginException; + void listen(TypeLiteral tl, Class clazz); + Module create() throws InvalidPluginException; } diff --git a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshAutoRegisterModuleGenerator.java b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshAutoRegisterModuleGenerator.java index 03485f71cb..afb2537bd5 100644 --- a/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshAutoRegisterModuleGenerator.java +++ b/gerrit-sshd/src/main/java/com/google/gerrit/sshd/SshAutoRegisterModuleGenerator.java @@ -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> commands = Maps.newHashMap(); + private final Multimap, Class> listeners = LinkedListMultimap.create(); private CommandName command; @Override @@ -39,6 +46,16 @@ class SshAutoRegisterModuleGenerator for (Map.Entry> e : commands.entrySet()) { bind(Commands.key(command, e.getKey())).to(e.getValue()); } + for (Map.Entry, Class> e : listeners.entries()) { + @SuppressWarnings("unchecked") + TypeLiteral type = (TypeLiteral) e.getKey(); + + @SuppressWarnings("unchecked") + Class impl = (Class) 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");