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