mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-17 02:31:14 +01:00
Verify pending plugins on startup
This commit is contained in:
@@ -80,7 +80,7 @@ public class DefaultPluginManager implements PluginManager {
|
||||
this.eventBus = eventBus;
|
||||
|
||||
this.computeInstallationDependencies();
|
||||
this.contextFactory = (availablePlugins -> PluginInstallationContext.of(getInstalled(), availablePlugins));
|
||||
this.contextFactory = (availablePlugins -> PluginInstallationContext.from(getInstalled(), availablePlugins));
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
@@ -30,11 +30,9 @@ import com.google.common.base.Function;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import java.util.Set;
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The ExplodedSmp object represents an extracted SCM-Manager plugin. The object
|
||||
|
||||
@@ -41,18 +41,30 @@ public final class PluginInstallationContext {
|
||||
return new PluginInstallationContext(Collections.emptyMap());
|
||||
}
|
||||
|
||||
public static PluginInstallationContext of(Iterable<InstalledPlugin> installed, Iterable<AvailablePlugin> pending) {
|
||||
public static PluginInstallationContext fromDescriptors(Iterable<? extends PluginDescriptor> installed, Iterable<? extends PluginDescriptor> pending) {
|
||||
Map<String, NameAndVersion> dependencies = new HashMap<>();
|
||||
append(dependencies, installed);
|
||||
append(dependencies, pending);
|
||||
appendDescriptors(dependencies, installed);
|
||||
appendDescriptors(dependencies, pending);
|
||||
return new PluginInstallationContext(dependencies);
|
||||
}
|
||||
|
||||
private static <P extends Plugin> void append(Map<String, NameAndVersion> dependencies, Iterable<P> plugins) {
|
||||
for (Plugin plugin : plugins) {
|
||||
PluginInformation information = plugin.getDescriptor().getInformation();
|
||||
dependencies.put(information.getName(), new NameAndVersion(information.getName(), information.getVersion()));
|
||||
}
|
||||
public static PluginInstallationContext from(Iterable<? extends Plugin> installed, Iterable<? extends Plugin> pending) {
|
||||
Map<String, NameAndVersion> dependencies = new HashMap<>();
|
||||
appendPlugins(dependencies, installed);
|
||||
appendPlugins(dependencies, pending);
|
||||
return new PluginInstallationContext(dependencies);
|
||||
}
|
||||
|
||||
private static <P extends PluginDescriptor> void appendDescriptors(Map<String, NameAndVersion> dependencies, Iterable<P> descriptors) {
|
||||
descriptors.forEach(desc -> appendPlugins(dependencies, desc.getInformation()));
|
||||
}
|
||||
|
||||
private static <P extends Plugin> void appendPlugins(Map<String, NameAndVersion> dependencies, Iterable<P> plugins) {
|
||||
plugins.forEach(plugin -> appendPlugins(dependencies, plugin.getDescriptor().getInformation()));
|
||||
}
|
||||
|
||||
private static void appendPlugins(Map<String, NameAndVersion> dependencies, PluginInformation information) {
|
||||
dependencies.put(information.getName(), new NameAndVersion(information.getName(), information.getVersion()));
|
||||
}
|
||||
|
||||
public Optional<NameAndVersion> find(String name) {
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
@@ -35,7 +35,6 @@ import com.google.common.hash.Hashing;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.lifecycle.classloading.ClassLoaderLifeCycle;
|
||||
import sonia.scm.plugin.ExplodedSmp.PathTransformer;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
@@ -50,11 +49,14 @@ import java.nio.file.Path;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
import static java.util.stream.Collectors.toSet;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -162,25 +164,16 @@ public final class PluginProcessor
|
||||
{
|
||||
logger.info("collect plugins");
|
||||
|
||||
Set<Path> archives = collect(pluginDirectory, new PluginArchiveFilter());
|
||||
Set<ExplodedSmp> installedPlugins = findInstalledPlugins();
|
||||
logger.debug("found {} installed plugins", installedPlugins.size());
|
||||
|
||||
logger.debug("extract {} archives", archives.size());
|
||||
Set<ExplodedSmp> newlyInstalledPlugins = installPending(installedPlugins);
|
||||
logger.debug("finished installation of {} plugins", newlyInstalledPlugins.size());
|
||||
|
||||
extract(archives);
|
||||
|
||||
List<Path> dirs =
|
||||
collectPluginDirectories(pluginDirectory)
|
||||
.stream()
|
||||
.filter(isPluginDirectory())
|
||||
.collect(toList());
|
||||
|
||||
logger.debug("process {} directories: {}", dirs.size(), dirs);
|
||||
|
||||
List<ExplodedSmp> smps = Lists.transform(dirs, new PathTransformer());
|
||||
Set<ExplodedSmp> plugins = concat(installedPlugins, newlyInstalledPlugins);
|
||||
|
||||
logger.trace("start building plugin tree");
|
||||
|
||||
PluginTree pluginTree = new PluginTree(smps);
|
||||
PluginTree pluginTree = new PluginTree(plugins);
|
||||
|
||||
logger.info("install plugin tree:\n{}", pluginTree);
|
||||
|
||||
@@ -195,6 +188,46 @@ public final class PluginProcessor
|
||||
return ImmutableSet.copyOf(wrappers);
|
||||
}
|
||||
|
||||
private ImmutableSet<ExplodedSmp> concat(Set<ExplodedSmp> installedPlugins, Set<ExplodedSmp> newlyInstalledPlugins) {
|
||||
return ImmutableSet.<ExplodedSmp>builder().addAll(installedPlugins).addAll(newlyInstalledPlugins).build();
|
||||
}
|
||||
|
||||
private Set<ExplodedSmp> installPending(Set<ExplodedSmp> installedPlugins) throws IOException {
|
||||
Set<Path> archives = collect(pluginDirectory, new PluginArchiveFilter());
|
||||
logger.debug("start installation of {} pending archives", archives.size());
|
||||
|
||||
Map<Path, InstalledPluginDescriptor> pending = new HashMap<>();
|
||||
for (Path archive : archives) {
|
||||
pending.put(archive, SmpDescriptorExtractor.extractPluginDescriptor(archive));
|
||||
}
|
||||
|
||||
PluginInstallationContext installationContext = PluginInstallationContext.fromDescriptors(
|
||||
installedPlugins.stream().map(ExplodedSmp::getPlugin).collect(toSet()),
|
||||
pending.values()
|
||||
);
|
||||
|
||||
for (Map.Entry<Path, InstalledPluginDescriptor> entry : pending.entrySet()) {
|
||||
try {
|
||||
PluginInstallationVerifier.verify(installationContext, entry.getValue());
|
||||
} catch (PluginException ex) {
|
||||
Path path = entry.getKey();
|
||||
logger.error("failed to install smp {}, because it could not be verified", path);
|
||||
logger.error("to restore scm-manager functionality remove the smp file {} from the plugin directory", path);
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
|
||||
return extract(archives);
|
||||
}
|
||||
|
||||
private Set<ExplodedSmp> findInstalledPlugins() throws IOException {
|
||||
return collectPluginDirectories(pluginDirectory)
|
||||
.stream()
|
||||
.filter(isPluginDirectory())
|
||||
.map(ExplodedSmp::create)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private Predicate<Path> isPluginDirectory() {
|
||||
return dir -> Files.exists(dir.resolve(DIRECTORY_METAINF).resolve("scm").resolve("plugin.xml"));
|
||||
}
|
||||
@@ -505,10 +538,12 @@ public final class PluginProcessor
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
private void extract(Iterable<Path> archives) throws IOException
|
||||
private Set<ExplodedSmp> extract(Iterable<Path> archives) throws IOException
|
||||
{
|
||||
logger.debug("extract archives");
|
||||
|
||||
ImmutableSet.Builder<ExplodedSmp> extracted = ImmutableSet.builder();
|
||||
|
||||
for (Path archive : archives)
|
||||
{
|
||||
File archiveFile = archive.toFile();
|
||||
@@ -519,17 +554,18 @@ public final class PluginProcessor
|
||||
|
||||
logger.debug("extract plugin {}", smp.getPlugin());
|
||||
|
||||
File directory =
|
||||
PluginsInternal.createPluginDirectory(pluginDirectory.toFile(),
|
||||
smp.getPlugin());
|
||||
File directory = PluginsInternal.createPluginDirectory(pluginDirectory.toFile(), smp.getPlugin());
|
||||
|
||||
String checksum = com.google.common.io.Files.hash(archiveFile,
|
||||
Hashing.sha256()).toString();
|
||||
String checksum = com.google.common.io.Files.hash(archiveFile, Hashing.sha256()).toString();
|
||||
File checksumFile = PluginsInternal.getChecksumFile(directory);
|
||||
|
||||
PluginsInternal.extract(smp, checksum, directory, checksumFile, false);
|
||||
moveArchive(archive);
|
||||
|
||||
extracted.add(ExplodedSmp.create(directory.toPath()));
|
||||
}
|
||||
|
||||
return extracted.build();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
@@ -30,6 +30,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
@@ -71,7 +72,7 @@ public final class PluginTree
|
||||
*
|
||||
* @param smps
|
||||
*/
|
||||
public PluginTree(List<ExplodedSmp> smps)
|
||||
public PluginTree(Collection<ExplodedSmp> smps)
|
||||
{
|
||||
|
||||
smps.forEach(s -> {
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
package sonia.scm.plugin;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
@@ -33,9 +33,12 @@ import java.nio.file.Path;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
class SmpDescriptorExtractor {
|
||||
final class SmpDescriptorExtractor {
|
||||
|
||||
InstalledPluginDescriptor extractPluginDescriptor(Path file) throws IOException {
|
||||
private SmpDescriptorExtractor() {
|
||||
}
|
||||
|
||||
static InstalledPluginDescriptor extractPluginDescriptor(Path file) throws IOException {
|
||||
try (ZipInputStream zipInputStream = new ZipInputStream(Files.newInputStream(file), StandardCharsets.UTF_8)) {
|
||||
ZipEntry nextEntry;
|
||||
while ((nextEntry = zipInputStream.getNextEntry()) != null) {
|
||||
|
||||
Reference in New Issue
Block a user