Create plugin archive cleaner

SCM-Manager created directories for each plugin every time the plugin is installed without deleting the old directories. With this change the old directories will be removed. 

Committed-by: Rene Pfeuffer <rene.pfeuffer@cloudogu.com>
This commit is contained in:
Laura Gorzitze
2023-09-04 11:56:54 +02:00
parent 6d2b7cdb85
commit e63f4fcd64
5 changed files with 217 additions and 9 deletions

View File

@@ -0,0 +1,61 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.plugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.util.IOUtil;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class PluginArchiveCleaner {
private static final Logger LOG = LoggerFactory.getLogger(PluginArchiveCleaner.class);
private static final int MAX_ARCHIVE_COUNT = 5;
public void cleanup(Path archivePath) throws IOException {
try (Stream<Path> pathStream = Files.list(archivePath)) {
List<Path> pathList = pathStream
.filter(PluginArchiveCleaner::isAnInstalledPluginDirectory)
.sorted()
.collect(Collectors.toList());
for (int i = 0; i <= pathList.size() - MAX_ARCHIVE_COUNT; i++) {
LOG.debug("Delete old installation directory {}", pathList.get(i));
IOUtil.deleteSilently(pathList.get(i).toFile());
}
}
}
private static boolean isAnInstalledPluginDirectory(Path path) {
return Files.isDirectory(path) && path.getFileName().toString().matches("\\d\\d\\d\\d-\\d\\d-\\d\\d");
}
}

View File

@@ -26,6 +26,7 @@ package sonia.scm.plugin;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.ImmutableSet;
@@ -117,18 +118,21 @@ public final class PluginProcessor
* @param classLoaderLifeCycle
* @param pluginDirectory
*/
public PluginProcessor(ClassLoaderLifeCycle classLoaderLifeCycle, Path pluginDirectory)
{
public PluginProcessor(ClassLoaderLifeCycle classLoaderLifeCycle, Path pluginDirectory){
this(classLoaderLifeCycle, pluginDirectory, new PluginArchiveCleaner());
}
@VisibleForTesting
PluginProcessor(ClassLoaderLifeCycle classLoaderLifeCycle, Path pluginDirectory, PluginArchiveCleaner pluginArchiveCleaner) {
this.classLoaderLifeCycle = classLoaderLifeCycle;
this.pluginDirectory = pluginDirectory;
this.pluginArchiveCleaner = pluginArchiveCleaner;
this.installedRootDirectory = pluginDirectory.resolve(DIRECTORY_INSTALLED);
this.installedDirectory = findInstalledDirectory();
try
{
try {
this.context = JAXBContext.newInstance(InstalledPluginDescriptor.class);
}
catch (JAXBException ex)
{
} catch (JAXBException ex) {
throw new PluginLoadException("could not create jaxb context", ex);
}
}
@@ -571,6 +575,8 @@ public final class PluginProcessor
extracted.add(ExplodedSmp.create(directory.toPath()));
}
pluginArchiveCleaner.cleanup(installedRootDirectory);
return extracted.build();
}
@@ -583,7 +589,7 @@ public final class PluginProcessor
private Path findInstalledDirectory()
{
Path directory = null;
Path installed = pluginDirectory.resolve(DIRECTORY_INSTALLED);
Path installed = installedRootDirectory;
Path date = installed.resolve(createDate());
for (int i = 0; i < 999; i++)
@@ -707,9 +713,12 @@ public final class PluginProcessor
/** Field description */
private final JAXBContext context;
private final Path installedRootDirectory;
/** Field description */
private final Path installedDirectory;
/** Field description */
private final Path pluginDirectory;
private final PluginArchiveCleaner pluginArchiveCleaner;
}