Class 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 JSch
      private class  SshCache.Entry  
    • 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 entry
      private 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 cache
      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
      private SshCache.Entry getCacheEntry​(com.jcraft.jsch.Session session)
      retrieves a session entry for a given session from the cache
      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
      com.jcraft.jsch.ChannelSftp getChannelSftp​(com.jcraft.jsch.Session session)
      retrieves an sftp channel from the cache
      static 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 necessary
      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
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • 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
    • Constructor Detail

      • SshCache

        private SshCache()
    • 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 for
        host - ditto
        port - 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 user
        host - of the connection
        port - 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 session
        host - of the session
        port - of the session
        newSession - 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 to
        channel - 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 to
        port - to use for session (-1 == use standard port)
        username - for the session to use
        userPassword - to use for authentication (optional)
        pemFile - File to use for public key authentication
        pemPassword - to use for accessing the pemFile (optional)
        passFile - to store credentials
        allowedAgentUse - Whether to communicate with an agent for authentication
        Returns:
        session or null if not successful
        Throws:
        java.io.IOException - if something goes wrong