Index: lams_central/src/java/org/lamsfoundation/lams/web/LamsStartupServlet.java =================================================================== diff -u -rb495ec47552a05d4e14f2b3e402ea5a8521c8542 -r80405cd7fe447a626fd5e1b62584ccfeae60ed15 --- lams_central/src/java/org/lamsfoundation/lams/web/LamsStartupServlet.java (.../LamsStartupServlet.java) (revision b495ec47552a05d4e14f2b3e402ea5a8521c8542) +++ lams_central/src/java/org/lamsfoundation/lams/web/LamsStartupServlet.java (.../LamsStartupServlet.java) (revision 80405cd7fe447a626fd5e1b62584ccfeae60ed15) @@ -41,25 +41,31 @@ // 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); + 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); + } + } 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.equals("Failed to delete " + file); - } - } + } + } else { + log.error("Cannot delete temporary files, do not have permission for folder: " + tempDirStr); } + } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Index: lams_common/src/java/org/lamsfoundation/lams/util/FileUtil.java =================================================================== diff -u -rb495ec47552a05d4e14f2b3e402ea5a8521c8542 -r80405cd7fe447a626fd5e1b62584ccfeae60ed15 --- lams_common/src/java/org/lamsfoundation/lams/util/FileUtil.java (.../FileUtil.java) (revision b495ec47552a05d4e14f2b3e402ea5a8521c8542) +++ lams_common/src/java/org/lamsfoundation/lams/util/FileUtil.java (.../FileUtil.java) (revision 80405cd7fe447a626fd5e1b62584ccfeae60ed15) @@ -88,14 +88,17 @@ 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 - * and all of its contained files. + * Deleting a directory using File.delete() only works if the directory is + * empty. This method deletes a directory and all of its contained files. * - * This method is not transactional - if it fails to delete some contained files or directories, it will continue - * deleting all the other files in the directory. If only a partial deletion is done, then the files and directories - * that could not be deleted are listed in the log file, and the method returns false. + * This method is not transactional - if it fails to delete some contained + * files or directories, it will continue deleting all the other files in + * the directory. If only a partial deletion is done, then the files and + * directories that could not be deleted are listed in the log file, and the + * method returns false. * - * This method has not been tested in Linux or Unix systems, so the behaviour across symbolic links is unknown. + * This method has not been tested in Linux or Unix systems, so the + * behaviour across symbolic links is unknown. */ public static boolean deleteDirectory(File directory) { boolean retValue = true; @@ -133,9 +136,11 @@ } /** - * Check if this directory is empty. If checkSubdirectories = true, then it also checks its subdirectories to make - * sure they aren't empty. If checkSubdirectories = true and the directory contains empty subdirectories it will - * return true. If checkSubdirectories = false and the directory contains empty subdirectories it will return false. + * Check if this directory is empty. If checkSubdirectories = true, then it + * also checks its subdirectories to make sure they aren't empty. If + * checkSubdirectories = true and the directory contains empty + * subdirectories it will return true. If checkSubdirectories = false and + * the directory contains empty subdirectories it will return false. */ public static boolean isEmptyDirectory(String directoryName, boolean checkSubdirectories) throws FileUtilException { @@ -173,15 +178,17 @@ } /** - * Create a temporary directory with the name in the form lamstmp_timestamp_suffix inside the default temporary-file - * directory for the system. This method is protected (rather than private) so that it may be called by the junit - * tests for this class. + * Create a temporary directory with the name in the form + * lamstmp_timestamp_suffix inside the default temporary-file directory for + * the system. This method is protected (rather than private) so that it may + * be called by the junit tests for this class. * * @param zipFileName * @return name of the new directory * @throws ZipFileUtilException - * if the java io temp directory is not defined, or we are unable to calculate a unique name for the - * expanded directory, or an IOException occurs. + * if the java io temp directory is not defined, or we are + * unable to calculate a unique name for the expanded directory, + * or an IOException occurs. */ public static String createTempDirectory(String suffix) throws FileUtilException { @@ -191,9 +198,19 @@ "No temporary directory known to the server. [System.getProperty( \"java.io.tmpdir\" ) returns null. ]\n Cannot upload package."); } + if (!(new File(tempSysDirName)).canWrite()) { + String javaTemp = System.getProperty("java.io.tmpdir"); + if (!(new File(javaTemp).canWrite())) { + throw new FileUtilException("Do not have write permissions for temporary directory: " + tempSysDirName + + " or java temp dir: " + javaTemp); + } + tempSysDirName = javaTemp; + } + String tempDirName = tempSysDirName + File.separator + FileUtil.prefix + generateUniqueContentFolderID() + "_" + suffix; File tempDir = new File(tempDirName); + // try 100 different variations. If I can't find a unique // one in 100 tries, then give up. int i = 0; @@ -214,16 +231,18 @@ } /** - * This method creates a directory with the name directoryName. Also creates any necessary parent - * directories that may not yet exist. + * This method creates a directory with the name directoryName. + * Also creates any necessary parent directories that may not yet exist. * - * If the directoryname is null or an empty string, a FileUtilException is thrown + * If the directoryname is null or an empty string, a FileUtilException is + * thrown * * @param directoryName - * the name of the directory to create - * @return boolean. Returns true if the directory is created and false otherwise + * the name of the directory to create + * @return boolean. Returns true if the directory is created and false + * otherwise * @throws FileUtilException - * if the directory name is null or an empty string + * if the directory name is null or an empty string */ public static boolean createDirectory(String directoryName) throws FileUtilException { boolean isCreated = false; @@ -239,19 +258,22 @@ } /** - * Creates a subdirectory under the parent directory parentDirName If the parent or child directory - * is null, FileUtilException is thrown. + * Creates a subdirectory under the parent directory + * parentDirName If the parent or child directory is null, + * FileUtilException is thrown. * * If the parent directory has not been created yet, it will be created. * * * @param parentDirName - * The name of the parent directory in which the subdirectory should be created in + * The name of the parent directory in which the subdirectory + * should be created in * @param subDirName - * The name of the subdirectory to create - * @return boolean. Returns true if the subdirectory was created and false otherwise + * The name of the subdirectory to create + * @return boolean. Returns true if the subdirectory was created and false + * otherwise * @throws FileUtilException - * if the parent/child directory name is null or empty. + * if the parent/child directory name is null or empty. */ public static boolean createDirectory(String parentDirName, String subDirName) throws FileUtilException { boolean isSubDirCreated = false; @@ -281,9 +303,10 @@ } /** - * If the directory name specified has a slash at the end of it such as "directoryName/", then the slash will be - * removed and "directoryName" will be returned. The createDirectory(parentdir, childdir) method requires that there - * is no slash at the end of the directory name. + * If the directory name specified has a slash at the end of it such as + * "directoryName/", then the slash will be removed and "directoryName" will + * be returned. The createDirectory(parentdir, childdir) method requires + * that there is no slash at the end of the directory name. * * @param stringToModify * @return @@ -297,8 +320,9 @@ * Checks to see if there is a slash at the end of the string. * * @param stringToCheck - * the directoryName to check - * @return boolean. Returns true if there is a slash at the end and false if not. + * the directoryName to check + * @return boolean. Returns true if there is a slash at the end and false if + * not. */ public static boolean trailingForwardSlashPresent(String stringToCheck) { int indexOfSlash = stringToCheck.lastIndexOf("/"); @@ -341,20 +365,24 @@ } /** - * Dump some data to a file in the Dump Directory. The directory is set in the LAMS configuration file. These dumps - * are primarily for support/debugging/problem reporting uses. + * Dump some data to a file in the Dump Directory. The directory is set in + * the LAMS configuration file. These dumps are primarily for + * support/debugging/problem reporting uses. * - * If the dump directory is not set, it will revert to the system temp directory. + * If the dump directory is not set, it will revert to the system temp + * directory. * - * Used by the FlashCrashDump servlet initially, may be used by other dump methods in future. + * Used by the FlashCrashDump servlet initially, may be used by other dump + * methods in future. * * @param data - * data to dump + * data to dump * @param id - * some identification name for the string. Does not need to be unique. e.g. FLASH_jsmith + * some identification name for the string. Does not need to be + * unique. e.g. FLASH_jsmith * @param extension - * optional extension to be added to filename e.g. xml. Note: do not include the "." - that will be - * added automatically. + * optional extension to be added to filename e.g. xml. Note: do + * not include the "." - that will be added automatically. * * @author Fiona Malikoff * @throws FileUtilException @@ -384,8 +412,9 @@ } /** - * get file name from a string which may include directory information. For example : "c:\\dir\\ndp\\pp.txt"; will - * return pp.txt.? If file has no path infomation, then just return input fileName. + * get file name from a string which may include directory information. For + * example : "c:\\dir\\ndp\\pp.txt"; will return pp.txt.? If file has no + * path infomation, then just return input fileName. * */ public static String getFileName(String fileName) { @@ -409,7 +438,7 @@ * Get file directory info. * * @param fileName - * with path info. + * with path info. * @return return only path info with the given fileName */ public static String getFileDirectory(String fileName) { @@ -431,11 +460,12 @@ } /** - * Merge two input parameter into full path and adjust File.separator to OS default separator as well. + * Merge two input parameter into full path and adjust File.separator to OS + * default separator as well. * * @param path * @param file - * could be file name,or sub directory path. + * could be file name,or sub directory path. * @return */ public static String getFullPath(String path, String file) { @@ -469,8 +499,8 @@ } /** - * get file extension name from a String, such as from "textabc.doc", return "doc" fileName also can contain - * directory infomation. + * get file extension name from a String, such as from "textabc.doc", return + * "doc" fileName also can contain directory infomation. */ public static String getFileExtension(String fileName) { if (fileName == null) { @@ -486,8 +516,8 @@ } /** - * Check whether file is executable according to its extenstion and executable extension name list from LAMS - * configuaration. + * Check whether file is executable according to its extenstion and + * executable extension name list from LAMS configuaration. * * @param filename * @return @@ -515,11 +545,14 @@ } /** - * Clean up any old directories in the java tmp directory, where the directory name starts with lamszip_ or lamstmp_ - * and is days old or older. This has the potential to be a heavy call - it has to do complete directory - * listing and then recursively delete the files and directories as needed. + * Clean up any old directories in the java tmp directory, where the + * directory name starts with lamszip_ or lamstmp_ and is days old + * or older. This has the potential to be a heavy call - it has to do + * complete directory listing and then recursively delete the files and + * directories as needed. * - * Note: this method has not been tested as it is rather hard to write a junit test for! + * Note: this method has not been tested as it is rather hard to write a + * junit test for! * * @param directories * @return number of directories deleted @@ -544,10 +577,11 @@ * List files in temp directory older than numDays. * * @param numDays - * Number of days old that the directory should be to be deleted. Must be greater than 0 + * Number of days old that the directory should be to be deleted. + * Must be greater than 0 * @return array of files older than input date * @throws FileUtilException - * if numDays <= 0 + * if numDays <= 0 */ public static File[] getOldTempFiles(int numDays) throws FileUtilException { // Contract checking @@ -618,8 +652,9 @@ } /** - * Encode a filename in such a way that the UTF-8 characters won't be munged during the download by a browser. Need - * the request to work out the user's browser type + * Encode a filename in such a way that the UTF-8 characters won't be munged + * during the download by a browser. Need the request to work out the user's + * browser type * * @return encoded filename * @throws UnsupportedEncodingException @@ -663,13 +698,14 @@ } /** - * Call xstream to get the POJOs from the XML file. To make it backwardly compatible we catch any exceptions due to - * added fields, remove the field using the ToolContentVersionFilter functionality and try to reparse. We can't - * nominate the problem fields in advance as we are making XML created by newer versions of LAMS compatible with an - * older version. + * Call xstream to get the POJOs from the XML file. To make it backwardly + * compatible we catch any exceptions due to added fields, remove the field + * using the ToolContentVersionFilter functionality and try to reparse. We + * can't nominate the problem fields in advance as we are making XML created + * by newer versions of LAMS compatible with an older version. * - * This logic depends on the exception message containing the text. When we upgrade xstream, we must check that this - * message doesn't change. + * This logic depends on the exception message containing the text. When we + * upgrade xstream, we must check that this message doesn't change. * *
      * 	com.thoughtworks.xstream.converters.ConversionException: unknownField : unknownField
