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
020import org.opends.server.util.TimeThread;
021import org.forgerock.opendj.server.config.server.TimeLimitLogRotationPolicyCfg;
022import org.forgerock.opendj.config.server.ConfigurationChangeListener;
023import org.forgerock.opendj.config.server.ConfigChangeResult;
024import java.util.List;
025
026/**
027 * This class implements a fixed time based rotation policy.
028 * Rotation will happen N seconds since the last rotation.
029 */
030public class TimeLimitRotationPolicy implements
031    RotationPolicy<TimeLimitLogRotationPolicyCfg>,
032    ConfigurationChangeListener<TimeLimitLogRotationPolicyCfg>
033{
034  private long timeInterval;
035
036  @Override
037  public void initializeLogRotationPolicy(TimeLimitLogRotationPolicyCfg config)
038  {
039    timeInterval = config.getRotationInterval();
040
041    config.addTimeLimitChangeListener(this);
042  }
043
044  @Override
045  public boolean isConfigurationChangeAcceptable(
046      TimeLimitLogRotationPolicyCfg config, List<LocalizableMessage> unacceptableReasons)
047  {
048    // Changes should always be OK
049    return true;
050  }
051
052  @Override
053  public ConfigChangeResult applyConfigurationChange(
054      TimeLimitLogRotationPolicyCfg config)
055  {
056    timeInterval = config.getRotationInterval();
057    return new ConfigChangeResult();
058  }
059
060
061  /**
062   * This method indicates if the log file should be
063   * rotated or not.
064   *
065   * @param writer The multi file text writer written the log file.
066   * @return true if the file should be rotated, false otherwise.
067   */
068  @Override
069  public boolean rotateFile(RotatableLogFile writer)
070  {
071    long currInterval = TimeThread.getTime() -
072        writer.getLastRotationTime().getTimeInMillis();
073
074    return currInterval > timeInterval;
075  }
076
077}