org.apache.commons.io.filefilter.AgeFileFilter Java Examples

The following examples show how to use org.apache.commons.io.filefilter.AgeFileFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LocalDocumentService.java    From elexis-3-core with Eclipse Public License 1.0 6 votes vote down vote up
private void deleteBackupFilesOlderThen(File backupDir, int days){
	try {
		if (backupDir.isDirectory()) {
			Collection<File> filesToDelete = FileUtils.listFiles(backupDir,
				new AgeFileFilter(DateUtils.addDays(new Date(), days * -1)),
				TrueFileFilter.TRUE);
			for (File file : filesToDelete) {
				boolean success = FileUtils.deleteQuietly(file);
				if (!success) {
					LoggerFactory.getLogger(getClass())
						.warn("Cannot delete old backup file at: " + file.getAbsolutePath());
				}
			}
		}
	} catch (Exception e) {
		LoggerFactory.getLogger(getClass()).warn("Cannot delete old backup files.", e);
	}
}
 
Example #2
Source File: MessageQueueHelper.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void unlockLockedFilesOnQueue() {
final PropertyHandler propertyHandler = PropertyHandler.getInstance();
    if (propertyHandler.hasProperty("MESSAGE_QUEUE_FOLDER")) {
        String messageQueueFolderPath = propertyHandler.getProperty("MESSAGE_QUEUE_FOLDER");
        File messageQueueFolder = new File(messageQueueFolderPath);
        if (messageQueueFolder.exists()) {
            String lockedFileSuffix = "_LOCK";
            SuffixFileFilter suffixFileFilter = new SuffixFileFilter(lockedFileSuffix);

            Integer numberOfMinutes = propertyHandler.getIntegerProperty("locked.file.retention", "2");

            AgeFileFilter ageFileFilter = new AgeFileFilter(System.currentTimeMillis() - (numberOfMinutes * 60 * 1000));

            Collection<File> lockedFiles = FileUtils.listFiles(messageQueueFolder, FileFilterUtils.and(suffixFileFilter, ageFileFilter), TrueFileFilter.INSTANCE);
            for (File file : lockedFiles) {
                String lockedFileName = file.getAbsolutePath();
                File unlockedFile = new File(StringUtils.remove(lockedFileName, lockedFileSuffix));
                file.setLastModified(new Date().getTime());
                Boolean succesFullyUnlocked = file.renameTo(unlockedFile);
                if (succesFullyUnlocked) {
                    LOG.info("File: " + lockedFileName + " successfully unlocked.");
                }
            }
        } else {
            LOG.info("No directory found on location: " + messageQueueFolderPath + ". No files unlocked");
        }
    } else {
        LOG.info("No MESSAGE_QUEUE_FOLDER property in properties file. No files unlocked.");
    }
}
 
Example #3
Source File: MessageQueueHelper.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void unlockLockedFilesOnQueue() {
   PropertyHandler propertyHandler = PropertyHandler.getInstance();
   if (propertyHandler.hasProperty("MESSAGE_QUEUE_FOLDER")) {
      String messageQueueFolderPath = propertyHandler.getProperty("MESSAGE_QUEUE_FOLDER");
      File messageQueueFolder = new File(messageQueueFolderPath);
      if (messageQueueFolder.exists()) {
         String lockedFileSuffix = "_LOCK";
         SuffixFileFilter suffixFileFilter = new SuffixFileFilter(lockedFileSuffix);
         Integer numberOfMinutes = propertyHandler.getIntegerProperty("locked.file.retention", "2");
         AgeFileFilter ageFileFilter = new AgeFileFilter(System.currentTimeMillis() - (long)(numberOfMinutes.intValue() * 60 * 1000));
         Collection<File> lockedFiles = FileUtils.listFiles(messageQueueFolder, FileFilterUtils.and(suffixFileFilter, ageFileFilter), TrueFileFilter.INSTANCE);
         Iterator var9 = lockedFiles.iterator();

         while(var9.hasNext()) {
            File file = (File)var9.next();
            String lockedFileName = file.getAbsolutePath();
            File unlockedFile = new File(StringUtils.remove(lockedFileName, lockedFileSuffix));
            file.setLastModified((new Date()).getTime());
            Boolean succesFullyUnlocked = file.renameTo(unlockedFile);
            if (succesFullyUnlocked.booleanValue()) {
               LOG.info("File: " + lockedFileName + " successfully unlocked.");
            }
         }
      } else {
         LOG.info("No directory found on location: " + messageQueueFolderPath + ". No files unlocked");
      }
   } else {
      LOG.info("No MESSAGE_QUEUE_FOLDER property in properties file. No files unlocked.");
   }

}
 
