From 5749fc65909f0ee934d0410406265387157575a1 Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 24 Apr 2020 12:21:47 +0800 Subject: Refactored pending task using OCP design pattern. --- .../keuin/kbackupfabric/operation/Confirmable.java | 20 ++++++++ .../kbackupfabric/operation/DeleteOperation.java | 59 ++++++++++++++++++++++ .../kbackupfabric/operation/RestoreOperation.java | 55 ++++++++++++++++++++ 3 files changed, 134 insertions(+) create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/Confirmable.java create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java (limited to 'src/main/java/com/keuin/kbackupfabric/operation') diff --git a/src/main/java/com/keuin/kbackupfabric/operation/Confirmable.java b/src/main/java/com/keuin/kbackupfabric/operation/Confirmable.java new file mode 100644 index 0000000..c8fdd71 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/Confirmable.java @@ -0,0 +1,20 @@ +package com.keuin.kbackupfabric.operation; + +import com.mojang.brigadier.context.CommandContext; +import net.minecraft.server.command.ServerCommandSource; + +public abstract class Confirmable { + + public static Confirmable createRestoreOperation(CommandContext context, String backupName) { + return new RestoreOperation(context, backupName); + } + + public static Confirmable createDeleteOperation(CommandContext context, String backupName) { + return new DeleteOperation(context, backupName); + } + + public abstract boolean confirm(); + + @Override + public abstract String toString(); +} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java new file mode 100644 index 0000000..1e28324 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/DeleteOperation.java @@ -0,0 +1,59 @@ +package com.keuin.kbackupfabric.operation; + +import com.mojang.brigadier.context.CommandContext; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.command.ServerCommandSource; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.File; +import java.io.IOException; + +import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.getBackupFileName; +import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.getBackupSaveDirectory; +import static com.keuin.kbackupfabric.util.PrintUtil.msgErr; +import static com.keuin.kbackupfabric.util.PrintUtil.msgInfo; +import static org.apache.commons.io.FileUtils.forceDelete; + +class DeleteOperation extends Confirmable { + + private static final Logger LOGGER = LogManager.getLogger(); + private final String backupName; + private final CommandContext context; + + DeleteOperation(CommandContext context, String backupName) { + this.backupName = backupName; + this.context = context; + } + + @Override + public boolean confirm() { + MinecraftServer server = context.getSource().getMinecraftServer(); + String backupFileName = getBackupFileName(backupName); + LOGGER.info("Deleting backup " + backupName); + File backupFile = new File(getBackupSaveDirectory(server), backupFileName); + int tryCounter = 0; + do { + if (tryCounter == 5) { + String msg = "Failed to delete file " + backupFileName; + LOGGER.error(msg); + msgErr(context, msg); + return false; + } + try { + if (!backupFile.delete()) + forceDelete(backupFile); + } catch (SecurityException | NullPointerException | IOException ignored) { + } + ++tryCounter; + } while (backupFile.exists()); + LOGGER.info("Deleted backup " + backupName); + msgInfo(context, "Deleted backup " + backupName); + return true; + } + + @Override + public String toString() { + return String.format("deletion of %s", backupName); + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java new file mode 100644 index 0000000..48dd413 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/RestoreOperation.java @@ -0,0 +1,55 @@ +package com.keuin.kbackupfabric.operation; + +import com.keuin.kbackupfabric.util.PrintUtil; +import com.keuin.kbackupfabric.worker.RestoreWorker; +import com.mojang.brigadier.context.CommandContext; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.command.ServerCommandSource; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.File; + +import static com.keuin.kbackupfabric.util.BackupFilesystemUtil.*; + +class RestoreOperation extends Confirmable { + + private static final Logger LOGGER = LogManager.getLogger(); + private final String backupName; + private final CommandContext context; + + RestoreOperation(CommandContext context, String backupName) { + this.backupName = backupName; + this.context = context; + } + + @Override + public boolean confirm() { + // do restore to backupName + MinecraftServer server = context.getSource().getMinecraftServer(); + PrintUtil.msgInfo(context, String.format("Restoring to previous world %s ...", backupName), true); + + String backupFileName = getBackupFileName(backupName); + LOGGER.debug("Backup file name: " + backupFileName); + File backupFile = new File(getBackupSaveDirectory(server), backupFileName); + + PrintUtil.msgInfo(context, "Server will shutdown in a few seconds, depended on your world size and the disk speed, the restore progress may take seconds or minutes.", true); + PrintUtil.msgInfo(context, "Please do not force the server stop, or the level would be broken.", true); + PrintUtil.msgInfo(context, "After it shuts down, please restart the server manually.", true); + final int WAIT_SECONDS = 10; + for (int i = 0; i < WAIT_SECONDS; ++i) { + try { + Thread.sleep(1000); + } catch (InterruptedException ignored) { + } + } + PrintUtil.msgInfo(context, "Shutting down ...", true); + RestoreWorker.invoke(server, backupFile.getPath(), getLevelPath(server)); + return true; + } + + @Override + public String toString() { + return String.format("restoration from %s", backupName); + } +} -- cgit v1.2.3