diff options
Diffstat (limited to 'gerrit-server/src/main/java/com/google/gerrit/server/plugins/PluginLoader.java')
-rw-r--r-- | gerrit-server/src/main/java/com/google/gerrit/server/plugins/PluginLoader.java | 109 |
1 files changed, 24 insertions, 85 deletions
diff --git a/gerrit-server/src/main/java/com/google/gerrit/server/plugins/PluginLoader.java b/gerrit-server/src/main/java/com/google/gerrit/server/plugins/PluginLoader.java index 42998c580c..d366dbfffd 100644 --- a/gerrit-server/src/main/java/com/google/gerrit/server/plugins/PluginLoader.java +++ b/gerrit-server/src/main/java/com/google/gerrit/server/plugins/PluginLoader.java @@ -24,14 +24,10 @@ import com.google.common.collect.Iterables; import com.google.common.collect.LinkedHashMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.google.common.collect.Ordering; import com.google.common.collect.SetMultimap; import com.google.common.collect.Sets; -import com.google.common.io.ByteStreams; -import com.google.gerrit.extensions.annotations.PluginName; 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.cache.PersistentCacheFactory; import com.google.gerrit.server.config.CanonicalWebUrl; @@ -44,7 +40,6 @@ import com.google.inject.Provider; import com.google.inject.Singleton; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; @@ -74,7 +69,7 @@ public class PluginLoader implements LifecycleListener { private static final Logger log = LoggerFactory.getLogger(PluginLoader.class); public String getPluginName(Path srcPath) { - return MoreObjects.firstNonNull(getGerritPluginName(srcPath), nameOf(srcPath)); + return MoreObjects.firstNonNull(getGerritPluginName(srcPath), PluginUtil.nameOf(srcPath)); } private final Path pluginsDir; @@ -139,32 +134,6 @@ public class PluginLoader implements LifecycleListener { } } - public static List<Path> listPlugins(Path pluginsDir, final String suffix) throws IOException { - if (pluginsDir == null || !Files.exists(pluginsDir)) { - return ImmutableList.of(); - } - DirectoryStream.Filter<Path> filter = - new DirectoryStream.Filter<Path>() { - @Override - public boolean accept(Path entry) throws IOException { - String n = entry.getFileName().toString(); - boolean accept = - !n.startsWith(".last_") && !n.startsWith(".next_") && Files.isRegularFile(entry); - if (!Strings.isNullOrEmpty(suffix)) { - accept &= n.endsWith(suffix); - } - return accept; - } - }; - try (DirectoryStream<Path> files = Files.newDirectoryStream(pluginsDir, filter)) { - return Ordering.natural().sortedCopy(files); - } - } - - public static List<Path> listPlugins(Path pluginsDir) throws IOException { - return listPlugins(pluginsDir, null); - } - public boolean isRemoteAdminEnabled() { return remoteAdmin; } @@ -191,8 +160,8 @@ public class PluginLoader implements LifecycleListener { checkRemoteInstall(); String fileName = originalName; - Path tmp = asTemp(in, ".next_" + fileName + "_", ".tmp", pluginsDir); - String name = MoreObjects.firstNonNull(getGerritPluginName(tmp), nameOf(fileName)); + Path tmp = PluginUtil.asTemp(in, ".next_" + fileName + "_", ".tmp", pluginsDir); + String name = MoreObjects.firstNonNull(getGerritPluginName(tmp), PluginUtil.nameOf(fileName)); if (!originalName.equals(name)) { log.warn( "Plugin provides its own name: <{}>, use it instead of the input name: <{}>", @@ -230,20 +199,6 @@ public class PluginLoader implements LifecycleListener { return name; } - static Path asTemp(InputStream in, String prefix, String suffix, Path dir) throws IOException { - Path tmp = Files.createTempFile(dir, prefix, suffix); - boolean keep = false; - try (OutputStream out = Files.newOutputStream(tmp)) { - ByteStreams.copy(in, out); - keep = true; - return tmp; - } finally { - if (!keep) { - Files.delete(tmp); - } - } - } - private synchronized void unloadPlugin(Plugin plugin) { persistentCacheFactory.onStop(plugin); String name = plugin.getName(); @@ -353,7 +308,16 @@ public class PluginLoader implements LifecycleListener { @Override public synchronized void start() { removeStalePluginFiles(); - log.info("Loading plugins from {}", pluginsDir.toAbsolutePath()); + Path absolutePath = pluginsDir.toAbsolutePath(); + if (!Files.exists(absolutePath)) { + log.info("{} does not exist; creating", absolutePath); + try { + Files.createDirectories(absolutePath); + } catch (IOException e) { + log.error("Failed to create {}: {}", absolutePath, e.getMessage()); + } + } + log.info("Loading plugins from {}", absolutePath); srvInfoImpl.state = ServerInformation.State.STARTUP; rescan(); srvInfoImpl.state = ServerInformation.State.RUNNING; @@ -428,7 +392,7 @@ public class PluginLoader implements LifecycleListener { String name = entry.getKey(); Path path = entry.getValue(); String fileName = path.getFileName().toString(); - if (!isJsPlugin(fileName) && !serverPluginFactory.handles(path)) { + if (!isUiPlugin(fileName) && !serverPluginFactory.handles(path)) { log.warn("No Plugin provider was found that handles this file format: {}", fileName); continue; } @@ -589,19 +553,7 @@ public class PluginLoader implements LifecycleListener { } } - public static String nameOf(Path plugin) { - return nameOf(plugin.getFileName().toString()); - } - - private static String nameOf(String name) { - if (name.endsWith(".disabled")) { - name = name.substring(0, name.lastIndexOf('.')); - } - int ext = name.lastIndexOf('.'); - return 0 < ext ? name.substring(0, ext) : name; - } - - private static String getExtension(String name) { + private String getExtension(String name) { int ext = name.lastIndexOf('.'); return 0 < ext ? name.substring(ext) : ""; } @@ -609,7 +561,7 @@ public class PluginLoader implements LifecycleListener { private Plugin loadPlugin(String name, Path srcPlugin, FileSnapshot snapshot) throws InvalidPluginException { String pluginName = srcPlugin.getFileName().toString(); - if (isJsPlugin(pluginName)) { + if (isUiPlugin(pluginName)) { return loadJsPlugin(name, srcPlugin, snapshot); } else if (serverPluginFactory.handles(srcPlugin)) { return loadServerPlugin(srcPlugin, snapshot); @@ -651,22 +603,9 @@ public class PluginLoader implements LifecycleListener { getPluginDataDir(name))); } - static ClassLoader parentFor(Plugin.ApiType type) throws InvalidPluginException { - switch (type) { - case EXTENSION: - return PluginName.class.getClassLoader(); - case PLUGIN: - return PluginLoader.class.getClassLoader(); - case JS: - return JavaScriptPlugin.class.getClassLoader(); - default: - throw new InvalidPluginException("Unsupported ApiType " + type); - } - } - // Only one active plugin per plugin name can exist for each plugin name. // Filter out disabled plugins and transform the multimap to a map - private static Map<String, Path> filterDisabled(SetMultimap<String, Path> pluginPaths) { + private Map<String, Path> filterDisabled(SetMultimap<String, Path> pluginPaths) { Map<String, Path> activePlugins = Maps.newHashMapWithExpectedSize(pluginPaths.keys().size()); for (String name : pluginPaths.keys()) { for (Path pluginPath : pluginPaths.asMap().get(name)) { @@ -734,21 +673,21 @@ public class PluginLoader implements LifecycleListener { private List<Path> scanPathsInPluginsDirectory(Path pluginsDir) { try { - return listPlugins(pluginsDir); + return PluginUtil.listPlugins(pluginsDir); } catch (IOException e) { log.error("Cannot list {}", pluginsDir.toAbsolutePath(), e); return ImmutableList.of(); } } - private static Iterable<Path> filterDisabledPlugins(Collection<Path> paths) { + private Iterable<Path> filterDisabledPlugins(Collection<Path> paths) { return Iterables.filter(paths, p -> !p.getFileName().toString().endsWith(".disabled")); } public String getGerritPluginName(Path srcPath) { String fileName = srcPath.getFileName().toString(); - if (isJsPlugin(fileName)) { - return fileName.substring(0, fileName.length() - 3); + if (isUiPlugin(fileName)) { + return fileName.substring(0, fileName.lastIndexOf('.')); } if (serverPluginFactory.handles(srcPath)) { return serverPluginFactory.getPluginName(srcPath); @@ -764,11 +703,11 @@ public class PluginLoader implements LifecycleListener { return map; } - private static boolean isJsPlugin(String name) { - return isPlugin(name, "js"); + private boolean isUiPlugin(String name) { + return isPlugin(name, "js") || isPlugin(name, "html"); } - private static boolean isPlugin(String fileName, String ext) { + private boolean isPlugin(String fileName, String ext) { String fullExt = "." + ext; return fileName.endsWith(fullExt) || fileName.endsWith(fullExt + ".disabled"); } |