Merge "Register @Listen annotated classes in Http and Ssh modules" into stable-2.6
This commit is contained in:
@@ -14,14 +14,20 @@
|
|||||||
|
|
||||||
package com.google.gerrit.httpd.plugins;
|
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.Maps;
|
||||||
|
import com.google.common.collect.Multimap;
|
||||||
import com.google.gerrit.extensions.annotations.Export;
|
import com.google.gerrit.extensions.annotations.Export;
|
||||||
import com.google.gerrit.server.plugins.InvalidPluginException;
|
import com.google.gerrit.server.plugins.InvalidPluginException;
|
||||||
import com.google.gerrit.server.plugins.ModuleGenerator;
|
import com.google.gerrit.server.plugins.ModuleGenerator;
|
||||||
import com.google.inject.Module;
|
import com.google.inject.Module;
|
||||||
import com.google.inject.Scopes;
|
import com.google.inject.Scopes;
|
||||||
|
import com.google.inject.TypeLiteral;
|
||||||
import com.google.inject.servlet.ServletModule;
|
import com.google.inject.servlet.ServletModule;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
@@ -29,6 +35,7 @@ import javax.servlet.http.HttpServlet;
|
|||||||
class HttpAutoRegisterModuleGenerator extends ServletModule
|
class HttpAutoRegisterModuleGenerator extends ServletModule
|
||||||
implements ModuleGenerator {
|
implements ModuleGenerator {
|
||||||
private final Map<String, Class<HttpServlet>> serve = Maps.newHashMap();
|
private final Map<String, Class<HttpServlet>> serve = Maps.newHashMap();
|
||||||
|
private final Multimap<TypeLiteral<?>, Class<?>> listeners = LinkedListMultimap.create();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configureServlets() {
|
protected void configureServlets() {
|
||||||
@@ -36,6 +43,16 @@ class HttpAutoRegisterModuleGenerator extends ServletModule
|
|||||||
bind(e.getValue()).in(Scopes.SINGLETON);
|
bind(e.getValue()).in(Scopes.SINGLETON);
|
||||||
serve(e.getKey()).with(e.getValue());
|
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
|
@Override
|
||||||
@@ -62,6 +79,11 @@ class HttpAutoRegisterModuleGenerator extends ServletModule
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void listen(TypeLiteral<?> tl, Class<?> clazz) {
|
||||||
|
listeners.put(tl, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Module create() throws InvalidPluginException {
|
public Module create() throws InvalidPluginException {
|
||||||
return this;
|
return this;
|
||||||
|
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
package com.google.gerrit.server.plugins;
|
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 static com.google.gerrit.server.plugins.PluginGuiceEnvironment.is;
|
||||||
|
|
||||||
import com.google.common.collect.LinkedListMultimap;
|
import com.google.common.collect.LinkedListMultimap;
|
||||||
@@ -26,7 +27,6 @@ import com.google.inject.AbstractModule;
|
|||||||
import com.google.inject.Module;
|
import com.google.inject.Module;
|
||||||
import com.google.inject.Scopes;
|
import com.google.inject.Scopes;
|
||||||
import com.google.inject.TypeLiteral;
|
import com.google.inject.TypeLiteral;
|
||||||
import com.google.inject.internal.UniqueAnnotations;
|
|
||||||
|
|
||||||
import org.eclipse.jgit.util.IO;
|
import org.eclipse.jgit.util.IO;
|
||||||
import org.objectweb.asm.AnnotationVisitor;
|
import org.objectweb.asm.AnnotationVisitor;
|
||||||
@@ -116,16 +116,7 @@ class AutoRegisterModules {
|
|||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Class<Object> impl = (Class<Object>) e.getValue();
|
Class<Object> impl = (Class<Object>) e.getValue();
|
||||||
|
|
||||||
Annotation n = impl.getAnnotation(Export.class);
|
Annotation n = calculateBindAnnotation(impl);
|
||||||
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();
|
|
||||||
}
|
|
||||||
bind(type).annotatedWith(n).to(impl);
|
bind(type).annotatedWith(n).to(impl);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -248,6 +239,8 @@ class AutoRegisterModules {
|
|||||||
if (env.hasDynamicSet(tl)) {
|
if (env.hasDynamicSet(tl)) {
|
||||||
sysSingletons.add(clazz);
|
sysSingletons.add(clazz);
|
||||||
sysListen.put(tl, clazz);
|
sysListen.put(tl, clazz);
|
||||||
|
httpGen.listen(tl, clazz);
|
||||||
|
sshGen.listen(tl, clazz);
|
||||||
} else if (env.hasDynamicMap(tl)) {
|
} else if (env.hasDynamicMap(tl)) {
|
||||||
if (clazz.getAnnotation(Export.class) == null) {
|
if (clazz.getAnnotation(Export.class) == null) {
|
||||||
throw new InvalidPluginException(String.format(
|
throw new InvalidPluginException(String.format(
|
||||||
@@ -256,6 +249,8 @@ class AutoRegisterModules {
|
|||||||
}
|
}
|
||||||
sysSingletons.add(clazz);
|
sysSingletons.add(clazz);
|
||||||
sysListen.put(tl, clazz);
|
sysListen.put(tl, clazz);
|
||||||
|
httpGen.listen(tl, clazz);
|
||||||
|
sshGen.listen(tl, clazz);
|
||||||
} else {
|
} else {
|
||||||
throw new InvalidPluginException(String.format(
|
throw new InvalidPluginException(String.format(
|
||||||
"Cannot register %s, server does not accept %s",
|
"Cannot register %s, server does not accept %s",
|
||||||
|
@@ -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
|
||||||
|
}
|
||||||
|
}
|
@@ -16,11 +16,14 @@ package com.google.gerrit.server.plugins;
|
|||||||
|
|
||||||
import com.google.gerrit.extensions.annotations.Export;
|
import com.google.gerrit.extensions.annotations.Export;
|
||||||
import com.google.inject.Module;
|
import com.google.inject.Module;
|
||||||
|
import com.google.inject.TypeLiteral;
|
||||||
|
|
||||||
public interface ModuleGenerator {
|
public interface ModuleGenerator {
|
||||||
void setPluginName(String name);
|
void setPluginName(String name);
|
||||||
|
|
||||||
void export(Export export, Class<?> type) throws InvalidPluginException;
|
void export(Export export, Class<?> type) throws InvalidPluginException;
|
||||||
|
|
||||||
|
void listen(TypeLiteral<?> tl, Class<?> clazz);
|
||||||
|
|
||||||
Module create() throws InvalidPluginException;
|
Module create() throws InvalidPluginException;
|
||||||
}
|
}
|
||||||
|
@@ -14,22 +14,29 @@
|
|||||||
|
|
||||||
package com.google.gerrit.sshd;
|
package com.google.gerrit.sshd;
|
||||||
|
|
||||||
|
import static com.google.gerrit.server.plugins.AutoRegisterUtil.calculateBindAnnotation;
|
||||||
|
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
|
import com.google.common.collect.LinkedListMultimap;
|
||||||
import com.google.common.collect.Maps;
|
import com.google.common.collect.Maps;
|
||||||
|
import com.google.common.collect.Multimap;
|
||||||
import com.google.gerrit.extensions.annotations.Export;
|
import com.google.gerrit.extensions.annotations.Export;
|
||||||
import com.google.gerrit.server.plugins.InvalidPluginException;
|
import com.google.gerrit.server.plugins.InvalidPluginException;
|
||||||
import com.google.gerrit.server.plugins.ModuleGenerator;
|
import com.google.gerrit.server.plugins.ModuleGenerator;
|
||||||
import com.google.inject.AbstractModule;
|
import com.google.inject.AbstractModule;
|
||||||
import com.google.inject.Module;
|
import com.google.inject.Module;
|
||||||
|
import com.google.inject.TypeLiteral;
|
||||||
|
|
||||||
import org.apache.sshd.server.Command;
|
import org.apache.sshd.server.Command;
|
||||||
|
|
||||||
|
import java.lang.annotation.Annotation;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
class SshAutoRegisterModuleGenerator
|
class SshAutoRegisterModuleGenerator
|
||||||
extends AbstractModule
|
extends AbstractModule
|
||||||
implements ModuleGenerator {
|
implements ModuleGenerator {
|
||||||
private final Map<String, Class<Command>> commands = Maps.newHashMap();
|
private final Map<String, Class<Command>> commands = Maps.newHashMap();
|
||||||
|
private final Multimap<TypeLiteral<?>, Class<?>> listeners = LinkedListMultimap.create();
|
||||||
private CommandName command;
|
private CommandName command;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -39,6 +46,16 @@ class SshAutoRegisterModuleGenerator
|
|||||||
for (Map.Entry<String, Class<Command>> e : commands.entrySet()) {
|
for (Map.Entry<String, Class<Command>> e : commands.entrySet()) {
|
||||||
bind(Commands.key(command, e.getKey())).to(e.getValue());
|
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) {
|
public void setPluginName(String name) {
|
||||||
@@ -66,6 +83,12 @@ class SshAutoRegisterModuleGenerator
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void listen(TypeLiteral<?> tl, Class<?> clazz) {
|
||||||
|
listeners.put(tl, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Module create() throws InvalidPluginException {
|
public Module create() throws InvalidPluginException {
|
||||||
Preconditions.checkState(command != null, "pluginName must be provided");
|
Preconditions.checkState(command != null, "pluginName must be provided");
|
||||||
|
Reference in New Issue
Block a user