Merge "Register @Listen annotated classes in Http and Ssh modules" into stable-2.6

This commit is contained in:
Shawn Pearce
2012-11-16 16:25:46 -08:00
committed by Gerrit Code Review
5 changed files with 95 additions and 11 deletions

View File

@@ -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<String, Class<HttpServlet>> serve = Maps.newHashMap();
private final Multimap<TypeLiteral<?>, 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<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);
}
}
@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;

View File

@@ -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<Object> impl = (Class<Object>) 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",

View File

@@ -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<Object> 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
}
}

View File

@@ -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;
}

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");