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 org.forgerock.i18n.LocalizableMessage;
019
020
021import org.forgerock.opendj.server.config.server.SizeLimitLogRotationPolicyCfg;
022import org.forgerock.opendj.config.server.ConfigurationChangeListener;
023import org.opends.server.types.InitializationException;
024import org.forgerock.opendj.config.server.ConfigChangeResult;
025import org.forgerock.opendj.config.server.ConfigException;
026
027import java.util.List;
028
029/** This class implements a rotation policy based on the size of the file. */
030public class SizeBasedRotationPolicy implements
031    RotationPolicy<SizeLimitLogRotationPolicyCfg>,
032    ConfigurationChangeListener<SizeLimitLogRotationPolicyCfg>
033{
034  private long sizeLimit;
035
036  SizeLimitLogRotationPolicyCfg currentConfig;
037
038  @Override
039  public void initializeLogRotationPolicy(SizeLimitLogRotationPolicyCfg config)
040      throws ConfigException, InitializationException
041  {
042    sizeLimit = config.getFileSizeLimit();
043
044    config.addSizeLimitChangeListener(this);
045    currentConfig = config;
046  }
047
048  @Override
049  public boolean isConfigurationChangeAcceptable(
050      SizeLimitLogRotationPolicyCfg config, List<LocalizableMessage> unacceptableReasons)
051  {
052    // Changes should always be OK
053    return true;
054  }
055
056  @Override
057  public ConfigChangeResult applyConfigurationChange(
058      SizeLimitLogRotationPolicyCfg config)
059  {
060    sizeLimit = config.getFileSizeLimit();
061    currentConfig = config;
062
063    return new ConfigChangeResult();
064  }
065
066  /**
067   * This method indicates if the log file should be
068   * rotated or not.
069   *
070   * @param writer The multi file text writer writing the log file.
071   * @return true if the file needs to be rotated, false otherwise.
072  */
073  @Override
074  public boolean rotateFile(RotatableLogFile writer)
075  {
076    long fileSize = writer.getBytesWritten();
077    return fileSize >= sizeLimit;
078  }
079
080}