View Javadoc
1   /*
2    * Copyright 2007-2008 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.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   * Purges old files from the temp directory specified in build.properties
29   */
30  public class PurgeTempFilesStep extends AbstractStep {
31      
32      private ConfigurationService kualiConfigurationService;
33      
34      /**
35       * Deletes all files in the temp directory that are over 1 day old
36       * 
37       * @see org.kuali.ole.sys.batch.Step#execute(String, Date)
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       * delete files in the specified directory that are older than the modification time
50       * 
51       * @param location the path to temp files
52       * @param modificationTime delete if file is older than this
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       * Sets the configurationService attribute value.
82       * @param configurationService The configurationService to set.
83       */
84      public void setConfigurationService(ConfigurationService configurationService) {
85          this.kualiConfigurationService = configurationService;
86      }
87      
88  }