Class FileConnector

  • All Implemented Interfaces:
    ConnectorInterface, VersionInfoInterface

    public class FileConnector
    extends Connector
    implements ConnectorInterface
    The file system Connector is a transport Connector that requires a Parser to operate. The file system Connector reads and writes files available on the system it runs on. Concurrent usage of a file can be controlled by means of a locking mechanism. This Connector can only be used in Iterator or AddOnly mode, or for the equivalent operations in Passive state.
    • Constructor Detail

      • FileConnector

        public FileConnector()
        Constructor. Initializes the connector to work in AddOnly and Iterator mode.
    • Method Detail

      • initialize

        public void initialize​(java.lang.Object o)
                        throws java.lang.Exception
        Initialize the connector. The connector may be passed a parameter of any kind by the user. It is up to the connector to determine whether this object can be used or not. The parameter is typically provided by a user script. When an AssemblyLine initializes it's Connectors, they are passed a ConnectorMode object.
        Specified by:
        initialize in interface ConnectorInterface
        Overrides:
        initialize in class Connector
        Parameters:
        o - Expects Reader or Writer to initialize the Parser with, or a ConnectorMode with the mode in which the Iterator is set to open the specified file in the configuration for reading or writing respectively for Iterator and AddOnly modes. If the object is not an instance of those classes, the iterator checks for a configuration parameter with name "fileMode" and if exists, checks if equals to "input" (and opens the file specified in the configuration for reading) or equals to "output" (and opens the file specified in the configuration for writing). An exception is thrown if none of these situations occur.
        Throws:
        java.lang.Exception - if the initialization of this connector fails.
      • selectEntries

        public void selectEntries()
                           throws java.lang.Exception
        Prepare the Connector for sequential read. Opens the file specified in FilePath field in the Config Tab. When the Connector is used as an Iterator in an AssemblyLine, this method will be called.
        Specified by:
        selectEntries in interface ConnectorInterface
        Overrides:
        selectEntries in class Connector
        Throws:
        java.lang.Exception - if an error occurs.
      • putEntry

        public void putEntry​(Entry entry)
                      throws java.lang.Exception
        Add a new entry to the data source
        Specified by:
        putEntry in interface ConnectorInterface
        Overrides:
        putEntry in class Connector
        Parameters:
        entry - The entry data to add
        Throws:
        java.lang.Exception - if an error occurs.
      • openReadFile

        public void openReadFile()
                          throws java.lang.Exception
        Opens the file specified by the FilePath field in the Config Tab for reading. If the default value is specified, then the standard input is used. The parser is initialized with the acquired stream.
        Throws:
        java.lang.Exception - If the FilePath field is empty, an Exception is thrown
      • openWriteFile

        public void openWriteFile()
                           throws java.lang.Exception
        Opens the file specified by the FilePath field in the Config Tab for writing. If the default value is specified, then the standard output is used. If the "fileMode" parameter is set to "append", then the stream is opened for appending. The parser is initialized with the acquired stream. If appending and the parser is a CSV parser, the method takes care for what is necessary to append to the stream.
        Throws:
        java.lang.Exception - If the FilePath field is empty, an Exception is thrown
      • acquireLock

        public void acquireLock​(java.nio.channels.FileChannel fc,
                                long timeout,
                                boolean shared)
                         throws java.lang.Exception
        Attempts to acquire a lock on a File Channel
        Parameters:
        fc - The File channel we are attempting to lock
        timeout - The max time in seconds in which the lock has to be acquired.
        shared - true to request a shared lock, in which case this channel must be open for reading (and possibly writing); false to request an exclusive lock, in which case this channel must be open for writing (and possibly reading)
        Throws:
        java.lang.Exception - If unable to acquire the lock within timeout time, an Exception is thrown
      • releaseLock

        public void releaseLock()
        Releases the acquired lock.
      • getVersion

        public java.lang.String getVersion()
        Version information.
        Specified by:
        getVersion in interface VersionInfoInterface
        Returns:
        version information
      • reconnect

        public void reconnect()
                       throws java.lang.Exception
        Reconnect to the underlying data source. Terminates the current connector, initializes it again and if in Iterator mode, prepares the connector for sequential read.
        Overrides:
        reconnect in class Connector
        Throws:
        java.lang.Exception - if an error occurs.
        See Also:
        Connector.initialize(Object)