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 CouldNotDoubleRenameException extends RetryIOException {
012        /**
013         * File to rename to _oldFile.
014         */
015        private File _curFile = null;
016    
017        /**
018         * New fname of _curFile.
019         */
020        private File _oldFile = null;
021    
022        /**
023         * File to rename to _oldFile.
024         */
025        private File _newFile = null;
026    
027        /**
028         * Constructs a new exception.
029         * @param curFile current file, to be renamed to oldFile
030         * @param oldFile old file
031         * @param newFile new file, to be renamed to curFile
032         */
033        public CouldNotDoubleRenameException(File curFile, File oldFile, File newFile) {
034            super("Could not rename file " + curFile.getPath()+ " to " + oldFile.getPath() + " and rename file " +
035                  newFile.getPath()+ " to " + curFile.getPath());
036            _curFile = curFile;
037            _oldFile = oldFile;
038            _newFile = newFile;
039        }
040    
041        /**
042         * Retry the failed operation one more time.
043         * @throws java.io.IOException
044         */
045        public void retry() throws IOException {
046            if (_curFile.exists() && !_oldFile.exists() && _newFile.exists()) {
047                boolean res = _curFile.renameTo(_oldFile);
048                if (!res) {
049                    throw new IOException(getMessage());
050                }
051                res = _newFile.renameTo(_curFile);
052                if (!res) {
053                    throw new IOException(getMessage());
054                }
055            }
056            else {
057                throw new IOException(getMessage()+"; cannot fix, situation not applicable");
058            }
059        }
060    }