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.util.Calendar;
020 import java.util.Date;
021
022 import org.apache.commons.lang.StringUtils;
023 import org.kuali.kfs.sys.KFSConstants;
024 import org.kuali.rice.kns.service.KualiConfigurationService;
025
026 /**
027 *
028 * Purges old files from the temp directory specified in build.properties
029 */
030 public class PurgeTempFilesStep extends AbstractStep {
031
032 private KualiConfigurationService kualiConfigurationService;
033
034 /**
035 * Deletes all files in the temp directory that are over 1 day old
036 *
037 * @see org.kuali.kfs.sys.batch.Step#execute(String, Date)
038 */
039 public boolean execute(String jobName, Date jobRunDate) throws InterruptedException {
040 Calendar calendar = getDateTimeService().getCurrentCalendar();
041 calendar.add(Calendar.DATE, -1);
042 String location = kualiConfigurationService.getPropertyString(KFSConstants.TEMP_DIRECTORY_KEY) + File.separator;
043 deleteTempBefore(location, calendar.getTimeInMillis());
044 return true;
045 }
046
047 /**
048 *
049 * delete files in the specified directory that are older than the modification time
050 *
051 * @param location the path to temp files
052 * @param modificationTime delete if file is older than this
053 */
054 private void deleteTempBefore(String location, long modificationTime) {
055 if (StringUtils.isBlank(location)) {
056 throw new RuntimeException("temp location is blank");
057 }
058 File tempDir = new File(location);
059 if (!tempDir.exists()) {
060 throw new RuntimeException("temp directory does not exist");
061 }
062 if (!tempDir.isDirectory()) {
063 throw new RuntimeException("temp directory is not a directory! " + tempDir.getAbsolutePath());
064 }
065 try {
066 File dir = new File(location);
067 String[] files = dir.list();
068 for (int i = 0; i < files.length; i++) {
069 String filename = files[i];
070 File f = new File(location + filename);
071 if(f.lastModified() < modificationTime) {
072 f.delete();
073 }
074 }
075 } catch (Exception e) {
076 throw new RuntimeException("Caught exception while trying to remove temp files at " + location, e);
077 }
078 }
079
080 /**
081 * Sets the configurationService attribute value.
082 * @param configurationService The configurationService to set.
083 */
084 public void setKualiConfigurationService(KualiConfigurationService configurationService) {
085 this.kualiConfigurationService = configurationService;
086 }
087
088 }