Example #4
Source File: MessageQueueHelper.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void unlockLockedFilesOnQueue() {
final PropertyHandler propertyHandler = PropertyHandler.getInstance();
    if (propertyHandler.hasProperty("MESSAGE_QUEUE_FOLDER")) {
        String messageQueueFolderPath = propertyHandler.getProperty("MESSAGE_QUEUE_FOLDER");
        File messageQueueFolder = new File(messageQueueFolderPath);
        if (messageQueueFolder.exists()) {
            String lockedFileSuffix = "_LOCK";
            SuffixFileFilter suffixFileFilter = new SuffixFileFilter(lockedFileSuffix);

            Integer numberOfMinutes = propertyHandler.getIntegerProperty("locked.file.retention", "2");

            AgeFileFilter ageFileFilter = new AgeFileFilter(System.currentTimeMillis() - (numberOfMinutes * 60 * 1000));

            Collection<File> lockedFiles = FileUtils.listFiles(messageQueueFolder, FileFilterUtils.and(suffixFileFilter, ageFileFilter), TrueFileFilter.INSTANCE);
            for (File file : lockedFiles) {
                String lockedFileName = file.getAbsolutePath();
                File unlockedFile = new File(StringUtils.remove(lockedFileName, lockedFileSuffix));
                file.setLastModified(new Date().getTime());
                Boolean succesFullyUnlocked = file.renameTo(unlockedFile);
                if (succesFullyUnlocked) {
                    LOG.info("File: " + lockedFileName + " successfully unlocked.");
                }
            }
        } else {
            LOG.info("No directory found on location: " + messageQueueFolderPath + ". No files unlocked");
        }
    } else {
        LOG.info("No MESSAGE_QUEUE_FOLDER property in properties file. No files unlocked.");
    }
}
 
Example #5
Source File: MessageQueueHelper.java    From freehealth-connector with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void unlockLockedFilesOnQueue() {
final PropertyHandler propertyHandler = PropertyHandler.getInstance();
    if (propertyHandler.hasProperty("MESSAGE_QUEUE_FOLDER")) {
        String messageQueueFolderPath = propertyHandler.getProperty("MESSAGE_QUEUE_FOLDER");
        File messageQueueFolder = new File(messageQueueFolderPath);
        if (messageQueueFolder.exists()) {
            String lockedFileSuffix = "_LOCK";
            SuffixFileFilter suffixFileFilter = new SuffixFileFilter(lockedFileSuffix);

            Integer numberOfMinutes = propertyHandler.getIntegerProperty("locked.file.retention", "2");

            AgeFileFilter ageFileFilter = new AgeFileFilter(System.currentTimeMillis() - (numberOfMinutes * 60 * 1000));

            Collection<File> lockedFiles = FileUtils.listFiles(messageQueueFolder, FileFilterUtils.and(suffixFileFilter, ageFileFilter), TrueFileFilter.INSTANCE);
            for (File file : lockedFiles) {
                String lockedFileName = file.getAbsolutePath();
                File unlockedFile = new File(StringUtils.remove(lockedFileName, lockedFileSuffix));
                file.setLastModified(new Date().getTime());
                Boolean succesFullyUnlocked = file.renameTo(unlockedFile);
                if (succesFullyUnlocked) {
                    LOG.info("File: " + lockedFileName + " successfully unlocked.");
                }
            }
        } else {
            LOG.info("No directory found on location: " + messageQueueFolderPath + ". No files unlocked");
        }
    } else {
        LOG.info("No MESSAGE_QUEUE_FOLDER property in properties file. No files unlocked.");
    }
}
 
Example #6
Source File: TaskLogCleanup.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
void cleanup() {
  String taskLogsHome = getTaskLogHome();

  if (taskLogsHome == null) {
    // we are forgiving if the task logs home is not defined. Just log a message with a call to action.
    log.warn("Unable to cleanup task log files. Please check that the 'tasklogfile' appender exists in logback.xml");
    return;
  }

  File logFilesHome = new File(taskLogsHome);

  log.info("Cleaning up log files in {} older than {} days", logFilesHome.getAbsolutePath(), numberOfDays);

  LocalDate now = LocalDate.now().minusDays(numberOfDays);
  Date thresholdDate = Date.from(now.atStartOfDay(ZoneId.systemDefault()).toInstant());
  AgeFileFilter ageFileFilter = new AgeFileFilter(thresholdDate);
  Iterator<File> filesToDelete = iterateFiles(logFilesHome, ageFileFilter, ageFileFilter);
  filesToDelete.forEachRemaining(f -> {
        try {
          forceDelete(f);
          log.info("Removed task log file {}", f.toString());
        }
        catch (IOException e) { // NOSONAR
          log.error("Unable to delete task file {}. Message was {}.", f.toString(), e.getMessage());
        }
      }
  );
}
 
Example #7
Source File: Files.java    From chipster with MIT License 2 votes vote down vote up
/**
 * Walks the baseDir recursively and deletes files and directories older than cutoff. 
 * When traversing directories, does not follow symbolic links.
 * 
 * If a directory is old but contains files (which are not too old), it is not deleted.
 * 
 * TODO better problem handling?
 * 
 * @param baseDir
 * @param cutoff milliseconds 
 * @throws IOException 
 */
public static void cleanOldFiles(File baseDir, long cutoff ) throws IOException {
	walkAndDelete(baseDir, new AgeFileFilter(System.currentTimeMillis() - cutoff));
}