view src/org/dancres/blitz/disk/BackupTask.java @ 27:511648fa4d64 Version 2.1

Version to 2.1
author Dan Creswell <dan.creswell@gmail.com>
date Mon, 04 Jan 2010 13:00:40 +0000
parents 3dc0c5604566
children
line wrap: on
line source

package org.dancres.blitz.disk;

import java.io.IOException;
import java.io.File;

import java.util.logging.Level;

import org.dancres.io.FileCopier;

class BackupTask implements Runnable {
    private File theSourceDir;
    private File theDestDir;

    private FileCopier theCopier = new FileCopier();

    private boolean isComplete = false;
    private IOException theException = null;

    BackupTask(File aSource, File aDest) {
        theSourceDir = aSource;
        theDestDir = aDest;
    }

    public void run() {
        Disk.theLogger.log(Level.SEVERE, "Start backup: " + theSourceDir
                           + ", " + theDestDir);

        File[] myFiles = theSourceDir.listFiles();

        Disk.theLogger.log(Level.SEVERE, "Number of files: " +
                           myFiles.length);

        try {
            for (int i = 0; i < myFiles.length; i++) {
                if (myFiles[i].isFile()) {
                    Disk.theLogger.log(Level.SEVERE, "copy file: " +
                                       myFiles[i]);

                    theCopier.copy(myFiles[i], theDestDir);
                }
            }
        } catch (IOException anIOE) {
            theException = anIOE;
        }

        Disk.theLogger.log(Level.SEVERE, "Backup complete");

        synchronized(this) {
            isComplete = true;
            notify();
        }
    }

    void waitForCompletion() throws IOException {
        synchronized(this) {
            while (!isComplete) {
                try {
                    wait();
                } catch (InterruptedException anIE) {
                }
            }
        }

        if (theException != null)
            throw theException;
    }
}