001: /*
002: * Copyright 2007 Hippo
003: *
004: * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS"
012: * BASIS, 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 nl.hippo.cms.background;
017:
018: import java.io.File;
019: import java.io.FilenameFilter;
020: import java.io.Serializable;
021: import java.util.Iterator;
022: import java.util.List;
023:
024: import org.apache.commons.io.FilenameUtils;
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: /**
029: * @author <a href="mailto:d.dam@hippo.nl">Dennis Dam</a>
030: * @author <a href="mailto:b.vanhalderen@hippo.nl">(Berry) A.W. van Halderen</a>
031: *
032: * @version $Id: TempFileCleanup.java 8209 2007-09-19 16:03:37Z ddam $
033: */
034: public class TempFileCleanup implements Runnable, Serializable {
035: private static final long serialVersionUID = -1L;
036:
037: private int cleanupAfterMs = -1; // clean up turned off
038:
039: private final Log logger = LogFactory.getLog(TempFileCleanup.class);
040:
041: private FilenameFilter tempFilenameFilter;
042:
043: public TempFileCleanup() {
044: }
045:
046: public void setTempFileWildcardPatterns(List patterns) {
047: tempFilenameFilter = new HippoCMSTempFileFilter(patterns);
048: }
049:
050: public void setCleanupAfterSecs(int cleanupAfterSecs) {
051: this .cleanupAfterMs = cleanupAfterSecs * 1000;
052: }
053:
054: public void run() {
055: String tmpdir = System.getProperty("java.io.tmpdir");
056: if (logger.isDebugEnabled()) {
057: logger.debug("Cleaning up temporary files from " + tmpdir);
058: }
059: if (tmpdir != null && tempFilenameFilter != null
060: && cleanupAfterMs > 0) {
061: File tempFolder = new File(tmpdir);
062: File[] checkFiles = tempFolder
063: .listFiles(tempFilenameFilter);
064: for (int i = 0; i < checkFiles.length; i++) {
065: File tempFile = checkFiles[i];
066: if (System.currentTimeMillis()
067: - tempFile.lastModified() > cleanupAfterMs) {
068: try {
069: if (logger.isDebugEnabled()) {
070: logger.debug("Deleting temporary file "
071: + tempFile.getAbsolutePath());
072: }
073: tempFile.delete();
074:
075: } catch (Exception e) {
076: logger.warn("Failed to delete temporary file "
077: + tempFile.getAbsolutePath(), e);
078: }
079: }
080: }
081: }
082: }
083:
084: class HippoCMSTempFileFilter implements FilenameFilter,
085: Serializable {
086: private static final long serialVersionUID = -1L;
087:
088: private List fileWildcardPatterns;
089:
090: public HippoCMSTempFileFilter(List fileWildcardPatterns) {
091: this .fileWildcardPatterns = fileWildcardPatterns;
092: }
093:
094: public boolean accept(File folder, String name) {
095: for (Iterator iter = fileWildcardPatterns.iterator(); iter
096: .hasNext();) {
097: String pattern = (String) iter.next();
098: if (FilenameUtils.wildcardMatch(name, pattern)) {
099: return true;
100: }
101: }
102: return false;
103: }
104: }
105:
106: }
|