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 ForgeRock AS. 015 */ 016package org.forgerock.audit.retention; 017 018import java.io.File; 019import java.util.Collections; 020import java.util.LinkedList; 021import java.util.List; 022 023/** 024 * Created a size based file retention policy. This policy stores a set number of archived files. 025 */ 026public class SizeBasedRetentionPolicy implements RetentionPolicy { 027 private final int maxNumberOfFiles; 028 029 /** 030 * Constructs a SizeBasedRetentionPolicy with a given maximum number of archived files. A negative value 031 * disables pruning of old archive files. 032 * @param maxNumberOfFiles The maximum number of archive files to keep. A negative value will disable 033 * pruning old archive files. 034 */ 035 public SizeBasedRetentionPolicy(final int maxNumberOfFiles) { 036 this.maxNumberOfFiles = maxNumberOfFiles; 037 } 038 039 @Override 040 public List<File> deleteFiles(FileNamingPolicy fileNamingPolicy) { 041 final List<File> managedArchivedFiles = fileNamingPolicy.listFiles(); 042 final int numberOfManagedArchiveFiles = managedArchivedFiles.size(); 043 if (maxNumberOfFiles <= 0 || numberOfManagedArchiveFiles <= maxNumberOfFiles) { 044 return Collections.emptyList(); 045 } else { 046 final List<File> filesToDelete = 047 new LinkedList<>(managedArchivedFiles.subList(0, numberOfManagedArchiveFiles - maxNumberOfFiles)); 048 return filesToDelete; 049 } 050 } 051}