1 22 23 package org.meshcms.core; 24 25 import java.io.*; 26 import org.meshcms.util.*; 27 28 31 public class DirectoryCleaner extends DirectoryParser { 32 private long currentTime; 33 private long maxLife; 34 35 38 public DirectoryCleaner(File directory, long maxLifeMillis) { 39 setInitialDir(directory); 40 maxLife = maxLifeMillis; 41 setRecursive(true); 42 setDaemon(true); 43 setName("DirectoryCleaner for \"" + Utils.getFilePath(directory) + '"'); 44 setPriority(Thread.MIN_PRIORITY); 45 } 46 47 protected boolean preProcess() { 48 currentTime = System.currentTimeMillis(); 49 return true; 50 } 51 52 protected void postProcessDirectory(File file, Path path) { 53 file.delete(); 56 } 57 58 protected void processFile(File file, Path path) { 59 String name = file.getName(); 60 61 int us = name.lastIndexOf('_'); 65 66 if (us >= 0) { 67 try { 68 if (currentTime - WebUtils.numericDateFormatter.parse(name.substring(us + 1, us + 15)).getTime() > maxLife) { 69 Utils.forceDelete(file); 70 } 71 72 return; 73 } catch (Exception ex) {} 74 } 75 76 if (currentTime - file.lastModified() > (long) (maxLife * (1.0 + Math.random() / 2.0))) { 78 Utils.forceDelete(file); 79 } 80 } 81 } 82 | Popular Tags |