Skip to content

Commit 431c428

Browse files
committed
Created the FileCleaningTracker, basically a non-static
version of the FileCleaner, which can be controlled by the user. PR: IO-116 git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@518041 13f79535-47bb-0310-9956-ffa450edef68
1 parent aa1ba1e commit 431c428

7 files changed

Lines changed: 724 additions & 369 deletions

File tree

pom.xml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@
131131
<role>Java Developer</role>
132132
</roles>
133133
</developer>
134-
134+
<developer>
135+
<name>Jochen Wiedmann</name>
136+
<id>jochen</id>
137+
<email>jochen.wiedmann@gmail.com</email>
138+
</developer>
135139
</developers>
136140

137141
<contributors>
@@ -220,4 +224,23 @@
220224
</plugins>
221225
</build>
222226

227+
<reporting>
228+
<plugins>
229+
<plugin>
230+
<groupId>org.apache.maven.plugins</groupId>
231+
<artifactId>maven-changes-plugin</artifactId>
232+
<configuration>
233+
<issueLinkTemplate>%URL%/../%ISSUE%</issueLinkTemplate>
234+
</configuration>
235+
<reportSets>
236+
<reportSet>
237+
<reports>
238+
<report>changes-report</report>
239+
<report>jira-report</report>
240+
</reports>
241+
</reportSet>
242+
</reportSets>
243+
</plugin>
244+
</plugins>
245+
</reporting>
223246
</project>

src/changes/changes.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
<!--
20+
This file is used by the maven-changes-plugin to generate the release notes.
21+
Useful ways of finding items to add to this file are:
22+
23+
1. Add items when you fix a bug or add a feature (this makes the
24+
release process easy :-).
25+
26+
2. Do a Jira search for tickets closed since the previous release.
27+
28+
3. Use the report generated by the maven-changelog-plugin to see all
29+
CVS commits. Set the project.properties' maven.changelog.range
30+
property to the number of days since the last release.
31+
32+
33+
The <action> type attribute can be add,update,fix,remove.
34+
-->
35+
36+
<document>
37+
<properties>
38+
<title>Release Notes</title>
39+
</properties>
40+
41+
<body>
42+
<release version="1.4" date="Not yet released">
43+
<action dev="jochen" type="fix" issue="IO-116">
44+
Created the FileCleaningTracker, basically a non-static
45+
version of the FileCleaner, which can be controlled by
46+
the user.
47+
</action>
48+
</release>
49+
</body>
50+
</document>

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

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

1919
import java.io.File;
20-
import java.lang.ref.PhantomReference;
21-
import java.lang.ref.ReferenceQueue;
22-
import java.util.Collection;
23-
import java.util.Vector;
2420

