001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.loggers;
028    import org.opends.messages.Message;
029    import static org.opends.messages.LoggerMessages.ERR_LOGGER_ERROR_LISTING_FILES;
030    
031    import org.opends.server.admin.std.server.FileCountLogRetentionPolicyCfg;
032    import org.opends.server.admin.server.ConfigurationChangeListener;
033    
034    import java.io.File;
035    import java.util.Arrays;
036    import java.util.List;
037    import java.util.ArrayList;
038    
039    import static org.opends.server.loggers.debug.DebugLogger.*;
040    import org.opends.server.loggers.debug.DebugTracer;
041    import org.opends.server.types.ConfigChangeResult;
042    import org.opends.server.types.ResultCode;
043    import org.opends.server.types.DirectoryException;
044    import org.opends.server.core.DirectoryServer;
045    
046    
047    /**
048     * This class implements a retention policy based on the number of files.
049     * Files will be cleaned up based on the number of files on disk.
050     */
051    public class FileNumberRetentionPolicy implements
052        RetentionPolicy<FileCountLogRetentionPolicyCfg>,
053        ConfigurationChangeListener<FileCountLogRetentionPolicyCfg>
054    {
055      /**
056       * The tracer object for the debug logger.
057       */
058      private static final DebugTracer TRACER = getTracer();
059    
060      private int numFiles = 0;
061      private FileCountLogRetentionPolicyCfg config;
062    
063      /**
064       * {@inheritDoc}
065       */
066      public void initializeLogRetentionPolicy(
067          FileCountLogRetentionPolicyCfg config)
068      {
069        this.numFiles = config.getNumberOfFiles();
070        this.config = config;
071    
072        config.addFileCountChangeListener(this);
073      }
074    
075      /**
076       * {@inheritDoc}
077       */
078      public boolean isConfigurationChangeAcceptable(
079          FileCountLogRetentionPolicyCfg config,
080          List<Message> unacceptableReasons)
081      {
082        // Changes should always be OK
083        return true;
084      }
085    
086      /**
087       * {@inheritDoc}
088       */
089      public ConfigChangeResult applyConfigurationChange(
090          FileCountLogRetentionPolicyCfg config)
091      {
092        // Default result code.
093        ResultCode resultCode = ResultCode.SUCCESS;
094        boolean adminActionRequired = false;
095        ArrayList<Message> messages = new ArrayList<Message>();
096    
097        this.numFiles = config.getNumberOfFiles();
098        this.config = config;
099    
100        return new ConfigChangeResult(resultCode, adminActionRequired, messages);
101      }
102    
103      /**
104       * {@inheritDoc}
105       */
106      public File[] deleteFiles(FileNamingPolicy fileNamingPolicy)
107          throws DirectoryException
108      {
109        File[] files = fileNamingPolicy.listFiles();
110        if(files == null)
111        {
112          Message message =
113              ERR_LOGGER_ERROR_LISTING_FILES.get(
114                  fileNamingPolicy.getInitialName().toString());
115          throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
116                                       message);
117        }
118    
119        ArrayList<File> filesToDelete = new ArrayList<File>();
120    
121        if (files.length <= numFiles)
122        {
123          return new File[0];
124        }
125    
126        // Sort files based on last modified time.
127        Arrays.sort(files, new FileComparator());
128    
129        for (int j = numFiles; j < files.length; j++)
130        {
131          filesToDelete.add(files[j]);
132        }
133    
134        return filesToDelete.toArray(new File[0]);
135      }
136    
137      /**
138       * {@inheritDoc}
139       */
140      public String toString()
141      {
142        return "Free Number Retention Policy " + config.dn().toString();
143      }
144    }
145