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 java.util.Arrays; 029import java.util.Calendar; 030import java.util.List; 031 032import org.forgerock.i18n.LocalizableMessage; 033import org.forgerock.i18n.slf4j.LocalizedLogger; 034import org.opends.server.admin.server.ConfigurationChangeListener; 035import org.opends.server.admin.std.server.FixedTimeLogRotationPolicyCfg; 036import org.forgerock.opendj.config.server.ConfigChangeResult; 037import org.opends.server.util.TimeThread; 038 039/** 040 * This class implements a rotation policy based on fixed day/time of day. 041 */ 042public class FixedTimeRotationPolicy implements 043 RotationPolicy<FixedTimeLogRotationPolicyCfg>, 044 ConfigurationChangeListener<FixedTimeLogRotationPolicyCfg> 045{ 046 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 047 048 /** The scheduled rotation times as ms offsets from the beginning of the day. */ 049 private int[] rotationTimes; 050 051 /** {@inheritDoc} */ 052 public void initializeLogRotationPolicy(FixedTimeLogRotationPolicyCfg config) 053 { 054 rotationTimes = new int[config.getTimeOfDay().size()]; 055 056 int i = 0; 057 for(String time : config.getTimeOfDay()) 058 { 059 rotationTimes[i++] = Integer.valueOf(time); 060 } 061 062 Arrays.sort(rotationTimes); 063 064 config.addFixedTimeChangeListener(this); 065 } 066 067 /** {@inheritDoc} */ 068 public boolean isConfigurationChangeAcceptable( 069 FixedTimeLogRotationPolicyCfg config, List<LocalizableMessage> unacceptableReasons) 070 { 071 // Changes should always be OK 072 return true; 073 } 074 075 /** {@inheritDoc} */ 076 public ConfigChangeResult applyConfigurationChange( 077 FixedTimeLogRotationPolicyCfg config) 078 { 079 final ConfigChangeResult ccr = new ConfigChangeResult(); 080 081 rotationTimes = new int[config.getTimeOfDay().size()]; 082 083 int i = 0; 084 for(String time : config.getTimeOfDay()) 085 { 086 rotationTimes[i++] = Integer.valueOf(time); 087 } 088 089 Arrays.sort(rotationTimes); 090 091 return ccr; 092 } 093 094 /** {@inheritDoc} */ 095 public boolean rotateFile(RotatableLogFile writer) 096 { 097 Calendar lastRotationTime = writer.getLastRotationTime(); 098 099 Calendar nextRotationTime = (Calendar)lastRotationTime.clone(); 100 int i = 0; 101 nextRotationTime.set(Calendar.HOUR_OF_DAY, rotationTimes[i] / 100); 102 nextRotationTime.set(Calendar.MINUTE, rotationTimes[i] % 100); 103 nextRotationTime.set(Calendar.SECOND, 0); 104 while(lastRotationTime.after(nextRotationTime)) 105 { 106 if(i == rotationTimes.length - 1) 107 { 108 nextRotationTime.add(Calendar.DATE, 1); 109 i = 0; 110 } 111 else 112 { 113 i++; 114 } 115 116 nextRotationTime.set(Calendar.HOUR_OF_DAY, rotationTimes[i] / 100); 117 nextRotationTime.set(Calendar.MINUTE, rotationTimes[i] % 100); 118 } 119 120 logger.trace("The next fixed rotation time is %s", rotationTimes[i]); 121 122 return TimeThread.getCalendar().after(nextRotationTime); 123 } 124} 125