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 SSHAuthenticationType
m_AuthenticationType
the type of authentication to use.protected String
m_Host
the SSH host.protected PlaceholderFile
m_KnownHosts
the file with known hosts.protected BasePassword
m_Password
the SSH password to use.protected int
m_Port
the SSH port.protected PlaceholderFile
m_PrivateKeyFile
the location of the private key.protected BasePassword
m_PrivateKeyPassphrase
the passphrase for the private key.protected com.jcraft.jsch.Session
m_Session
the SSH session.protected SSHSessionProvider
m_SessionProvider
the session provider to use.protected boolean
m_StrictHostKeyChecking
whether to perform strict host key checking (only disable for testing!! insecure!!).protected String
m_User
the 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 void
disconnect()
Disconnects the SSH session, if necessary.SSHAuthenticationType
getAuthenticationType()
Returns the type of authentication to use.String
getHost()
Returns the host to connect to.PlaceholderFile
getKnownHosts()
Returns the file with the known hosts.BasePassword
getPassword()
Returns the SSH password to use.int
getPort()
Returns the port to connect to.PlaceholderFile
getPrivateKeyFile()
Returns the location of the private key file.BasePassword
getPrivateKeyPassphrase()
Returns the passphrase for the private key file, ignored if empty.SSHSessionProvider
getSessionProvider()
Returns the current session provider.boolean
getStrictHostKeyChecking()
Returns whether to perform strict host key checking.String
getUser()
Returns the SSH user name to use.boolean
hasParentDirectory()
Returns whether the watch directory has a parent directory.boolean
isLocal()
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.SftpFileObject
newDirectory(String dir)
Returns a new directory relative to the watch directory.SftpFileObject
newDirectory(String parent, String dir)
Returns a new directory generated from parent and sub-directory.protected com.jcraft.jsch.Session
newSession()
Returns a new session for the host/port defined in the options.protected com.jcraft.jsch.Session
newSession(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 void
search(com.jcraft.jsch.ChannelSftp channel, String current, List<SortContainer> files, int depth)
Performs the recursive search.void
setAuthenticationType(SSHAuthenticationType value)
Sets the type of authentication to use.void
setHost(String value)
Sets the host to connect to.void
setKnownHosts(PlaceholderFile value)
Sets the file with the known hosts.void
setPassword(BasePassword value)
Sets the SSH password to use.void
setPort(int value)
Sets the port to connect to.void
setPrivateKeyFile(PlaceholderFile value)
Sets the location of the private key file.void
setPrivateKeyPassphrase(BasePassword value)
Sets the passphrase for the private key file, ignored if empty.void
setSessionProvider(SSHSessionProvider value)
Sets the session provider to use.void
setStrictHostKeyChecking(boolean value)
Sets whether to perform strict host key checking.void
setUser(String value)
Sets the SSH user to use.void
setWatchDir(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:
setWatchDir
in interfaceDirectoryLister
- Overrides:
setWatchDir
in 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:
setPassword
in interfacePasswordSupporter
- Parameters:
value
- the password
-
getPassword
public BasePassword getPassword()
Returns the SSH password to use.- Specified by:
getPassword
in 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:
isLocal
in interfaceDirectoryLister
- Returns:
- true if local lister
-
hasParentDirectory
public boolean hasParentDirectory()
Returns whether the watch directory has a parent directory.- Specified by:
hasParentDirectory
in interfaceDirectoryLister
- Returns:
- true if parent directory available
-
newDirectory
public SftpFileObject newDirectory(String dir)
Returns a new directory relative to the watch directory.- Specified by:
newDirectory
in 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:
newDirectory
in 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 Exception
Performs 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:
list
in 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:
listObjects
in interfaceDirectoryLister
- Returns:
- the list of file/directory wrappers
-
-