01: /*
02: * Copyright 2007 The Kuali Foundation.
03: *
04: * Licensed under the Educational Community License, Version 1.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.opensource.org/licenses/ecl1.php
09: *
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.kfs.batch;
17:
18: import java.io.File;
19: import java.util.Calendar;
20:
21: import org.apache.commons.lang.StringUtils;
22: import org.kuali.core.service.KualiConfigurationService;
23: import org.kuali.kfs.KFSConstants;
24: import org.kuali.kfs.context.SpringContext;
25:
26: /**
27: *
28: * Purges old files from the temp directory specified in build.properties
29: */
30: public class PurgeTempFilesStep extends AbstractStep {
31:
32: private KualiConfigurationService kualiConfigurationService;
33:
34: /**
35: * Deletes all files in the temp directory that are over 1 day old
36: *
37: * @see org.kuali.kfs.batch.Step#execute(java.lang.String)
38: */
39: public boolean execute(String jobName) throws InterruptedException {
40: Calendar calendar = getDateTimeService().getCurrentCalendar();
41: calendar.add(Calendar.DATE, -1);
42: String location = kualiConfigurationService
43: .getPropertyString(KFSConstants.TEMP_DIRECTORY_KEY)
44: + "/";
45: deleteTempBefore(location, calendar.getTimeInMillis());
46: return true;
47: }
48:
49: /**
50: *
51: * delete files in the specified directory that are older than the modification time
52: *
53: * @param location the path to temp files
54: * @param modificationTime delete if file is older than this
55: */
56: private void deleteTempBefore(String location, long modificationTime) {
57: if (StringUtils.isBlank(location)) {
58: throw new RuntimeException("temp location is blank");
59: }
60: File tempDir = new File(location);
61: if (!tempDir.exists()) {
62: throw new RuntimeException("temp directory does not exist");
63: }
64: if (!tempDir.isDirectory()) {
65: throw new RuntimeException(
66: "temp directory is not a directory! "
67: + tempDir.getAbsolutePath());
68: }
69: try {
70: File dir = new File(location);
71: String[] files = dir.list();
72: for (int i = 0; i < files.length; i++) {
73: String filename = files[i];
74: File f = new File(location + filename);
75: if (f.lastModified() < modificationTime) {
76: f.delete();
77: }
78: }
79: } catch (Exception e) {
80: throw new RuntimeException(
81: "Caught exception while trying to remove temp files at "
82: + location, e);
83: }
84: }
85:
86: /**
87: * Sets the configurationService attribute value.
88: * @param configurationService The configurationService to set.
89: */
90: public void setKualiConfigurationService(
91: KualiConfigurationService configurationService) {
92: this.kualiConfigurationService = configurationService;
93: }
94:
95: }
|