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    
030    
031    import org.opends.server.admin.std.server.SizeLimitLogRotationPolicyCfg;
032    import org.opends.server.admin.server.ConfigurationChangeListener;
033    import org.opends.server.types.InitializationException;
034    import org.opends.server.types.ConfigChangeResult;
035    import org.opends.server.types.ResultCode;
036    import org.opends.server.config.ConfigException;
037    
038    
039    import java.util.List;
040    import java.util.ArrayList;
041    
042    /**
043     * This class implements a rotation policy based on the size of the
044     * file.
045     */
046    public class SizeBasedRotationPolicy implements
047        RotationPolicy<SizeLimitLogRotationPolicyCfg>,
048        ConfigurationChangeListener<SizeLimitLogRotationPolicyCfg>
049    {
050      private long sizeLimit;
051    
052      SizeLimitLogRotationPolicyCfg currentConfig;
053    
054      /**
055       * {@inheritDoc}
056       */
057      public void initializeLogRotationPolicy(SizeLimitLogRotationPolicyCfg config)
058          throws ConfigException, InitializationException
059      {
060        sizeLimit = config.getFileSizeLimit();
061    
062        config.addSizeLimitChangeListener(this);
063        currentConfig = config;
064      }
065    
066      /**
067       * {@inheritDoc}
068       */
069      public boolean isConfigurationChangeAcceptable(
070          SizeLimitLogRotationPolicyCfg config, List<Message> unacceptableReasons)
071      {
072        // Changes should always be OK
073        return true;
074      }
075    
076      /**
077       * {@inheritDoc}
078       */
079      public ConfigChangeResult applyConfigurationChange(
080          SizeLimitLogRotationPolicyCfg config)
081      {
082        // Default result code.
083        ResultCode resultCode = ResultCode.SUCCESS;
084        boolean adminActionRequired = false;
085        ArrayList<Message> messages = new ArrayList<Message>();
086    
087        sizeLimit = config.getFileSizeLimit();
088    
089        currentConfig = config;
090    
091        return new ConfigChangeResult(resultCode, adminActionRequired, messages);
092      }
093    
094      /**
095       * This method indicates if the log file should be
096       * rotated or not.
097       *
098       * @param writer The multi file text writer writing the log file.
099       * @return true if the file needs to be rotated, false otherwise.
100      */
101      public boolean rotateFile(MultifileTextWriter writer)
102      {
103        long fileSize = writer.getBytesWritten();
104    
105        return fileSize >= sizeLimit;
106      }
107    
108    }
109