View Javadoc
1   /*
2    * Copyright 2009 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.service.impl;
17  
18  import java.io.File;
19  import java.util.List;
20  
21  import org.apache.commons.io.filefilter.AndFileFilter;
22  import org.apache.commons.io.filefilter.IOFileFilter;
23  import org.apache.commons.io.filefilter.OrFileFilter;
24  import org.kuali.ole.sys.FileUtil;
25  import org.kuali.ole.sys.batch.FilePurgeCustomAge;
26  import org.kuali.ole.sys.batch.FilePurgeDirectoryWalker;
27  import org.kuali.ole.sys.batch.FilePurgeStep;
28  import org.kuali.ole.sys.batch.MaxAgePurgeFileFilter;
29  import org.kuali.ole.sys.batch.NotAmongDirectoriesFileFilter;
30  import org.kuali.ole.sys.batch.service.FilePurgeService;
31  import org.kuali.rice.coreservice.framework.parameter.ParameterService;
32  
33  /**
34   * Default implementation of the FilePurgeService
35   */
36  public class FilePurgeServiceImpl implements FilePurgeService {
37      protected org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(FilePurgeServiceImpl.class);
38      private ParameterService parameterService;
39      
40      protected static final String DAYS_BEFORE_PURGE_PARAMETER_SUFFIX = "_NUMBER_OF_DAYS_OLD";
41      protected static final String DAYS_BEFORE_PURGE_PARAMETER_PREFIX = "DEFAULT";
42      
43      /**
44       * Uses a FilePurgeDirectoryWalker to get a List of Files to purge, then purges each
45       * @see org.kuali.ole.gl.batch.service.FilePurgeService#purgeFiles(java.lang.String, java.util.List)
46       */
47      public void purgeFiles(String directory, List<FilePurgeCustomAge> customAges) {
48          //add a step to check for directory existence
49          FileUtil.createDirectory(directory);
50          
51          purgeCustomAgeFiles(directory, customAges);
52          purgeDefaultFiles(directory, customAges);
53      }
54      
55      /**
56       * Purges any files in the given directory associated with custom ages
57       * @param directory the directory to purge files from
58       * @param customAges custom ages to purge files for
59       */
60      protected void purgeCustomAgeFiles(String directory, List<FilePurgeCustomAge> customAges) {
61          // purge custom age directories
62          if (customAges != null && customAges.size() > 0) {
63              FilePurgeDirectoryWalker directoryWalker = getCustomAgesDirectoryWalker(customAges);
64              List<File> filesToPurge = directoryWalker.getFilesToPurge(directory);
65              for (File fileToPurge : filesToPurge) {
66                  LOG.info("Purging file "+fileToPurge.getPath());
67                  fileToPurge.delete();
68              }
69          }
70      }
71      
72      /**
73       * Purges any files in the given directory not associated with custom ages
74       * @param directory the directory to purge files from
75       * @param customAges the custom ages with directories to avoid
76       */
77      protected void purgeDefaultFiles(String directory, List<FilePurgeCustomAge> customAges) {
78          // purge standard directories
79          FilePurgeDirectoryWalker directoryWalker = getDefaultDirectoryWalker(customAges);
80          List<File> filesToPurge = directoryWalker.getFilesToPurge(directory);
81          for (File fileToPurge : filesToPurge) {
82              LOG.info("Purging file "+fileToPurge.getPath());
83              fileToPurge.delete();
84          }
85      }
86      
87      /**
88       * Gets a directory walker which will 
89       * @param customAges the custom ages to purge files for
90       * @return a new FilePurgeDirectoryWalker which will walk directories for us
91       */
92      protected FilePurgeDirectoryWalker getCustomAgesDirectoryWalker(List<FilePurgeCustomAge> customAges) {
93          OrFileFilter fileFilter = new OrFileFilter();
94          for (FilePurgeCustomAge customAge : customAges) {
95              fileFilter.addFileFilter(customAge.getFileFilter());
96          }
97          return new FilePurgeDirectoryWalker(fileFilter);
98      }
99      
100     /**
101      * Gets the directory walker for the default directories
102      * @param customAges the custom ages, because custom age directories will not be purged
103      * @return a new FilePurgeDirectoryWalker
104      */
105     protected FilePurgeDirectoryWalker getDefaultDirectoryWalker(List<FilePurgeCustomAge> customAges) {
106         IOFileFilter ageFileFilter = buildDefaultAgeFileFilter();
107         if (customAges != null && customAges.size() > 0) {
108             AndFileFilter andFileFilter = new AndFileFilter();
109             andFileFilter.addFileFilter(ageFileFilter);
110             andFileFilter.addFileFilter(buildAnyDirectoryButCustomAgeDirectoryFileFilter(customAges));
111             return new FilePurgeDirectoryWalker(andFileFilter);
112         } else {
113             return new FilePurgeDirectoryWalker(ageFileFilter);
114         }
115     }
116     
117     /**
118      * Builds a file filter which will skip the directories taken by the CustomAges
119      * @param customAges the customAges to avoid
120      * @return a file filter
121      */
122     protected IOFileFilter buildAnyDirectoryButCustomAgeDirectoryFileFilter(List<FilePurgeCustomAge> customAges) {
123         NotAmongDirectoriesFileFilter skipDirectoriesFileFilter = new NotAmongDirectoriesFileFilter(customAges);
124         return skipDirectoriesFileFilter;
125     }
126 
127     /**
128      * @see org.kuali.ole.gl.batch.service.FilePurgeService#getAgeInDaysForCustomAge(org.kuali.ole.sys.batch.FilePurgeCustomAge)
129      */
130     public int getDaysBeforePurgeForCustomAge(FilePurgeCustomAge customAge) {
131         final String parameterName = customAge.getParameterPrefix()+getDaysBeforePurgeSuffix();
132         return retrieveDaysBeforePurgeParameterValue(parameterName);
133     }
134     
135     /**
136      * @return the standard suffix of parameter names from a custom age
137      */
138     protected String getDaysBeforePurgeSuffix() {
139         return FilePurgeServiceImpl.DAYS_BEFORE_PURGE_PARAMETER_SUFFIX;
140     }
141 
142     /**
143      * @see org.kuali.ole.gl.batch.service.FilePurgeService#getStandardDaysBeforePurge()
144      */
145     public int getStandardDaysBeforePurge() {
146         final String parameterName = getStandardDaysBeforePurgeParameterName();
147         return retrieveDaysBeforePurgeParameterValue(parameterName);
148     }
149     
150     /**
151      * @return the parameter name to find the default days before purging files
152      */
153     protected String getStandardDaysBeforePurgeParameterName() {
154         return FilePurgeServiceImpl.DAYS_BEFORE_PURGE_PARAMETER_PREFIX+FilePurgeServiceImpl.DAYS_BEFORE_PURGE_PARAMETER_SUFFIX;
155     }
156     
157     /**
158      * Retrieves the parameter value of the OLE-SYS / FilePurgeStep / parameterName parameter and converts it to an integer number of days
159      * @param parameterName the name of the parameter to retrieve the value of
160      * @return the integer number of days
161      */
162     protected int retrieveDaysBeforePurgeParameterValue(String parameterName) {
163         final String parameterValue = getParameterService().getParameterValueAsString(FilePurgeStep.class, parameterName);
164         Integer parameterValueAsInteger = null;
165         parameterValueAsInteger = new Integer(parameterValue);
166         return (parameterValueAsInteger == null ? Integer.MAX_VALUE : parameterValueAsInteger.intValue());
167     }
168 
169     /**
170      * Builds an age file filter for the default removal run
171      * @return a properly constructed IOFileFilter
172      */
173     protected IOFileFilter buildDefaultAgeFileFilter() {
174         return new MaxAgePurgeFileFilter();
175     }
176 
177     /**
178      * Gets the parameterService attribute. 
179      * @return Returns the parameterService.
180      */
181     public ParameterService getParameterService() {
182         return parameterService;
183     }
184 
185     /**
186      * Sets the parameterService attribute value.
187      * @param parameterService The parameterService to set.
188      */
189     public void setParameterService(ParameterService parameterService) {
190         this.parameterService = parameterService;
191     }
192 }