Package net.sf.basedb.opengrid
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
-
Nested Class Summary
Modifier and TypeClassDescription(package private) static class
Wrapper around the outputstream so that we can close the related resource when the stream is closed.(package private) static class
Wraps an UploadSource implementation with a LocalSourceFile implementation so that the file data can be sent to the remote server with SSHj. -
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
AbstractSession
(T remote, net.schmizz.sshj.SSHClient ssh) Session are typically created fromRemoteHost.connect(int)
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
void
downloadFile
(String fromPath, DownloadTarget target) Download a remote file to a local destination.<R,
C extends CmdResult<R>>
Cexecute
(C cmd, int timeout) Execute a command on the remote host and store the results in the CmdResult instance.executeCmd
(String cmd, int timeout) Execute a command on the remote host.getHost()
Get the host that created this session.getMetadata
(String path) Get metadata about the file on the remote server.private InputStream
getRemoteInputStream
(String fromPath, FileMetaData metadata) private OutputStream
getRemoteOutputStream
(String toPath, boolean overwrite, FileMetaData metadata, FilePermission permission) private net.schmizz.sshj.sftp.SFTPClient
boolean
isClosed()
Check if the session has been closed or not.mkdirs
(FilePermission permission, String... paths) Create directories on the remote server if they do not already exists.readFile
(String fromPath, FileMetaData metadata) Read from a file on the remote server.void
uploadFile
(UploadSource source, String toPath, FilePermission permission) Upload a file with SFTP.writeFile
(String toPath, boolean overwrite, FileMetaData metadata, FilePermission permission) Write to a file on the remote server.
-
Field Details
-
logger
-
remote
-
ssh
private final net.schmizz.sshj.SSHClient ssh -
sftp
private net.schmizz.sshj.sftp.SFTPClient sftp
-
-
Constructor Details
-
AbstractSession
Session are typically created fromRemoteHost.connect(int)
.
-
-
Method Details
-
isClosed
public boolean isClosed()Check if the session has been closed or not. -
close
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
-
getHost
Get the host that created this session. -
executeCmd
Execute a command on the remote host. This method should normally not throw any exceptions. The result is returned as aCmdResult
object where theCmdResult.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 executetimeout
- Timeout in seconds to wait for the command to finish- Returns:
- The result of the command
-
execute
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 thenCmdResult.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 executetimeout
- Timeout in seconds to wait for the command to finish- Returns:
- The 'cmd' instance
- Since:
- 1.4
-
getSFTPClient
- Throws:
IOException
-
mkdirs
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
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 storedoverwrite
- TRUE to overwrite existing filesmetadata
- 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
Download a remote file to a local destination.- Parameters:
fromPath
- The path of the remote filetarget
- The local target the remote file is downloaded to
-
readFile
Read from a file on the remote server.- Parameters:
fromPath
- The path of the remote filemetadata
- 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
Get metadata about the file on the remote server.- Throws:
IOException
- Since:
- 1.14
-
getRemoteInputStream
- Throws:
IOException
-
getRemoteOutputStream
private OutputStream getRemoteOutputStream(String toPath, boolean overwrite, FileMetaData metadata, FilePermission permission) throws IOException - Throws:
IOException
-