aboutsummaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
authorPaul Hawke <paul.hawke@gmail.com>2013-03-09 00:57:26 -0600
committerPaul Hawke <paul.hawke@gmail.com>2013-03-09 00:57:26 -0600
commit1d08f5f14b5bfeb6ab61f0f653a42f8c731d191d (patch)
tree0c79867cd19bd1fe773565703dd88d7cb8e08921 /samples
parent43e6f0f0ba498a7460ec75cf73487040fc5ce2a0 (diff)
downloadnanohttpd-1d08f5f14b5bfeb6ab61f0f653a42f8c731d191d.tar.gz
Example showing temp file management
Diffstat (limited to 'samples')
-rw-r--r--samples/src/main/java/fi/iki/elonen/TempFilesServer.java55
1 files changed, 55 insertions, 0 deletions
diff --git a/samples/src/main/java/fi/iki/elonen/TempFilesServer.java b/samples/src/main/java/fi/iki/elonen/TempFilesServer.java
new file mode 100644
index 0000000..84eb871
--- /dev/null
+++ b/samples/src/main/java/fi/iki/elonen/TempFilesServer.java
@@ -0,0 +1,55 @@
+package fi.iki.elonen;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @author Paul S. Hawke (paul.hawke@gmail.com)
+ * On: 3/9/13 at 12:47 AM
+ */
+public class TempFilesServer extends DebugServer {
+ public static void main(String[] args) {
+ TempFilesServer server = new TempFilesServer();
+ server.setTempFileManagerFactory(new ExampleManagerFactory());
+ ServerRunner.executeInstance(server);
+ }
+
+ private static class ExampleManagerFactory implements TempFileManagerFactory {
+ @Override
+ public TempFileManager create() {
+ return new ExampleManager();
+ }
+ }
+
+ private static class ExampleManager implements TempFileManager {
+ private final String tmpdir;
+ private final List<TempFile> tempFiles;
+
+ private ExampleManager() {
+ tmpdir = System.getProperty("java.io.tmpdir");
+ tempFiles = new ArrayList<TempFile>();
+ }
+
+ @Override
+ public TempFile createTempFile() throws Exception {
+ DefaultTempFile tempFile = new DefaultTempFile(tmpdir);
+ tempFiles.add(tempFile);
+ System.out.println("Created tempFile: " + tempFile.getName());
+ return tempFile;
+ }
+
+ @Override
+ public void clear() {
+ if (!tempFiles.isEmpty()) {
+ System.out.println("Cleaning up:");
+ }
+ for (TempFile file : tempFiles) {
+ try {
+ System.out.println(" "+file.getName());
+ file.delete();
+ } catch (Exception ignored) {}
+ }
+ tempFiles.clear();
+ }
+ }
+}