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 2006-2008 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2016 ForgeRock AS. 016 */ 017package org.opends.server.loggers; 018import static org.opends.messages.LoggerMessages.*; 019 020import java.io.File; 021import java.util.ArrayList; 022import java.util.Arrays; 023import java.util.List; 024 025import org.forgerock.i18n.LocalizableMessage; 026import org.forgerock.opendj.config.server.ConfigurationChangeListener; 027import org.forgerock.opendj.server.config.server.FileCountLogRetentionPolicyCfg; 028import org.opends.server.core.DirectoryServer; 029import org.forgerock.opendj.config.server.ConfigChangeResult; 030import org.opends.server.types.DirectoryException; 031 032 033/** 034 * This class implements a retention policy based on the number of files. 035 * Files will be cleaned up based on the number of files on disk. 036 */ 037public class FileNumberRetentionPolicy implements 038 RetentionPolicy<FileCountLogRetentionPolicyCfg>, 039 ConfigurationChangeListener<FileCountLogRetentionPolicyCfg> 040{ 041 042 private int numFiles; 043 private FileCountLogRetentionPolicyCfg config; 044 045 @Override 046 public void initializeLogRetentionPolicy( 047 FileCountLogRetentionPolicyCfg config) 048 { 049 this.numFiles = config.getNumberOfFiles(); 050 this.config = config; 051 052 config.addFileCountChangeListener(this); 053 } 054 055 @Override 056 public boolean isConfigurationChangeAcceptable( 057 FileCountLogRetentionPolicyCfg config, 058 List<LocalizableMessage> unacceptableReasons) 059 { 060 // Changes should always be OK 061 return true; 062 } 063 064 @Override 065 public ConfigChangeResult applyConfigurationChange( 066 FileCountLogRetentionPolicyCfg config) 067 { 068 this.numFiles = config.getNumberOfFiles(); 069 this.config = config; 070 return new ConfigChangeResult(); 071 } 072 073 @Override 074 public File[] deleteFiles(FileNamingPolicy fileNamingPolicy) 075 throws DirectoryException 076 { 077 File[] files = fileNamingPolicy.listFiles(); 078 if(files == null) 079 { 080 throw new DirectoryException(DirectoryServer.getServerErrorResultCode(), 081 ERR_LOGGER_ERROR_LISTING_FILES.get(fileNamingPolicy.getInitialName())); 082 } 083 084 if (files.length <= numFiles) 085 { 086 return new File[0]; 087 } 088 089 // Sort files based on last modified time. 090 Arrays.sort(files, new FileComparator()); 091 092 ArrayList<File> filesToDelete = new ArrayList<>(); 093 for (int j = numFiles; j < files.length; j++) 094 { 095 filesToDelete.add(files[j]); 096 } 097 return filesToDelete.toArray(new File[0]); 098 } 099 100 @Override 101 public String toString() 102 { 103 return "Free Number Retention Policy " + config.dn(); 104 } 105} 106