1 package fi.iki.elonen; 2 3 import fi.iki.elonen.debug.DebugServer; 4 5 import java.util.ArrayList; 6 import java.util.List; 7 8 /** 9 * @author Paul S. Hawke (paul.hawke@gmail.com) 10 * On: 3/9/13 at 12:47 AM 11 */ 12 public class TempFilesServer extends DebugServer { main(String[] args)13 public static void main(String[] args) { 14 TempFilesServer server = new TempFilesServer(); 15 server.setTempFileManagerFactory(new ExampleManagerFactory()); 16 ServerRunner.executeInstance(server); 17 } 18 19 private static class ExampleManagerFactory implements TempFileManagerFactory { 20 @Override create()21 public TempFileManager create() { 22 return new ExampleManager(); 23 } 24 } 25 26 private static class ExampleManager implements TempFileManager { 27 private final String tmpdir; 28 private final List<TempFile> tempFiles; 29 ExampleManager()30 private ExampleManager() { 31 tmpdir = System.getProperty("java.io.tmpdir"); 32 tempFiles = new ArrayList<TempFile>(); 33 } 34 35 @Override createTempFile()36 public TempFile createTempFile() throws Exception { 37 DefaultTempFile tempFile = new DefaultTempFile(tmpdir); 38 tempFiles.add(tempFile); 39 System.out.println("Created tempFile: " + tempFile.getName()); 40 return tempFile; 41 } 42 43 @Override clear()44 public void clear() { 45 if (!tempFiles.isEmpty()) { 46 System.out.println("Cleaning up:"); 47 } 48 for (TempFile file : tempFiles) { 49 try { 50 System.out.println(" "+file.getName()); 51 file.delete(); 52 } catch (Exception ignored) {} 53 } 54 tempFiles.clear(); 55 } 56 } 57 } 58