Class SshCache
- java.lang.Object
-
- org.apache.ivy.plugins.repository.ssh.SshCache
-
public final class SshCache extends java.lang.Object
a class to cache SSH Connections and Channel for the SSH Repository each session is defined by connecting user / host / port two maps are used to find cache entries one map is using the above keys, the other uses the session itself
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
SshCache.CfUserInfo
feeds in password silently into JSchprivate class
SshCache.Entry
-
Field Summary
Fields Modifier and Type Field Description private static SshCache
instance
private java.util.Map<com.jcraft.jsch.Session,SshCache.Entry>
sessionCacheMap
key is the session itselfprivate static int
SSH_DEFAULT_PORT
private java.util.Map<java.lang.String,SshCache.Entry>
uriCacheMap
key is username / host / port
-
Constructor Summary
Constructors Modifier Constructor Description private
SshCache()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
attachChannelSftp(com.jcraft.jsch.Session session, com.jcraft.jsch.ChannelSftp channel)
attaches a channelSftp to an existing session cache entryprivate boolean
attemptAgentUse(com.jcraft.jsch.JSch jsch)
Attempts to connect to a local SSH agent (using either UNIX sockets or PuTTY's Pageant)void
clearSession(com.jcraft.jsch.Session session)
discards session entries from the cacheprivate static java.lang.String
createCacheKey(java.lang.String user, java.lang.String host, int port)
Creates a combined cache key from the given key partsprivate SshCache.Entry
getCacheEntry(com.jcraft.jsch.Session session)
retrieves a session entry for a given session from the cacheprivate SshCache.Entry
getCacheEntry(java.lang.String user, java.lang.String host, int port)
retrieves a session entry for a given hostname from the cachecom.jcraft.jsch.ChannelSftp
getChannelSftp(com.jcraft.jsch.Session session)
retrieves an sftp channel from the cachestatic SshCache
getInstance()
com.jcraft.jsch.Session
getSession(java.lang.String host, int port, java.lang.String username, java.lang.String userPassword, java.io.File pemFile, java.lang.String pemPassword, java.io.File passFile, boolean allowedAgentUse)
Gets a session from the cache or establishes a new session if necessaryprivate void
setSession(java.lang.String user, java.lang.String host, int port, com.jcraft.jsch.Session newSession)
Sets a session to a given combined key into the cache If an old session object already exists, close and remove it
-
-
-
Field Detail
-
SSH_DEFAULT_PORT
private static final int SSH_DEFAULT_PORT
- See Also:
- Constant Field Values
-
instance
private static SshCache instance
-
uriCacheMap
private final java.util.Map<java.lang.String,SshCache.Entry> uriCacheMap
key is username / host / port- See Also:
for details
-
sessionCacheMap
private final java.util.Map<com.jcraft.jsch.Session,SshCache.Entry> sessionCacheMap
key is the session itself
-
-
Method Detail
-
getInstance
public static SshCache getInstance()
-
getCacheEntry
private SshCache.Entry getCacheEntry(java.lang.String user, java.lang.String host, int port)
retrieves a session entry for a given hostname from the cache- Parameters:
user
- to retrieve session forhost
- dittoport
- ditto- Returns:
- null or the existing entry
-
createCacheKey
private static java.lang.String createCacheKey(java.lang.String user, java.lang.String host, int port)
Creates a combined cache key from the given key parts- Parameters:
user
- name of the userhost
- of the connectionport
- of the connection- Returns:
- key for the cache
-
getCacheEntry
private SshCache.Entry getCacheEntry(com.jcraft.jsch.Session session)
retrieves a session entry for a given session from the cache- Parameters:
session
- to retrieve cache entry for- Returns:
- null or the existing entry
-
setSession
private void setSession(java.lang.String user, java.lang.String host, int port, com.jcraft.jsch.Session newSession)
Sets a session to a given combined key into the cache If an old session object already exists, close and remove it- Parameters:
user
- of the sessionhost
- of the sessionport
- of the sessionnewSession
- Session to save
-
clearSession
public void clearSession(com.jcraft.jsch.Session session)
discards session entries from the cache- Parameters:
session
- to clear
-
getChannelSftp
public com.jcraft.jsch.ChannelSftp getChannelSftp(com.jcraft.jsch.Session session) throws java.io.IOException
retrieves an sftp channel from the cache- Parameters:
session
- to connect to- Returns:
- channelSftp or null if not successful (channel not existent or dead)
- Throws:
java.io.IOException
- should never happen
-
attachChannelSftp
public void attachChannelSftp(com.jcraft.jsch.Session session, com.jcraft.jsch.ChannelSftp channel)
attaches a channelSftp to an existing session cache entry- Parameters:
session
- to attach the channel tochannel
- channel to attach
-
attemptAgentUse
private boolean attemptAgentUse(com.jcraft.jsch.JSch jsch)
Attempts to connect to a local SSH agent (using either UNIX sockets or PuTTY's Pageant)- Parameters:
jsch
- Connection to be attached to an available local agent- Returns:
- true if connected to agent, false otherwise
-
getSession
public com.jcraft.jsch.Session getSession(java.lang.String host, int port, java.lang.String username, java.lang.String userPassword, java.io.File pemFile, java.lang.String pemPassword, java.io.File passFile, boolean allowedAgentUse) throws java.io.IOException
Gets a session from the cache or establishes a new session if necessary- Parameters:
host
- to connect toport
- to use for session (-1 == use standard port)username
- for the session to useuserPassword
- to use for authentication (optional)pemFile
- File to use for public key authenticationpemPassword
- to use for accessing the pemFile (optional)passFile
- to store credentialsallowedAgentUse
- Whether to communicate with an agent for authentication- Returns:
- session or null if not successful
- Throws:
java.io.IOException
- if something goes wrong
-
-