1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.mortbay.util.daemon;
16
17 import java.io.File;
18 import java.io.FilenameFilter;
19 import java.util.Timer;
20 import java.util.TimerTask;
21
22 public class Sweeper implements Runnable, FilenameFilter
23 {
24
25 public static final long DEFAULT_INTERVAL = 3600000;
26
27 public static boolean LOG_TO_CONSOLE = true;
28
29 public Sweeper()
30 {
31
32 }
33
34 public Sweeper(long interval)
35 {
36 if(interval==0)
37 run();
38 else
39 schedule(new Timer(), interval);
40 }
41
42 public Sweeper(Timer timer, long interval)
43 {
44 schedule(timer, interval);
45 }
46
47 private void schedule(Timer timer, long interval)
48 {
49 if (LOG_TO_CONSOLE)
50 System.err.println("Sweeping "+System.getProperty("java.io.tmpdir")+" every "+interval+"ms");
51 timer.scheduleAtFixedRate(new TimerTask()
52 {
53 public void run()
54 {
55 Sweeper.this.run();
56 }
57
58 }, 1000, interval);
59 }
60
61 public void run()
62 {
63 File tmpDir = new File(System.getProperty("java.io.tmpdir"));
64 File[] files = tmpDir.listFiles(this);
65 for(int i=0; i<files.length; i++)
66 {
67 File dir = files[i];
68 File sentinel = new File(dir, ".active");
69 if(!sentinel.exists() && delete(dir) && LOG_TO_CONSOLE)
70 System.err.println("removed dir: " + dir);
71 }
72 }
73
74 public static boolean delete(File file)
75 {
76 if (file.isDirectory())
77 {
78 File[] files = file.listFiles();
79 for (int i=0; i<files.length; i++)
80 delete(files[i]);
81 }
82 return file.delete();
83 }
84
85 public boolean accept(File file, String name)
86 {
87 return name.startsWith("Jetty_") && file.isDirectory();
88 }
89
90 public static void main(String[] args)
91 {
92 new Sweeper(args.length!=0 ? Long.parseLong(args[0]) :
93 Long.getLong("interval", DEFAULT_INTERVAL).longValue());
94 }
95
96 }