Class FilePathHelper

java.lang.Object
net.simplace.core.service.FilePathHelper

public final class FilePathHelper extends Object
Singleton manager for temporary and cache directories.

Provides a session temp directory (unique while the software is running), a cache directory (shared by all sessions) and random temp directories (unique per call). All of them are located below the temp root directory, which is read from the system property SETTINGS.USER_TMP or set with setTempDirRoot(String).

Author:
Andreas Enders
  • Field Details

    • LOG_DIR_PROPERTY

      public static final String LOG_DIR_PROPERTY
      System property that overrides the log directory (also used by log4j2.xml).
      See Also:
  • Method Details

    • getInstance

      public static FilePathHelper getInstance()
      Returns the singleton instance of FilePathHelper.
      Returns:
      the singleton instance of FilePathHelper
    • getTempDirRoot

      public String getTempDirRoot()
      Returns the temp root directory shared by all sessions. The path is read from the system property SETTINGS.USER_TMP if available, otherwise the last value set with setTempDirRoot(String) is used. The directory is created if it does not exist.
      Returns:
      path of the temp root directory, with forward slashes
    • setTempDirRoot

      public String setTempDirRoot(String aFilePath)
      Sets the temp root directory, also in the system property SETTINGS.USER_TMP, and creates it if it does not exist.
      Parameters:
      aFilePath - path of the new temp root directory
      Returns:
      aFilePath
    • getTempDir

      public String getTempDir()
      Returns the temp directory of this session. It is created below the temp root directory on first call and reused afterwards.
      Returns:
      path of the session temp directory, with forward slashes
    • getRandomTempDir

      public String getRandomTempDir()
      Creates a new, uniquely named temp directory below the temp root directory. Each call returns a different directory.
      Returns:
      path of the new directory, with forward slashes
    • getCacheDir

      public String getCacheDir()
      Returns the cache directory shared by all sessions. The path is read from the system property SETTINGS.USER_CACHE if available, otherwise <temp root>/CACHE is used. The directory is created if it does not exist.
      Returns:
      path of the cache directory, with forward slashes
    • setNewCacheVersion

      public void setNewCacheVersion(String aSystemID, String aFileName, String aTargetDir, float aVersion)
      Stores the version of a cached file in a file <name>.version in the target directory. Errors are logged, not thrown.
      Parameters:
      aSystemID - not used
      aFileName - name or path of the cached file; only the file name is used
      aTargetDir - directory in which the version file is written
      aVersion - version to store
      See Also:
    • getCacheVersion

      public float getCacheVersion(String aSystemID, String aFileName, String aTargetDir)
      Reads the version of a cached file from the file <name>.version in the target directory.
      Parameters:
      aSystemID - not used
      aFileName - name or path of the cached file; only the file name is used
      aTargetDir - directory containing the version file
      Returns:
      the stored version, or 0 if there is no version file or it cannot be read
      See Also:
    • getRandomFile

      public File getRandomFile(String aFileName)
      Returns a file with the given name in the session temp directory. The file is not created and the name is not made unique.
      Parameters:
      aFileName - file name; invalid characters are replaced
      Returns:
      the file object
    • getTempFile

      public File getTempFile(String aFileName) throws IOException
      Creates a new, empty file with a unique name in the session temp directory.
      Parameters:
      aFileName - template for the file name; the extension from the last . (e.g. .xml) is used as suffix, the rest as prefix (extended to the minimum length of 3 characters)
      Returns:
      the created file
      Throws:
      IOException - if the file cannot be created
    • removeTempDir

      public boolean removeTempDir()
      Deletes the session temp directory with its content.
      Returns:
      true if the directory was deleted
    • removeAllClientTempDirs

      public boolean removeAllClientTempDirs()
      Deletes all files and directories in the temp root directory that have not been modified for about 11 hours (40,000 seconds).
      Returns:
      true if all old entries were deleted
    • getLogDir

      public static File getLogDir()
      Returns the directory of the log files. It is the value of the system property "simplace.logdir" or, if not set, <user.home>/.simplace/log. The same rule is used by the log4j2.xml configurations, so the log never ends up in the (possibly read-only) current directory.
      Returns:
      the log directory
    • getLogFile

      public static File getLogFile()
      Returns:
      the log file SIMPLACE.log in the log directory
    • getResourcePath

      public static File getResourcePath(File aFile)
      Looks up a file as resource on the class path of the context class loader.
      Parameters:
      aFile - relative path of the resource
      Returns:
      the resource as file, or null if it is not found