Issue #1590 - Revert Java 8 class usage back to Java 7 limits

This commit is contained in:
Joakim Erdfelt 2017-06-02 11:33:42 -07:00
parent c8cd6629d9
commit 806dff64f3
2 changed files with 114 additions and 94 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.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.Timer;
@ -57,7 +55,8 @@ public class RolloverFileOutputStream extends FilterOutputStream
private SimpleDateFormat _fileBackupFormat;
private SimpleDateFormat _fileDateFormat;
private String _filename;
private final TimeZone _timeZone;
private final String _filename;
private File _file;
private boolean _append;
private int _retainDays;
@ -118,7 +117,7 @@ public class RolloverFileOutputStream extends FilterOutputStream
TimeZone zone)
throws IOException
{
this(filename,append,retainDays,zone,null,null,ZonedDateTime.now(zone.toZoneId()));
this(filename,append,retainDays,zone,null,null);
}
/* ------------------------------------------------------------ */
@ -140,21 +139,22 @@ public class RolloverFileOutputStream extends FilterOutputStream
String backupFormat)
throws IOException
{
this(filename,append,retainDays,zone,dateFormat,backupFormat,ZonedDateTime.now(zone.toZoneId()));
this(filename,append,retainDays,zone,dateFormat,backupFormat,Calendar.getInstance(zone));
}
RolloverFileOutputStream(String filename,
boolean append,
int retainDays,
TimeZone zone,
String dateFormat,
String backupFormat,
ZonedDateTime now)
Calendar now)
throws IOException
{
super(null);
_timeZone = zone;
if (dateFormat==null)
dateFormat=ROLLOVER_FILE_DATE_FORMAT;
_fileDateFormat = new SimpleDateFormat(dateFormat);
@ -175,7 +175,8 @@ public class RolloverFileOutputStream extends FilterOutputStream
if (filename==null)
throw new IllegalArgumentException("Invalid filename");
_filename=filename;
File testfile = new File(filename);
_filename=testfile.getCanonicalPath();
_append=append;
_retainDays=retainDays;
@ -195,23 +196,32 @@ public class RolloverFileOutputStream extends FilterOutputStream
/**
* Get the "start of day" for the provided DateTime at the zone specified.
*
* @param now the date time to calculate from
* @param cal the date time to calculate from
* @return start of the day of the date provided
*/
public static ZonedDateTime toMidnight(ZonedDateTime now)
public static Calendar toMidnight(Calendar cal)
{
return now.toLocalDate().atStartOfDay(now.getZone()).plus(1, ChronoUnit.DAYS);
Calendar ret = Calendar.getInstance();
ret.setTimeZone(cal.getTimeZone());
ret.setTime(cal.getTime());
ret.set(Calendar.HOUR_OF_DAY, 0);
ret.set(Calendar.MINUTE, 0);
ret.set(Calendar.SECOND, 0);
ret.set(Calendar.MILLISECOND, 0);
// next days midnight
ret.add(Calendar.DAY_OF_MONTH, 1);
return ret;
}
/* ------------------------------------------------------------ */
private void scheduleNextRollover(ZonedDateTime now)
private void scheduleNextRollover(Calendar now)
{
_rollTask = new RollTask();
// Get tomorrow's midnight based on Configured TimeZone
ZonedDateTime midnight = toMidnight(now);
// Establish next day's midnight of provided calendar
Calendar midnight = toMidnight(now);
// Schedule next rollover event to occur, based on local machine's Unix Epoch milliseconds
long delay = midnight.toInstant().toEpochMilli() - now.toInstant().toEpochMilli();
long delay = midnight.getTimeInMillis() - now.getTimeInMillis();
__rollover.schedule(_rollTask,delay);
}
@ -236,13 +246,11 @@ public class RolloverFileOutputStream extends FilterOutputStream
}
/* ------------------------------------------------------------ */
synchronized void setFile(ZonedDateTime now)
synchronized void setFile(Calendar now)
throws IOException
{
// Check directory
File file = new File(_filename);
_filename=file.getCanonicalPath();
file=new File(_filename);
File file=new File(_filename);
File dir= new File(file.getParent());
if (!dir.isDirectory() || !dir.canWrite())
throw new IOException("Cannot write log directory "+dir);
@ -254,7 +262,7 @@ public class RolloverFileOutputStream extends FilterOutputStream
{
file=new File(dir,
filename.substring(0,i)+
_fileDateFormat.format(new Date(now.toInstant().toEpochMilli()))+
_fileDateFormat.format(now.getTime()) +
filename.substring(i+YYYY_MM_DD.length()));
}
@ -267,7 +275,7 @@ public class RolloverFileOutputStream extends FilterOutputStream
// Yep
_file=file;
if (!_append && file.exists())
file.renameTo(new File(file.toString()+"."+_fileBackupFormat.format(new Date(now.toInstant().toEpochMilli()))));
file.renameTo(new File(file.toString()+"."+_fileBackupFormat.format(now.getTime())));
OutputStream oldOut=out;
out=new FileOutputStream(file.toString(),_append);
if (oldOut!=null)
@ -277,12 +285,13 @@ public class RolloverFileOutputStream extends FilterOutputStream
}
/* ------------------------------------------------------------ */
void removeOldFiles(ZonedDateTime now)
void removeOldFiles(Calendar now)
{
if (_retainDays>0)
{
// Establish expiration time, based on configured TimeZone
long expired = now.minus(_retainDays, ChronoUnit.DAYS).toInstant().toEpochMilli();
now.add(Calendar.DAY_OF_MONTH, (-1)*_retainDays);
long expired = now.getTimeInMillis();
File file= new File(_filename);
File dir = new File(file.getParent());
@ -356,7 +365,7 @@ public class RolloverFileOutputStream extends FilterOutputStream
{
synchronized(RolloverFileOutputStream.class)
{
ZonedDateTime now = ZonedDateTime.now(_fileDateFormat.getTimeZone().toZoneId());
Calendar now = Calendar.getInstance(_timeZone);
RolloverFileOutputStream.this.setFile(now);
RolloverFileOutputStream.this.scheduleNextRollover(now);
RolloverFileOutputStream.this.removeOldFiles(now);

View File

@ -26,64 +26,70 @@ import java.io.FileReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileTime;
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.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.resource.ResourceTest;
import org.eclipse.jetty.toolchain.test.TestingDir;
import org.hamcrest.Matchers;
import org.junit.Rule;
import org.junit.Test;
public class RolloverFileOutputStreamTest
{
private static ZoneId toZoneId(String timezoneId)
@Rule
public TestingDir testingDir = new TestingDir();
private static TimeZone toZoneId(String timezoneId)
{
ZoneId zone = TimeZone.getTimeZone(timezoneId).toZoneId();
// System.out.printf(".toZoneId(\"%s\") = [id=%s,normalized=%s]%n", timezoneId, zone.getId(), zone.normalized());
TimeZone zone = TimeZone.getTimeZone(timezoneId);
// System.err.printf("toZoneId('%s'): displayName=%s, id=%s%n", timezoneId, zone.getDisplayName(), zone.getID());
return zone;
}
private static ZonedDateTime toDateTime(String timendate, ZoneId zone)
private static Calendar toDateTime(String timendate, TimeZone zone) throws ParseException
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd-hh:mm:ss.S a z")
.withZone(zone);
return ZonedDateTime.parse(timendate, formatter);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd-hh:mm:ss.S a z");
formatter.setTimeZone(zone);
Date parsed = formatter.parse(timendate);
Calendar cal = Calendar.getInstance(zone);
cal.setTime(parsed);
return cal;
}
private static String toString(TemporalAccessor date)
private static String toString(Calendar date)
{
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd-hh:mm:ss.S a z");
return formatter.format(date);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd-hh:mm:ss.S a z");
formatter.setTimeZone(date.getTimeZone());
return formatter.format(date.getTime());
}
private void assertSequence(ZonedDateTime midnight, Object[][] expected)
private void assertSequence(Calendar midnight, Object[][] expected)
{
ZonedDateTime nextEvent = midnight;
Calendar nextEvent = midnight;
for (int i = 0; i < expected.length; i++)
{
long lastMs = nextEvent.toInstant().toEpochMilli();
nextEvent = nextEvent.toLocalDate().plus(1, ChronoUnit.DAYS).atStartOfDay(nextEvent.getZone());
long lastMs = nextEvent.getTimeInMillis();
nextEvent = RolloverFileOutputStream.toMidnight(nextEvent);
assertThat("Next Event", toString(nextEvent), is(expected[i][0]));
long duration = (nextEvent.toInstant().toEpochMilli() - lastMs);
long duration = (nextEvent.getTimeInMillis() - lastMs);
assertThat("Duration to next event", duration, is((long) expected[i][1]));
}
}
@Test
public void testMidnightRolloverCalc_PST_DST_Start()
public void testMidnightRolloverCalc_PST_DST_Start() throws ParseException
{
ZoneId zone = toZoneId("PST");
ZonedDateTime initialDate = toDateTime("2016.03.10-01:23:45.0 PM PST", zone);
TimeZone zone = toZoneId("PST");
Calendar initialDate = toDateTime("2016.03.10-01:23:45.0 PM PST", zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
Calendar midnight = RolloverFileOutputStream.toMidnight(initialDate);
assertThat("Midnight", toString(midnight), is("2016.03.11-12:00:00.0 AM PST"));
Object expected[][] = {
@ -98,12 +104,12 @@ public class RolloverFileOutputStreamTest
}
@Test
public void testMidnightRolloverCalc_PST_DST_End()
public void testMidnightRolloverCalc_PST_DST_End() throws ParseException
{
ZoneId zone = toZoneId("PST");
ZonedDateTime initialDate = toDateTime("2016.11.03-11:22:33.0 AM PDT", zone);
TimeZone zone = toZoneId("PST");
Calendar initialDate = toDateTime("2016.11.03-11:22:33.0 AM PDT", zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
Calendar midnight = RolloverFileOutputStream.toMidnight(initialDate);
assertThat("Midnight", toString(midnight), is("2016.11.04-12:00:00.0 AM PDT"));
Object expected[][] = {
@ -118,12 +124,12 @@ public class RolloverFileOutputStreamTest
}
@Test
public void testMidnightRolloverCalc_Sydney_DST_Start()
public void testMidnightRolloverCalc_Sydney_DST_Start() throws ParseException
{
ZoneId zone = toZoneId("Australia/Sydney");
ZonedDateTime initialDate = toDateTime("2016.09.31-01:23:45.0 PM AEST", zone);
TimeZone zone = toZoneId("Australia/Sydney");
Calendar initialDate = toDateTime("2016.09.30-01:23:45.0 PM AEST", zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
Calendar midnight = RolloverFileOutputStream.toMidnight(initialDate);
assertThat("Midnight", toString(midnight), is("2016.10.01-12:00:00.0 AM AEST"));
Object expected[][] = {
@ -138,12 +144,12 @@ public class RolloverFileOutputStreamTest
}
@Test
public void testMidnightRolloverCalc_Sydney_DST_End()
public void testMidnightRolloverCalc_Sydney_DST_End() throws ParseException
{
ZoneId zone = toZoneId("Australia/Sydney");
ZonedDateTime initialDate = toDateTime("2016.04.01-11:22:33.0 AM AEDT", zone);
TimeZone zone = toZoneId("Australia/Sydney");
Calendar initialDate = toDateTime("2016.04.01-11:22:33.0 AM AEDT", zone);
ZonedDateTime midnight = RolloverFileOutputStream.toMidnight(initialDate);
Calendar midnight = RolloverFileOutputStream.toMidnight(initialDate);
assertThat("Midnight", toString(midnight), is("2016.04.02-12:00:00.0 AM AEDT"));
Object expected[][] = {
@ -158,28 +164,28 @@ public class RolloverFileOutputStreamTest
}
@Test
public void testFilehandling() throws Exception
public void testFileHandling() throws Exception
{
File testDir = MavenTestingUtils.getTargetTestingDir(ResourceTest.class.getName());
File testDir = testingDir.getEmptyPathDir().toFile();
Path testPath = testDir.toPath();
FS.ensureEmpty(testDir);
ZoneId zone = toZoneId("Australia/Sydney");
ZonedDateTime now = toDateTime("2016.04.10-08:30:12.3 AM AEDT", zone);
TimeZone zone = toZoneId("Australia/Sydney");
Calendar now = toDateTime("2016.04.10-08:30:12.3 AM AEST", zone);
File template = new File(testDir,"test-rofos-yyyy_mm_dd.log");
try (RolloverFileOutputStream rofos =
new RolloverFileOutputStream(template.getAbsolutePath(),false,3,TimeZone.getTimeZone(zone),null,null,now))
new RolloverFileOutputStream(template.getAbsolutePath(),false,3,zone,null,null,now))
{
rofos.write("TICK".getBytes());
rofos.flush();
}
now = now.plus(5,ChronoUnit.MINUTES);
now.add(Calendar.MINUTE, 5);
try (RolloverFileOutputStream rofos =
new RolloverFileOutputStream(template.getAbsolutePath(),false,3,TimeZone.getTimeZone(zone),null,null,now))
new RolloverFileOutputStream(template.getAbsolutePath(),false,3,zone,null,null,now))
{
rofos.write("TOCK".getBytes());
rofos.flush();
@ -194,32 +200,37 @@ public class RolloverFileOutputStreamTest
assertThat(Arrays.asList(ls),Matchers.containsInAnyOrder(backup,"test-rofos-2016_04_10.log"));
Files.setLastModifiedTime(testPath.resolve(backup),FileTime.from(now.toInstant()));
Files.setLastModifiedTime(testPath.resolve("test-rofos-2016_04_10.log"),FileTime.from(now.toInstant()));
Files.setLastModifiedTime(testPath.resolve(backup),FileTime.fromMillis(now.getTimeInMillis()));
Files.setLastModifiedTime(testPath.resolve("test-rofos-2016_04_10.log"),FileTime.fromMillis(now.getTimeInMillis()));
// Copy calendar (don't want to change "now")
Calendar time = Calendar.getInstance();
time.setTimeZone(now.getTimeZone());
time.setTime(now.getTime());
time.add(Calendar.DAY_OF_MONTH, -1);
ZonedDateTime time = now.minus(1,ChronoUnit.DAYS);
for (int i=10;i-->5;)
{
String file = "test-rofos-2016_04_0"+i+".log";
Path path = testPath.resolve(file);
FS.touch(path);
Files.setLastModifiedTime(path,FileTime.from(time.toInstant()));
Files.setLastModifiedTime(path,FileTime.fromMillis(time.getTimeInMillis()));
if (i%2==0)
{
file = "test-rofos-2016_04_0"+i+".log.083512300";
path = testPath.resolve(file);
FS.touch(path);
Files.setLastModifiedTime(path,FileTime.from(time.toInstant()));
time = time.minus(1,ChronoUnit.DAYS);
Files.setLastModifiedTime(path,FileTime.fromMillis(time.getTimeInMillis()));
time.add(Calendar.DAY_OF_MONTH, -1);
}
file = "unrelated-"+i;
path = testPath.resolve(file);
FS.touch(path);
Files.setLastModifiedTime(path,FileTime.from(time.toInstant()));
Files.setLastModifiedTime(path,FileTime.fromMillis(time.getTimeInMillis()));
time = time.minus(1,ChronoUnit.DAYS);
time.add(Calendar.DAY_OF_MONTH, -1);
}
ls = testDir.list();
@ -233,7 +244,7 @@ public class RolloverFileOutputStreamTest
"test-rofos-2016_04_10.log",
"test-rofos-2016_04_06.log.083512300",
"test-rofos-2016_04_08.log.083512300",
"test-rofos-2016_04_10.log.083512300",
"test-rofos-2016_04_10.log.083512003",
"unrelated-9",
"unrelated-8",
"unrelated-7",
@ -249,7 +260,7 @@ public class RolloverFileOutputStreamTest
"test-rofos-2016_04_09.log",
"test-rofos-2016_04_10.log",
"test-rofos-2016_04_08.log.083512300",
"test-rofos-2016_04_10.log.083512300",
"test-rofos-2016_04_10.log.083512003",
"unrelated-9",
"unrelated-8",
"unrelated-7",
@ -266,16 +277,16 @@ public class RolloverFileOutputStreamTest
@Test
public void testRollover() throws Exception
{
File testDir = MavenTestingUtils.getTargetTestingDir(ResourceTest.class.getName());
File testDir = testingDir.getEmptyPathDir().toFile();
FS.ensureEmpty(testDir);
ZoneId zone = toZoneId("Australia/Sydney");
ZonedDateTime now = toDateTime("2016.04.10-11:59:55.0 PM AEDT", zone);
TimeZone zone = toZoneId("Australia/Sydney");
Calendar now = toDateTime("2016.04.10-11:59:58.0 PM AEST", zone);
File template = new File(testDir,"test-rofos-yyyy_mm_dd.log");
try (RolloverFileOutputStream rofos =
new RolloverFileOutputStream(template.getAbsolutePath(),false,0,TimeZone.getTimeZone(zone),null,null,now))
new RolloverFileOutputStream(template.getAbsolutePath(),false,0,zone,null,null,now))
{
rofos.write("BEFORE".getBytes());
rofos.flush();
@ -283,7 +294,7 @@ public class RolloverFileOutputStreamTest
assertThat(ls.length,is(1));
assertThat(ls[0],is("test-rofos-2016_04_10.log"));
TimeUnit.SECONDS.sleep(10);
TimeUnit.SECONDS.sleep(5);
rofos.write("AFTER".getBytes());
ls = testDir.list();
assertThat(ls.length,is(2));