1 |
|
---|
2 | package web.listeners;
|
---|
3 |
|
---|
4 | import java.io.File;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.util.Properties;
|
---|
7 | import java.util.Timer;
|
---|
8 | import javax.servlet.ServletContext;
|
---|
9 | import javax.servlet.ServletContextEvent;
|
---|
10 | import javax.servlet.ServletContextListener;
|
---|
11 | import tasks.FileSweeper;
|
---|
12 | import util.FileUtil;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | *@author University of Waikato - Te Whare WÄnanga o Waikato
|
---|
16 | * @version 1.0
|
---|
17 | * @since 2014-11-20
|
---|
18 | */
|
---|
19 | public class MyServletContextListener implements ServletContextListener {
|
---|
20 |
|
---|
21 | private final String PROPERTIES_PATH = "../properties/web.properties";
|
---|
22 | private final String WEB_DIRECTORY_NAME = "webtmpdir";
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Creates a web temporary directory inside the temporary directory to use
|
---|
26 | * as the temporary directory for the web application.
|
---|
27 | * @param sce
|
---|
28 | */
|
---|
29 | public void contextInitialized(ServletContextEvent sce) {
|
---|
30 | final File tmpdir = getTempDirectory();
|
---|
31 | final File webtmpdir = new File(tmpdir, WEB_DIRECTORY_NAME);
|
---|
32 | if (! webtmpdir.mkdir()) {
|
---|
33 | System.err.println("Coult not create the servlet's temporary directory: " + webtmpdir.getAbsolutePath());
|
---|
34 | }
|
---|
35 | final ServletContext sc = sce.getServletContext();
|
---|
36 | sc.setAttribute("tmpdir", webtmpdir.getAbsolutePath());
|
---|
37 |
|
---|
38 | //Fire off the file sweeper to run each hour.
|
---|
39 | Timer timer = new Timer(true);
|
---|
40 | timer.scheduleAtFixedRate(new FileSweeper(webtmpdir, 60), 60 * 60 * 1000, 60 * 60 * 1000);
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Deletes the web temporary directory inside the temporary directory
|
---|
45 | * including all nested files.
|
---|
46 | * @param sce
|
---|
47 | */
|
---|
48 | public void contextDestroyed(ServletContextEvent sce) {
|
---|
49 | final ServletContext sc = sce.getServletContext();
|
---|
50 | final File webtmpdir = new File((String)sc.getAttribute("tmpdir"));
|
---|
51 | if (! FileUtil.deleteFile(webtmpdir)) {
|
---|
52 | System.err.println("Could not delete the servlet's temporary directory: " + webtmpdir.getAbsolutePath());
|
---|
53 | }
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Returns the temp directory property associated with the tmpdir key in
|
---|
58 | * the web.properties file. If the property does not exist then the default
|
---|
59 | * temp directory defined by System.getProperty("java.io.tmpdir") is returned.
|
---|
60 | * @return the temporary directory.
|
---|
61 | */
|
---|
62 | private File getTempDirectory() {
|
---|
63 | final Properties properties = new Properties();
|
---|
64 | try {
|
---|
65 | properties.load(getClass().getResourceAsStream(PROPERTIES_PATH));
|
---|
66 | } catch (Exception e) {
|
---|
67 | System.err.println("getTempDirectory(): Did not find custom properties file: '"
|
---|
68 | + PROPERTIES_PATH + "'");
|
---|
69 | System.err.println("Defaulting to Java property java.io.tmpdir="
|
---|
70 | + System.getProperty("java.io.tmpdir"));
|
---|
71 |
|
---|
72 | }
|
---|
73 | return new File(properties.getProperty("tmpdir", System.getProperty("java.io.tmpdir")));
|
---|
74 | }
|
---|
75 | }
|
---|