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:
Shawn O. Pearce
2012-05-10 17:44:24 -07:00
parent d7f89fb05f
commit a252ee599d
5 changed files with 75 additions and 1 deletions

View File

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

View File

@@ -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) {