Index: lams_central/src/java/org/lamsfoundation/lams/web/LamsStartupServlet.java =================================================================== diff -u --- lams_central/src/java/org/lamsfoundation/lams/web/LamsStartupServlet.java (revision 0) +++ lams_central/src/java/org/lamsfoundation/lams/web/LamsStartupServlet.java (revision b495ec47552a05d4e14f2b3e402ea5a8521c8542) @@ -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 =================================================================== diff -u -r4abf8ee86bc60a0b94edfdc4c907c28c6c5df13e -rb495ec47552a05d4e14f2b3e402ea5a8521c8542 --- lams_central/web/WEB-INF/web.xml (.../web.xml) (revision 4abf8ee86bc60a0b94edfdc4c907c28c6c5df13e) +++ lams_central/web/WEB-INF/web.xml (.../web.xml) (revision b495ec47552a05d4e14f2b3e402ea5a8521c8542) @@ -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 =================================================================== diff -u -r88885f462c4ff03e52e374f971ee2069f95b570f -rb495ec47552a05d4e14f2b3e402ea5a8521c8542 --- lams_common/src/java/org/lamsfoundation/lams/util/FileUtil.java (.../FileUtil.java) (revision 88885f462c4ff03e52e374f971ee2069f95b570f) +++ lams_common/src/java/org/lamsfoundation/lams/util/FileUtil.java (.../FileUtil.java) (revision b495ec47552a05d4e14f2b3e402ea5a8521c8542) @@ -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