Author: jbq Date: Tue Feb 27 15:29:12 2007 New Revision: 512491 URL: http://svn.apache.org/viewvc?view=rev&rev=512491 Log: Merged with revision 497650:
better creation of the tmp/work dir and cleanup Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/MockServletContext.java Modified: incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/MockServletContext.java URL: http://svn.apache.org/viewvc/incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/MockServletContext.java?view=diff&rev=512491&r1=512490&r2=512491 ============================================================================== --- incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/MockServletContext.java (original) +++ incubator/wicket/branches/wicket-1.x/wicket/src/main/java/wicket/protocol/http/MockServletContext.java Tue Feb 27 15:29:12 2007 @@ -27,6 +27,7 @@ import java.util.Enumeration; import java.util.HashSet; import java.util.Set; +import java.util.UUID; import javax.servlet.RequestDispatcher; import javax.servlet.Servlet; @@ -106,6 +107,58 @@ mimeTypes.put("gif", "image/gif"); mimeTypes.put("jpg", "image/jpeg"); mimeTypes.put("png", "image/png"); + + // Set ServletContext temp dir + setAttribute("javax.servlet.context.tempdir", createTempDir("wicket" + UUID.randomUUID())); + } + + /** + * Creates a temp directory + * + * @param name + * @return File + */ + public static File createTempDir(String name) + { + String tempDir = System.getProperty("java.io.tmpdir"); + if (tempDir == null || tempDir.trim().length() == 0) + { + throw new RuntimeException( + "Could not create a temporary directory. System's [[java.io.tmpdir]] property is not " + + "properly set. Current value is [[" + tempDir + + "]]. Set via [[java -Djava.io.tmpdir=/var/tmp]]"); + } + + if (!tempDir.endsWith(File.separator)) + { + tempDir += File.separator; + } + + tempDir += name; + + File dir = new File(tempDir); + + int counter = 0; + while (dir.exists()) + { + dir = new File(dir.getAbsolutePath() + counter); + counter++; + if (counter > 100) + { + throw new RuntimeException("Could not create temporary directory [[" + + dir.getAbsolutePath() + "]] after attempting 100 tries"); + } + } + + if (!dir.mkdirs()) + { + throw new RuntimeException("Could not create path for tempdir [[" + + dir.getAbsolutePath() + "]]"); + } + + dir.deleteOnExit(); + + return dir; } /** @@ -262,7 +315,7 @@ { name = name.substring(1); } - + File f = new File(webappRoot, name); if (!f.exists()) { @@ -321,7 +374,7 @@ { name = name.substring(1); } - + File f = new File(webappRoot, name); if (!f.exists()) { @@ -353,7 +406,7 @@ { name = name.substring(1); } - + File f = new File(webappRoot, name); if (!f.exists()) { @@ -555,4 +608,4 @@ { attributes.put(name, o); } -} \ No newline at end of file +}