1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.ole.sys.batch;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.List;
23
24 import org.apache.commons.io.DirectoryWalker;
25 import org.apache.commons.io.filefilter.IOFileFilter;
26
27
28
29
30
31 public class FilePurgeDirectoryWalker extends DirectoryWalker {
32 private org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(this.getClass());
33
34
35
36
37 public FilePurgeDirectoryWalker(IOFileFilter fileFilter) {
38 super(fileFilter, fileFilter, -1);
39 }
40
41
42
43
44 public List<File> getFilesToPurge(String directory) {
45 List<File> results = new ArrayList<File>();
46
47 try {
48 walk(new File(directory), results);
49 }
50 catch (IOException ioe) {
51 throw new RuntimeException("Could not walk directory "+directory, ioe);
52 }
53
54 return results;
55 }
56
57
58
59
60 @Override
61 protected boolean handleDirectory(File directory, int depth, Collection results) throws IOException {
62 if (getLastSubDirectoryName(directory.getName()).startsWith(".")) return false;
63 return true;
64 }
65
66
67
68
69
70
71 protected String getLastSubDirectoryName(String directoryName) {
72 final int lastIndex = directoryName.lastIndexOf(File.separator);
73 if (lastIndex > -1) {
74 return directoryName.substring(lastIndex+1);
75 }
76 return directoryName;
77 }
78
79
80
81
82 @Override
83 protected void handleDirectoryEnd(File directory, int depth, Collection results) throws IOException {
84 LOG.debug("Leaving directory "+directory.getName());
85 super.handleDirectoryEnd(directory, depth, results);
86 }
87
88
89
90
91 @Override
92 protected void handleDirectoryStart(File directory, int depth, Collection results) throws IOException {
93 LOG.debug("Entering directory "+directory.getName());
94 super.handleDirectoryStart(directory, depth, results);
95 }
96
97
98
99
100 @Override
101 protected void handleEnd(Collection results) throws IOException {
102 LOG.debug("ending process");
103 super.handleEnd(results);
104 }
105
106
107
108
109 @Override
110 protected void handleFile(File file, int depth, Collection results) throws IOException {
111 results.add(file);
112 }
113
114
115
116
117 @Override
118 protected void handleStart(File startDirectory, Collection results) throws IOException {
119 LOG.debug("starting process");
120 super.handleStart(startDirectory, results);
121 }
122 }