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.util.Calendar;
20 import java.util.Date;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.kuali.ole.sys.OLEConstants;
24 import org.kuali.rice.core.api.config.property.ConfigurationService;
25
26
27
28
29
30 public class PurgeTempFilesStep extends AbstractStep {
31
32 private ConfigurationService kualiConfigurationService;
33
34
35
36
37
38
39 public boolean execute(String jobName, Date jobRunDate) throws InterruptedException {
40 Calendar calendar = getDateTimeService().getCurrentCalendar();
41 calendar.add(Calendar.DATE, -1);
42 String location = kualiConfigurationService.getPropertyValueAsString(OLEConstants.TEMP_DIRECTORY_KEY) + File.separator;
43 deleteTempBefore(location, calendar.getTimeInMillis());
44 return true;
45 }
46
47
48
49
50
51
52
53
54 private void deleteTempBefore(String location, long modificationTime) {
55 if (StringUtils.isBlank(location)) {
56 throw new RuntimeException("temp location is blank");
57 }
58 File tempDir = new File(location);
59 if (!tempDir.exists()) {
60 throw new RuntimeException("temp directory does not exist");
61 }
62 if (!tempDir.isDirectory()) {
63 throw new RuntimeException("temp directory is not a directory! " + tempDir.getAbsolutePath());
64 }
65 try {
66 File dir = new File(location);
67 String[] files = dir.list();
68 for (int i = 0; i < files.length; i++) {
69 String filename = files[i];
70 File f = new File(location + filename);
71 if(f.lastModified() < modificationTime) {
72 f.delete();
73 }
74 }
75 } catch (Exception e) {
76 throw new RuntimeException("Caught exception while trying to remove temp files at " + location, e);
77 }
78 }
79
80
81
82
83
84 public void setConfigurationService(ConfigurationService configurationService) {
85 this.kualiConfigurationService = configurationService;
86 }
87
88 }