Auto register static/init.js as JavaScript plugin

When plugin does not expose Guice Modules explicitly,
auto discover and register static/init.js as WebUi extension
if found by the plugin content scanner.
This simplify JavaScript development from now on
no Java (or other scripting) code/classes are
required to extend Gerrit WebUi.

(based on the idea and initial patch by Dariusz: Ia5b3cb63f62)

Change-Id: I8c793764ac1876dc62d740d28c0d4cf6b9409b10
This commit is contained in:
Luca Milanesio
2014-07-09 10:05:28 +01:00
parent 64802216fa
commit f721eb5772
6 changed files with 67 additions and 7 deletions

View File

@@ -23,12 +23,17 @@ import com.google.common.collect.Sets;
import com.google.gerrit.extensions.annotations.Export;
import com.google.gerrit.extensions.annotations.ExtensionPoint;
import com.google.gerrit.extensions.annotations.Listen;
import com.google.gerrit.extensions.webui.JavaScriptPlugin;
import com.google.gerrit.server.plugins.PluginContentScanner.ExtensionMetaData;
import com.google.inject.AbstractModule;
import com.google.inject.Module;
import com.google.inject.Scopes;
import com.google.inject.TypeLiteral;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.util.Arrays;
@@ -36,12 +41,14 @@ import java.util.Map;
import java.util.Set;
class AutoRegisterModules {
private static final Logger log = LoggerFactory.getLogger(AutoRegisterModules.class);
private final String pluginName;
private final PluginGuiceEnvironment env;
private final PluginContentScanner scanner;
private final ClassLoader classLoader;
private final ModuleGenerator sshGen;
private final ModuleGenerator httpGen;
private final HttpModuleGenerator httpGen;
private Set<Class<?>> sysSingletons;
private Multimap<TypeLiteral<?>, Class<?>> sysListen;
@@ -117,6 +124,19 @@ class AutoRegisterModules {
for (ExtensionMetaData listener : extensions.get(Listen.class)) {
listen(listener);
}
exportInitJs();
}
private void exportInitJs() {
try {
if (scanner.getEntry(JavaScriptPlugin.STATIC_INIT_JS).isPresent()) {
httpGen.export(JavaScriptPlugin.INIT_JS);
}
} catch (IOException e) {
log.warn(String.format("Cannot access %s from plugin %s: "
+ "JavaScript auto-discovered plugin will not be registered",
JavaScriptPlugin.STATIC_INIT_JS, pluginName), e);
}
}
private void export(ExtensionMetaData def) throws InvalidPluginException {

View File

@@ -0,0 +1,19 @@
// Copyright (C) 2014 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;
public interface HttpModuleGenerator extends ModuleGenerator {
void export(String javascript);
}

View File

@@ -83,7 +83,7 @@ public class PluginGuiceEnvironment {
private Module httpModule;
private Provider<ModuleGenerator> sshGen;
private Provider<ModuleGenerator> httpGen;
private Provider<HttpModuleGenerator> httpGen;
private Map<TypeLiteral<?>, DynamicItem<?>> sysItems;
private Map<TypeLiteral<?>, DynamicItem<?>> sshItems;
@@ -187,7 +187,7 @@ public class PluginGuiceEnvironment {
public void setHttpInjector(Injector injector) {
httpModule = copy(injector);
httpGen = injector.getProvider(ModuleGenerator.class);
httpGen = injector.getProvider(HttpModuleGenerator.class);
httpItems = dynamicItemsOf(injector);
httpSets = dynamicSetsOf(injector);
httpMaps = dynamicMapsOf(injector);
@@ -204,7 +204,7 @@ public class PluginGuiceEnvironment {
return httpModule;
}
ModuleGenerator newHttpModuleGenerator() {
HttpModuleGenerator newHttpModuleGenerator() {
return httpGen.get();
}