/** * Call this method to cause the file cleaner thread to terminate when * there are no more objects being tracked for deletion. * <p> * In a simple environment, you don't need this method as the file cleaner * thread will simply exit when the JVM exits. In a more complex environment, * with multiple class loaders (such as an application server), you should be * aware that the file cleaner thread will continue running even if the class * loader it was started from terminates. This can constitute a memory leak. * <p> * For example, suppose that you have developed a web application, which * contains the commons-io jar file in your WEB-INF/lib directory. In other * words, the FileCleaner class is loaded through the class loader of your * web application. If the web application is terminated, but the servlet * container is still running, then the file cleaner thread will still exist, * posing a memory leak. * <p> * This method allows the thread to be terminated. Simply call this method * in the resource cleanup code, such as * {@code javax.servlet.ServletContextListener.contextDestroyed(javax.servlet.ServletContextEvent)}. * One called, no new objects can be tracked by the file cleaner. * @deprecated Use {@link FileCleaningTracker#exitWhenFinished()}. */ @Deprecated public static synchronized void exitWhenFinished() { theInstance.exitWhenFinished(); }
/** * Call this method to cause the file cleaner thread to terminate when * there are no more objects being tracked for deletion. * <p> * In a simple environment, you don't need this method as the file cleaner * thread will simply exit when the JVM exits. In a more complex environment, * with multiple class loaders (such as an application server), you should be * aware that the file cleaner thread will continue running even if the class * loader it was started from terminates. This can consitute a memory leak. * <p> * For example, suppose that you have developed a web application, which * contains the commons-io jar file in your WEB-INF/lib directory. In other * words, the FileCleaner class is loaded through the class loader of your * web application. If the web application is terminated, but the servlet * container is still running, then the file cleaner thread will still exist, * posing a memory leak. * <p> * This method allows the thread to be terminated. Simply call this method * in the resource cleanup code, such as {@link javax.servlet.ServletContextListener#contextDestroyed}. * One called, no new objects can be tracked by the file cleaner. */ public static synchronized void exitWhenFinished() { theInstance.exitWhenFinished(); }
/** * Called when the web application is being destroyed. * Calls {@link FileCleaningTracker#exitWhenFinished()}. * * @param sce The servlet context, used for calling * {@link #getFileCleaningTracker(ServletContext)}. */ @Override public void contextDestroyed(ServletContextEvent sce) { getFileCleaningTracker(sce.getServletContext()).exitWhenFinished(); }
@Test public void testFileCleanerExitWhenFinished_NoTrackAfter() throws Exception { assertFalse(theInstance.exitWhenFinished); theInstance.exitWhenFinished(); assertTrue(theInstance.exitWhenFinished); assertEquals(null, theInstance.reaper); final String path = testFile.getPath(); final Object marker = new Object(); try { theInstance.track(path, marker); fail(); } catch (final IllegalStateException ex) { // expected } assertTrue(theInstance.exitWhenFinished); assertEquals(null, theInstance.reaper); }
@Test public void testFileCleanerExitWhenFinished2() throws Exception { final String path = testFile.getPath(); assertFalse(testFile.exists()); RandomAccessFile r = new RandomAccessFile(testFile, "rw"); assertTrue(testFile.exists()); assertEquals(0, theInstance.getTrackCount()); theInstance.track(path, r); assertEquals(1, theInstance.getTrackCount()); assertFalse(theInstance.exitWhenFinished); assertTrue(theInstance.reaper.isAlive()); r.close(); testFile = null; r = null; waitUntilTrackCount(); pauseForDeleteToComplete(new File(path)); assertEquals(0, theInstance.getTrackCount()); assertEquals(showFailures(), false, new File(path).exists()); assertFalse(theInstance.exitWhenFinished); assertTrue(theInstance.reaper.isAlive()); assertFalse(theInstance.exitWhenFinished); theInstance.exitWhenFinished(); for (int i = 0; i < 20 && theInstance.reaper.isAlive(); i++) { TestUtils.sleep(500L); // allow reaper thread to die } assertTrue(theInstance.exitWhenFinished); assertFalse(theInstance.reaper.isAlive()); }
@Test public void testFileCleanerExitWhenFinished1() throws Exception { final String path = testFile.getPath(); assertEquals("1-testFile exists: " + testFile, false, testFile.exists()); RandomAccessFile r = new RandomAccessFile(testFile, "rw"); assertEquals("2-testFile exists", true, testFile.exists()); assertEquals("3-Track Count", 0, theInstance.getTrackCount()); theInstance.track(path, r); assertEquals("4-Track Count", 1, theInstance.getTrackCount()); assertEquals("5-exitWhenFinished", false, theInstance.exitWhenFinished); assertEquals("6-reaper.isAlive", true, theInstance.reaper.isAlive()); assertEquals("7-exitWhenFinished", false, theInstance.exitWhenFinished); theInstance.exitWhenFinished(); assertEquals("8-exitWhenFinished", true, theInstance.exitWhenFinished); assertEquals("9-reaper.isAlive", true, theInstance.reaper.isAlive()); r.close(); testFile = null; r = null; waitUntilTrackCount(); pauseForDeleteToComplete(new File(path)); assertEquals("10-Track Count", 0, theInstance.getTrackCount()); assertEquals("11-testFile exists " + showFailures(), false, new File(path).exists()); assertEquals("12-exitWhenFinished", true, theInstance.exitWhenFinished); assertEquals("13-reaper.isAlive", false, theInstance.reaper.isAlive()); }
@Test public void testFileCleanerExitWhenFinishedFirst() throws Exception { assertFalse(theInstance.exitWhenFinished); theInstance.exitWhenFinished(); assertTrue(theInstance.exitWhenFinished); assertEquals(null, theInstance.reaper); waitUntilTrackCount(); assertEquals(0, theInstance.getTrackCount()); assertTrue(theInstance.exitWhenFinished); assertEquals(null, theInstance.reaper); }
@Override public void destroy() { cleaner.exitWhenFinished(); } }
/** * Call this method to cause the file cleaner thread to terminate when * there are no more objects being tracked for deletion. * <p> * In a simple environment, you don't need this method as the file cleaner * thread will simply exit when the JVM exits. In a more complex environment, * with multiple class loaders (such as an application server), you should be * aware that the file cleaner thread will continue running even if the class * loader it was started from terminates. This can consitute a memory leak. * <p> * For example, suppose that you have developed a web application, which * contains the commons-io jar file in your WEB-INF/lib directory. In other * words, the FileCleaner class is loaded through the class loader of your * web application. If the web application is terminated, but the servlet * container is still running, then the file cleaner thread will still exist, * posing a memory leak. * <p> * This method allows the thread to be terminated. Simply call this method * in the resource cleanup code, such as {@link javax.servlet.ServletContextListener#contextDestroyed}. * One called, no new objects can be tracked by the file cleaner. * @deprecated Use {@link FileCleaningTracker#exitWhenFinished()}. */ public static synchronized void exitWhenFinished() { theInstance.exitWhenFinished(); }
/** * Call this method to cause the file cleaner thread to terminate when * there are no more objects being tracked for deletion. * <p> * In a simple environment, you don't need this method as the file cleaner * thread will simply exit when the JVM exits. In a more complex environment, * with multiple class loaders (such as an application server), you should be * aware that the file cleaner thread will continue running even if the class * loader it was started from terminates. This can consitute a memory leak. * <p> * For example, suppose that you have developed a web application, which * contains the commons-io jar file in your WEB-INF/lib directory. In other * words, the FileCleaner class is loaded through the class loader of your * web application. If the web application is terminated, but the servlet * container is still running, then the file cleaner thread will still exist, * posing a memory leak. * <p> * This method allows the thread to be terminated. Simply call this method * in the resource cleanup code, such as {@link javax.servlet.ServletContextListener#contextDestroyed}. * One called, no new objects can be tracked by the file cleaner. * @deprecated Use {@link FileCleaningTracker#exitWhenFinished()}. */ public static synchronized void exitWhenFinished() { theInstance.exitWhenFinished(); }
/** * Call this method to cause the file cleaner thread to terminate when * there are no more objects being tracked for deletion. * <p> * In a simple environment, you don't need this method as the file cleaner * thread will simply exit when the JVM exits. In a more complex environment, * with multiple class loaders (such as an application server), you should be * aware that the file cleaner thread will continue running even if the class * loader it was started from terminates. This can consitute a memory leak. * <p> * For example, suppose that you have developed a web application, which * contains the commons-io jar file in your WEB-INF/lib directory. In other * words, the FileCleaner class is loaded through the class loader of your * web application. If the web application is terminated, but the servlet * container is still running, then the file cleaner thread will still exist, * posing a memory leak. * <p> * This method allows the thread to be terminated. Simply call this method * in the resource cleanup code, such as {@link javax.servlet.ServletContextListener#contextDestroyed}. * One called, no new objects can be tracked by the file cleaner. * @deprecated Use {@link FileCleaningTracker#exitWhenFinished()}. */ @Deprecated public static synchronized void exitWhenFinished() { theInstance.exitWhenFinished(); }
/** * Call this method to cause the file cleaner thread to terminate when * there are no more objects being tracked for deletion. * <p> * In a simple environment, you don't need this method as the file cleaner * thread will simply exit when the JVM exits. In a more complex environment, * with multiple class loaders (such as an application server), you should be * aware that the file cleaner thread will continue running even if the class * loader it was started from terminates. This can consitute a memory leak. * <p> * For example, suppose that you have developed a web application, which * contains the commons-io jar file in your WEB-INF/lib directory. In other * words, the FileCleaner class is loaded through the class loader of your * web application. If the web application is terminated, but the servlet * container is still running, then the file cleaner thread will still exist, * posing a memory leak. * <p> * This method allows the thread to be terminated. Simply call this method * in the resource cleanup code, such as * {@code javax.servlet.ServletContextListener.contextDestroyed(javax.servlet.ServletContextEvent)}. * One called, no new objects can be tracked by the file cleaner. * @deprecated Use {@link FileCleaningTracker#exitWhenFinished()}. */ @Deprecated public static synchronized void exitWhenFinished() { theInstance.exitWhenFinished(); }
/** * Call this method to cause the file cleaner thread to terminate when * there are no more objects being tracked for deletion. * <p> * In a simple environment, you don't need this method as the file cleaner * thread will simply exit when the JVM exits. In a more complex environment, * with multiple class loaders (such as an application server), you should be * aware that the file cleaner thread will continue running even if the class * loader it was started from terminates. This can consitute a memory leak. * <p> * For example, suppose that you have developed a web application, which * contains the commons-io jar file in your WEB-INF/lib directory. In other * words, the FileCleaner class is loaded through the class loader of your * web application. If the web application is terminated, but the servlet * container is still running, then the file cleaner thread will still exist, * posing a memory leak. * <p> * This method allows the thread to be terminated. Simply call this method * in the resource cleanup code, such as {@link javax.servlet.ServletContextListener#contextDestroyed}. * One called, no new objects can be tracked by the file cleaner. * @deprecated Use {@link FileCleaningTracker#exitWhenFinished()}. */ @Deprecated public static synchronized void exitWhenFinished() { theInstance.exitWhenFinished(); }
/** * Call this method to cause the file cleaner thread to terminate when * there are no more objects being tracked for deletion. * <p> * In a simple environment, you don't need this method as the file cleaner * thread will simply exit when the JVM exits. In a more complex environment, * with multiple class loaders (such as an application server), you should be * aware that the file cleaner thread will continue running even if the class * loader it was started from terminates. This can constitute a memory leak. * <p> * For example, suppose that you have developed a web application, which * contains the commons-io jar file in your WEB-INF/lib directory. In other * words, the FileCleaner class is loaded through the class loader of your * web application. If the web application is terminated, but the servlet * container is still running, then the file cleaner thread will still exist, * posing a memory leak. * <p> * This method allows the thread to be terminated. Simply call this method * in the resource cleanup code, such as * {@code javax.servlet.ServletContextListener.contextDestroyed(javax.servlet.ServletContextEvent)}. * One called, no new objects can be tracked by the file cleaner. * @deprecated Use {@link FileCleaningTracker#exitWhenFinished()}. */ @Deprecated public static synchronized void exitWhenFinished() { theInstance.exitWhenFinished(); }
/** * Called when the web application is being destroyed. * Calls {@link FileCleaningTracker#exitWhenFinished()}. * * @param sce The servlet context, used for calling * {@link #getFileCleaningTracker(ServletContext)}. */ public void contextDestroyed(ServletContextEvent sce) { getFileCleaningTracker(sce.getServletContext()).exitWhenFinished(); }
/** * Called when the web application is being destroyed. * Calls {@link FileCleaningTracker#exitWhenFinished()}. * @param sce The servlet context, used for calling * {@link #getFileCleaningTracker(ServletContext)}. */ public void contextDestroyed(ServletContextEvent sce) { getFileCleaningTracker(sce.getServletContext()).exitWhenFinished(); } }
@Override public void contextDestroyed(ServletContextEvent sce) { Settings settings = Settings.get(); DiskFileItemFactory f = (DiskFileItemFactory)settings.get(ID); FileCleaningTracker tracker = f.getFileCleaningTracker(); if (tracker != null){ logger.info("The uploaded temp files will be deleted."); tracker.exitWhenFinished(); } } @Override
/** * Parse the request and store it as a request attribute. */ private MultipartForm parseRequest(HttpServletRequest request) throws IOException, ServletException { MultipartForm form = new MultipartForm(); ServletFileUpload upload = newServletFileUpload(); final List fileItems; try { fileItems = upload.parseRequest(request); } catch (FileUploadException e) { throw new ServletException("Could not upload files:" + e.getMessage(), e); } for (Iterator fileItemIterator = fileItems.iterator(); fileItemIterator.hasNext(); ) { FileItem item = (FileItem) fileItemIterator.next(); if (item.isFormField()) { addField(request, item, form); } else { addFile(item, form); } } // clean up files created by commons-upload which are not cleaned up automatically on Win ((DiskFileItemFactory) upload.getFileItemFactory()).getFileCleaningTracker().exitWhenFinished(); request.setAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME, form); return form; }