Index: lams_central/src/java/org/lamsfoundation/lams/web/LamsStartupServlet.java
===================================================================
RCS file: /usr/local/cvsroot/lams_central/src/java/org/lamsfoundation/lams/web/LamsStartupServlet.java,v
diff -u
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lams_central/src/java/org/lamsfoundation/lams/web/LamsStartupServlet.java 9 Dec 2009 01:02:57 -0000 1.1
@@ -0,0 +1,88 @@
+package org.lamsfoundation.lams.web;
+
+import java.io.File;
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.log4j.Logger;
+import org.lamsfoundation.lams.util.Configuration;
+import org.lamsfoundation.lams.util.ConfigurationKeys;
+
+/**
+ *
+ * @author lfoxton
+ *
+ * A servlet that loads at startup to do some maintainence like removing
+ * temp directories
+ *
+ * @web:servlet name="lamsStartupServlet" load-on-startup = "1"
+ *
+ * @web:servlet-mapping url-pattern="/lamsstartupservlet"
+ */
+
+public class LamsStartupServlet extends HttpServlet {
+
+ private static Logger log = Logger.getLogger(LamsStartupServlet.class);
+ private static final long serialVersionUID = 8010145709788505351L;
+
+ /**
+ * Initialize the servlet.
+ * Retrieve from the servlet configuration the "baseDir" which is the root
+ * of the file repository:
+ * If not specified the value of "/UserFiles/" will be used.
+ *
+ */
+ public void init() throws ServletException {
+
+ // Removing all the files in the temp directory
+ String tempDirStr = Configuration.get(ConfigurationKeys.LAMS_TEMP_DIR);
+ File tempDir = new File(tempDirStr);
+ File[] files = tempDir.listFiles();
+ log.info("Deleting temporary files from: " + tempDir);
+ for (File file : files) {
+
+ if (file.isDirectory()) {
+ // Recursively delete each directory
+ log.debug("Deleting temporary file directory: " + file);
+ if (!deleteDir(file)) {
+ log.equals("Failed to delete " + file);
+ }
+ } else {
+ //Delete each file
+ log.debug("Deleting temporary file: " + file);
+ if (!file.delete()) {
+ log.equals("Failed to delete " + file);
+ }
+ }
+
+ }
+ }
+
+ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+ }
+
+ /**
+ * Deletes all files and subdirectories under dir. Returns true if all
+ * deletions were successful. If a deletion fails, the method stops
+ * attempting to delete and returns false.
+ */
+ private boolean deleteDir(File dir) {
+ if (dir.isDirectory()) {
+ String[] children = dir.list();
+ for (int i = 0; i < children.length; i++) {
+ boolean success = deleteDir(new File(dir, children[i]));
+ if (!success) {
+ return false;
+ }
+ }
+ }
+
+ // The directory is now empty so delete it
+ return dir.delete();
+ }
+
+}
Index: lams_central/web/WEB-INF/web.xml
===================================================================
RCS file: /usr/local/cvsroot/lams_central/web/WEB-INF/Attic/web.xml,v
diff -u -r1.46 -r1.47
--- lams_central/web/WEB-INF/web.xml 19 Aug 2009 06:45:19 -0000 1.46
+++ lams_central/web/WEB-INF/web.xml 9 Dec 2009 01:02:57 -0000 1.47
@@ -313,6 +313,13 @@
+ lamsStartupServlet
+ org.lamsfoundation.lams.web.LamsStartupServlet
+
+ 1
+
+
+
GetRecordingServlet
org.lamsfoundation.lams.webservice.GetRecordingServlet
@@ -511,6 +518,10 @@
/servlet/notebook/storeNotebookEntry
+ lamsStartupServlet
+ /lamsstartupservlet
+
+
GetRecordingServlet
/GetRecording
Index: lams_common/src/java/org/lamsfoundation/lams/util/FileUtil.java
===================================================================
RCS file: /usr/local/cvsroot/lams_common/src/java/org/lamsfoundation/lams/util/FileUtil.java,v
diff -u -r1.34 -r1.35
--- lams_common/src/java/org/lamsfoundation/lams/util/FileUtil.java 31 Mar 2009 14:15:16 -0000 1.34
+++ lams_common/src/java/org/lamsfoundation/lams/util/FileUtil.java 9 Dec 2009 01:02:58 -0000 1.35
@@ -85,7 +85,7 @@
private static final long numMilliSecondsInADay = 24 * 60 * 60 * 1000;
protected static final String prefix = "lamstmp_"; // protected rather than private to suit junit test
- public static final String TEMP_DIR = System.getProperty("java.io.tmpdir");
+ public static final String TEMP_DIR = Configuration.get(ConfigurationKeys.LAMS_TEMP_DIR);
/**
* Deleting a directory using File.delete() only works if the directory is empty. This method deletes a directory