diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java b/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java index e30b443acf1..94bfae53f89 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/RolloverFileOutputStream.java @@ -233,9 +233,19 @@ public class RolloverFileOutputStream extends OutputStream File file = new File(_filename); _filename = file.getCanonicalPath(); file = new File(_filename); - File dir = new File(file.getParent()); - if (!dir.isDirectory() || !dir.canWrite()) + File dir = file.getParentFile(); + if (!dir.exists()) + { + throw new IOException("Log directory does not exist. Path=" + dir); + } + else if (!dir.isDirectory()) + { + throw new IOException("Path for Log directory is not a directory. Path=" + dir); + } + else if (!dir.canWrite()) + { throw new IOException("Cannot write log directory " + dir); + } // Is this a rollover file? String filename = file.getName(); diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java index ee3872f98ea..7c1f6710643 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/RolloverFileOutputStreamTest.java @@ -181,6 +181,47 @@ public class RolloverFileOutputStreamTest assertSequence(midnight, expected); } + @Test + public void testMissingDirectory() + { + + ZoneId zone = toZoneId("Australia/Sydney"); + ZonedDateTime now = toDateTime("2016.04.10-08:30:12.3 AM AEDT", zone); + String templateString = "missingDir/test-rofos-yyyy_mm_dd.log"; + String excMessageExpected = "Log directory does not exist."; + + try (RolloverFileOutputStream rofos = + new RolloverFileOutputStream(templateString, false, 3, TimeZone.getTimeZone(zone), null, null, now)) + { + rofos.write("TICK".getBytes()); + rofos.flush(); + } + catch (Exception ex) + { + boolean exClassOK = false; + if (ex instanceof java.io.IOException) + { + exClassOK = true; + } + assertThat("Exception Type", exClassOK, is(true)); + + String excMessageActual = ex.getMessage(); + boolean messageOK = false; + if (excMessageActual != null) + { + excMessageActual = excMessageActual.trim(); + if (!excMessageActual.isEmpty()) + { + if (excMessageActual.contains(excMessageExpected)) + { + messageOK = true; + } + } + } + assertThat("Exception Message", messageOK, is(true)); + } + } + @Test public void testFileHandling() throws Exception {