001: /*
002: * Created on Jan 12, 2006
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package org.enhydra.snapperAdmin.business;
008:
009: import java.io.File;
010: import java.util.ArrayList;
011: import java.util.Iterator;
012: import java.util.List;
013:
014: /**
015: * @author Zoran Milakovic
016: *
017: * TODO To change the template for this generated type comment go to
018: * Window - Preferences - Java - Code Style - Code Templates
019: */
020:
021: public class FileDeleter extends Thread {
022:
023: private int minSleep = 1;
024:
025: private static List filesToDelete = new ArrayList();
026:
027: static FileDeleter fd = null;
028:
029: private FileDeleter() {
030: }
031:
032: /**
033: * Starts new FileDeleter thread,which will delete file.
034: *
035: * @param file
036: * file to be deleted.
037: */
038: public static void delete(File file) {
039: if (fd != null) {
040: if (!fd.isAlive()) {
041: fd = new FileDeleter();
042: fd.start();
043: }
044: synchronized (filesToDelete) {
045: filesToDelete.add(file);
046: }
047: //System.out.println("Add file to delete: "+file.getAbsolutePath());
048: //System.out.println("filesToDelete.size(): "+filesToDelete.size());
049: } else {
050: fd = new FileDeleter();
051: synchronized (filesToDelete) {
052: filesToDelete.add(file);
053: }
054: ///System.out.println("Add file to delete: "+file.getAbsolutePath());
055: // System.out.println("filesToDelete.size(): "+filesToDelete.size());
056: fd.start();
057: }
058: }
059:
060: public void run() {
061: while (filesToDelete.size() > 0) {
062: //System.out.println("filesToDelete.size(): "+filesToDelete.size());
063: boolean deleted = false;
064: List toDelete = new ArrayList();
065: synchronized (filesToDelete) {
066: Iterator iter = filesToDelete.iterator();
067: while (iter.hasNext()) {
068: File fileToDelete = (File) iter.next();
069: //System.out.println("Try to delete: "+fileToDelete.getAbsolutePath());
070: if (fileToDelete != null) {
071: if (fileToDelete.exists()) {
072: deleted = fileToDelete.delete();
073: if (deleted) {
074: toDelete.add(fileToDelete);
075: //System.out.println("DELETED: " + fileToDelete.getAbsolutePath());
076: }
077: } else {
078: toDelete.add(fileToDelete);
079: }
080: }
081: }
082:
083: //remove from list
084: Iterator itera = toDelete.iterator();
085: while (itera.hasNext()) {
086: filesToDelete.remove((File) itera.next());
087: }
088: toDelete.clear();
089:
090: }
091:
092: try {
093: //wait for minSleep minutes, and try again
094: Thread.sleep(60000 * minSleep);
095: } catch (java.lang.InterruptedException e) {
096: //do nothing
097: //System.out.println("java.lang.InterruptedException catched...");
098: }
099: }
100:
101: //set fd to null
102: fd = null;
103:
104: }
105: }
|