2521
/**
2622
* Keeps track of files awaiting deletion, and deletes them when an associated
@@ -39,25 +35,13 @@
3935
* @author Noel Bergman
4036
* @author Martin Cooper
4137
* @version $Id$
38+
* @deprecated Use {@link FileCleaningTracker}
4239
*/
4340
public class FileCleaner {
44-
45-
/**
46-
* Queue of <code>Tracker</code> instances being watched.
47-
*/
48-
static ReferenceQueue /* Tracker */ q = new ReferenceQueue();
49-
/**
50-
* Collection of <code>Tracker</code> instances in existence.
51-
*/
52-
static Collection /* Tracker */ trackers = new Vector(); // synchronized
53-
/**
54-
* Whether to terminate the thread when the tracking is complete.
55-
*/
56-
static volatile boolean exitWhenFinished = false;
5741
/**
58-
* The thread that will clean up registered files.
42+
* The instance to use for the deprecated, static methods.
5943
*/
60-
static Thread reaper;
44+
static final FileCleaningTracker theInstance = new FileCleaningTracker();
6145

6246
//-----------------------------------------------------------------------
6347
/**
@@ -68,9 +52,10 @@ public class FileCleaner {
6852
* @param file the file to be tracked, not null
6953
* @param marker the marker object used to track the file, not null
7054
* @throws NullPointerException if the file is null
55+
* @deprecated Use {@link FileCleaningTracker#track(File, Object)}.
7156
*/
7257
public static void track(File file, Object marker) {
73-
track(file, marker, (FileDeleteStrategy) null);
58+
theInstance.track(file, marker);
7459
}
7560

7661
/**
@@ -82,12 +67,10 @@ public static void track(File file, Object marker) {
8267
* @param marker the marker object used to track the file, not null
8368
* @param deleteStrategy the strategy to delete the file, null means normal
8469
* @throws NullPointerException if the file is null
70+
* @deprecated Use {@link FileCleaningTracker#track(File, Object, FileDeleteStrategy)}.
8571
*/
8672
public static void track(File file, Object marker, FileDeleteStrategy deleteStrategy) {
87-
if (file == null) {
88-
throw new NullPointerException("The file must not be null");
89-
}
90-
addTracker(file.getPath(), marker, deleteStrategy);
73+
theInstance.track(file, marker, deleteStrategy);
9174
}
9275

9376
/**
@@ -98,9 +81,10 @@ public static void track(File file, Object marker, FileDeleteStrategy deleteStra
9881
* @param path the full path to the file to be tracked, not null
9982
* @param marker the marker object used to track the file, not null
10083
* @throws NullPointerException if the path is null
84+
* @deprecated Use {@link FileCleaningTracker#track(String, Object)}.
10185
*/
10286
public static void track(String path, Object marker) {
103-
track(path, marker, (FileDeleteStrategy) null);
87+
theInstance.track(path, marker);
10488
}
10589

10690
/**
@@ -112,31 +96,10 @@ public static void track(String path, Object marker) {
11296
* @param marker the marker object used to track the file, not null
11397
* @param deleteStrategy the strategy to delete the file, null means normal
11498
* @throws NullPointerException if the path is null
99+
* @deprecated Use {@link FileCleaningTracker#track(String, Object, FileDeleteStrategy)}.
115100
*/
116101
public static void track(String path, Object marker, FileDeleteStrategy deleteStrategy) {
117-
if (path == null) {
118-
throw new NullPointerException("The path must not be null");
119-
}
120-
addTracker(path, marker, deleteStrategy);
121-
}
122-
123-
/**
124-
* Adds a tracker to the list of trackers.
125-
*
126-
* @param path the full path to the file to be tracked, not null
127-
* @param marker the marker object used to track the file, not null
128-
* @param deleteStrategy the strategy to delete the file, null means normal
129-
*/
130-
private static synchronized void addTracker(String path, Object marker, FileDeleteStrategy deleteStrategy) {
131-
// synchronized block protects reaper
132-
if (exitWhenFinished) {
133-
throw new IllegalStateException("No new trackers can be added once exitWhenFinished() is called");
134-
}
135-
if (reaper == null) {
136-
reaper = new Reaper();
137-
reaper.start();
138-
}
139-
trackers.add(new Tracker(path, deleteStrategy, marker, q));
102+
theInstance.track(path, marker, deleteStrategy);
140103
}
141104

142105
//-----------------------------------------------------------------------
@@ -145,9 +108,10 @@ private static synchronized void addTracker(String path, Object marker, FileDele
145108
* awaiting deletion.
146109
*
147110
* @return the number of files being tracked
111+
* @deprecated Use {@link FileCleaningTracker#getTrackCount()}.
148112
*/
149113
public static int getTrackCount() {
150-
return trackers.size();
114+
return theInstance.getTrackCount();
151115
}
152116

153117
/**
@@ -170,90 +134,19 @@ public static int getTrackCount() {
170134
* This method allows the thread to be terminated. Simply call this method
171135
* in the resource cleanup code, such as {@link javax.servlet.ServletContextListener#contextDestroyed}.
172136
* One called, no new objects can be tracked by the file cleaner.
137+
* @deprecated Use {@link FileCleaningTracker#exitWhenFinished()}.
173138
*/
174139
public static synchronized void exitWhenFinished() {
175-
// synchronized block protects reaper
176-
exitWhenFinished = true;
177-
if (reaper != null) {
178-
synchronized (reaper) {
179-
reaper.interrupt();
180-
}
181-
}
182-
}
183-
184-
//-----------------------------------------------------------------------
185-
/**
186-
* The reaper thread.
187-
*/
188-
static final class Reaper extends Thread {
189-
/** Construct a new Reaper */
190-
Reaper() {
191-
super("File Reaper");
192-
setPriority(Thread.MAX_PRIORITY);
193-
setDaemon(true);
194-
}
195-
196-
/**
197-
* Run the reaper thread that will delete files as their associated
198-
* marker objects are reclaimed by the garbage collector.
199-
*/
200-
public void run() {
201-
// thread exits when exitWhenFinished is true and there are no more tracked objects
202-
while (exitWhenFinished == false || trackers.size() > 0) {
203-
Tracker tracker = null;
204-
try {
205-
// Wait for a tracker to remove.
206-
tracker = (Tracker) q.remove();
207-
} catch (Exception e) {
208-
continue;
209-
}
210-
if (tracker != null) {
211-
tracker.delete();
212-
tracker.clear();
213-
trackers.remove(tracker);
214-
}
215-
}
216-
}
140+
theInstance.exitWhenFinished();
217141
}
218142

219-
//-----------------------------------------------------------------------
220143
/**
221-
* Inner class which acts as the reference for a file pending deletion.
144+
* Returns the singleton instance, which is used by the deprecated, static methods.
145+
* This is mainly useful for code, which wants to support the new
146+
* {@link FileCleaningTracker} class while maintain compatibility with the
147+
* deprecated {@link FileCleaner}.
222148
*/
223-
static final class Tracker extends PhantomReference {
224-
225-
/**
226-
* The full path to the file being tracked.
227-
*/
228-
private final String path;
229-
/**
230-
* The strategy for deleting files.
231-
*/
232-
private final FileDeleteStrategy deleteStrategy;
233-
234-
/**
235-
* Constructs an instance of this class from the supplied parameters.
236-
*
237-
* @param path the full path to the file to be tracked, not null
238-
* @param deleteStrategy the strategy to delete the file, null means normal
239-
* @param marker the marker object used to track the file, not null
240-
* @param queue the queue on to which the tracker will be pushed, not null
241-
*/
242-
Tracker(String path, FileDeleteStrategy deleteStrategy, Object marker, ReferenceQueue queue) {
243-
super(marker, queue);
244-
this.path = path;
245-
this.deleteStrategy = (deleteStrategy == null ? FileDeleteStrategy.NORMAL : deleteStrategy);
246-
}
247-
248-
/**
249-
* Deletes the file associated with this tracker instance.
250-
*
251-
* @return <code>true</code> if the file was deleted successfully;
252-
* <code>false</code> otherwise.
253-
*/
254-
public boolean delete() {
255-
return deleteStrategy.deleteQuietly(new File(path));
256-
}
149+
public static FileCleaningTracker getInstance() {
150+
return theInstance;
257151
}
258-
259152
}

0 commit comments

Comments
 (0)