X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FBackupFiles.java;h=c67c307d223badfe1d10349283b4a846e57aee75;hb=fdde9a078d7bdb46ed9fb7fe115ea83c84a19c81;hp=05a1ac49af36627ff3178dba8cfbd6259bee0369;hpb=74755239a1beeeaba47c0fee8bfc14f8395e65ac;p=jalview.git diff --git a/src/jalview/io/BackupFiles.java b/src/jalview/io/BackupFiles.java index 05a1ac4..c67c307 100644 --- a/src/jalview/io/BackupFiles.java +++ b/src/jalview/io/BackupFiles.java @@ -29,8 +29,14 @@ import java.nio.file.StandardCopyOption; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.TreeMap; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import jalview.bin.Cache; import jalview.bin.Console; @@ -105,35 +111,115 @@ public class BackupFiles private static final String oldTempFileSuffix = "_oldfile_tobedeleted"; - private static ArrayList savesInProgress = new ArrayList<>(); + // thread pool used for completablefutures + private static final ExecutorService executorService = Executors + .newFixedThreadPool(3); + + private static List savesInProgress = new ArrayList<>(); + + private CompletableFuture myFuture = null; private boolean addSaveInProgress() { - if (savesInProgress.contains(file)) + if (savesInProgress.contains(this)) { return false; } else { - savesInProgress.add(file); + this.setMyFuture(); + savesInProgress.add(this); return true; } } - private boolean removeSaveInProgress() + private boolean removeSaveInProgress(boolean ret) { - if (savesInProgress.contains(file)) + if (savesInProgress.contains(this)) { + this.getMyFuture().complete(ret); // remove all occurrences - while (savesInProgress.remove(file)) + while (savesInProgress.remove(this)) { } return true; } - else + return false; + } + + private static CompletableFuture getNewFuture() + { + return new CompletableFuture() { - return false; + }; + } + + private CompletableFuture getMyFuture() + { + return this.myFuture; + } + + private void setMyFuture() + { + this.myFuture = getNewFuture(); + } + + public static boolean hasSavesInProgress() + { + boolean has = false; + for (CompletableFuture cf : savesInProgressCompletableFutures(true)) + { + has |= !cf.isDone(); + } + return has; + } + + public static List savesInProgressFiles(boolean all) + { + List files = new ArrayList<>(); + for (BackupFiles bfile : savesInProgress) + { + if (all || !bfile.getMyFuture().isDone()) + files.add(bfile.getFile()); + } + return files; + } + + public static List> savesInProgressCompletableFutures( + boolean all) + { + List> cfs = new ArrayList<>(); + for (BackupFiles bfile : savesInProgress) + { + if (all || !bfile.getMyFuture().isDone()) + cfs.add(bfile.getMyFuture()); } + return cfs; + } + + public static Future allSaved() + { + CompletableFuture f = new CompletableFuture<>(); + + executorService.submit(() -> { + for (BackupFiles buf : savesInProgress) + { + boolean allSaved = true; + try + { + allSaved &= buf.getMyFuture().get(); + } catch (InterruptedException e) + { + Console.debug("InterruptedException waiting for files to save", + e); + } catch (ExecutionException e) + { + Console.debug("ExecutionException waiting for files to save", e); + } + f.complete(allSaved); + } + }); + return f; } public BackupFiles(String filename) @@ -146,6 +232,12 @@ public class BackupFiles public BackupFiles(File file) { classInit(); + if (file.getParentFile() == null) + { + // filename probably in pwd represented with no parent -- fix it before + // it's a problem + file = file.getAbsoluteFile(); + } this.file = file; // add this file from the save in progress stack @@ -166,7 +258,8 @@ public class BackupFiles if (file != null) { String tempfilename = file.getName(); - File tempdir = file.getParentFile(); + File tempdir = file.getAbsoluteFile().getParentFile(); + tempdir.mkdirs(); Console.trace( "BACKUPFILES [file!=null] attempting to create temp file for " + tempfilename + " in dir " + tempdir); @@ -196,7 +289,7 @@ public class BackupFiles this.setTempFile(temp); } - public static void classInit() + private static void classInit() { Console.initLogger(); Console.trace("BACKUPFILES classInit"); @@ -855,7 +948,7 @@ public class BackupFiles } // remove this file from the save in progress stack - removeSaveInProgress(); + removeSaveInProgress(rename); return rename; } @@ -928,6 +1021,11 @@ public class BackupFiles return ret; } + public File getFile() + { + return file; + } + public static boolean moveFileToFile(File oldFile, File newFile) { Console.initLogger();