@Override public void contextDestroyed(ServletContextEvent event) { log.info("Uninitializing Myrrix in servlet context..."); ClientThread theClientThread = clientThread; if (theClientThread != null) { try { theClientThread.close(); } catch (IOException e) { log.warn("Error while closing client thread", e); } } ServletContext context = event.getServletContext(); Closeable recommender = (Closeable) context.getAttribute(AbstractMyrrixServlet.RECOMMENDER_KEY); if (recommender != null) { try { recommender.close(); } catch (IOException e) { log.warn("Unexpected error while closing", e); } } IOUtils.deleteRecursively(tempDirToDelete); log.info("Myrrix is uninitialized"); }
@Override public synchronized void close() { if (!closed) { closed = true; if (tomcat != null) { try { tomcat.stop(); tomcat.destroy(); } catch (LifecycleException le) { log.warn("Unexpected error while stopping", le); } if (!IOUtils.deleteRecursively(noSuchBaseDir)) { log.info("Could not delete {}", noSuchBaseDir); } } } }
recommender.close(); IOUtils.deleteRecursively(trainingDataDir);
@After public void tearDown() throws Exception { IOUtils.deleteRecursively(testTempDir); }
@Test public void testDeleteRecursively() throws IOException { File tempDir = getTestTempDir(); assertTrue(tempDir.exists()); File subFile1 = new File(tempDir, "subFile1"); Files.write(SOME_BYTES, subFile1); assertTrue(subFile1.exists()); File subDir1 = new File(tempDir, "subDir1"); subDir1.mkdirs(); assertTrue(subDir1.exists()); File subFile2 = new File(subDir1, "subFile2"); Files.write(SOME_BYTES, subFile2); assertTrue(subFile2.exists()); File subDir2 = new File(subDir1, "subDir2"); subDir2.mkdirs(); assertTrue(subDir2.exists()); IOUtils.deleteRecursively(tempDir); assertFalse(tempDir.exists()); assertFalse(subFile1.exists()); assertFalse(subDir1.exists()); assertFalse(subFile2.exists()); assertFalse(subDir2.exists()); }
} finally { recommender.close(); IOUtils.deleteRecursively(tempDir);