Line data Source code
1 : // Copyright (C) 2017 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.plugins; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.common.collect.ImmutableList; 19 : import com.google.common.collect.Ordering; 20 : import com.google.common.io.ByteStreams; 21 : import com.google.gerrit.extensions.annotations.PluginName; 22 : import com.google.gerrit.extensions.webui.JavaScriptPlugin; 23 : import java.io.IOException; 24 : import java.io.InputStream; 25 : import java.io.OutputStream; 26 : import java.nio.file.DirectoryStream; 27 : import java.nio.file.Files; 28 : import java.nio.file.Path; 29 : import java.util.List; 30 : 31 0 : public class PluginUtil { 32 : public static List<Path> listPlugins(Path pluginsDir, String suffix) throws IOException { 33 138 : if (pluginsDir == null || !Files.exists(pluginsDir)) { 34 0 : return ImmutableList.of(); 35 : } 36 138 : DirectoryStream.Filter<Path> filter = 37 : entry -> { 38 0 : String n = entry.getFileName().toString(); 39 0 : boolean accept = 40 0 : !n.startsWith(".last_") && !n.startsWith(".next_") && Files.isRegularFile(entry); 41 0 : if (!Strings.isNullOrEmpty(suffix)) { 42 0 : accept &= n.endsWith(suffix); 43 : } 44 0 : return accept; 45 : }; 46 138 : try (DirectoryStream<Path> files = Files.newDirectoryStream(pluginsDir, filter)) { 47 138 : return Ordering.natural().sortedCopy(files); 48 : } 49 : } 50 : 51 : static List<Path> listPlugins(Path pluginsDir) throws IOException { 52 138 : return listPlugins(pluginsDir, null); 53 : } 54 : 55 : static Path asTemp(InputStream in, String prefix, String suffix, Path dir) throws IOException { 56 3 : if (!Files.exists(dir)) { 57 1 : Files.createDirectories(dir); 58 : } 59 3 : Path tmp = Files.createTempFile(dir, prefix, suffix); 60 3 : boolean keep = false; 61 3 : try (OutputStream out = Files.newOutputStream(tmp)) { 62 3 : ByteStreams.copy(in, out); 63 3 : keep = true; 64 3 : return tmp; 65 : } finally { 66 3 : if (!keep) { 67 0 : Files.delete(tmp); 68 : } 69 : } 70 : } 71 : 72 : public static String nameOf(Path plugin) { 73 1 : return nameOf(plugin.getFileName().toString()); 74 : } 75 : 76 : static String nameOf(String name) { 77 3 : if (name.endsWith(".disabled")) { 78 0 : name = name.substring(0, name.lastIndexOf('.')); 79 : } 80 3 : int ext = name.lastIndexOf('.'); 81 3 : return 0 < ext ? name.substring(0, ext) : name; 82 : } 83 : 84 : static ClassLoader parentFor(Plugin.ApiType type) throws InvalidPluginException { 85 1 : switch (type) { 86 : case EXTENSION: 87 1 : return PluginName.class.getClassLoader(); 88 : case PLUGIN: 89 0 : return PluginLoader.class.getClassLoader(); 90 : case JS: 91 0 : return JavaScriptPlugin.class.getClassLoader(); 92 : default: 93 0 : throw new InvalidPluginException("Unsupported ApiType " + type); 94 : } 95 : } 96 : }