ServerPlugin: Extract plugin info glue into separate module class

Change-Id: I07ba3d465567b86abf225949fb87fbe648580c2c
This commit is contained in:
Dave Borowitz 2015-02-24 14:01:48 -08:00
parent 6bf2bb6ea5
commit 27b3652319
4 changed files with 87 additions and 89 deletions

View File

@ -18,9 +18,7 @@ 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.
@ -39,7 +37,6 @@ import java.lang.annotation.Target;
* }
* </pre>
*/
@Target({ElementType.PARAMETER, ElementType.FIELD})
@Retention(RUNTIME)
@BindingAnnotation
public @interface PluginData {

View File

@ -1,38 +0,0 @@
// Copyright (C) 2015 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.PluginData;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.io.File;
import java.nio.file.Path;
@Singleton
class PluginDataAsFileProvider implements Provider<File> {
private final Provider<Path> pathProvider;
@Inject
PluginDataAsFileProvider(@PluginData Provider<Path> pathProvider) {
this.pathProvider = pathProvider;
}
@Override
public File get() {
return pathProvider.get().toFile();
}
}

View File

@ -17,26 +17,18 @@ 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;
import com.google.gerrit.extensions.registration.ReloadableRegistrationHandle;
import com.google.gerrit.lifecycle.LifecycleManager;
import com.google.gerrit.server.PluginUser;
import com.google.gerrit.server.util.RequestContext;
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.internal.storage.file.FileSnapshot;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.jar.Attributes;
@ -134,6 +126,14 @@ public class ServerPlugin extends Plugin {
return getSrcFile();
}
Path getDataDir() {
return dataDir;
}
String getPluginCanonicalWebUrl() {
return pluginCanonicalWebUrl;
}
private static Manifest getPluginManifest(PluginContentScanner scanner)
throws InvalidPluginException {
try {
@ -232,49 +232,11 @@ public class ServerPlugin extends Plugin {
}
private Injector newRootInjector(final PluginGuiceEnvironment env) {
List<Module> modules = Lists.newArrayListWithCapacity(4);
List<Module> modules = Lists.newArrayListWithCapacity(2);
if (getApiType() == ApiType.PLUGIN) {
modules.add(env.getSysModule());
}
modules.add(new AbstractModule() {
@Override
protected void configure() {
bind(PluginUser.class).toInstance(getPluginUser());
bind(String.class)
.annotatedWith(PluginName.class)
.toInstance(getName());
bind(String.class)
.annotatedWith(PluginCanonicalWebUrl.class)
.toInstance(pluginCanonicalWebUrl);
bind(Path.class)
.annotatedWith(PluginData.class)
.toProvider(new Provider<Path>() {
private volatile boolean ready;
@Override
public Path get() {
if (!ready) {
synchronized (dataDir) {
if (!ready) {
try {
Files.createDirectories(dataDir);
} catch (IOException e) {
throw new ProvisionException(String.format(
"Cannot create %s for plugin %s",
dataDir.toAbsolutePath(), getName()), e);
}
ready = true;
}
}
}
return dataDir;
}
});
bind(File.class).annotatedWith(PluginData.class)
.toProvider(PluginDataAsFileProvider.class);
}
});
modules.add(new ServerPluginInfoModule(this));
return Guice.createInjector(modules);
}

View File

@ -0,0 +1,77 @@
// Copyright (C) 2015 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.PluginCanonicalWebUrl;
import com.google.gerrit.extensions.annotations.PluginData;
import com.google.gerrit.extensions.annotations.PluginName;
import com.google.gerrit.server.PluginUser;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.ProvisionException;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
class ServerPluginInfoModule extends AbstractModule {
private final ServerPlugin plugin;
private final Path dataDir;
private volatile boolean ready;
ServerPluginInfoModule(ServerPlugin plugin) {
this.plugin = plugin;
this.dataDir = plugin.getDataDir();
}
@Override
protected void configure() {
bind(PluginUser.class).toInstance(plugin.getPluginUser());
bind(String.class)
.annotatedWith(PluginName.class)
.toInstance(plugin.getName());
bind(String.class)
.annotatedWith(PluginCanonicalWebUrl.class)
.toInstance(plugin.getPluginCanonicalWebUrl());
}
@Provides
@PluginData
Path getPluginData() {
if (!ready) {
synchronized (dataDir) {
if (!ready) {
try {
Files.createDirectories(dataDir);
} catch (IOException e) {
throw new ProvisionException(String.format(
"Cannot create %s for plugin %s",
dataDir.toAbsolutePath(), plugin.getName()), e);
}
ready = true;
}
}
}
return dataDir;
}
@Provides
@PluginData
File getPluginDataAsFile(@PluginData Path pluginData) {
return pluginData.toFile();
}
}