@@ -787,21 +823,24 @@
      * Exports data in MS Excel (.xls) format.
      * 
      * @param out
-     *                output stream to which the file written; usually taken from HTTP response; it is not closed
-     *                afterwards
+     *            output stream to which the file written; usually taken from
+     *            HTTP response; it is not closed afterwards
      * @param sheetName
-     *                name of first sheet in Excel workbook; data will be stored in this sheet
+     *            name of first sheet in Excel workbook; data will be stored in
+     *            this sheet
      * @param title
-     *                title printed in the first (0,0) cell
+     *            title printed in the first (0,0) cell
      * @param dateHeader
-     *                text describing current date; if NULL then no date is printed; if not
-     *                NULL then text is written out along with current date in the cell; the date is
-     *                formatted according to {@link #EXPORT_TO_SPREADSHEET_TITLE_DATE_FORMAT}
+     *            text describing current date; if NULL then no
+     *            date is printed; if not NULL then text is written
+     *            out along with current date in the cell; the date is formatted
+     *            according to {@link #EXPORT_TO_SPREADSHEET_TITLE_DATE_FORMAT}
      * @param columnNames
-     *                name of the columns that describe data parameter
+     *            name of the columns that describe data parameter
      * @param data
-     *                array of data to print out; first index of array describes a row, second a column; dates are
-     *                formatted according to {@link #EXPORT_TO_SPREADSHEET_CELL_DATE_FORMAT}
+     *            array of data to print out; first index of array describes a
+     *            row, second a column; dates are formatted according to
+     *            {@link #EXPORT_TO_SPREADSHEET_CELL_DATE_FORMAT}
      * @throws IOException
      * @throws JXLException
      */
@@ -861,22 +900,25 @@
     }
 
     /**
-     * Exports data in CSV format. It uses UTF-8 character set and semicolon as separator.
+     * Exports data in CSV format. It uses UTF-8 character set and semicolon as
+     * separator.
      * 
      * @param out
-     *                output stream to which the file written; usually taken from HTTP response; it is not closed
-     *                afterwards
+     *            output stream to which the file written; usually taken from
+     *            HTTP response; it is not closed afterwards
      * @param title
-     *                title printed in the first (0,0) cell
+     *            title printed in the first (0,0) cell
      * @param dateHeader
-     *                text describing current date; if NULL then no date is printed; if not
-     *                NULL then text is written out along with current date in the cell; the date is
-     *                formatted according to {@link #EXPORT_TO_SPREADSHEET_TITLE_DATE_FORMAT}
+     *            text describing current date; if NULL then no
+     *            date is printed; if not NULL then text is written
+     *            out along with current date in the cell; the date is formatted
+     *            according to {@link #EXPORT_TO_SPREADSHEET_TITLE_DATE_FORMAT}
      * @param columnNames
-     *                name of the columns that describe data parameter
+     *            name of the columns that describe data parameter
      * @param data
-     *                array of data to print out; first index of array describes a row, second a column; dates are
-     *                formatted according to {@link #EXPORT_TO_SPREADSHEET_CELL_DATE_FORMAT}
+     *            array of data to print out; first index of array describes a
+     *            row, second a column; dates are formatted according to
+     *            {@link #EXPORT_TO_SPREADSHEET_CELL_DATE_FORMAT}
      * @throws IOException
      */
     public static void exportToolToCSV(OutputStream out, String title, String dateHeader, String[] columnNames,