001 /*
002 * Copyright 2011 The Kuali Foundation.
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.kuali.kfs.sys.batch;
017
018 import java.io.File;
019 import java.io.IOException;
020 import java.util.ArrayList;
021 import java.util.Calendar;
022 import java.util.Collection;
023 import java.util.List;
024
025 import org.apache.commons.io.DirectoryWalker;
026 import org.apache.commons.io.filefilter.IOFileFilter;
027 import org.kuali.kfs.sys.batch.service.FilePurgeService;
028 import org.kuali.kfs.sys.context.SpringContext;
029 import org.kuali.rice.kns.service.DateTimeService;
030
031 /**
032 * A directory walker which finds files to purge; it's relatively simple, simply adding a file to
033 * the given results if the IOFileMatcher has matched it
034 */
035 public class FilePurgeDirectoryWalker extends DirectoryWalker {
036 private org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(this.getClass());
037
038 /**
039 * Constructs a FilePurgeDirectoryWalker
040 */
041 public FilePurgeDirectoryWalker(IOFileFilter fileFilter) {
042 super(fileFilter, fileFilter, -1);
043 }
044
045 /**
046 * @see org.kuali.kfs.sys.batch.service.FilePurgeDirectoryWalker#getFilesToPurge(java.lang.String, java.util.List)
047 */
048 public List<File> getFilesToPurge(String directory) {
049 List<File> results = new ArrayList<File>();
050
051 try {
052 walk(new File(directory), results);
053 }
054 catch (IOException ioe) {
055 throw new RuntimeException("Could not walk directory "+directory, ioe);
056 }
057
058 return results;
059 }
060
061 /**
062 * @see org.apache.commons.io.DirectoryWalker#handleDirectory(java.io.File, int, java.util.Collection)
063 */
064 @Override
065 protected boolean handleDirectory(File directory, int depth, Collection results) throws IOException {
066 if (getLastSubDirectoryName(directory.getName()).startsWith(".")) return false; // don't follow hidden directories
067 return true;
068 }
069
070 /**
071 * Finds the last subdirectory name of the given directory name and returns it
072 * @param directoryName a directory name with a sub directory
073 * @return the last subdirectory name
074 */
075 protected String getLastSubDirectoryName(String directoryName) {
076 final int lastIndex = directoryName.lastIndexOf(File.separator);
077 if (lastIndex > -1) {
078 return directoryName.substring(lastIndex+1);
079 }
080 return directoryName; // no directory separator...so just return the whole thing
081 }
082
083 /**
084 * @see org.apache.commons.io.DirectoryWalker#handleDirectoryEnd(java.io.File, int, java.util.Collection)
085 */
086 @Override
087 protected void handleDirectoryEnd(File directory, int depth, Collection results) throws IOException {
088 LOG.debug("Leaving directory "+directory.getName());
089 super.handleDirectoryEnd(directory, depth, results);
090 }
091
092 /**
093 * @see org.apache.commons.io.DirectoryWalker#handleDirectoryStart(java.io.File, int, java.util.Collection)
094 */
095 @Override
096 protected void handleDirectoryStart(File directory, int depth, Collection results) throws IOException {
097 LOG.debug("Entering directory "+directory.getName());
098 super.handleDirectoryStart(directory, depth, results);
099 }
100
101 /**
102 * @see org.apache.commons.io.DirectoryWalker#handleEnd(java.util.Collection)
103 */
104 @Override
105 protected void handleEnd(Collection results) throws IOException {
106 LOG.debug("ending process");
107 super.handleEnd(results);
108 }
109
110 /**
111 * @see org.apache.commons.io.DirectoryWalker#handleFile(java.io.File, int, java.util.Collection)
112 */
113 @Override
114 protected void handleFile(File file, int depth, Collection results) throws IOException {
115 results.add(file);
116 }
117
118 /**
119 * @see org.apache.commons.io.DirectoryWalker#handleStart(java.io.File, java.util.Collection)
120 */
121 @Override
122 protected void handleStart(File startDirectory, Collection results) throws IOException {
123 LOG.debug("starting process");
124 super.handleStart(startDirectory, results);
125 }
126 }