source: other-projects/the-macronizer/trunk/src/java/web/listeners/MyServletContextListener.java@ 35719

Last change on this file since 35719 was 35719, checked in by cstephen, 3 years ago

Add support for JSON response to direct input queries. Cleanup other components.

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