You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2011/06/30 15:27:04 UTC

svn commit: r1141529 - in /wicket/trunk/wicket-util/src: main/java/org/apache/wicket/util/file/Files.java test/java/org/apache/wicket/util/file/FilesTest.java

Author: mgrigorov
Date: Thu Jun 30 13:27:04 2011
New Revision: 1141529

URL: http://svn.apache.org/viewvc?rev=1141529&view=rev
Log:
WICKET-3851 Remove usage of System.gc() in Wicket code


Added:
    wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/file/FilesTest.java
Modified:
    wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/Files.java

Modified: wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/Files.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/Files.java?rev=1141529&r1=1141528&r2=1141529&view=diff
==============================================================================
--- wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/Files.java (original)
+++ wicket/trunk/wicket-util/src/main/java/org/apache/wicket/util/file/Files.java Thu Jun 30 13:27:04 2011
@@ -30,6 +30,8 @@ import org.apache.wicket.util.io.Streams
 import org.apache.wicket.util.lang.Args;
 import org.apache.wicket.util.string.Strings;
 import org.apache.wicket.util.time.Time;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * File utility methods.
@@ -38,6 +40,8 @@ import org.apache.wicket.util.time.Time;
  */
 public class Files
 {
+	private static final Logger logger = LoggerFactory.getLogger(Files.class);
+
 	// protocols for urls
 	private static final String URL_FILE_PREFIX = "file:";
 	private static final String URL_LOCAL_JAR_FILE_PREFIX = "jar:file:";
@@ -99,7 +103,11 @@ public class Files
 	}
 
 	/**
-	 * Deletes a file, dealing with a particularly nasty bug on Windows.
+	 * Deletes a file.
+	 * <p>
+	 * If the file cannot be deleted for any reason then at most 10 retries are attempted with delay
+	 * of 100ms. If the file still cannot be deleted then it is scheduled to be deleted at JVM exit
+	 * time.
 	 * 
 	 * @param file
 	 *            File to delete
@@ -107,13 +115,13 @@ public class Files
 	 */
 	public static boolean remove(final java.io.File file)
 	{
-		// Delete current file
-		if (!file.delete())
+		int retries = 10;
+
+		boolean deleted = false;
+
+		while ((deleted = file.delete()) == false && retries > 0)
 		{
-			// NOTE: fix for java/win bug. see:
-			// http://forum.java.sun.com/thread.jsp?forum=4&thread=158689&tstart=
-			// 0&trange=15
-			System.gc();
+			retries--;
 			try
 			{
 				Thread.sleep(100);
@@ -121,11 +129,17 @@ public class Files
 			catch (InterruptedException ignored)
 			{
 			}
+		}
 
-			// Try one more time to delete the file
-			return file.delete();
+		if (deleted == false && logger.isWarnEnabled())
+		{
+			logger.warn(
+				"Cannot delete file '{}' for unknown reason. The file will be scheduled for deletion at JVM exit time.",
+				file);
+			file.deleteOnExit();
 		}
-		return true;
+
+		return deleted;
 	}
 
 	/**
@@ -249,16 +263,17 @@ public class Files
 			}
 		}
 	}
-	
+
 	/**
-	 * for urls that point to local files (e.g. 'file:' or 'jar:file:') this
-	 * methods returns a reference to the local file
-	 *
-	 * @param url url of the resource
+	 * for urls that point to local files (e.g. 'file:' or 'jar:file:') this methods returns a
+	 * reference to the local file
+	 * 
+	 * @param url
+	 *            url of the resource
 	 * 
 	 * @return reference to a local file if url contains one, <code>null</code> otherwise
 	 * 
-	 * @see #getLocalFileFromUrl(String) 
+	 * @see #getLocalFileFromUrl(String)
 	 */
 	public static File getLocalFileFromUrl(URL url)
 	{
@@ -266,14 +281,15 @@ public class Files
 	}
 
 	/**
-	 * for urls that point to local files (e.g. 'file:' or 'jar:file:') this
-	 * methods returns a reference to the local file
-	 *
-	 * @param url url of the resource
+	 * for urls that point to local files (e.g. 'file:' or 'jar:file:') this methods returns a
+	 * reference to the local file
+	 * 
+	 * @param url
+	 *            url of the resource
 	 * 
 	 * @return reference to a local file if url contains one, <code>null</code> otherwise
 	 * 
-	 * @see #getLocalFileFromUrl(URL) 
+	 * @see #getLocalFileFromUrl(URL)
 	 */
 	public static File getLocalFileFromUrl(String url)
 	{

Added: wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/file/FilesTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/file/FilesTest.java?rev=1141529&view=auto
==============================================================================
--- wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/file/FilesTest.java (added)
+++ wicket/trunk/wicket-util/src/test/java/org/apache/wicket/util/file/FilesTest.java Thu Jun 30 13:27:04 2011
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.util.file;
+
+import java.io.IOException;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests for {@link Files}
+ */
+public class FilesTest extends Assert
+{
+
+	/**
+	 * @throws IOException
+	 */
+	@Test
+	public void remove() throws IOException
+	{
+		java.io.File file = java.io.File.createTempFile("wicket-test--", ".tmp");
+		assertTrue("The just created file should exist!", file.exists());
+
+		boolean removed = Files.remove(file);
+		assertFalse("The just removed file should not exist!", file.exists());
+		assertTrue("Files.remove(file) should remove the file", removed);
+	}
+}