/*
 * Copyright 2015 Adaptris Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

package com.adaptris.core.lms;

import java.io.File;
import java.io.FileFilter;
import java.util.Calendar;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.adaptris.core.fs.CompositeFileFilter;

/**
 * @author lchan
 * @author $Author: lchan $
 */
public class FilePurge {
  private FileFilter fileFilter;
  private Calendar nextRun = null;
  private transient Log logR = LogFactory.getLog(this.getClass());

  private static final FilePurge purger = new FilePurge();
  private static final String PURGE_FILTER = "Glob=" + FileBackedMessageFactory.TMP_FILE_PREFIX + "*"
      + FileBackedMessageFactory.TMP_FILE_SUFFIX + "__@@__" + "OlderThan=-PT30S";

  private FilePurge() {
    fileFilter = new CompositeFileFilter(PURGE_FILTER, true);
    nextRun = Calendar.getInstance();
  }

  public static FilePurge getInstance() {
    return purger;
  }

  public void purge() {
    String oldName = Thread.currentThread().getName();
    try {
      Thread.currentThread().setName("FilePurge");
      File dir = new File(System.getProperty("java.io.tmpdir"));
      File[] files = dir.listFiles(fileFilter);
      for (File file : files) {
        try {
          if (file.exists()) {
            logR.trace("Deleting " + file.getCanonicalPath());
            file.delete();
          }
        }
        catch (Exception e) {
          ;
        }
      }
    }
    catch (Exception ignoredIntentionally) {
      ;
    }
    Thread.currentThread().setName(oldName);
  }

}

@@ -690,24 +690,45 @@
 va.util.
-Calendar
+Arrays;%0Aimport java.util.List
 ;%0A%0Aimpor
@@ -748,27 +748,28 @@
 commons.
-logging.Log
+io.FileUtils
 ;%0Aimport
@@ -792,26 +792,37 @@
 ons.
-logging.LogFactory
+io.filefilter.RegexFileFilter
 ;%0A%0Ai
@@ -852,241 +852,76 @@
 .fs.
-CompositeFileFilter;%0A%0A/**%0A * @author lchan%0A * @author $Author: lchan $%0A */%0Apublic class FilePurge %7B%0A  private FileFilter fileFilter;%0A  private Calendar nextRun = null;%0A  private transient Log logR = LogFactory.getLog(this.getClass())
+OlderThan;%0A%0Apublic class FilePurge %7B%0A  private FileFilter fileFilter
 ;%0A%0A 
@@ -1024,17 +1024,18 @@
 LTER
- = %22Glob=
+_NAME = %22%5E
 %22 + 
@@ -1082,84 +1082,63 @@
  + %22
+.
 *%22
+;
 %0A  
-    + FileBackedMessageFactory.TMP_FILE_SUFFIX + %22__@@__%22 + %22OlderThan=
+private static final String PURGE_FILTER_AGE = %22
 -PT3
@@ -1192,17 +1192,17 @@
 new 
-Composite
+Temporary
 File
@@ -1212,64 +1212,8 @@
 ter(
-PURGE_FILTER, true);%0A    nextRun = Calendar.getInstance(
 );%0A 
@@ -1314,119 +1314,13 @@
     
-String oldName = Thread.currentThread().getName();%0A    try %7B%0A      Thread.currentThread().setName(%22FilePurge%22);
+try %7B
 %0A   
@@ -1473,330 +1473,523 @@
     
-try %7B%0A          if (file.exists()) %7B
+FileUtils.deleteQuietly(file);
 %0A      
+%7D%0A
     
-  logR.trace(%22Deleting %22 + file.getCanonicalPath());
+%7D%0A    catch (Exception ignoredIntentionally) %7B
 %0A      
+;%0A
     
-  file.delete();%0A          %7D%0A        %7D%0A        catch (Exception e) %7B%0A          ;%0A        %7D%0A      %7D%0A    %7D%0A    catch (Exception ignoredIntentionally) %7B%0A      ;%0A    %7D%0A    Thread.currentThread().setName(oldName);%0A  %7D%0A
+%7D%0A  %7D%0A%0A  private class TemporaryFileFilter implements FileFilter %7B%0A    private List%3CFileFilter%3E filters = Arrays.asList(new RegexFileFilter(PURGE_FILTER_NAME), new OlderThan(PURGE_FILTER_AGE));%0A%0A    @Override%0A    public boolean accept(File pathname) %7B%0A      int result = 0;%0A      for (FileFilter f : filters) %7B%0A        result += f.accept(pathname) ? 1 : 0;%0A      %7D%0A      return result == filters.size();%0A    %7D%0A%0A  %7D
 %0A%7D%0A

clean up purging