org.apache.jmeter.services

## Class FileServer

- java.lang.Object
- - org.apache.jmeter.services.FileServer

-     public class FileServer
      extends java.lang.Object

  This class provides thread-safe access to files, and to provide some simplifying assumptions about where to find files and how to name them. For instance, putting supporting files in the same directory as the saved test plan file allows users to refer to the file with just it's name - this FileServer class will find the file without a problem. Eventually, I want all in-test file access to be done through here, with the goal of packaging up entire test plans as a directory structure that can be sent via rmi to remote servers (currently, one must make sure the remote server has all support files in a relative-same location) and to package up test plans to execute on unknown boxes that only have Java installed.

- - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `closeFile(java.lang.String name)`
    `void` | `closeFiles()`
    `java.lang.String` | `getBaseDir()`
    `java.io.File` | `getBaseDirRelative()` Calculates the relative path from `DEFAULT_BASE` to the current base, which must be the same as or a child of the default.
    `static java.lang.String` | `getDefaultBase()`
    `static FileServer` | `getFileServer()`
    `java.lang.String[]` | `getParsedLine(java.lang.String alias, boolean recycle, boolean ignoreFirstLine, char delim)`
    `java.io.File` | `getRandomFile(java.lang.String basedir, java.lang.String[] extensions)` Method will get a random file in a base directory TODO hey, not sure this method belongs here.
    `java.io.File` | `getResolvedFile(java.lang.String path)` Get `File` instance for provided file path, resolve file location relative to base dir or script dir when needed
    `java.lang.String` | `getScriptName()`
    `java.lang.String` | `readLine(java.lang.String filename)` Get the next line of the named file, recycle by default.
    `java.lang.String` | `readLine(java.lang.String filename, boolean recycle)` Get the next line of the named file, first line is name to false
    `java.lang.String` | `readLine(java.lang.String filename, boolean recycle, boolean ignoreFirstLine)` Get the next line of the named file
    `void` | `reserveFile(java.lang.String filename)` Creates an association between a filename and a File inputOutputObject, and stores it for later use - unless it is already stored.
    `void` | `reserveFile(java.lang.String filename, java.lang.String charsetName)` Creates an association between a filename and a File inputOutputObject, and stores it for later use - unless it is already stored.
    `void` | `reserveFile(java.lang.String filename, java.lang.String charsetName, java.lang.String alias)` Creates an association between a filename and a File inputOutputObject, and stores it for later use - unless it is already stored.
    `java.lang.String` | `reserveFile(java.lang.String filename, java.lang.String charsetName, java.lang.String alias, boolean hasHeader)` Creates an association between a filename and a File inputOutputObject, and stores it for later use - unless it is already stored.
    `void` | `resetBase()` Resets the current base to `DEFAULT_BASE`.
    `static java.lang.String` | `resolveBaseRelativeName(java.lang.String relativeName)` Resolve a file name that may be relative to the base directory.
    `void` | `setBase(java.io.File jmxBase)` Sets the current base directory for relative file names.
    `void` | `setBasedir(java.lang.String basedir)` Sets the current base directory for relative file names from the provided path.
    `void` | `setBaseForScript(java.io.File scriptPath)` Sets the current base directory for relative file names from the provided script file.
    `void` | `setScriptName(java.lang.String scriptName)`
    `void` | `write(java.lang.String filename, java.lang.String value)`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### getFileServer

          public static FileServer getFileServer()

      Returns:
        the singleton instance of the server.

    - #### resetBase

          public void resetBase()

      Resets the current base to `DEFAULT_BASE`.

    - #### setBasedir

          public void setBasedir(java.lang.String basedir)

      Sets the current base directory for relative file names from the provided path. If the path does not refer to an existing directory, then its parent is used. Normally the provided path is a file, so using the parent directory is appropriate.
      Parameters:
        `basedir` - the path to set, or `null` if the GUI is being cleared
      Throws:
        `java.lang.IllegalStateException` - if files are still open

    - #### setBaseForScript

          public void setBaseForScript(java.io.File scriptPath)

      Sets the current base directory for relative file names from the provided script file. The parameter is assumed to be the path to a JMX file, so the base directory is derived from its parent.
      Parameters:
        `scriptPath` - the path of the script file; must be not be `null`
      Throws:
        `java.lang.IllegalStateException` - if files are still open
        `java.lang.IllegalArgumentException` - if scriptPath parameter is null

    - #### setBase

          public void setBase(java.io.File jmxBase)

      Sets the current base directory for relative file names.
      Parameters:
        `jmxBase` - the path of the script file base directory, cannot be null
      Throws:
        `java.lang.IllegalStateException` - if files are still open
        `java.lang.IllegalArgumentException` - if `basepath` is null

    - #### getBaseDir

          public java.lang.String getBaseDir()

    - #### getDefaultBase

          public static java.lang.String getDefaultBase()

    - #### getBaseDirRelative

          public java.io.File getBaseDirRelative()

      Calculates the relative path from `DEFAULT_BASE` to the current base, which must be the same as or a child of the default.
      Returns:
        the relative path, or `"."` if the path cannot be determined

    - #### reserveFile

          public void reserveFile(java.lang.String filename)

      Creates an association between a filename and a File inputOutputObject, and stores it for later use - unless it is already stored.
      Parameters:
        `filename` - - relative (to base) or absolute file name (must not be null)

    - #### reserveFile

          public void reserveFile(java.lang.String filename,
                                  java.lang.String charsetName)

      Creates an association between a filename and a File inputOutputObject, and stores it for later use - unless it is already stored.
      Parameters:
        `filename` - - relative (to base) or absolute file name (must not be null)
        `charsetName` - - the character set encoding to use for the file (may be null)

    - #### reserveFile

          public void reserveFile(java.lang.String filename,
                                  java.lang.String charsetName,
                                  java.lang.String alias)

      Creates an association between a filename and a File inputOutputObject, and stores it for later use - unless it is already stored.
      Parameters:
        `filename` - - relative (to base) or absolute file name (must not be null)
        `charsetName` - - the character set encoding to use for the file (may be null)
        `alias` - - the name to be used to access the object (must not be null)

    - #### reserveFile

          public java.lang.String reserveFile(java.lang.String filename,
                                              java.lang.String charsetName,
                                              java.lang.String alias,
                                              boolean hasHeader)

      Creates an association between a filename and a File inputOutputObject, and stores it for later use - unless it is already stored.
      Parameters:
        `filename` - - relative (to base) or absolute file name (must not be null or empty)
        `charsetName` - - the character set encoding to use for the file (may be null)
        `alias` - - the name to be used to access the object (must not be null)
        `hasHeader` - true if the file has a header line describing the contents
      Returns:
        the header line; may be null
      Throws:
        `java.lang.IllegalArgumentException` - if header could not be read or filename is null or empty

    - #### readLine

          public java.lang.String readLine(java.lang.String filename)
                                    throws java.io.IOException

      Get the next line of the named file, recycle by default.
      Parameters:
        `filename` - the filename or alias that was used to reserve the file
      Returns:
        String containing the next line in the file
      Throws:
        `java.io.IOException` - when reading of the file fails, or the file was not reserved properly

    - #### readLine

          public java.lang.String readLine(java.lang.String filename,
                                           boolean recycle)
                                    throws java.io.IOException

      Get the next line of the named file, first line is name to false
      Parameters:
        `filename` - the filename or alias that was used to reserve the file
        `recycle` - - should file be restarted at EOF?
      Returns:
        String containing the next line in the file (null if EOF reached and not recycle)
      Throws:
        `java.io.IOException` - when reading of the file fails, or the file was not reserved properly

    - #### readLine

          public java.lang.String readLine(java.lang.String filename,
                                           boolean recycle,
                                           boolean ignoreFirstLine)
                                    throws java.io.IOException

      Get the next line of the named file
      Parameters:
        `filename` - the filename or alias that was used to reserve the file
        `recycle` - - should file be restarted at EOF?
        `ignoreFirstLine` - - Ignore first line
      Returns:
        String containing the next line in the file (null if EOF reached and not recycle)
      Throws:
        `java.io.IOException` - when reading of the file fails, or the file was not reserved properly

    - #### getParsedLine

          public java.lang.String[] getParsedLine(java.lang.String alias,
                                                  boolean recycle,
                                                  boolean ignoreFirstLine,
                                                  char delim)
                                           throws java.io.IOException

      Parameters:
        `alias` - the file name or alias
        `recycle` - whether the file should be re-started on EOF
        `ignoreFirstLine` - whether the file contains a file header which will be ignored
        `delim` - the delimiter to use for parsing
      Returns:
        the parsed line, will be empty if the file is at EOF
      Throws:
        `java.io.IOException` - when reading of the aliased file fails, or the file was not reserved properly

    - #### write

          public void write(java.lang.String filename,
                            java.lang.String value)
                     throws java.io.IOException

      Throws:
        `java.io.IOException`

    - #### closeFiles

          public void closeFiles()
                          throws java.io.IOException

      Throws:
        `java.io.IOException`

    - #### closeFile

          public void closeFile(java.lang.String name)
                         throws java.io.IOException

      Parameters:
        `name` - the name or alias of the file to be closed
      Throws:
        `java.io.IOException` - when closing of the aliased file fails

    - #### getRandomFile

          public java.io.File getRandomFile(java.lang.String basedir,
                                            java.lang.String[] extensions)

      Method will get a random file in a base directory

      TODO hey, not sure this method belongs here. FileServer is for thread safe File access relative to current test's base directory.

      Parameters:
        `basedir` - name of the directory in which the files can be found
        `extensions` - array of allowed extensions, if `null` is given, any file be allowed
      Returns:
        a random File from the `basedir` that matches one of the extensions

    - #### getResolvedFile

          public java.io.File getResolvedFile(java.lang.String path)

      Get `File` instance for provided file path, resolve file location relative to base dir or script dir when needed
      Parameters:
        `path` - original path to file, maybe relative
      Returns:
        `File` instance

    - #### resolveBaseRelativeName

          public static java.lang.String resolveBaseRelativeName(java.lang.String relativeName)

      Resolve a file name that may be relative to the base directory. If the name begins with the value of the JMeter property "jmeter.save.saveservice.base_prefix" - default "~/" - then the name is assumed to be relative to the basename.
      Parameters:
        `relativeName` - filename that should be checked for `jmeter.save.saveservice.base_prefix`
      Returns:
        the updated filename

    - #### getScriptName

          public java.lang.String getScriptName()

      Returns:
        JMX Script name
      Since:
        2.6

    - #### setScriptName

          public void setScriptName(java.lang.String scriptName)

      Parameters:
        `scriptName` - Script name
      Since:
        2.6

Copyright © 1998-2018 Apache Software Foundation. All Rights Reserved.