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    import org.opends.server.util.TimeThread;
031    import org.opends.server.admin.std.server.TimeLimitLogRotationPolicyCfg;
032    import org.opends.server.admin.server.ConfigurationChangeListener;
033    import org.opends.server.types.ConfigChangeResult;
034    import org.opends.server.types.ResultCode;
035    
036    
037    import java.util.List;
038    import java.util.ArrayList;
039    
040    /**
041     * This class implements a fixed time based rotation policy.
042     * Rotation will happen N seconds since the last rotation.
043     */
044    public class TimeLimitRotationPolicy implements
045        RotationPolicy<TimeLimitLogRotationPolicyCfg>,
046        ConfigurationChangeListener<TimeLimitLogRotationPolicyCfg>
047    {
048      private long timeInterval = 0;
049    
050      /**
051       * {@inheritDoc}
052       */
053      public void initializeLogRotationPolicy(TimeLimitLogRotationPolicyCfg config)
054      {
055        timeInterval = config.getRotationInterval();
056    
057        config.addTimeLimitChangeListener(this);
058      }
059    
060      /**
061       * {@inheritDoc}
062       */
063      public boolean isConfigurationChangeAcceptable(
064          TimeLimitLogRotationPolicyCfg config, List<Message> unacceptableReasons)
065      {
066        // Changes should always be OK
067        return true;
068      }
069    
070      /**
071       * {@inheritDoc}
072       */
073      public ConfigChangeResult applyConfigurationChange(
074          TimeLimitLogRotationPolicyCfg config)
075      {
076        // Default result code.
077        ResultCode resultCode = ResultCode.SUCCESS;
078        boolean adminActionRequired = false;
079        ArrayList<Message> messages = new ArrayList<Message>();
080    
081        timeInterval = config.getRotationInterval();
082    
083        return new ConfigChangeResult(resultCode, adminActionRequired, messages);
084      }
085    
086    
087      /**
088       * This method indicates if the log file should be
089       * rotated or not.
090       *
091       * @param writer The mutli file text writer written the log file.
092       * @return true if the file should be rotated, false otherwise.
093       */
094      public boolean rotateFile(MultifileTextWriter writer)
095      {
096        long currInterval = TimeThread.getTime() -
097            writer.getLastRotationTime().getTimeInMillis();
098    
099        return currInterval > timeInterval;
100      }
101    
102    }
103