Package omero.util
Class TempFileManager
- java.lang.Object
-
- omero.util.TempFileManager
-
public class TempFileManager extends java.lang.ObjectCreates 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 aTempFileManagerinstance 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 booleanaccess(java.io.File dir)Returns true if the current user can write to the give directory.protected voidcleanTempDir()Deletes the top-level temporary directory.protected voidcleanup()Releases the lock and deletes the top-level temporary directory.protected voidcleanUserDir()Attempts to delete all directories under self.userdir other than the one owned by this process.protected booleancreate(java.io.File dir)If the given directory doesn't exist, creates it and returns true.static java.io.Filecreate_path()CallscreatePath(String, String, boolean)with defaults of "omero", ".tmp", and false.static java.io.Filecreate_path(java.lang.String prefix)CallscreatePath(String, String, boolean)with defaults of ".tmp", and false.static java.io.Filecreate_path(java.lang.String prefix, java.lang.String suffix)CallscreatePath(String, String, boolean)with ".tmp", and false arguments.static java.io.Filecreate_path(java.lang.String prefix, java.lang.String suffix, boolean folder)java.io.FilecreatePath(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.FilecreateTempFile(java.lang.String prefix, java.lang.String suffix)EmulatesFile.createTempFile(String, String)by callingcreate_path(String, String).static voidgettempdir()CallsgetTempDir().java.io.FilegetTempDir()Returns the directory under which all temporary files and folders will be created.static voidmain(java.lang.String[] _args)Command-line interface to the globalTempFileManagerinstance.protected java.lang.Stringpid()Returns some representation of the current process's idstatic voidremove_path(java.io.File file)CallsremovePath(File).voidremovePath(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.Filetmpdir()Returns a platform-specific user-writable temporary directory.protected java.lang.Stringusername()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 aTempFileManagerinstance with the user's temporary directory containing the given prefix value. Also adds ashutdown hookto callcleanup()on exit.- Parameters:
prefix- the prefix for the user's temporary directory
-
-
Method Detail
-
cleanup
protected void cleanup() throws java.io.IOExceptionReleases 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.IOExceptionUsesFile.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.IOExceptionIf the given file is under the top-level temporary directory then it is deleted whether file or folder. Otherwise aRuntimeExceptionis thrown.- Throws:
java.io.IOException
-
cleanTempDir
protected void cleanTempDir() throws java.io.IOExceptionDeletes the top-level temporary directory.- Throws:
java.io.IOException
-
cleanUserDir
protected void cleanUserDir() throws java.io.IOExceptionAttempts 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.IOExceptionEmulatesFile.createTempFile(String, String)by callingcreate_path(String, String).- Throws:
java.io.IOException
-
create_path
public static java.io.File create_path() throws java.io.IOExceptionCallscreatePath(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.IOExceptionCallscreatePath(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.IOExceptionCallscreatePath(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.IOExceptionCallsremovePath(File).- Throws:
java.io.IOException
-
gettempdir
public static void gettempdir()
CallsgetTempDir().
-
main
public static void main(java.lang.String[] _args) throws java.io.IOExceptionCommand-line interface to the globalTempFileManagerinstance. Valid arguments: "--debug", "clean", "dir", and for testing, "lock"- Throws:
java.io.IOException
-
-