Package adams.core.io.lister
Class SftpDirectoryLister
- java.lang.Object
-
- adams.core.logging.LoggingObject
-
- adams.core.logging.CustomLoggingLevelObject
-
- adams.core.io.lister.AbstractDirectoryLister
-
- adams.core.io.lister.AbstractRecursiveDirectoryLister
-
- adams.core.io.lister.SftpDirectoryLister
-
- All Implemented Interfaces:
DirectoryLister,RecursiveDirectoryLister,LoggingLevelHandler,LoggingSupporter,PasswordSupporter,SizeOfHandler,Stoppable,StoppableWithFeedback,Serializable
public class SftpDirectoryLister extends AbstractRecursiveDirectoryLister implements PasswordSupporter
Lists files/dirs on a remote server using SFTP. The provided session provider takes precedence of the parameters.- Author:
- FracPete (fracpete at waikato dot ac dot nz)
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected SSHAuthenticationTypem_AuthenticationTypethe type of authentication to use.protected Stringm_Hostthe SSH host.protected PlaceholderFilem_KnownHoststhe file with known hosts.protected BasePasswordm_Passwordthe SSH password to use.protected intm_Portthe SSH port.protected PlaceholderFilem_PrivateKeyFilethe location of the private key.protected BasePasswordm_PrivateKeyPassphrasethe passphrase for the private key.protected com.jcraft.jsch.Sessionm_Sessionthe SSH session.protected SSHSessionProviderm_SessionProviderthe session provider to use.protected booleanm_StrictHostKeyCheckingwhether to perform strict host key checking (only disable for testing!!protected Stringm_Userthe SSH user to use.-
Fields inherited from class adams.core.io.lister.AbstractRecursiveDirectoryLister
m_MaxDepth, m_Recursive
-
Fields inherited from class adams.core.io.lister.AbstractDirectoryLister
m_ListDirs, m_ListFiles, m_MaxItems, m_RegExp, m_SortDescending, m_Sorting, m_Stopped, m_WatchDir
-
Fields inherited from class adams.core.logging.LoggingObject
m_Logger, m_LoggingIsEnabled, m_LoggingLevel
-
-
Constructor Summary
Constructors Constructor Description SftpDirectoryLister()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voiddisconnect()Disconnects the SSH session, if necessary.SSHAuthenticationTypegetAuthenticationType()Returns the type of authentication to use.StringgetHost()Returns the host to connect to.PlaceholderFilegetKnownHosts()Returns the file with the known hosts.BasePasswordgetPassword()Returns the SSH password to use.intgetPort()Returns the port to connect to.PlaceholderFilegetPrivateKeyFile()Returns the location of the private key file.BasePasswordgetPrivateKeyPassphrase()Returns the passphrase for the private key file, ignored if empty.SSHSessionProvidergetSessionProvider()Returns the current session provider.booleangetStrictHostKeyChecking()Returns whether to perform strict host key checking.StringgetUser()Returns the SSH user name to use.booleanhasParentDirectory()Returns whether the watch directory has a parent directory.booleanisLocal()Returns whether the directory lister operates locally or remotely.String[]list()Returns the list of files/directories in the watched directory.SftpFileObject[]listObjects()Returns the list of files/directories in the watched directory.SftpFileObjectnewDirectory(String dir)Returns a new directory relative to the watch directory.SftpFileObjectnewDirectory(String parent, String dir)Returns a new directory generated from parent and sub-directory.protected com.jcraft.jsch.SessionnewSession()Returns a new session for the host/port defined in the options.protected com.jcraft.jsch.SessionnewSession(String host, int port)Returns a new session for the given host/port.List<SftpFileObject>search(com.jcraft.jsch.ChannelSftp channel)Returns the list of files/directories in the watched directory.protected voidsearch(com.jcraft.jsch.ChannelSftp channel, String current, List<SortContainer> files, int depth)Performs the recursive search.voidsetAuthenticationType(SSHAuthenticationType value)Sets the type of authentication to use.voidsetHost(String value)Sets the host to connect to.voidsetKnownHosts(PlaceholderFile value)Sets the file with the known hosts.voidsetPassword(BasePassword value)Sets the SSH password to use.voidsetPort(int value)Sets the port to connect to.voidsetPrivateKeyFile(PlaceholderFile value)Sets the location of the private key file.voidsetPrivateKeyPassphrase(BasePassword value)Sets the passphrase for the private key file, ignored if empty.voidsetSessionProvider(SSHSessionProvider value)Sets the session provider to use.voidsetStrictHostKeyChecking(boolean value)Sets whether to perform strict host key checking.voidsetUser(String value)Sets the SSH user to use.voidsetWatchDir(String value)Sets the directory to watch.-
Methods inherited from class adams.core.io.lister.AbstractRecursiveDirectoryLister
getMaxDepth, getRecursive, setMaxDepth, setRecursive, toString
-
Methods inherited from class adams.core.io.lister.AbstractDirectoryLister
getDebug, getListDirs, getListFiles, getMaxItems, getRegExp, getSortDescending, getSorting, getWatchDir, isStopped, setDebug, setListDirs, setListFiles, setMaxItems, setRegExp, setSortDescending, setSorting, stopExecution
-
Methods inherited from class adams.core.logging.CustomLoggingLevelObject
setLoggingLevel
-
Methods inherited from class adams.core.logging.LoggingObject
configureLogger, getLogger, getLoggingLevel, initializeLogging, isLoggingEnabled, sizeOf
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface adams.core.io.lister.DirectoryLister
getListDirs, getListFiles, getMaxItems, getRegExp, getSortDescending, getSorting, getWatchDir, setListDirs, setListFiles, setMaxItems, setRegExp, setSortDescending, setSorting
-
Methods inherited from interface adams.core.logging.LoggingLevelHandler
getLoggingLevel, setLoggingLevel
-
Methods inherited from interface adams.core.logging.LoggingSupporter
getLogger, isLoggingEnabled
-
Methods inherited from interface adams.core.Stoppable
stopExecution
-
Methods inherited from interface adams.core.StoppableWithFeedback
isStopped
-
-
-
-
Field Detail
-
m_Host
protected String m_Host
the SSH host.
-
m_Port
protected int m_Port
the SSH port.
-
m_AuthenticationType
protected SSHAuthenticationType m_AuthenticationType
the type of authentication to use.
-
m_User
protected String m_User
the SSH user to use.
-
m_Password
protected BasePassword m_Password
the SSH password to use.
-
m_PrivateKeyFile
protected PlaceholderFile m_PrivateKeyFile
the location of the private key.
-
m_PrivateKeyPassphrase
protected BasePassword m_PrivateKeyPassphrase
the passphrase for the private key.
-
m_KnownHosts
protected PlaceholderFile m_KnownHosts
the file with known hosts.
-
m_StrictHostKeyChecking
protected boolean m_StrictHostKeyChecking
whether to perform strict host key checking (only disable for testing!! insecure!!).
-
m_SessionProvider
protected SSHSessionProvider m_SessionProvider
the session provider to use.
-
m_Session
protected transient com.jcraft.jsch.Session m_Session
the SSH session.
-
-
Method Detail
-
setWatchDir
public void setWatchDir(String value)
Sets the directory to watch.- Specified by:
setWatchDirin interfaceDirectoryLister- Overrides:
setWatchDirin classAbstractDirectoryLister- Parameters:
value- the directory
-
setHost
public void setHost(String value)
Sets the host to connect to.- Parameters:
value- the host name/ip
-
getHost
public String getHost()
Returns the host to connect to.- Returns:
- the host name/ip
-
setPort
public void setPort(int value)
Sets the port to connect to.- Parameters:
value- the port
-
getPort
public int getPort()
Returns the port to connect to.- Returns:
- the port
-
setAuthenticationType
public void setAuthenticationType(SSHAuthenticationType value)
Sets the type of authentication to use.- Parameters:
value- the type
-
getAuthenticationType
public SSHAuthenticationType getAuthenticationType()
Returns the type of authentication to use.- Returns:
- the type
-
setUser
public void setUser(String value)
Sets the SSH user to use.- Parameters:
value- the user name
-
getUser
public String getUser()
Returns the SSH user name to use.- Returns:
- the user name
-
setPassword
public void setPassword(BasePassword value)
Sets the SSH password to use.- Specified by:
setPasswordin interfacePasswordSupporter- Parameters:
value- the password
-
getPassword
public BasePassword getPassword()
Returns the SSH password to use.- Specified by:
getPasswordin interfacePasswordSupporter- Returns:
- the password
-
setPrivateKeyFile
public void setPrivateKeyFile(PlaceholderFile value)
Sets the location of the private key file.- Parameters:
value- the key file
-
getPrivateKeyFile
public PlaceholderFile getPrivateKeyFile()
Returns the location of the private key file.- Returns:
- the key file
-
setPrivateKeyPassphrase
public void setPrivateKeyPassphrase(BasePassword value)
Sets the passphrase for the private key file, ignored if empty.- Parameters:
value- the passphrase
-
getPrivateKeyPassphrase
public BasePassword getPrivateKeyPassphrase()
Returns the passphrase for the private key file, ignored if empty.- Returns:
- the passphrase
-
setKnownHosts
public void setKnownHosts(PlaceholderFile value)
Sets the file with the known hosts.- Parameters:
value- the file
-
getKnownHosts
public PlaceholderFile getKnownHosts()
Returns the file with the known hosts.- Returns:
- the file
-
setStrictHostKeyChecking
public void setStrictHostKeyChecking(boolean value)
Sets whether to perform strict host key checking. NB: only disabled for testing, as it is very insecure to disable it!- Parameters:
value- if true then strict checking is on
-
getStrictHostKeyChecking
public boolean getStrictHostKeyChecking()
Returns whether to perform strict host key checking. NB: only disabled for testing, as it is very insecure to disable it!- Returns:
- true if strict checking is on
-
setSessionProvider
public void setSessionProvider(SSHSessionProvider value)
Sets the session provider to use.- Parameters:
value- the session provider
-
getSessionProvider
public SSHSessionProvider getSessionProvider()
Returns the current session provider.- Returns:
- the provider, null if none set
-
newSession
protected com.jcraft.jsch.Session newSession()
Returns a new session for the host/port defined in the options.- Returns:
- the session
-
newSession
protected com.jcraft.jsch.Session newSession(String host, int port)
Returns a new session for the given host/port.- Parameters:
host- the host to create the session for- Returns:
- the session
-
disconnect
protected void disconnect()
Disconnects the SSH session, if necessary.
-
isLocal
public boolean isLocal()
Returns whether the directory lister operates locally or remotely.- Specified by:
isLocalin interfaceDirectoryLister- Returns:
- true if local lister
-
hasParentDirectory
public boolean hasParentDirectory()
Returns whether the watch directory has a parent directory.- Specified by:
hasParentDirectoryin interfaceDirectoryLister- Returns:
- true if parent directory available
-
newDirectory
public SftpFileObject newDirectory(String dir)
Returns a new directory relative to the watch directory.- Specified by:
newDirectoryin interfaceDirectoryLister- Parameters:
dir- the directory name- Returns:
- the new wrapper
-
newDirectory
public SftpFileObject newDirectory(String parent, String dir)
Returns a new directory generated from parent and sub-directory.- Specified by:
newDirectoryin interfaceDirectoryLister- Parameters:
parent- the parent directorydir- the directory name- Returns:
- the new wrapper
-
search
protected void search(com.jcraft.jsch.ChannelSftp channel, String current, List<SortContainer> files, int depth) throws ExceptionPerforms the recursive search. Search goes deeper if != 0 (use -1 to start with for infinite search).- Parameters:
channel- the SFTP channel to usecurrent- the current directoryfiles- the files collected so fardepth- the depth indicator (searched no deeper, if 0)- Throws:
Exception- if listing fails
-
search
public List<SftpFileObject> search(com.jcraft.jsch.ChannelSftp channel) throws Exception
Returns the list of files/directories in the watched directory. In case the execution gets stopped, this method returns a 0-length array.- Parameters:
channel- the SFTP channel to use- Returns:
- the list of absolute file/directory names
- Throws:
Exception- if listing fails
-
list
public String[] list()
Returns the list of files/directories in the watched directory. In case the execution gets stopped, this method returns a 0-length array.- Specified by:
listin interfaceDirectoryLister- Returns:
- the list of absolute file/directory names
-
listObjects
public SftpFileObject[] listObjects()
Returns the list of files/directories in the watched directory. In case the execution gets stopped, this method returns a 0-length array.- Specified by:
listObjectsin interfaceDirectoryLister- Returns:
- the list of file/directory wrappers
-
-