001    package edu.rice.cs.cunit.instrumentors;
002    
003    import java.io.File;
004    import java.io.IOException;
005    
006    /**
007     * Exception inside the file instrumentor, preventing it to delete and rename two files.
008     *
009     * @author Mathias Ricken
010     */
011    public class CouldNotDeleteAndRenameException extends RetryIOException {
012        /**
013         * File to delete.
014         */
015        private File _toDelete = null;
016    
017        /**
018         * File to rename to _toDelete.
019         */
020        private File _toRename = null;
021    
022        /**
023         * Constructs a new exception.
024         * @param toDelete file to delete
025         * @param toRename file to rename to the first file
026         */
027        public CouldNotDeleteAndRenameException(File toDelete, File toRename) {
028            super("Could not delete file " + toDelete.getPath()+ " or rename file " + toRename.getPath());
029            _toDelete = toDelete;
030            _toRename = toRename;
031        }
032    
033        /**
034         * Retry the failed operation one more time.
035         * @throws java.io.IOException
036         */
037        public void retry() throws IOException {
038            if (_toDelete.exists() && _toRename.exists()) {
039                if (_toDelete.exists() && !(_toDelete.delete())) {
040                    throw new IOException(getMessage());
041                }
042                if (_toRename.exists() && !(_toRename.renameTo(_toDelete))) {
043                    throw new IOException(getMessage());
044                }
045            }
046            else {
047                throw new IOException(getMessage() + "; cannot fix, situation not applicable");
048            }
049        }
050    
051    
052    }