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:07:00 +02:00
commit fe4928b8ea
1 changed files with 25 additions and 28 deletions

View File

@ -55,7 +55,7 @@ public class RolloverFileOutputStream extends FilterOutputStream
final static int ROLLOVER_FILE_RETAIN_DAYS = 31; final static int ROLLOVER_FILE_RETAIN_DAYS = 31;
private RollTask _rollTask; private RollTask _rollTask;
private ZonedDateTime midnight; private ZonedDateTime _midnight;
private SimpleDateFormat _fileBackupFormat; private SimpleDateFormat _fileBackupFormat;
private SimpleDateFormat _fileDateFormat; private SimpleDateFormat _fileDateFormat;
@ -176,13 +176,14 @@ public class RolloverFileOutputStream extends FilterOutputStream
__rollover=new Timer(RolloverFileOutputStream.class.getName(),true); __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) // Calculate Today's Midnight, based on Configured TimeZone (will be in past, even if by a few milliseconds)
midnight = toMidnight(ZonedDateTime.now(zone.toZoneId()), zone.toZoneId()); ZonedDateTime now = ZonedDateTime.now(zone.toZoneId());
_midnight = toMidnight(now, zone.toZoneId());
// This will schedule the rollover event to the next midnight // This will schedule the rollover event to the next midnight
scheduleNextRollover(); scheduleNextRollover(now);
} }
} }
/* ------------------------------------------------------------ */
/** /**
* Get the "start of day" for the provided DateTime at the zone specified. * Get the "start of day" for the provided DateTime at the zone specified.
* *
@ -194,7 +195,8 @@ public class RolloverFileOutputStream extends FilterOutputStream
{ {
return dateTime.toLocalDate().atStartOfDay(zone); return dateTime.toLocalDate().atStartOfDay(zone);
} }
/* ------------------------------------------------------------ */
/** /**
* Get the next "start of day" for the provided date. * Get the next "start of day" for the provided date.
* *
@ -208,26 +210,18 @@ public class RolloverFileOutputStream extends FilterOutputStream
// the calculation. // the calculation.
return dateTime.toLocalDate().plus(1, ChronoUnit.DAYS).atStartOfDay(dateTime.getZone()); return dateTime.toLocalDate().plus(1, ChronoUnit.DAYS).atStartOfDay(dateTime.getZone());
} }
private void scheduleNextRollover() /* ------------------------------------------------------------ */
private void scheduleNextRollover(ZonedDateTime now)
{ {
_rollTask = new RollTask(); _rollTask = new RollTask();
// Get tomorrow's midnight based on Configured TimeZone // Get tomorrow's midnight based on Configured TimeZone
midnight = nextMidnight(midnight); while (_midnight.isBefore(now))
_midnight = nextMidnight(_midnight);
long now = System.currentTimeMillis();
long delay = midnight.toInstant().toEpochMilli() - System.currentTimeMillis(); long delay = _midnight.toInstant().toEpochMilli() - now.toInstant().toEpochMilli();
try // Schedule next rollover event to occur, based on local machine's Unix Epoch milliseconds
{ __rollover.schedule(_rollTask,delay);
// Schedule next rollover event to occur, based on local machine's Unix Epoch milliseconds
__rollover.schedule(_rollTask, delay);
}
catch (IllegalArgumentException e)
{
System.err.printf("[ERROR] midnight=%s, midnight-epoch=%d, now=%d, delay=%d - ",
midnight, midnight.toInstant().toEpochMilli(), now, delay);
e.printStackTrace(System.err);
}
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
@ -294,12 +288,11 @@ public class RolloverFileOutputStream extends FilterOutputStream
} }
/* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */
private void removeOldFiles() private void removeOldFiles(ZonedDateTime now)
{ {
if (_retainDays>0) if (_retainDays>0)
{ {
// Establish expiration time, based on configured TimeZone // Establish expiration time, based on configured TimeZone
ZonedDateTime now = ZonedDateTime.now(this.midnight.getZone());
long expired = now.toInstant().toEpochMilli(); long expired = now.toInstant().toEpochMilli();
File file= new File(_filename); File file= new File(_filename);
@ -372,9 +365,13 @@ public class RolloverFileOutputStream extends FilterOutputStream
{ {
try try
{ {
RolloverFileOutputStream.this.setFile(); synchronized(RolloverFileOutputStream.class)
RolloverFileOutputStream.this.scheduleNextRollover(); {
RolloverFileOutputStream.this.removeOldFiles(); ZonedDateTime now = ZonedDateTime.now(_midnight.getZone());
RolloverFileOutputStream.this.setFile();
RolloverFileOutputStream.this.scheduleNextRollover(now);
RolloverFileOutputStream.this.removeOldFiles(now);
}
} }
catch(Throwable t) catch(Throwable t)
{ {