Merge remote-tracking branch 'origin/jetty-9.3.x' into jetty-9.4.x

This commit is contained in:
Greg Wilkins 2017-04-28 11:40:26 +02:00
commit 89bb7db58c
2 changed files with 27 additions and 50 deletions

View File

@ -24,9 +24,7 @@ import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
@ -55,7 +53,6 @@ public class RolloverFileOutputStream extends FilterOutputStream
final static int ROLLOVER_FILE_RETAIN_DAYS = 31;
private RollTask _rollTask;
private ZonedDateTime _midnight;
private SimpleDateFormat _fileBackupFormat;
private SimpleDateFormat _fileDateFormat;
@ -120,7 +117,6 @@ public class RolloverFileOutputStream extends FilterOutputStream
TimeZone zone)
throws IOException
{
this(filename,append,retainDays,zone,null,null);
}
@ -168,7 +164,6 @@ public class RolloverFileOutputStream extends FilterOutputStream
_filename=filename;
_append=append;
_retainDays=retainDays;
setFile();
synchronized(RolloverFileOutputStream.class)
{
@ -176,8 +171,8 @@ public class RolloverFileOutputStream extends FilterOutputStream
__rollover=new Timer(RolloverFileOutputStream.class.getName(),true);
// Calculate Today's Midnight, based on Configured TimeZone (will be in past, even if by a few milliseconds)
ZonedDateTime now = ZonedDateTime.now(zone.toZoneId());
_midnight = toMidnight(now, zone.toZoneId());
ZonedDateTime now = ZonedDateTime.now(zone.toZoneId());
setFile(now);
// This will schedule the rollover event to the next midnight
scheduleNextRollover(now);
}
@ -187,28 +182,12 @@ public class RolloverFileOutputStream extends FilterOutputStream
/**
* Get the "start of day" for the provided DateTime at the zone specified.
*
* @param dateTime the date time to calculate from
* @param zone the zone to return the date in
* @param now the date time to calculate from
* @return start of the day of the date provided
*/
public static ZonedDateTime toMidnight(ZonedDateTime dateTime, ZoneId zone)
public static ZonedDateTime toMidnight(ZonedDateTime now)
{
return dateTime.toLocalDate().atStartOfDay(zone);
}
/* ------------------------------------------------------------ */
/**
* Get the next "start of day" for the provided date.
*
* @param dateTime the date to calculate from
* @return the start of the next day
*/
public static ZonedDateTime nextMidnight(ZonedDateTime dateTime)
{
// Increment to next day, based on Configured TimeZone, then find start of that day.
// Will always be in the future, even if the Daylights Savings Time kicks in during
// the calculation.
return dateTime.toLocalDate().plus(1, ChronoUnit.DAYS).atStartOfDay(dateTime.getZone());
return now.toLocalDate().atStartOfDay(now.getZone());
}
/* ------------------------------------------------------------ */
@ -216,11 +195,10 @@ public class RolloverFileOutputStream extends FilterOutputStream
{
_rollTask = new RollTask();
// Get tomorrow's midnight based on Configured TimeZone
while (_midnight.isBefore(now))
_midnight = nextMidnight(_midnight);
ZonedDateTime midnight = toMidnight(now);
long delay = _midnight.toInstant().toEpochMilli() - now.toInstant().toEpochMilli();
// Schedule next rollover event to occur, based on local machine's Unix Epoch milliseconds
long delay = midnight.toInstant().toEpochMilli() - now.toInstant().toEpochMilli();
__rollover.schedule(_rollTask,delay);
}
@ -245,7 +223,7 @@ public class RolloverFileOutputStream extends FilterOutputStream
}
/* ------------------------------------------------------------ */
private synchronized void setFile()
private synchronized void setFile(ZonedDateTime now)
throws IOException
{
// Check directory
@ -256,8 +234,6 @@ public class RolloverFileOutputStream extends FilterOutputStream
if (!dir.isDirectory() || !dir.canWrite())
throw new IOException("Cannot write log directory "+dir);
Date now=new Date();
// Is this a rollover file?
String filename=file.getName();
int i=filename.toLowerCase(Locale.ENGLISH).indexOf(YYYY_MM_DD);
@ -265,7 +241,7 @@ public class RolloverFileOutputStream extends FilterOutputStream
{
file=new File(dir,
filename.substring(0,i)+
_fileDateFormat.format(now)+
_fileDateFormat.format(new Date(now.toInstant().toEpochMilli()))+
filename.substring(i+YYYY_MM_DD.length()));
}
@ -323,19 +299,19 @@ public class RolloverFileOutputStream extends FilterOutputStream
/* ------------------------------------------------------------ */
@Override
public void write (byte[] buf)
throws IOException
{
out.write (buf);
}
throws IOException
{
out.write (buf);
}
/* ------------------------------------------------------------ */
@Override
public void write (byte[] buf, int off, int len)
throws IOException
{
out.write (buf, off, len);
}
throws IOException
{
out.write (buf, off, len);
}
/* ------------------------------------------------------------ */
@Override
public void close()
@ -367,8 +343,8 @@ public class RolloverFileOutputStream extends FilterOutputStream
{
synchronized(RolloverFileOutputStream.class)
{
ZonedDateTime now = ZonedDateTime.now(_midnight.getZone());
RolloverFileOutputStream.this.setFile();
ZonedDateTime now = ZonedDateTime.now(_fileDateFormat.getTimeZone().toZoneId());
RolloverFileOutputStream.this.setFile(now);
RolloverFileOutputStream.this.scheduleNextRollover(now);
RolloverFileOutputStream.this.removeOldFiles(now);
}

View File

@ -24,6 +24,7 @@ import static org.junit.Assert.assertThat;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAccessor;
import java.util.TimeZone;
@ -58,7 +59,7 @@ public class RolloverFileOutputStreamTest
for (int i = 0; i < expected.length; i++)
{
long currentMillis = nextEvent.toInstant().toEpochMilli();
nextEvent = RolloverFileOutputStream.nextMidnight(nextEvent);
nextEvent = nextEvent.toLocalDate().plus(1, ChronoUnit.DAYS).atStartOfDay(nextEvent.getZone());
assertThat("Next Event", toString(nextEvent), is(expected[i][0]));
long duration = (nextEvent.toInstant().toEpochMilli() - currentMillis);
assertThat("Duration to next event", duration, is((long) expected[i][1]));
@ -74,7 +75,7 @@ public class RolloverFileOutputStreamTest
ZoneId zone = toZoneId("PST");
ZonedDateTime initialDate = toDateTime("2017.04.27-08:00:00.0 PM PDT", zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate, zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
assertThat("Midnight", toString(midnight), is("2017.04.27-12:00:00.0 AM PDT"));
Object expected[][] = {
@ -94,7 +95,7 @@ public class RolloverFileOutputStreamTest
ZoneId zone = toZoneId("PST");
ZonedDateTime initialDate = toDateTime("2016.03.11-01:23:45.0 PM PST", zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate, zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
assertThat("Midnight", toString(midnight), is("2016.03.11-12:00:00.0 AM PST"));
Object expected[][] = {
@ -114,7 +115,7 @@ public class RolloverFileOutputStreamTest
ZoneId zone = toZoneId("PST");
ZonedDateTime initialDate = toDateTime("2016.11.04-11:22:33.0 AM PDT", zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate, zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
assertThat("Midnight", toString(midnight), is("2016.11.04-12:00:00.0 AM PDT"));
Object expected[][] = {
@ -134,7 +135,7 @@ public class RolloverFileOutputStreamTest
ZoneId zone = toZoneId("Australia/Sydney");
ZonedDateTime initialDate = toDateTime("2016.10.01-01:23:45.0 PM AEST", zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate, zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
assertThat("Midnight", toString(midnight), is("2016.10.01-12:00:00.0 AM AEST"));
Object expected[][] = {
@ -154,7 +155,7 @@ public class RolloverFileOutputStreamTest
ZoneId zone = toZoneId("Australia/Sydney");
ZonedDateTime initialDate = toDateTime("2016.04.02-11:22:33.0 AM AEDT", zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate, zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
assertThat("Midnight", toString(midnight), is("2016.04.02-12:00:00.0 AM AEDT"));
Object expected[][] = {