Class AbstractSession<T extends AbstractHost<?>>

java.lang.Object
net.sf.basedb.opengrid.AbstractSession<T>
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
OpenGridSession, RemoteSession

public abstract class AbstractSession<T extends AbstractHost<?>> extends Object implements Closeable
Represents an open SSH session to a remote host. It is possible to execute commands, upload and download files as long as the session is open. Do not forget to close() the session after use. A session is not thread-safe and need external synchronization if used with multiple threads.
Since:
1.0
Author:
nicklas
  • Field Details

    • logger

      private static final ExtensionsLogger logger
    • remote

      private final T extends AbstractHost<?> remote
    • ssh

      private final net.schmizz.sshj.SSHClient ssh
    • sftp

      private net.schmizz.sshj.sftp.SFTPClient sftp
  • Constructor Details

    • AbstractSession

      protected AbstractSession(T remote, net.schmizz.sshj.SSHClient ssh)
      Session are typically created from RemoteHost.connect(int).
  • Method Details

    • isClosed

      public boolean isClosed()
      Check if the session has been closed or not.
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • getHost

      public T getHost()
      Get the host that created this session.
    • executeCmd

      public CmdResult<String> executeCmd(String cmd, int timeout)
      Execute a command on the remote host. This method should normally not throw any exceptions. The result is returned as a CmdResult object where the CmdResult.getResult() returns either the stdout or stderr stream (depending on exit status). Note! Since 1.4 the timeout is a soft timeout. The command may take up to 10 times longer as long as it is returning data back in either the stdout or stderr streams.
      Parameters:
      cmd - The command to execute
      timeout - Timeout in seconds to wait for the command to finish
      Returns:
      The result of the command
    • execute

      public <R, C extends CmdResult<R>> C execute(C cmd, int timeout)
      Execute a command on the remote host and store the results in the CmdResult instance. Exit status, stdout and stderr will be updated on the 'cmd' instance and then CmdResult.parseResult() is called to allow implementations to parse the output and update the other result information. Note! Since 1.4 the timeout is a soft timeout. The command may take up to 10 times longer as long as it is returning data back in either the stdout or stderr streams.
      Parameters:
      cmd - The command to execute
      timeout - Timeout in seconds to wait for the command to finish
      Returns:
      The 'cmd' instance
      Since:
      1.4
    • getSFTPClient

      private net.schmizz.sshj.sftp.SFTPClient getSFTPClient() throws IOException
      Throws:
      IOException
    • mkdirs

      public CmdResult<String> mkdirs(FilePermission permission, String... paths)
      Create directories on the remote server if they do not already exists.
      Parameters:
      permission - The permission to give to directories that are created. Null to use default permissions. Already existing directories are not affected.
      paths - The paths to create
      Returns:
      The result of the command (do not forget to check the exit status)
      Since:
      1.1
    • uploadFile

      public void uploadFile(UploadSource source, String toPath, FilePermission permission)
      Upload a file with SFTP. The target directory should be a full absolute path and must already exist.
    • writeFile

      public OutputStream writeFile(String toPath, boolean overwrite, FileMetaData metadata, FilePermission permission)
      Write to a file on the remote server. If the file doesn't exists it is created. The target directory should be a full absolute path and must already exist.
      Parameters:
      toPath - Absolute path where the uploaded file should be stored
      overwrite - TRUE to overwrite existing files
      metadata - Information about the file (or null if not important)
      permission - Permission to set on the file
      Returns:
      An output stream to write the file data to
      Since:
      1.1
    • downloadFile

      public void downloadFile(String fromPath, DownloadTarget target)
      Download a remote file to a local destination.
      Parameters:
      fromPath - The path of the remote file
      target - The local target the remote file is downloaded to
    • readFile

      public InputStream readFile(String fromPath, FileMetaData metadata)
      Read from a file on the remote server.
      Parameters:
      fromPath - The path of the remote file
      metadata - A metadata instance for collecting metadata about the remote file or null if not important. The metadata will be set before the file download is started
      Returns:
      An InputStream reading from the remote file
    • getMetadata

      public FileMetaData getMetadata(String path) throws IOException
      Get metadata about the file on the remote server.
      Throws:
      IOException
      Since:
      1.14
    • getRemoteInputStream

      private InputStream getRemoteInputStream(String fromPath, FileMetaData metadata) throws IOException
      Throws:
      IOException
    • getRemoteOutputStream

      private OutputStream getRemoteOutputStream(String toPath, boolean overwrite, FileMetaData metadata, FilePermission permission) throws IOException
      Throws:
      IOException