View Javadoc
1   /*
2    * Copyright 2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * A directory walker which finds files to purge; it's relatively simple, simply adding a file to
29   * the given results if the IOFileMatcher has matched it
30   */
31  public class FilePurgeDirectoryWalker extends DirectoryWalker {
32      private org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(this.getClass());
33      
34      /**
35       * Constructs a FilePurgeDirectoryWalker
36       */
37      public FilePurgeDirectoryWalker(IOFileFilter fileFilter) {
38          super(fileFilter, fileFilter, -1);
39      }
40  
41      /**
42       * @see org.kuali.ole.sys.batch.service.FilePurgeDirectoryWalker#getFilesToPurge(java.lang.String, java.util.List)
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       * @see org.apache.commons.io.DirectoryWalker#handleDirectory(java.io.File, int, java.util.Collection)
59       */
60      @Override
61      protected boolean handleDirectory(File directory, int depth, Collection results) throws IOException {
62          if (getLastSubDirectoryName(directory.getName()).startsWith(".")) return false; // don't follow hidden directories
63          return true;
64      }
65      
66      /**
67       * Finds the last subdirectory name of the given directory name and returns it
68       * @param directoryName a directory name with a sub directory
69       * @return the last subdirectory name
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; // no directory separator...so just return the whole thing
77      }
78  
79      /**
80       * @see org.apache.commons.io.DirectoryWalker#handleDirectoryEnd(java.io.File, int, java.util.Collection)
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       * @see org.apache.commons.io.DirectoryWalker#handleDirectoryStart(java.io.File, int, java.util.Collection)
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       * @see org.apache.commons.io.DirectoryWalker#handleEnd(java.util.Collection)
99       */
100     @Override
101     protected void handleEnd(Collection results) throws IOException {
102         LOG.debug("ending process");
103         super.handleEnd(results);
104     }
105 
106     /**
107      * @see org.apache.commons.io.DirectoryWalker#handleFile(java.io.File, int, java.util.Collection)
108      */
109     @Override
110     protected void handleFile(File file, int depth, Collection results) throws IOException {
111         results.add(file);
112     }
113 
114     /**
115      * @see org.apache.commons.io.DirectoryWalker#handleStart(java.io.File, java.util.Collection)
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 }