Provide a data directory to plugins on demand
If a plugin requests a data directory with a @PluginData File dependency, a data directory will be created automatically by the server in $site_path/data/$plugin_name and passed to the plugin. Plugins can use this to store any data they want. Change-Id: I98de44a033889740cafc8a3d72c582c0e99b7ffd
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
// 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.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;
|
||||
|
||||
/**
|
||||
* Local path where a plugin can store its own private data.
|
||||
* <p>
|
||||
* A plugin or extension may receive this string by Guice injection to discover
|
||||
* a directory where it can store configuration or other data that is private:
|
||||
*
|
||||
* <pre>
|
||||
* @Inject
|
||||
* MyType(@PluginData java.io.File myDir) {
|
||||
* new FileInputStream(new File(myDir, "my.config"));
|
||||
* }
|
||||
* </pre>
|
||||
*/
|
||||
@Target({ElementType.PARAMETER, ElementType.FIELD})
|
||||
@Retention(RUNTIME)
|
||||
@BindingAnnotation
|
||||
public @interface PluginData {
|
||||
}
|
@@ -72,6 +72,7 @@ public class SitePathInitializer {
|
||||
mkdir(site.mail_dir);
|
||||
mkdir(site.static_dir);
|
||||
mkdir(site.plugins_dir);
|
||||
mkdir(site.data_dir);
|
||||
|
||||
for (InitStep step : steps) {
|
||||
step.run();
|
||||
|
@@ -31,6 +31,7 @@ public final class SitePaths {
|
||||
public final File tmp_dir;
|
||||
public final File logs_dir;
|
||||
public final File plugins_dir;
|
||||
public final File data_dir;
|
||||
public final File mail_dir;
|
||||
public final File hooks_dir;
|
||||
public final File static_dir;
|
||||
@@ -66,6 +67,7 @@ public final class SitePaths {
|
||||
lib_dir = new File(site_path, "lib");
|
||||
tmp_dir = new File(site_path, "tmp");
|
||||
plugins_dir = new File(site_path, "plugins");
|
||||
data_dir = new File(site_path, "data");
|
||||
logs_dir = new File(site_path, "logs");
|
||||
mail_dir = new File(etc_dir, "mail");
|
||||
hooks_dir = new File(site_path, "hooks");
|
||||
|
@@ -16,6 +16,7 @@ package com.google.gerrit.server.plugins;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.gerrit.extensions.annotations.PluginData;
|
||||
import com.google.gerrit.extensions.annotations.PluginName;
|
||||
import com.google.gerrit.extensions.registration.RegistrationHandle;
|
||||
import com.google.gerrit.extensions.registration.ReloadableRegistrationHandle;
|
||||
@@ -26,6 +27,8 @@ import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Module;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.ProvisionException;
|
||||
|
||||
import org.eclipse.jgit.storage.file.FileSnapshot;
|
||||
|
||||
@@ -51,6 +54,7 @@ public class Plugin {
|
||||
private final FileSnapshot snapshot;
|
||||
private final JarFile jarFile;
|
||||
private final Manifest manifest;
|
||||
private final File dataDir;
|
||||
private final ClassLoader classLoader;
|
||||
private Class<? extends Module> sysModule;
|
||||
private Class<? extends Module> sshModule;
|
||||
@@ -67,6 +71,7 @@ public class Plugin {
|
||||
FileSnapshot snapshot,
|
||||
JarFile jarFile,
|
||||
Manifest manifest,
|
||||
File dataDir,
|
||||
ClassLoader classLoader,
|
||||
@Nullable Class<? extends Module> sysModule,
|
||||
@Nullable Class<? extends Module> sshModule,
|
||||
@@ -76,6 +81,7 @@ public class Plugin {
|
||||
this.snapshot = snapshot;
|
||||
this.jarFile = jarFile;
|
||||
this.manifest = manifest;
|
||||
this.dataDir = dataDir;
|
||||
this.classLoader = classLoader;
|
||||
this.sysModule = sysModule;
|
||||
this.sshModule = sshModule;
|
||||
@@ -181,6 +187,27 @@ public class Plugin {
|
||||
bind(String.class)
|
||||
.annotatedWith(PluginName.class)
|
||||
.toInstance(name);
|
||||
|
||||
bind(File.class)
|
||||
.annotatedWith(PluginData.class)
|
||||
.toProvider(new Provider<File>() {
|
||||
private volatile boolean ready;
|
||||
|
||||
@Override
|
||||
public File get() {
|
||||
if (!ready) {
|
||||
synchronized (dataDir) {
|
||||
if (!dataDir.exists() && !dataDir.mkdirs()) {
|
||||
throw new ProvisionException(String.format(
|
||||
"Cannot create %s for plugin %s",
|
||||
dataDir.getAbsolutePath(), name));
|
||||
}
|
||||
ready = true;
|
||||
}
|
||||
}
|
||||
return dataDir;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
return Guice.createInjector(modules);
|
||||
|
@@ -58,6 +58,7 @@ public class PluginLoader implements LifecycleListener {
|
||||
static final Logger log = LoggerFactory.getLogger(PluginLoader.class);
|
||||
|
||||
private final File pluginsDir;
|
||||
private final File dataDir;
|
||||
private final File tmpDir;
|
||||
private final PluginGuiceEnvironment env;
|
||||
private final ServerInformationImpl srvInfoImpl;
|
||||
@@ -73,6 +74,7 @@ public class PluginLoader implements LifecycleListener {
|
||||
ServerInformationImpl sii,
|
||||
@GerritServerConfig Config cfg) {
|
||||
pluginsDir = sitePaths.plugins_dir;
|
||||
dataDir = sitePaths.data_dir;
|
||||
tmpDir = sitePaths.tmp_dir;
|
||||
env = pe;
|
||||
srvInfoImpl = sii;
|
||||
@@ -343,7 +345,7 @@ public class PluginLoader implements LifecycleListener {
|
||||
return new Plugin(name,
|
||||
srcJar, snapshot,
|
||||
jarFile, manifest,
|
||||
pluginLoader,
|
||||
new File(dataDir, name), pluginLoader,
|
||||
sysModule, sshModule, httpModule);
|
||||
} finally {
|
||||
if (!keep) {
|
||||
|
Reference in New Issue
Block a user