2008-03-19 22:14:38 +00:00
|
|
|
|
|
|
|
package net.sourceforge.tuned;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2008-03-25 21:03:32 +00:00
|
|
|
import java.util.ArrayList;
|
2008-03-19 22:14:38 +00:00
|
|
|
import java.util.HashMap;
|
2008-03-25 21:03:32 +00:00
|
|
|
import java.util.List;
|
2008-03-19 22:14:38 +00:00
|
|
|
import java.util.Map;
|
2008-10-22 22:07:02 +00:00
|
|
|
import java.util.UUID;
|
2008-03-19 22:14:38 +00:00
|
|
|
|
|
|
|
|
2008-10-10 19:20:37 +00:00
|
|
|
public final class TemporaryFolder {
|
2008-03-19 22:14:38 +00:00
|
|
|
|
|
|
|
private static final String tmpdir = System.getProperty("java.io.tmpdir");
|
|
|
|
|
|
|
|
private static final Map<String, TemporaryFolder> folders = new HashMap<String, TemporaryFolder>();
|
|
|
|
|
|
|
|
|
2008-10-22 22:07:02 +00:00
|
|
|
/**
|
|
|
|
* Get a {@link TemporaryFolder} instance for a given name. The actual directory will be
|
2008-10-29 22:54:16 +00:00
|
|
|
* created lazily (e.g. when a file is created). The name of the directory will start with
|
|
|
|
* the given name (lower-case) and contain a unique id, so multiple application instances
|
|
|
|
* may run at the same time without the risk of interference.
|
2008-10-22 22:07:02 +00:00
|
|
|
*
|
|
|
|
* @param name case-insensitive name of a temporary folder (e.g. application name)
|
|
|
|
* @return temporary folder for this name
|
|
|
|
*/
|
2008-03-19 22:14:38 +00:00
|
|
|
public static TemporaryFolder getFolder(String name) {
|
2008-10-29 22:54:16 +00:00
|
|
|
// make name case-insensitive
|
|
|
|
name = name.toLowerCase();
|
|
|
|
|
2008-03-19 22:14:38 +00:00
|
|
|
synchronized (folders) {
|
2008-10-29 22:54:16 +00:00
|
|
|
TemporaryFolder folder = folders.get(name);
|
2008-03-19 22:14:38 +00:00
|
|
|
|
|
|
|
if (folder == null) {
|
2008-10-22 22:07:02 +00:00
|
|
|
folder = new TemporaryFolder(new File(tmpdir, String.format("%s [%s]", name, UUID.randomUUID())));
|
2008-10-29 22:54:16 +00:00
|
|
|
folders.put(name, folder);
|
2008-03-19 22:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return folder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete all temporary folders on shutdown
|
|
|
|
*/
|
|
|
|
static {
|
2008-03-22 21:12:34 +00:00
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread("TemporaryFolder Cleanup") {
|
2008-03-19 22:14:38 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
synchronized (folders) {
|
|
|
|
for (TemporaryFolder folder : folders.values()) {
|
|
|
|
folder.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private final File root;
|
|
|
|
|
|
|
|
|
|
|
|
private TemporaryFolder(File root) {
|
|
|
|
this.root = root;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
/**
|
2008-10-22 22:07:02 +00:00
|
|
|
* Create an empty file in this temporary folder.
|
2008-06-02 19:12:28 +00:00
|
|
|
*
|
|
|
|
* @param name name of the file
|
|
|
|
* @return newly created file
|
|
|
|
* @throws IOException if an I/O error occurred
|
|
|
|
*/
|
2008-03-19 22:14:38 +00:00
|
|
|
public File createFile(String name) throws IOException {
|
2008-03-20 20:11:08 +00:00
|
|
|
|
2008-10-22 22:07:02 +00:00
|
|
|
// if the directory does not exist it will be created
|
2008-07-30 22:37:01 +00:00
|
|
|
File file = new File(getFolder(), name);
|
2008-03-19 22:14:38 +00:00
|
|
|
file.createNewFile();
|
2008-03-20 20:11:08 +00:00
|
|
|
|
2008-03-19 22:14:38 +00:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
/**
|
|
|
|
* Creates an empty file in this temporary folder, using the given prefix and suffix to
|
|
|
|
* generate its name.
|
|
|
|
*
|
|
|
|
* @param prefix The prefix string to be used in generating the file's name; must be at
|
|
|
|
* least three characters long
|
|
|
|
* @param suffix The suffix string to be used in generating the file's name; may be null,
|
|
|
|
* in which case the suffix ".tmp" will be used
|
|
|
|
* @return An abstract pathname denoting a newly-created empty file
|
|
|
|
* @throws IOException If a file could not be created
|
|
|
|
* @see File#createTempFile(String, String)
|
|
|
|
*/
|
|
|
|
public File createFile(String prefix, String suffix) throws IOException {
|
2008-07-30 22:37:01 +00:00
|
|
|
return File.createTempFile(prefix, suffix, getFolder());
|
2008-06-02 19:12:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean deleteFile(String name) {
|
2008-07-30 22:37:01 +00:00
|
|
|
return new File(getFolder(), name).delete();
|
2008-03-19 22:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-06-02 19:12:28 +00:00
|
|
|
/**
|
2008-10-22 22:07:02 +00:00
|
|
|
* Retrieve the {@link File} object for this {@link TemporaryFolder}. The actual directory
|
|
|
|
* for the {@link TemporaryFolder} instance will be created by this method.
|
2008-06-02 19:12:28 +00:00
|
|
|
*
|
|
|
|
* @return the {@link File} object for this {@link TemporaryFolder}
|
|
|
|
*/
|
2008-03-25 21:03:32 +00:00
|
|
|
public File getFolder() {
|
2008-10-22 22:07:02 +00:00
|
|
|
if (!root.exists()) {
|
2008-07-30 22:37:01 +00:00
|
|
|
root.mkdirs();
|
2008-10-22 22:07:02 +00:00
|
|
|
}
|
2008-07-30 22:37:01 +00:00
|
|
|
|
2008-03-25 21:03:32 +00:00
|
|
|
return root;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-10-22 22:35:17 +00:00
|
|
|
public TemporaryFolder subFolder(String name) {
|
2008-07-30 22:37:01 +00:00
|
|
|
return new TemporaryFolder(new File(getFolder(), name));
|
2008-03-25 21:03:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<File> list(boolean recursive) {
|
|
|
|
List<File> list = new ArrayList<File>();
|
|
|
|
|
|
|
|
list(root, list, recursive);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void list(File file, List<File> list, boolean recursive) {
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
for (File entry : file.listFiles()) {
|
|
|
|
if (entry.isDirectory()) {
|
|
|
|
if (recursive) {
|
|
|
|
list(entry, list, recursive);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
list.add(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-03-19 22:14:38 +00:00
|
|
|
public void delete() {
|
|
|
|
delete(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete files/folders recursively
|
|
|
|
*
|
|
|
|
* @param file file/folder that will be deleted
|
|
|
|
*/
|
|
|
|
private void delete(File file) {
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
for (File entry : file.listFiles()) {
|
|
|
|
delete(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|