Package omero.util

Class 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
    • 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()
      Calls createPath(String, String, boolean) with defaults of "omero", ".tmp", and false.
      static java.io.File create_path​(java.lang.String prefix)
      Calls createPath(String, String, boolean) with defaults of ".tmp", and false.
      static java.io.File create_path​(java.lang.String prefix, java.lang.String suffix)
      Calls createPath(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)
      Uses File.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)
      Emulates File.createTempFile(String, String) by calling create_path(String, String).
      static void gettempdir()
      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 global TempFileManager instance.
      protected java.lang.String pid()
      Returns some representation of the current process's id
      static void remove_path​(java.io.File 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • TempFileManager

        public TempFileManager​(java.lang.String prefix)
        Initializes a TempFileManager instance with the user's temporary directory containing the given prefix value. Also adds a shutdown hook to call cleanup() 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
        Uses File.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 a RuntimeException 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
        Emulates File.createTempFile(String, String) by calling create_path(String, String).
        Throws:
        java.io.IOException
      • create_path

        public static java.io.File create_path()
                                        throws java.io.IOException
        Calls createPath(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
        Calls createPath(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
        Calls createPath(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
        Throws:
        java.io.IOException
      • gettempdir

        public static void gettempdir()
      • main

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