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 -r1.2.2.2 -r1.2.2.3 --- lams_central/src/java/org/lamsfoundation/lams/web/LamsStartupServlet.java 9 Dec 2009 01:53:59 -0000 1.2.2.2 +++ lams_central/src/java/org/lamsfoundation/lams/web/LamsStartupServlet.java 11 Feb 2010 00:32:59 -0000 1.2.2.3 @@ -16,8 +16,8 @@ * * @author lfoxton * - * A servlet that loads at startup to do some maintainence like removing - * temp directories + * A servlet that loads at startup to do some maintainence like removing temp + * directories * * @web:servlet name="lamsStartupServlet" load-on-startup = "1" * @@ -26,69 +26,72 @@ public class LamsStartupServlet extends HttpServlet { - private static Logger log = Logger.getLogger(LamsStartupServlet.class); - private static final long serialVersionUID = 8010145709788505351L; + 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 { + /** + * 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); + // Removing all the files in the temp directory + String tempDirStr = Configuration.get(ConfigurationKeys.LAMS_TEMP_DIR); + File tempDir = new File(tempDirStr); - if (tempDir != null && tempDir.canWrite()) { - File[] files = tempDir.listFiles(); - log.info("Deleting temporary files from: " + tempDir); - for (File file : files) { + if (tempDir != null && tempDir.canWrite()) { + 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.error("Failed to delete " + file); - } + if (file.getName().startsWith("lams")) { + if (file.isDirectory() && file.getName().startsWith("lams")) { + // Recursively delete each directory + log.debug("Deleting temporary file directory: " + file); + if (!deleteDir(file)) { + log.error("Failed to delete " + file); + } + } else { + // Delete each file + log.debug("Deleting temporary file: " + file); + if (!file.delete()) { + log.error("Failed to delete " + file); + } + } + } + + } } else { - //Delete each file - log.debug("Deleting temporary file: " + file); - if (!file.delete()) { - log.error("Failed to delete " + file); - } + log.error("Cannot delete temporary files, do not have permission for folder: " + tempDirStr); } - } - } else { - log.error("Cannot delete temporary files, do not have permission for folder: " + tempDirStr); } - } + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + } - 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; + /** + * 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(); } - // The directory is now empty so delete it - return dir.delete(); - } - } Index: lams_central/src/java/org/lamsfoundation/lams/web/planner/PedagogicalPlannerAction.java =================================================================== RCS file: /usr/local/cvsroot/lams_central/src/java/org/lamsfoundation/lams/web/planner/PedagogicalPlannerAction.java,v diff -u -r1.18.2.3 -r1.18.2.3.2.1 --- lams_central/src/java/org/lamsfoundation/lams/web/planner/PedagogicalPlannerAction.java 19 Jun 2009 04:32:10 -0000 1.18.2.3 +++ lams_central/src/java/org/lamsfoundation/lams/web/planner/PedagogicalPlannerAction.java 11 Feb 2010 00:32:59 -0000 1.18.2.3.2.1 @@ -1100,7 +1100,7 @@ throws ServletException { File designFile = null; try { - designFile = new File(FileUtil.TEMP_DIR, fileName); + designFile = new File(FileUtil.getTempDir(), fileName); InputStream inputStream = getContentHandler().getFileNode(fileUuid).getFile(); copyFileFromRepository(inputStream, designFile); } catch (Exception e) { 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.6.1 -r1.34.6.2 --- lams_common/src/java/org/lamsfoundation/lams/util/FileUtil.java 9 Dec 2009 01:54:00 -0000 1.34.6.1 +++ lams_common/src/java/org/lamsfoundation/lams/util/FileUtil.java 11 Feb 2010 00:32:56 -0000 1.34.6.2 @@ -87,8 +87,6 @@ protected static final String prefix = "lamstmp_"; // protected rather than // private to suit junit // test - public static final String TEMP_DIR = Configuration - .get(ConfigurationKeys.LAMS_TEMP_DIR); /** * Deleting a directory using File.delete() only works if the directory is @@ -201,7 +199,7 @@ public static String createTempDirectory(String suffix) throws FileUtilException { - String tempSysDirName = FileUtil.TEMP_DIR; + String tempSysDirName = getTempDir(); if (tempSysDirName == null) { throw new FileUtilException( "No temporary directory known to the server. [System.getProperty( \"java.io.tmpdir\" ) returns null. ]\n Cannot upload package."); @@ -359,7 +357,7 @@ String dumpDirectory = Configuration .get(ConfigurationKeys.LAMS_DUMP_DIR); if (dumpDirectory == null) { - dumpDirectory = FileUtil.TEMP_DIR; + dumpDirectory = getTempDir(); } createDirectory(dumpDirectory); @@ -627,7 +625,7 @@ + date.toString() + " (server time) (" + newestDateToKeep + ")"); - File tempSysDir = new File(FileUtil.TEMP_DIR); + File tempSysDir = new File(getTempDir()); File candidates[] = tempSysDir.listFiles(new TempDirectoryFilter( newestDateToKeep, FileUtil.log)); return candidates; @@ -1020,4 +1018,31 @@ } csv.close(); } + + /** + * Gets the temp dir, creates if not exists, returns java system temp dir if inaccessible + * @return + */ + public static String getTempDir() { + + String ret = Configuration.get(ConfigurationKeys.LAMS_TEMP_DIR); + File tempDir = new File(ret); + + // Create if not exists + if (!tempDir.exists()) { + boolean success = tempDir.mkdirs(); + if (!success) { + log.error("Could not create temp directory: " + ret); + return System.getProperty("java.io.tmpdir"); + } + } + + // Return java temp dir if not accessible + if (!tempDir.canWrite()) { + return System.getProperty("java.io.tmpdir"); + } else { + return ret; + } + + } } \ No newline at end of file Index: lams_common/src/java/org/lamsfoundation/lams/web/servlet/AbstractExportPortfolioServlet.java =================================================================== RCS file: /usr/local/cvsroot/lams_common/src/java/org/lamsfoundation/lams/web/servlet/AbstractExportPortfolioServlet.java,v diff -u -r1.11 -r1.11.10.1 --- lams_common/src/java/org/lamsfoundation/lams/web/servlet/AbstractExportPortfolioServlet.java 21 Nov 2007 00:44:39 -0000 1.11 +++ lams_common/src/java/org/lamsfoundation/lams/web/servlet/AbstractExportPortfolioServlet.java 11 Feb 2010 00:32:56 -0000 1.11.10.1 @@ -105,7 +105,7 @@ directoryName = WebUtil.readStrParam(request, AttributeNames.PARAM_DIRECTORY_NAME); //put the path together again, since the given directory was a relative one. - String absoluteDirectoryPath = FileUtil.TEMP_DIR + File.separator + directoryName; + String absoluteDirectoryPath = FileUtil.getTempDir() + File.separator + directoryName; if (log.isDebugEnabled()) { log.debug("Directory name to store files is "+directoryName); Index: lams_common/test/java/org/lamsfoundation/lams/util/zipfile/TestZipFileUtil.java =================================================================== RCS file: /usr/local/cvsroot/lams_common/test/java/org/lamsfoundation/lams/util/zipfile/Attic/TestZipFileUtil.java,v diff -u -r1.9 -r1.9.14.1 --- lams_common/test/java/org/lamsfoundation/lams/util/zipfile/TestZipFileUtil.java 3 Apr 2007 03:35:50 -0000 1.9 +++ lams_common/test/java/org/lamsfoundation/lams/util/zipfile/TestZipFileUtil.java 11 Feb 2010 00:32:56 -0000 1.9.14.1 @@ -307,7 +307,7 @@ public void testCreateZipFile2() throws FileUtilException, IOException, ZipFileUtilException { String directoryToZip = "ZipTestDirectory"; - String directoryToPlaceZip = FileUtil.TEMP_DIR + File.separator + "ZipTmpDir"; + String directoryToPlaceZip = FileUtil.getTempDir() + File.separator + "ZipTmpDir"; String zipFileName= "testCreateZipFile.zip"; if (createTestDirectoryAndFiles(directoryToZip)) Index: lams_contentrepository/src/java/org/lamsfoundation/lams/contentrepository/service/SimpleRepository.java =================================================================== RCS file: /usr/local/cvsroot/lams_contentrepository/src/java/org/lamsfoundation/lams/contentrepository/service/SimpleRepository.java,v diff -u -r1.14 -r1.14.10.1 --- lams_contentrepository/src/java/org/lamsfoundation/lams/contentrepository/service/SimpleRepository.java 6 Nov 2007 05:47:54 -0000 1.14 +++ lams_contentrepository/src/java/org/lamsfoundation/lams/contentrepository/service/SimpleRepository.java 11 Feb 2010 00:32:58 -0000 1.14.10.1 @@ -672,8 +672,8 @@ if(toFileName == null){ IValue prop = node.getProperty(PropertyName.FILENAME); toFileName = prop != null ? prop.getString() : null; - FileUtil.createDirectory(FileUtil.TEMP_DIR); - toFileName = FileUtil.getFullPath(FileUtil.TEMP_DIR,toFileName); + FileUtil.createDirectory(FileUtil.getTempDir()); + toFileName = FileUtil.getFullPath(FileUtil.getTempDir(),toFileName); } ZipFileUtil.createZipFile(FileUtil.getFileName(toFileName),tempRoot,FileUtil.getFileDirectory(toFileName)); } @@ -707,8 +707,8 @@ if(toFileName == null){ IValue prop = node.getProperty(PropertyName.FILENAME); toFileName = prop != null ? prop.getString() : null; - FileUtil.createDirectory(FileUtil.TEMP_DIR); - toFileName = FileUtil.getFullPath(FileUtil.TEMP_DIR,toFileName); + FileUtil.createDirectory(FileUtil.getTempDir()); + toFileName = FileUtil.getFullPath(FileUtil.getTempDir(),toFileName); } OutputStream os = new FileOutputStream(toFileName); byte[] out = new byte[8 * 1024]; Index: lams_learning/src/java/org/lamsfoundation/lams/learning/export/service/ExportPortfolioService.java =================================================================== RCS file: /usr/local/cvsroot/lams_learning/src/java/org/lamsfoundation/lams/learning/export/service/ExportPortfolioService.java,v diff -u -r1.34 -r1.34.8.1 --- lams_learning/src/java/org/lamsfoundation/lams/learning/export/service/ExportPortfolioService.java 23 Jan 2009 09:32:20 -0000 1.34 +++ lams_learning/src/java/org/lamsfoundation/lams/learning/export/service/ExportPortfolioService.java 11 Feb 2010 00:32:58 -0000 1.34.8.1 @@ -326,7 +326,7 @@ // for security reasons, append the relative directory name to the end of the export url instead of the // whole path - String relativePath = activitySubDirectory.substring(FileUtil.TEMP_DIR.length() + 1, activitySubDirectory + String relativePath = activitySubDirectory.substring(FileUtil.getTempDir().length() + 1, activitySubDirectory .length()); // Some activities (parallel, optional, sequence) don't have export urls. Index: lams_learning/src/java/org/lamsfoundation/lams/learning/export/web/action/ExportDownloadServlet.java =================================================================== RCS file: /usr/local/cvsroot/lams_learning/src/java/org/lamsfoundation/lams/learning/export/web/action/ExportDownloadServlet.java,v diff -u -r1.11.10.2 -r1.11.10.3 --- lams_learning/src/java/org/lamsfoundation/lams/learning/export/web/action/ExportDownloadServlet.java 5 Feb 2010 05:09:45 -0000 1.11.10.2 +++ lams_learning/src/java/org/lamsfoundation/lams/learning/export/web/action/ExportDownloadServlet.java 11 Feb 2010 00:32:58 -0000 1.11.10.3 @@ -101,7 +101,7 @@ private String constructAbsolutePath(String relativePath) { - return FileUtil.TEMP_DIR + File.separator + return FileUtil.getTempDir() + File.separator + getDirname(relativePath) + File.separator + ExportPortfolioConstants.EXPORT_TEMP_FILENAME; }