001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2015-2016 ForgeRock AS. 015 */ 016package org.forgerock.audit.retention; 017 018import java.io.File; 019import java.util.Collections; 020import java.util.Comparator; 021import java.util.LinkedList; 022import java.util.List; 023 024import org.forgerock.audit.util.LastModifiedTimeFileComparator; 025 026/** 027 * A {@link RetentionPolicy} that will retain/delete log files based off the total disk space used. 028 */ 029public class DiskSpaceUsedRetentionPolicy implements RetentionPolicy { 030 private final long maxDiskSpaceToUse; 031 private final Comparator<File> comparator = new LastModifiedTimeFileComparator(); 032 033 /** 034 * Constructs a {@link DiskSpaceUsedRetentionPolicy} with a given maximum of disk space to use in bytes. 035 * @param maxDiskSpaceToUse The maximum amount of disk space the historical audit files can occupy. 036 */ 037 public DiskSpaceUsedRetentionPolicy(final long maxDiskSpaceToUse) { 038 this.maxDiskSpaceToUse = maxDiskSpaceToUse; 039 } 040 041 @Override 042 public List<File> deleteFiles(FileNamingPolicy fileNamingPolicy) { 043 final List<File> archivedFiles = fileNamingPolicy.listFiles(); 044 long currentDiskSpaceUsed = 0L; 045 for (final File file: archivedFiles) { 046 currentDiskSpaceUsed += file.length(); 047 } 048 049 if (currentDiskSpaceUsed <= maxDiskSpaceToUse) { 050 return Collections.emptyList(); 051 } 052 053 final long freeSpaceNeeded = currentDiskSpaceUsed - maxDiskSpaceToUse; 054 Collections.sort(archivedFiles, comparator); 055 056 long freedSpace = 0L; 057 List<File> filesToDelete = new LinkedList<>(); 058 for (File file : archivedFiles) { 059 filesToDelete.add(file); 060 freedSpace += file.length(); 061 if (freedSpace >= freeSpaceNeeded) { 062 break; 063 } 064 } 065 066 return filesToDelete; 067 } 068}