From e59bdf4ac0b66ce683be1bbc36852bfce6f70a63 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sun, 4 Oct 2020 13:43:15 +0800 Subject: Pump up --- .../kbackupfabric/operation/BackupOperation.java | 30 ++++++++-------- .../abstracts/AbstractAsyncOperation.java | 13 +++++-- .../abstracts/AbstractBlockingOperation.java | 2 +- .../abstracts/AbstractConfirmableOperation.java | 2 +- .../abstracts/AbstractSerialOperation.java | 17 +++++++++ .../abstracts/AbstractSerializedOperation.java | 12 ------- .../abstracts/InvokableAsyncOperation.java | 1 - .../abstracts/InvokableBlockingOperation.java | 1 + .../operation/abstracts/InvokableOperation.java | 2 +- .../operation/backup/BackupMethod.java | 5 +-- .../operation/backup/PrimitiveBackupMehtod.java | 42 ---------------------- .../operation/backup/PrimitiveBackupMethod.java | 42 ++++++++++++++++++++++ 12 files changed, 92 insertions(+), 77 deletions(-) create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractSerialOperation.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractSerializedOperation.java delete mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMehtod.java create mode 100644 src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java (limited to 'src/main') diff --git a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java index 4c08d14..071726b 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/BackupOperation.java @@ -56,21 +56,23 @@ public class BackupOperation extends InvokableAsyncBlockingOperation { String levelPath = getLevelPath(server); String backupFileName = getBackupFileName(backupName); - backupMethod.backup(backupName,levelPath,backupSaveDirectory); - - // Restore old autosave switch stat - server.getWorlds().forEach(world -> world.savingDisabled = oldWorldsSavingDisabled.getOrDefault(world, true)); - - // Print finish message: time elapsed and file size - long timeElapsedMillis = System.currentTimeMillis() - startTime; - String msgText = String.format("Backup finished. Time elapsed: %.2fs.", timeElapsedMillis / 1000.0); - File backupZipFile = new File(backupSaveDirectory, backupFileName); - try { - msgText += String.format(" File size: %s.", humanFileSize(backupZipFile.length())); - } catch (SecurityException ignored) { + if(backupMethod.backup(backupName,levelPath,backupSaveDirectory)) { + // Restore old autosave switch stat + server.getWorlds().forEach(world -> world.savingDisabled = oldWorldsSavingDisabled.getOrDefault(world, true)); + + // Print finish message: time elapsed and file size + long timeElapsedMillis = System.currentTimeMillis() - startTime; + String msgText = String.format("Backup finished. Time elapsed: %.2fs.", timeElapsedMillis / 1000.0); + File backupZipFile = new File(backupSaveDirectory, backupFileName); + try { + msgText += String.format(" File size: %s.", humanFileSize(backupZipFile.length())); + } catch (SecurityException ignored) { + } + PrintUtil.msgInfo(context, msgText, true); + } else { + // failed + PrintUtil.msgErr(context, "Backup operation failed. No further information."); } - PrintUtil.msgInfo(context, msgText, true); - } catch (SecurityException e) { msgInfo(context, String.format("Failed to create backup saving directory: %s. Failed to backup.", backupSaveDirectory)); } catch (IOException e) { diff --git a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractAsyncOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractAsyncOperation.java index f1a19de..bb0c704 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractAsyncOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractAsyncOperation.java @@ -1,6 +1,10 @@ package com.keuin.kbackupfabric.operation.abstracts; -public abstract class AbstractAsyncOperation extends AbstractSerializedOperation { +/** + * A basic async operation, but not invokable. + * If you want a invokable interface (InvokableOperation), use InvokableAsyncOperation instead. + */ +public abstract class AbstractAsyncOperation extends AbstractSerialOperation { private final Thread thread; private final String name; @@ -14,7 +18,7 @@ public abstract class AbstractAsyncOperation extends AbstractSerializedOperation /** * Start the worker thread. * - * @return true if succeed starting, false if already started. + * @return true if succeed starting, false if this operation is already started, or the sync method failed. */ @Override protected final boolean operate() { @@ -30,13 +34,16 @@ public abstract class AbstractAsyncOperation extends AbstractSerializedOperation /** * Implement your async operation here. - * When this method returns, the operation must finish. + * After starting the operation, this method will be run in another thread after the sync method returns. + * When this method returns, the operation must have been finished. */ protected abstract void async(); /** * If necessary, implement your sync operations here. * It will be invoked before starting the async thread. + * If this method failed, the async method will not be invoked. + * @return whether this method succeed. */ protected boolean sync() { return true; diff --git a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractBlockingOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractBlockingOperation.java index 15b72c9..b9638a8 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractBlockingOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractBlockingOperation.java @@ -2,7 +2,7 @@ package com.keuin.kbackupfabric.operation.abstracts; import com.keuin.kbackupfabric.operation.abstracts.i.Blocking; -public abstract class AbstractBlockingOperation extends AbstractSerializedOperation implements Blocking { +public abstract class AbstractBlockingOperation extends AbstractSerialOperation implements Blocking { private static final Object sync = new Object(); private static boolean isBlocking = false; diff --git a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractConfirmableOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractConfirmableOperation.java index 0a56008..e234ddb 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractConfirmableOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractConfirmableOperation.java @@ -1,6 +1,6 @@ package com.keuin.kbackupfabric.operation.abstracts; -public abstract class AbstractConfirmableOperation extends AbstractSerializedOperation { +public abstract class AbstractConfirmableOperation extends AbstractSerialOperation { public final boolean confirm() { return operate(); diff --git a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractSerialOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractSerialOperation.java new file mode 100644 index 0000000..92dfb53 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractSerialOperation.java @@ -0,0 +1,17 @@ +package com.keuin.kbackupfabric.operation.abstracts; + +/** + * The most basic operation abstraction. + * This class represents an serial operation, which is limited in a non-public method. + * Note that the operation is not invokable by default, you should use InvokableOperation in order to provide a public method for users to call. + */ +public abstract class AbstractSerialOperation { + /** + * Do your operation here. + * This method is not designed to be public. + * When this method returns, the operation must have been finished. + * + * @return whether the operation succeed. + */ + protected abstract boolean operate(); +} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractSerializedOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractSerializedOperation.java deleted file mode 100644 index 3168e8c..0000000 --- a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/AbstractSerializedOperation.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.keuin.kbackupfabric.operation.abstracts; - -public abstract class AbstractSerializedOperation { - /** - * Do your operation here. - * This method is not designed to be public. - * When this method returns, the operation must have finished. - * - * @return the stat code. - */ - protected abstract boolean operate(); -} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableAsyncOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableAsyncOperation.java index 518f670..707e3c5 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableAsyncOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableAsyncOperation.java @@ -4,7 +4,6 @@ public abstract class InvokableAsyncOperation extends InvokableOperation { private final AbstractAsyncOperation asyncOperation; - public InvokableAsyncOperation(String name) { asyncOperation = new AbstractAsyncOperation(name) { @Override diff --git a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableBlockingOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableBlockingOperation.java index b1a3748..508c676 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableBlockingOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableBlockingOperation.java @@ -27,6 +27,7 @@ public abstract class InvokableBlockingOperation extends InvokableOperation impl operation.block(blockState); } + @Deprecated protected void noUnblocking(boolean b) { operation.noUnblocking(b); } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableOperation.java b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableOperation.java index 47c1ca8..19105f8 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableOperation.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/abstracts/InvokableOperation.java @@ -2,7 +2,7 @@ package com.keuin.kbackupfabric.operation.abstracts; import com.keuin.kbackupfabric.operation.abstracts.i.Invokable; -public abstract class InvokableOperation extends AbstractSerializedOperation implements Invokable { +public abstract class InvokableOperation extends AbstractSerialOperation implements Invokable { public boolean invoke() { return operate(); } diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java index 97c609d..b0b77cb 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/BackupMethod.java @@ -10,10 +10,11 @@ public interface BackupMethod { /** * Perform a backup with given method. The backup will be saved as the given name. - * Note: real file name is depended by the backup type. + * Note: real file name depends on the backup type. * @param backupName the backup name. + * @return if the backup operation succeed. */ - void backup(@NotNull String backupName, @NotNull String levelPath, @NotNull String backupSaveDirectory) throws IOException; + boolean backup(@NotNull String backupName, @NotNull String levelPath, @NotNull String backupSaveDirectory) throws IOException; BackupFileNameBuilder getBackupFileNameBuilder(); diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMehtod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMehtod.java deleted file mode 100644 index 3c2a608..0000000 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMehtod.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.keuin.kbackupfabric.operation.backup; - -import com.keuin.kbackupfabric.exception.ZipUtilException; -import com.keuin.kbackupfabric.metadata.BackupMetadata; -import com.keuin.kbackupfabric.util.PrintUtil; -import com.keuin.kbackupfabric.util.ZipUtil; -import com.keuin.kbackupfabric.util.backup.builder.BackupFileNameBuilder; -import com.keuin.kbackupfabric.util.backup.formatter.BackupFileNameFormatter; - -import java.io.File; -import java.io.IOException; -import java.time.LocalDate; -import java.time.LocalDateTime; - -public class PrimitiveBackupMehtod implements BackupMethod { - @Override - public boolean backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException { - try { - String backupFileName = BackupFileNameBuilder.primitiveZipBackup().build(LocalDateTime.now(),backupName); - BackupMetadata backupMetadata = new BackupMetadata(System.currentTimeMillis(), backupName); - - PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSaveDirectory)); - PrintUtil.info("Compressing level ..."); - ZipUtil.makeBackupZip(levelPath, backupSaveDirectory, backupFileName, backupMetadata); - - } catch (ZipUtilException exception) { - PrintUtil.info("Infinite recursive of directory tree detected, backup was aborted."); - return false; - } - return true; - } - - @Override - public BackupFileNameBuilder getBackupFileNameBuilder() { - return BackupFileNameBuilder.primitiveZipBackup(); - } - - @Override - public BackupFileNameFormatter getBackupFileNameFormatter() { - return BFNF; - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java b/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java new file mode 100644 index 0000000..bb1c8cb --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java @@ -0,0 +1,42 @@ +package com.keuin.kbackupfabric.operation.backup; + +import com.keuin.kbackupfabric.exception.ZipUtilException; +import com.keuin.kbackupfabric.metadata.BackupMetadata; +import com.keuin.kbackupfabric.util.PrintUtil; +import com.keuin.kbackupfabric.util.ZipUtil; +import com.keuin.kbackupfabric.util.backup.builder.BackupFileNameBuilder; +import com.keuin.kbackupfabric.util.backup.formatter.BackupFileNameFormatter; + +import java.io.File; +import java.io.IOException; +import java.time.LocalDate; +import java.time.LocalDateTime; + +public class PrimitiveBackupMethod implements BackupMethod { + @Override + public boolean backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException { + try { + String backupFileName = BackupFileNameBuilder.primitiveZipBackup().build(LocalDateTime.now(),backupName); + BackupMetadata backupMetadata = new BackupMetadata(System.currentTimeMillis(), backupName); + + PrintUtil.info(String.format("zip(srcPath=%s, destPath=%s)", levelPath, backupSaveDirectory)); + PrintUtil.info("Compressing level ..."); + ZipUtil.makeBackupZip(levelPath, backupSaveDirectory, backupFileName, backupMetadata); + + } catch (ZipUtilException exception) { + PrintUtil.info("Infinite recursive of directory tree detected, backup was aborted."); + return false; + } + return true; + } + + @Override + public BackupFileNameBuilder getBackupFileNameBuilder() { + return BackupFileNameBuilder.primitiveZipBackup(); + } + + @Override + public BackupFileNameFormatter getBackupFileNameFormatter() { + return BFNF; + } +} -- cgit v1.2.3