File autoSavedHomeFile = this.autoSavedFiles.get(home);
if (autoSavedHomeFile == null) {
File recoveredFilesFolder = getRecoveryFolder();
if (!recoveredFilesFolder.exists()) {
if (!recoveredFilesFolder.mkdirs()) {
throw new RecorderException("Can't create folder " + recoveredFilesFolder + " to store recovered files");
}
}
// Find a unique file for home in recovered files sub folder
if (autoSavedHome.getName() != null) {
String homeFile = new File(autoSavedHome.getName()).getName();
autoSavedHomeFile = new File(recoveredFilesFolder, homeFile + RECOVERED_FILE_EXTENSION);
if (autoSavedHomeFile.exists()) {
autoSavedHomeFile = new File(recoveredFilesFolder,
UUID.randomUUID() + "-" + homeFile + RECOVERED_FILE_EXTENSION);
}
} else {
autoSavedHomeFile = new File(recoveredFilesFolder,
UUID.randomUUID() + RECOVERED_FILE_EXTENSION);
}
}
freeLockedFile(autoSavedHomeFile);
if (autoSavedHome.isModified()) {
this.autoSavedFiles.put(home, autoSavedHomeFile);
try {
// Save home and lock the saved file to avoid possible auto recovery processes to read it
homeRecorder.writeHome(autoSavedHome, autoSavedHomeFile.getPath());
FileOutputStream lockedOutputStream = null;
try {
lockedOutputStream = new FileOutputStream(autoSavedHomeFile, true);
lockedOutputStream.getChannel().lock();
this.lockedOutputStreams.put(autoSavedHomeFile, lockedOutputStream);
} catch (OverlappingFileLockException ex) {
// Don't try to race with other processes that acquired a lock on the file
} catch (IOException ex) {
if (lockedOutputStream != null) {
try {
lockedOutputStream.close();
} catch (IOException ex1) {
// Forget it
}
}
throw new RecorderException("Can't lock saved home", ex);
}
} catch (InterruptedRecorderException ex) {
// Forget exception that probably happen because of shutdown hook management
}
} else {