diff options
author | Keuin <[email protected]> | 2020-10-04 13:43:15 +0800 |
---|---|---|
committer | keuin <[email protected]> | 2020-10-04 13:43:15 +0800 |
commit | e59bdf4ac0b66ce683be1bbc36852bfce6f70a63 (patch) | |
tree | 6c8b97f97af857d2740e7b79d46349cc83a8c7dd /src/main | |
parent | d9c2af035ac2edb68bc8b6a1f94b7c772c3509c8 (diff) |
Pump up
Diffstat (limited to 'src/main')
11 files changed, 51 insertions, 36 deletions
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/PrimitiveBackupMethod.java index 3c2a608..bb1c8cb 100644 --- a/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMehtod.java +++ b/src/main/java/com/keuin/kbackupfabric/operation/backup/PrimitiveBackupMethod.java @@ -12,7 +12,7 @@ import java.io.IOException; import java.time.LocalDate; import java.time.LocalDateTime; -public class PrimitiveBackupMehtod implements BackupMethod { +public class PrimitiveBackupMethod implements BackupMethod { @Override public boolean backup(String backupName, String levelPath, String backupSaveDirectory) throws IOException { try { |