X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fio%2FBackupFiles.java;h=c67c307d223badfe1d10349283b4a846e57aee75;hb=b19880049cdb11882e68c75d7960465be4aa8665;hp=5594857866be98ab878411f4a5bf79255e0b9aa5;hpb=6c9f1f0278e44bdb3d777c0e4227c22fa09425bf;p=jalview.git diff --git a/src/jalview/io/BackupFiles.java b/src/jalview/io/BackupFiles.java index 5594857..c67c307 100644 --- a/src/jalview/io/BackupFiles.java +++ b/src/jalview/io/BackupFiles.java @@ -29,10 +29,17 @@ 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; import jalview.gui.Desktop; import jalview.gui.JvOptionPane; import jalview.util.MessageManager; @@ -104,6 +111,117 @@ public class BackupFiles private static final String oldTempFileSuffix = "_oldfile_tobedeleted"; + // 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(this)) + { + return false; + } + else + { + this.setMyFuture(); + savesInProgress.add(this); + return true; + } + } + + private boolean removeSaveInProgress(boolean ret) + { + if (savesInProgress.contains(this)) + { + this.getMyFuture().complete(ret); + // remove all occurrences + while (savesInProgress.remove(this)) + { + } + return true; + } + return false; + } + + private static CompletableFuture getNewFuture() + { + return new CompletableFuture() + { + }; + } + + 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) { this(new File(filename)); @@ -114,7 +232,17 @@ 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 + addSaveInProgress(); + BackupFilesPresetEntry bfpe = BackupFilesPresetEntry .getSavedBackupEntry(); this.suffix = bfpe.suffix; @@ -130,18 +258,19 @@ public class BackupFiles if (file != null) { String tempfilename = file.getName(); - File tempdir = file.getParentFile(); - Cache.log.debug( + File tempdir = file.getAbsoluteFile().getParentFile(); + tempdir.mkdirs(); + Console.trace( "BACKUPFILES [file!=null] attempting to create temp file for " + tempfilename + " in dir " + tempdir); temp = File.createTempFile(tempfilename, TEMP_FILE_EXT + newTempFileSuffix, tempdir); - Cache.log.debug( + Console.debug( "BACKUPFILES using temp file " + temp.getAbsolutePath()); } else { - Cache.log.debug( + Console.trace( "BACKUPFILES [file==null] attempting to create default temp file " + DEFAULT_TEMP_FILE + " with extension " + TEMP_FILE_EXT); @@ -149,29 +278,29 @@ public class BackupFiles } } catch (IOException e) { - Cache.log - .error("Could not create temp file to save to (IOException)"); - Cache.log.error(e.getMessage()); - Cache.log.debug(e.getStackTrace()); + Console.error("Could not create temp file to save to (IOException)"); + Console.error(e.getMessage()); + Console.debug(Cache.getStackTraceString(e)); } catch (Exception e) { - Cache.log.error("Exception creating temp file for saving"); - Cache.log.debug(e.getStackTrace()); + Console.error("Exception creating temp file for saving"); + Console.debug(Cache.getStackTraceString(e)); } this.setTempFile(temp); } - public static void classInit() + private static void classInit() { - Cache.log.debug("BACKUPFILES classInit"); + Console.initLogger(); + Console.trace("BACKUPFILES classInit"); boolean e = Cache.getDefault(ENABLED, !Platform.isJS()); setEnabled(e); - Cache.log.debug("BACKUPFILES " + (e ? "enabled" : "disabled")); + Console.trace("BACKUPFILES " + (e ? "enabled" : "disabled")); BackupFilesPresetEntry bfpe = BackupFilesPresetEntry .getSavedBackupEntry(); - Cache.log.debug("BACKUPFILES preset scheme " + bfpe.toString()); + Console.trace("BACKUPFILES preset scheme " + bfpe.toString()); setConfirmDelete(bfpe.confirmDelete); - Cache.log.debug("BACKUPFILES confirm delete " + bfpe.confirmDelete); + Console.trace("BACKUPFILES confirm delete " + bfpe.confirmDelete); } public static void setEnabled(boolean flag) @@ -215,10 +344,9 @@ public class BackupFiles path = this.getTempFile().getCanonicalPath(); } catch (IOException e) { - Cache.log.error( - "IOException when getting Canonical Path of temp file '" - + this.getTempFile().getName() + "'"); - Cache.log.debug(e.getStackTrace()); + Console.error("IOException when getting Canonical Path of temp file '" + + this.getTempFile().getName() + "'"); + Console.debug(Cache.getStackTraceString(e)); } return path; } @@ -254,7 +382,7 @@ public class BackupFiles || suffix.length() == 0) { // nothing to do - Cache.log.debug("BACKUPFILES rollBackupFiles nothing to do." + ", " + Console.debug("BACKUPFILES rollBackupFiles nothing to do." + ", " + "filename: " + (file != null ? file.getName() : "null") + ", " + "file exists: " + file.exists() + ", " + "enabled: " + enabled + ", " + "max: " + max + ", " + "suffix: '" + suffix @@ -262,7 +390,7 @@ public class BackupFiles return true; } - Cache.log.debug("BACKUPFILES rollBackupFiles starting"); + Console.trace("BACKUPFILES rollBackupFiles starting"); String dir = ""; File dirFile; @@ -270,19 +398,18 @@ public class BackupFiles { dirFile = file.getParentFile(); dir = dirFile.getCanonicalPath(); - Cache.log.debug("BACKUPFILES dir: " + dir); + Console.trace("BACKUPFILES dir: " + dir); } catch (Exception e) { - Cache.log.error( - "Could not get canonical path for file '" + file + "'"); - Cache.log.error(e.getMessage()); - Cache.log.debug(e.getStackTrace()); + Console.error("Could not get canonical path for file '" + file + "'"); + Console.error(e.getMessage()); + Console.debug(Cache.getStackTraceString(e)); return false; } String filename = file.getName(); String basename = filename; - Cache.log.debug("BACKUPFILES filename is " + filename); + Console.trace("BACKUPFILES filename is " + filename); boolean ret = true; // Create/move backups up one @@ -294,12 +421,11 @@ public class BackupFiles File[] backupFiles = dirFile.listFiles(bff); int nextIndexNum = 0; - Cache.log - .debug("BACKUPFILES backupFiles.length: " + backupFiles.length); + Console.trace("BACKUPFILES backupFiles.length: " + backupFiles.length); if (backupFiles.length == 0) { // No other backup files. Just need to move existing file to backupfile_1 - Cache.log.debug( + Console.trace( "BACKUPFILES no existing backup files, setting index to 1"); nextIndexNum = 1; } @@ -313,7 +439,7 @@ public class BackupFiles if (reverseOrder) { // backup style numbering - Cache.log.debug("BACKUPFILES rolling files in reverse order"); + Console.trace("BACKUPFILES rolling files in reverse order"); int tempMax = noMax ? -1 : max; // noMax == true means no limits @@ -345,7 +471,7 @@ public class BackupFiles // no "oldest" file to delete previousFile = backupfile_n; fileToBeDeleted = null; - Cache.log.debug("BACKUPFILES No oldest file to delete"); + Console.trace("BACKUPFILES No oldest file to delete"); continue; } @@ -356,9 +482,9 @@ public class BackupFiles File replacementFile = backupfile_n; long fileToBeDeletedLMT = fileToBeDeleted.lastModified(); long replacementFileLMT = replacementFile.lastModified(); - Cache.log.debug("BACKUPFILES fileToBeDeleted is " + Console.trace("BACKUPFILES fileToBeDeleted is " + fileToBeDeleted.getAbsolutePath()); - Cache.log.debug("BACKUPFILES replacementFile is " + Console.trace("BACKUPFILES replacementFile is " + backupfile_n.getAbsolutePath()); try @@ -372,7 +498,7 @@ public class BackupFiles .format(fileToBeDeletedLMT); String replacementFileLMTString = sdf .format(replacementFileLMT); - Cache.log.warn("WARNING! I am set to delete backupfile " + Console.warn("WARNING! I am set to delete backupfile " + fileToBeDeleted.getName() + " has modification time " + fileToBeDeletedLMTString @@ -383,7 +509,7 @@ public class BackupFiles boolean delete = confirmNewerDeleteFile(fileToBeDeleted, replacementFile, true); - Cache.log.debug("BACKUPFILES " + Console.trace("BACKUPFILES " + (delete ? "confirmed" : "not") + " deleting file " + fileToBeDeleted.getAbsolutePath() + " which is newer than " @@ -396,7 +522,7 @@ public class BackupFiles } else { - Cache.log.debug("BACKUPFILES moving " + Console.debug("BACKUPFILES moving " + fileToBeDeleted.getAbsolutePath() + " to " + oldestTempFile.getAbsolutePath()); moveFileToFile(fileToBeDeleted, oldestTempFile); @@ -404,7 +530,7 @@ public class BackupFiles } else { - Cache.log.debug("BACKUPFILES going to move " + Console.debug("BACKUPFILES going to move " + fileToBeDeleted.getAbsolutePath() + " to " + oldestTempFile.getAbsolutePath()); moveFileToFile(fileToBeDeleted, oldestTempFile); @@ -413,10 +539,10 @@ public class BackupFiles } catch (Exception e) { - Cache.log.error( + Console.error( "Error occurred, probably making new temp file for '" + fileToBeDeleted.getName() + "'"); - Cache.log.error(e.getStackTrace()); + Console.error(Cache.getStackTraceString(e)); } // reset @@ -431,7 +557,9 @@ public class BackupFiles { if (previousFile != null) { - ret = ret && moveFileToFile(backupfile_n, previousFile); + // using boolean '&' instead of '&&' as don't want moveFileToFile + // attempt to be conditional (short-circuit) + ret = ret & moveFileToFile(backupfile_n, previousFile); } } @@ -456,13 +584,13 @@ public class BackupFiles } bfsb.append(backupFiles[i].getName()); } - Cache.log.debug("BACKUPFILES backupFiles: " + bfsb.toString()); + Console.trace("BACKUPFILES backupFiles: " + bfsb.toString()); // noMax == true means keep all backup files if ((!noMax) && bfTreeMap.size() >= max) { - Cache.log.debug("BACKUPFILES noMax: " + noMax + ", " + "max: " - + max + ", " + "bfTreeMap.size(): " + bfTreeMap.size()); + Console.trace("BACKUPFILES noMax: " + noMax + ", " + "max: " + max + + ", " + "bfTreeMap.size(): " + bfTreeMap.size()); // need to delete some files to keep number of backups to designated // max. // Note that if the suffix is not numbered then do not delete any @@ -471,7 +599,7 @@ public class BackupFiles int numToDelete = suffix.indexOf(NUM_PLACEHOLDER) > -1 ? bfTreeMap.size() - max + 1 : 0; - Cache.log.debug("BACKUPFILES numToDelete: " + numToDelete); + Console.trace("BACKUPFILES numToDelete: " + numToDelete); // the "replacement" file is the latest backup file being kept (it's // not replacing though) File replacementFile = numToDelete < backupFiles.length @@ -484,7 +612,7 @@ public class BackupFiles File fileToBeDeleted = backupFiles[i]; boolean delete = true; - Cache.log.debug( + Console.trace( "BACKUPFILES fileToBeDeleted: " + fileToBeDeleted); boolean newer = false; @@ -501,7 +629,7 @@ public class BackupFiles String replacementFileLMTString = sdf .format(replacementFileLMT); - Cache.log.warn("WARNING! I am set to delete backupfile '" + Console.warn("WARNING! I am set to delete backupfile '" + fileToBeDeleted.getName() + "' has modification time " + fileToBeDeletedLMTString @@ -516,14 +644,14 @@ public class BackupFiles { // User has confirmed delete -- no need to add it to the list fileToBeDeleted.delete(); - Cache.log.debug("BACKUPFILES deleting fileToBeDeleted: " + Console.debug("BACKUPFILES deleting fileToBeDeleted: " + fileToBeDeleted); delete = false; } else { // keeping file, nothing to do! - Cache.log.debug("BACKUPFILES keeping fileToBeDeleted: " + Console.debug("BACKUPFILES keeping fileToBeDeleted: " + fileToBeDeleted); } } @@ -531,7 +659,7 @@ public class BackupFiles if (delete) { addDeleteFile(fileToBeDeleted); - Cache.log.debug("BACKUPFILES addDeleteFile(fileToBeDeleted): " + Console.debug("BACKUPFILES addDeleteFile(fileToBeDeleted): " + fileToBeDeleted); } @@ -547,15 +675,17 @@ public class BackupFiles String latestBackupFilename = dir + File.separatorChar + BackupFilenameParts.getBackupFilename(nextIndexNum, basename, suffix, digits); - Cache.log.debug("BACKUPFILES Moving old file [" + file + Console.trace("BACKUPFILES Moving old file [" + file + "] to latestBackupFilename [" + latestBackupFilename + "]"); - ret |= moveFileToFile(file, new File(latestBackupFilename)); - Cache.log.debug( + // using boolean '&' instead of '&&' as don't want moveFileToFile attempt to + // be conditional (short-circuit) + ret = ret & moveFileToFile(file, new File(latestBackupFilename)); + Console.debug( "BACKUPFILES moving " + file + " to " + latestBackupFilename + " was " + (ret ? "" : "NOT ") + "successful"); if (tidyUp) { - Cache.log.debug("BACKUPFILES tidying up files"); + Console.debug("BACKUPFILES tidying up files"); tidyUpFiles(); } @@ -611,7 +741,7 @@ public class BackupFiles saveFile = nextTempFile(ftbd.getName(), ftbd.getParentFile()); } catch (Exception e) { - Cache.log.error( + Console.error( "Error when confirming to keep backup file newer than other backup files."); e.printStackTrace(); } @@ -721,10 +851,10 @@ public class BackupFiles for (int i = 0; i < deleteFiles.size(); i++) { File fileToDelete = deleteFiles.get(i); - Cache.log.debug( - "BACKUPFILES deleting fileToDelete:" + fileToDelete); + Console.trace("BACKUPFILES about to delete fileToDelete:" + + fileToDelete); fileToDelete.delete(); - Cache.log.warn("deleting '" + fileToDelete.getName() + "'"); + Console.warn("deleted '" + fileToDelete.getName() + "'"); } } @@ -796,6 +926,12 @@ public class BackupFiles "label.backupfiles_confirm_save_new_saved_file_not_ok")); // "The new saved file might not be okay." } + if (messageSB.length() > 0) + { + messageSB.append("\n"); + } + messageSB + .append(MessageManager.getString("label.continue_operation")); int confirmButton = Platform.isHeadless() ? JvOptionPane.OK_OPTION : JvOptionPane.showConfirmDialog(Desktop.desktop, @@ -811,6 +947,9 @@ public class BackupFiles tidyUpFiles(); } + // remove this file from the save in progress stack + removeSaveInProgress(rename); + return rename; } @@ -827,8 +966,7 @@ public class BackupFiles dirFile = file.getParentFile(); } catch (Exception e) { - Cache.log.error( - "Could not get canonical path for file '" + file + "'"); + Console.error("Could not get canonical path for file '" + file + "'"); return new TreeMap<>(); } @@ -869,46 +1007,52 @@ public class BackupFiles int pos = deleteFiles.indexOf(fileToBeDeleted); if (pos > -1) { - Cache.log.debug("BACKUPFILES not adding file " + Console.debug("BACKUPFILES not adding file " + fileToBeDeleted.getAbsolutePath() + " to the delete list (already at index" + pos + ")"); return true; } else { - Cache.log.debug("BACKUPFILES adding file " + Console.debug("BACKUPFILES adding file " + fileToBeDeleted.getAbsolutePath() + " to the delete list"); deleteFiles.add(fileToBeDeleted); } return ret; } + public File getFile() + { + return file; + } + public static boolean moveFileToFile(File oldFile, File newFile) { + Console.initLogger(); boolean ret = false; Path oldPath = Paths.get(oldFile.getAbsolutePath()); Path newPath = Paths.get(newFile.getAbsolutePath()); try { // delete destination file - not usually necessary but Just In Case... - Cache.log.debug("BACKUPFILES deleting " + newFile.getAbsolutePath()); + Console.trace("BACKUPFILES deleting " + newFile.getAbsolutePath()); newFile.delete(); - Cache.log.debug("BACKUPFILES moving " + oldFile.getAbsolutePath() + Console.trace("BACKUPFILES moving " + oldFile.getAbsolutePath() + " to " + newFile.getAbsolutePath()); Files.move(oldPath, newPath, StandardCopyOption.REPLACE_EXISTING); ret = true; - Cache.log.debug("BACKUPFILES move seems to have succeeded"); + Console.trace("BACKUPFILES move seems to have succeeded"); } catch (IOException e) { - Cache.log.warn("Could not move file '" + oldPath.toString() + "' to '" + Console.warn("Could not move file '" + oldPath.toString() + "' to '" + newPath.toString() + "'"); - Cache.log.error(e.getMessage()); - Cache.log.debug(e.getStackTrace()); + Console.error(e.getMessage()); + Console.debug(Cache.getStackTraceString(e)); ret = false; } catch (Exception e) { - Cache.log.error(e.getMessage()); - Cache.log.debug(e.getStackTrace()); + Console.error(e.getMessage()); + Console.debug(Cache.getStackTraceString(e)); ret = false; } return ret;