Package omero.util
Class TempFileManager
- java.lang.Object
-
- omero.util.TempFileManager
-
public class TempFileManager extends java.lang.Object
Creates temporary files and folders and makes a best effort to remove them on exit (or sooner). Typically only a single instance of this class will exist (held in a private static field).- Since:
- 4.1
-
-
Constructor Summary
Constructors Constructor Description TempFileManager()
Default constructor, passes "omero" toTempFileManager(String)
TempFileManager(java.lang.String prefix)
Initializes aTempFileManager
instance with the user's temporary directory containing the given prefix value.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
access(java.io.File dir)
Returns true if the current user can write to the give directory.protected void
cleanTempDir()
Deletes the top-level temporary directory.protected void
cleanup()
Releases the lock and deletes the top-level temporary directory.protected void
cleanUserDir()
Attempts to delete all directories under self.userdir other than the one owned by this process.protected boolean
create(java.io.File dir)
If the given directory doesn't exist, creates it and returns true.static java.io.File
create_path()
CallscreatePath(String, String, boolean)
with defaults of "omero", ".tmp", and false.static java.io.File
create_path(java.lang.String prefix)
CallscreatePath(String, String, boolean)
with defaults of ".tmp", and false.static java.io.File
create_path(java.lang.String prefix, java.lang.String suffix)
CallscreatePath(String, String, boolean)
with ".tmp", and false arguments.static java.io.File
create_path(java.lang.String prefix, java.lang.String suffix, boolean folder)
java.io.File
createPath(java.lang.String prefix, java.lang.String suffix, boolean folder)
UsesFile.createTempFile(String, String, File)
to create temporary files and folders under the top-level temporary directory.static java.io.File
createTempFile(java.lang.String prefix, java.lang.String suffix)
EmulatesFile.createTempFile(String, String)
by callingcreate_path(String, String)
.static void
gettempdir()
CallsgetTempDir()
.java.io.File
getTempDir()
Returns the directory under which all temporary files and folders will be created.static void
main(java.lang.String[] _args)
Command-line interface to the globalTempFileManager
instance.protected java.lang.String
pid()
Returns some representation of the current process's idstatic void
remove_path(java.io.File file)
CallsremovePath(File)
.void
removePath(java.io.File file)
If the given file is under the top-level temporary directory then it is deleted whether file or folder.protected java.io.File
tmpdir()
Returns a platform-specific user-writable temporary directory.protected java.lang.String
username()
Returns the current OS-user's name.
-
-
-
Constructor Detail
-
TempFileManager
public TempFileManager()
Default constructor, passes "omero" toTempFileManager(String)
-
TempFileManager
public TempFileManager(java.lang.String prefix)
Initializes aTempFileManager
instance with the user's temporary directory containing the given prefix value. Also adds ashutdown hook
to callcleanup()
on exit.- Parameters:
prefix
- the prefix for the user's temporary directory
-
-
Method Detail
-
cleanup
protected void cleanup() throws java.io.IOException
Releases the lock and deletes the top-level temporary directory. The lock is released first since on some platforms like Windows the lock file cannot be deleted even by the owner of the lock.- Throws:
java.io.IOException
-
tmpdir
protected java.io.File tmpdir()
Returns a platform-specific user-writable temporary directory. First, the value of "OMERO_TEMPDIR" is attempted (if available), then user's home ("user.home") directory, then the global temp director ("java.io.tmpdir"). Typical errors for any of the possible temp locations are:- non-existence
- inability to lock
- See Also:
- ticket:1653
-
username
protected java.lang.String username()
Returns the current OS-user's name.
-
pid
protected java.lang.String pid()
Returns some representation of the current process's id
-
access
protected boolean access(java.io.File dir)
Returns true if the current user can write to the give directory.
-
create
protected boolean create(java.io.File dir)
If the given directory doesn't exist, creates it and returns true. Otherwise false. Note: Java doesn't allow setting the mode for the directory but it is intended to be 0700.
-
getTempDir
public java.io.File getTempDir()
Returns the directory under which all temporary files and folders will be created.
-
createPath
public java.io.File createPath(java.lang.String prefix, java.lang.String suffix, boolean folder) throws java.io.IOException
UsesFile.createTempFile(String, String, File)
to create temporary files and folders under the top-level temporary directory. For folders, first a temporary file is created, then deleted, and finally a directory produced.- Throws:
java.io.IOException
-
removePath
public void removePath(java.io.File file) throws java.io.IOException
If the given file is under the top-level temporary directory then it is deleted whether file or folder. Otherwise aRuntimeException
is thrown.- Throws:
java.io.IOException
-
cleanTempDir
protected void cleanTempDir() throws java.io.IOException
Deletes the top-level temporary directory.- Throws:
java.io.IOException
-
cleanUserDir
protected void cleanUserDir() throws java.io.IOException
Attempts to delete all directories under self.userdir other than the one owned by this process. If a directory is locked, it is skipped.- Throws:
java.io.IOException
-
createTempFile
public static java.io.File createTempFile(java.lang.String prefix, java.lang.String suffix) throws java.io.IOException
EmulatesFile.createTempFile(String, String)
by callingcreate_path(String, String)
.- Throws:
java.io.IOException
-
create_path
public static java.io.File create_path() throws java.io.IOException
CallscreatePath(String, String, boolean)
with defaults of "omero", ".tmp", and false.- Throws:
java.io.IOException
-
create_path
public static java.io.File create_path(java.lang.String prefix) throws java.io.IOException
CallscreatePath(String, String, boolean)
with defaults of ".tmp", and false.- Throws:
java.io.IOException
-
create_path
public static java.io.File create_path(java.lang.String prefix, java.lang.String suffix) throws java.io.IOException
CallscreatePath(String, String, boolean)
with ".tmp", and false arguments.- Throws:
java.io.IOException
-
create_path
public static java.io.File create_path(java.lang.String prefix, java.lang.String suffix, boolean folder) throws java.io.IOException
- Throws:
java.io.IOException
-
remove_path
public static void remove_path(java.io.File file) throws java.io.IOException
CallsremovePath(File)
.- Throws:
java.io.IOException
-
gettempdir
public static void gettempdir()
CallsgetTempDir()
.
-
main
public static void main(java.lang.String[] _args) throws java.io.IOException
Command-line interface to the globalTempFileManager
instance. Valid arguments: "--debug", "clean", "dir", and for testing, "lock"- Throws:
java.io.IOException
-
-