Allow plugins to get their canonical web URL injected

This change allows a plugin to get its canonical web URL injected at
runtime.

The URL is composed of the server's canonical web URL and the plugin's
name, i.e. `http://review.example.com:8080/plugins/plugin-name/`.

The canonical web URL may be injected into any .jar plugin regardless
of whether or not the plugin provides an HTTP servlet.

Change-Id: Id1a8489d49bfebff4ffdb8de58e9d0445113a09b
This commit is contained in:
David Pursehouse
2013-10-18 18:57:56 +09:00
parent 68dc638cd4
commit 8ed0d92050
4 changed files with 85 additions and 2 deletions

View File

@@ -191,6 +191,29 @@ public class MyClass {
}
----
A plugin can get its canonical web URL injected at runtime:
[source,java]
----
public class MyClass {
private final String url;
@Inject
public MyClass(@PluginCanonicalWebUrl String url) {
this.url = url;
}
[...]
}
----
The URL is composed of the server's canonical web URL and the plugin's
name, i.e. `http://review.example.com:8080/plugin-name`.
The canonical web URL may be injected into any .jar plugin regardless of
whether or not the plugin provides an HTTP servlet.
[[reload_method]]
Reload Method
~~~~~~~~~~~~~

View File

@@ -0,0 +1,42 @@
// Copyright (C) 2013 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.extensions.annotations;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import com.google.inject.BindingAnnotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
/**
* Annotation applied to a String containing the plugin canonical web URL.
* <p>
* A plugin or extension may receive this string by Guice injection to discover
* the canonical web URL under which the plugin is available:
*
* <pre>
* @Inject
* MyType(@PluginCanonicalWebUrl String myUrl) {
* ...
* }
* </pre>
*/
@Target({ElementType.PARAMETER, ElementType.FIELD})
@Retention(RUNTIME)
@BindingAnnotation
public @interface PluginCanonicalWebUrl {
}

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.server.plugins;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.extensions.annotations.PluginCanonicalWebUrl;
import com.google.gerrit.extensions.annotations.PluginData;
import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.extensions.registration.RegistrationHandle;
@@ -59,6 +60,7 @@ class JarPlugin extends Plugin {
private final JarFile jarFile;
private final Manifest manifest;
private final File dataDir;
private final String pluginCanonicalWebUrl;
private final ClassLoader classLoader;
private Class<? extends Module> sysModule;
private Class<? extends Module> sshModule;
@@ -71,6 +73,7 @@ class JarPlugin extends Plugin {
private List<ReloadableRegistrationHandle<?>> reloadableHandles;
public JarPlugin(String name,
String pluginCanonicalWebUrl,
PluginUser pluginUser,
File srcJar,
FileSnapshot snapshot,
@@ -83,6 +86,7 @@ class JarPlugin extends Plugin {
@Nullable Class<? extends Module> sshModule,
@Nullable Class<? extends Module> httpModule) {
super(name, srcJar, pluginUser, snapshot, apiType);
this.pluginCanonicalWebUrl = pluginCanonicalWebUrl;
this.jarFile = jarFile;
this.manifest = manifest;
this.dataDir = dataDir;
@@ -193,6 +197,9 @@ class JarPlugin extends Plugin {
bind(String.class)
.annotatedWith(PluginName.class)
.toInstance(getName());
bind(String.class)
.annotatedWith(PluginCanonicalWebUrl.class)
.toInstance(pluginCanonicalWebUrl);
bind(File.class)
.annotatedWith(PluginData.class)

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.server.plugins;
import com.google.common.base.CharMatcher;
import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.base.Predicate;
@@ -30,6 +31,7 @@ import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.gerrit.extensions.systemstatus.ServerInformation;
import com.google.gerrit.extensions.webui.JavaScriptPlugin;
import com.google.gerrit.server.PluginUser;
import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gerrit.server.config.ConfigUtil;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.config.SitePaths;
@@ -86,6 +88,7 @@ public class PluginLoader implements LifecycleListener {
private final Queue<Plugin> toCleanup;
private final Provider<PluginCleanerTask> cleaner;
private final PluginScannerThread scanner;
private final Provider<String> urlProvider;
@Inject
public PluginLoader(SitePaths sitePaths,
@@ -93,7 +96,8 @@ public class PluginLoader implements LifecycleListener {
ServerInformationImpl sii,
PluginUser.Factory puf,
Provider<PluginCleanerTask> pct,
@GerritServerConfig Config cfg) {
@GerritServerConfig Config cfg,
@CanonicalWebUrl Provider<String> provider) {
pluginsDir = sitePaths.plugins_dir;
dataDir = sitePaths.data_dir;
tmpDir = sitePaths.tmp_dir;
@@ -106,6 +110,7 @@ public class PluginLoader implements LifecycleListener {
toCleanup = Queues.newArrayDeque();
cleanupHandles = Maps.newConcurrentMap();
cleaner = pct;
urlProvider = provider;
long checkFrequency = ConfigUtil.getTimeUnit(cfg,
"plugins", null, "checkFrequency",
@@ -504,7 +509,13 @@ public class PluginLoader implements LifecycleListener {
Class<? extends Module> sysModule = load(sysName, pluginLoader);
Class<? extends Module> sshModule = load(sshName, pluginLoader);
Class<? extends Module> httpModule = load(httpName, pluginLoader);
Plugin plugin = new JarPlugin(name, pluginUserFactory.create(name),
String url = String.format("%s/plugins/%s/",
CharMatcher.is('/').trimTrailingFrom(urlProvider.get()),
name);
Plugin plugin = new JarPlugin(name, url,
pluginUserFactory.create(name),
srcJar, snapshot,
jarFile, manifest,
new File(dataDir, name), type, pluginLoader,