Skip to content

Commit b85271a

Browse files
committed
Made the FileCleaningTracker serializable.
This is required by commons-fileupload, because the DiskFileItem's may be part of the HTTP session while still carrying a reference to the tracker. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@518770 13f79535-47bb-0310-9956-ffa450edef68
1 parent 431c428 commit b85271a

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

src/java/org/apache/commons/io/FileCleaningTracker.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.apache.commons.io;
1818

1919
import java.io.File;
20+
import java.io.ObjectStreamException;
21+
import java.io.Serializable;
2022
import java.lang.ref.PhantomReference;
2123
import java.lang.ref.ReferenceQueue;
2224
import java.util.Collection;
@@ -40,23 +42,28 @@
4042
* @author Martin Cooper
4143
* @version $Id: FileCleaner.java 490987 2006-12-29 12:11:48Z scolebourne $
4244
*/
43-
public class FileCleaningTracker {
45+
public class FileCleaningTracker implements Serializable {
46+
/**
47+
* UID for serializing instances of this class.
48+
*/
49+
private static final long serialVersionUID = -8153509976492548871L;
50+
4451
/**
4552
* Queue of <code>Tracker</code> instances being watched.
4653
*/
47-
ReferenceQueue /* Tracker */ q = new ReferenceQueue();
54+
transient ReferenceQueue /* Tracker */ q = new ReferenceQueue();
4855
/**
4956
* Collection of <code>Tracker</code> instances in existence.
5057
*/
51-
final Collection /* Tracker */ trackers = new Vector(); // synchronized
58+
final transient Collection /* Tracker */ trackers = new Vector(); // synchronized
5259
/**
5360
* Whether to terminate the thread when the tracking is complete.
5461
*/
55-
volatile boolean exitWhenFinished = false;
62+
transient volatile boolean exitWhenFinished = false;
5663
/**
5764
* The thread that will clean up registered files.
5865
*/
59-
Thread reaper;
66+
transient Thread reaper;
6067

6168
//-----------------------------------------------------------------------
6269
/**
@@ -255,4 +262,14 @@ public boolean delete() {
255262
}
256263
}
257264

265+
/**
266+
* This method is called when an instance is deserialized.
267+
* It replaces the deserialized instance with a new, fresh
268+
* instance.
269+
* @return A new instance, which hasn't been in use so far.
270+
* @throws ObjectStreamException Not actually thrown.
271+
*/
272+
private Object readResolve() throws ObjectStreamException {
273+
return new FileCleaningTracker();
274+
}
258275
}

src/test/org/apache/commons/io/FileUtilsCleanDirectoryTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public void testDeletesNested() throws Exception {
103103
}
104104

105105
public void testThrowsOnNullList() throws Exception {
106-
if (!chmod(top, 0, false)) {
106+
if (System.getProperty("os.name").startsWith("Win") || !chmod(top, 0, false)) {
107107
// test wont work if we can't restrict permissions on the
108108
// directory, so skip it.
109109
return;
@@ -122,7 +122,7 @@ public void testThrowsOnCannotDeleteFile() throws Exception {
122122
final File file = new File(top, "restricted");
123123
FileUtils.touch(file);
124124

125-
if (!chmod(top, 500, false)) {
125+
if (System.getProperty("os.name").startsWith("Win") || !chmod(top, 500, false)) {
126126
// test wont work if we can't restrict permissions on the
127127
// directory, so skip it.
128128
return;

0 commit comments

Comments
 (0)