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 legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.loggers;
028import org.forgerock.i18n.LocalizableMessage;
029
030import org.opends.server.util.TimeThread;
031import org.opends.server.admin.std.server.TimeLimitLogRotationPolicyCfg;
032import org.opends.server.admin.server.ConfigurationChangeListener;
033import org.forgerock.opendj.config.server.ConfigChangeResult;
034import java.util.List;
035
036/**
037 * This class implements a fixed time based rotation policy.
038 * Rotation will happen N seconds since the last rotation.
039 */
040public class TimeLimitRotationPolicy implements
041    RotationPolicy<TimeLimitLogRotationPolicyCfg>,
042    ConfigurationChangeListener<TimeLimitLogRotationPolicyCfg>
043{
044  private long timeInterval;
045
046  /** {@inheritDoc} */
047  public void initializeLogRotationPolicy(TimeLimitLogRotationPolicyCfg config)
048  {
049    timeInterval = config.getRotationInterval();
050
051    config.addTimeLimitChangeListener(this);
052  }
053
054  /** {@inheritDoc} */
055  public boolean isConfigurationChangeAcceptable(
056      TimeLimitLogRotationPolicyCfg config, List<LocalizableMessage> unacceptableReasons)
057  {
058    // Changes should always be OK
059    return true;
060  }
061
062  /** {@inheritDoc} */
063  public ConfigChangeResult applyConfigurationChange(
064      TimeLimitLogRotationPolicyCfg config)
065  {
066    timeInterval = config.getRotationInterval();
067    return new ConfigChangeResult();
068  }
069
070
071  /**
072   * This method indicates if the log file should be
073   * rotated or not.
074   *
075   * @param writer The multi file text writer written the log file.
076   * @return true if the file should be rotated, false otherwise.
077   */
078  public boolean rotateFile(RotatableLogFile writer)
079  {
080    long currInterval = TimeThread.getTime() -
081        writer.getLastRotationTime().getTimeInMillis();
082
083    return currInterval > timeInterval;
084  }
085
086}