omero.util
Class TempFileManager

java.lang.Object
  extended by omero.util.TempFileManager

public class TempFileManager
extends 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 (static manager constant)

Since:
4.1
Author:
Josh Moore, josh at glencoesoftware.com

Constructor Summary
TempFileManager()
          Default constructor, passes "omero" to TempFileManager(String)
TempFileManager(String prefix)
          Initializes a TempFileManager instance with a userDir containing the given prefix value.
 
Method Summary
protected  boolean access(File dir)
          Returns true if the current user can write to the give directory.
protected  void cleanTempDir()
          Deletes dir
protected  void cleanup()
          Releases lock and deletes dir.
protected  void cleanUserDir()
          Attempts to delete all directories under self.userdir other than the one owned by this process.
static File create_path()
          Calls createPath(String, String, boolean) on manager with defaults of "omero", ".tmp", and false.
static File create_path(String prefix)
          Calls createPath(String, String, boolean) on manager with defaults of ".tmp", and false.
static File create_path(String prefix, String suffix)
          Calls createPath(String, String, boolean) on manager with ".tmp", and false arguments.
static File create_path(String prefix, String suffix, boolean folder)
          Calls createPath(String, String, boolean) on manager.
protected  boolean create(File dir)
          If the given directory doesn't exist, creates it and returns true.
 File createPath(String prefix, String suffix, boolean folder)
          Uses File.createTempFile(String, String, File) to create temporary files and folders under dir.
static File createTempFile(String prefix, String suffix)
          Emulates File.createTempFile(String, String) by calling create_path(String, String).
 File getTempDir()
          Returns the directory under which all temporary files and folders will be created.
static void main(String[] _args)
          Command-line interface to the global TempFileManager instance ( #manger).
protected  String pid()
          Returns some representation of the current process's id
static void remove_path(File file)
          Calls removePath(File) on manager.
 void removePath(File file)
          If the given file is under dir, then it is deleted whether file or folder.
protected  File tmpdir()
          Returns a platform-specific user-writable temporary directory.
protected  String username()
          Returns the current OS-user's name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TempFileManager

public TempFileManager()
Default constructor, passes "omero" to TempFileManager(String)


TempFileManager

public TempFileManager(String prefix)
Initializes a TempFileManager instance with a userDir containing the given prefix value. Also adds a shutdown hook to call cleanup() on exit.

Method Detail

cleanup

protected void cleanup()
                throws IOException
Releases lock and deletes dir. 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:
IOException

tmpdir

protected 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:

See Also:
ticket:1653

username

protected String username()
Returns the current OS-user's name.


pid

protected String pid()
Returns some representation of the current process's id


access

protected boolean access(File dir)
Returns true if the current user can write to the give directory.


create

protected boolean create(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 File getTempDir()
Returns the directory under which all temporary files and folders will be created.


createPath

public File createPath(String prefix,
                       String suffix,
                       boolean folder)
                throws IOException
Uses File.createTempFile(String, String, File) to create temporary files and folders under dir. For folders, first a temporary file is created, then deleted, and finally a directory produced.

Throws:
IOException

removePath

public void removePath(File file)
                throws IOException
If the given file is under dir, then it is deleted whether file or folder. Otherwise a RuntimeException is thrown.

Throws:
IOException

cleanTempDir

protected void cleanTempDir()
                     throws IOException
Deletes dir

Throws:
IOException

cleanUserDir

protected void cleanUserDir()
                     throws 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:
IOException

createTempFile

public static File createTempFile(String prefix,
                                  String suffix)
                           throws IOException
Emulates File.createTempFile(String, String) by calling create_path(String, String).

Throws:
IOException

create_path

public static File create_path()
                        throws IOException
Calls createPath(String, String, boolean) on manager with defaults of "omero", ".tmp", and false.

Throws:
IOException

create_path

public static File create_path(String prefix)
                        throws IOException
Calls createPath(String, String, boolean) on manager with defaults of ".tmp", and false.

Throws:
IOException

create_path

public static File create_path(String prefix,
                               String suffix)
                        throws IOException
Calls createPath(String, String, boolean) on manager with ".tmp", and false arguments.

Throws:
IOException

create_path

public static File create_path(String prefix,
                               String suffix,
                               boolean folder)
                        throws IOException
Calls createPath(String, String, boolean) on manager.

Throws:
IOException

remove_path

public static void remove_path(File file)
                        throws IOException
Calls removePath(File) on manager.

Throws:
IOException

main

public static void main(String[] _args)
                 throws IOException
Command-line interface to the global TempFileManager instance ( #manger). Valid arguments: "--debug", "clean", "dir", and for testing, "lock"

Throws:
IOException


OmeroJava Api

Version: Beta-4.1.1-r5927-b91

Copyright © 2009 The University of Dundee. All Rights Reserved.