| | |
| | | */ |
| | | public class FileManager { |
| | | |
| | | /** |
| | | * Describes the approach taken to deleting a file or directory. |
| | | */ |
| | | public enum DeletionPolicy { |
| | | |
| | | /** |
| | | * Delete the file or directory immediately. |
| | | */ |
| | | DELETE_IMMEDIATELY, |
| | | |
| | | /** |
| | | * Mark the file or directory for deletion after the JVM has exited. |
| | | */ |
| | | DELETE_ON_EXIT, |
| | | |
| | | /** |
| | | * First try to delete the file immediately. If the deletion was |
| | | * unsuccessful mark the file for deleteion when the JVM has |
| | | * existed. |
| | | */ |
| | | DELETE_ON_EXIT_IF_UNSUCCESSFUL |
| | | |
| | | } |
| | | |
| | | static private final Logger LOG = |
| | | Logger.getLogger(FileManager.class.getName()); |
| | | |
| | |
| | | throws ApplicationException |
| | | { |
| | | if (filter == null || filter.accept(object)) { |
| | | new DeleteOperation(object, false).apply(); |
| | | new DeleteOperation(object, DeletionPolicy.DELETE_IMMEDIATELY).apply(); |
| | | } |
| | | } |
| | | |
| | |
| | | * @throws org.opends.quicksetup.ApplicationException if something goes wrong. |
| | | */ |
| | | public void deleteRecursively(File file) throws ApplicationException { |
| | | deleteRecursively(file, null, false); |
| | | deleteRecursively(file, null, |
| | | FileManager.DeletionPolicy.DELETE_IMMEDIATELY); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @param file the path to be deleted. |
| | | * @param filter the filter of the files to know if the file can be deleted |
| | | * directly or not. |
| | | * @param onExit when true just marks the files for deletion after the |
| | | * @param deletePolicy describes how deletions are to be made |
| | | * JVM exits rather than deleting the files immediately. |
| | | * @throws ApplicationException if something goes wrong. |
| | | */ |
| | | public void deleteRecursively(File file, FileFilter filter, boolean onExit) |
| | | public void deleteRecursively(File file, FileFilter filter, |
| | | DeletionPolicy deletePolicy) |
| | | throws ApplicationException { |
| | | operateRecursively(new DeleteOperation(file, onExit), filter); |
| | | operateRecursively(new DeleteOperation(file, deletePolicy), filter); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | private class DeleteOperation extends FileOperation { |
| | | |
| | | private boolean afterExit; |
| | | private DeletionPolicy deletionPolicy; |
| | | |
| | | /** |
| | | * Creates a delete operation. |
| | | * @param objectFile to delete |
| | | * @param afterExit boolean indicates that the actual delete |
| | | * @param deletionPolicy describing how files will be deleted |
| | | * is to take place after this program exists. This is useful |
| | | * for cleaning up files that are currently in use. |
| | | */ |
| | | public DeleteOperation(File objectFile, boolean afterExit) { |
| | | public DeleteOperation(File objectFile, DeletionPolicy deletionPolicy) { |
| | | super(objectFile); |
| | | this.afterExit = afterExit; |
| | | this.deletionPolicy = deletionPolicy; |
| | | } |
| | | |
| | | /** |
| | | * {@inheritDoc} |
| | | */ |
| | | public FileOperation copyForChild(File child) { |
| | | return new DeleteOperation(child, afterExit); |
| | | return new DeleteOperation(child, deletionPolicy); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | int nTries = 5; |
| | | for (int i = 0; i < nTries && !delete; i++) { |
| | | if (afterExit) { |
| | | if (DeletionPolicy.DELETE_ON_EXIT.equals(deletionPolicy)) { |
| | | file.deleteOnExit(); |
| | | delete = true; |
| | | } else { |
| | | delete = file.delete(); |
| | | if (!delete && DeletionPolicy.DELETE_ON_EXIT_IF_UNSUCCESSFUL. |
| | | equals(deletionPolicy)) { |
| | | file.deleteOnExit(); |
| | | delete = true; |
| | | } |
| | | } |
| | | if (!delete) { |
| | | try { |