public void connect()

in ch-commons-rfs/src/main/java/com/cloudhopper/commons/rfs/provider/SftpRemoteFileSystem.java [118:227]


    public void connect() throws FileSystemException {
        // make sure we don't connect twice
        if (session != null) {
            throw new FileSystemException("Already connected to SFTP server");
        }

        // validate the url -- required for sftp (user and host)

        // setup a new SFTP session
        jsch = new JSch();

        // attempt to load identities from the operating system
        // find any .ssh directories we'll scan
        File[] sshDirs = findSshDirs();
        for (File sshDir : sshDirs) {
            logger.info("Going to scan directory for .ssh private keys: " + sshDir.getAbsolutePath());
        }

        // find any identities that we'll then load
        File[] sshPrivateKeys = findSshPrivateKeys(sshDirs);
        for (File sshPrivateKeyFile : sshPrivateKeys) {
            logger.info("Attempting to load .ssh private key (identity): " + sshPrivateKeyFile.getAbsolutePath());
            try {
                jsch.addIdentity(sshPrivateKeyFile.getAbsolutePath());
            } catch (JSchException e) {
                logger.warn("Failed to load private key file " + sshPrivateKeyFile + " - going to ignore");
            }
        }

        try {
            session = jsch.getSession(getURL().getUsername(), getURL().getHost(), (getURL().getPort() == null ? 22 : getURL().getPort().intValue()));
        } catch (JSchException e) {
            throw new FileSystemException("Unable to create SSH session: " + e.getMessage(), e);
        }

        // create fully trusted instance -- any hosts will be accepted
        session.setUserInfo(new UserInfo() {
            public String getPassphrase() {
                return null;
            }
            public String getPassword() {
                return null;
            }
            public boolean promptPassphrase(String string) {
                return false;
            }
            public boolean promptPassword(String string) {
                return false;
            }
            // called when a host's authenticity is questioned
            public boolean promptYesNo(String string) {
                //logger.debug("Jsch promptYesNo: " + string);
                return true;
            }
            public void showMessage(String string) {
                //logger.debug("Jsch showMessage: " + string);
            }
        });

        // if the password is set
        if (getURL().getPassword() != null) {
            session.setPassword(getURL().getPassword());
        }

        // don't cause app to hang
        session.setDaemonThread(true);

        try {
            session.connect();
        } catch (JSchException e) {
            session = null;
            throw new FileSystemException("Unable to connect to SSH server: " + e.getMessage(), e);
        }

        logger.info("Connected to remote SSH server " + getURL().getUsername() + "@" + getURL().getHost());

        // create an SFTP channel
        try {
            channel = (ChannelSftp) session.openChannel("sftp");
            channel.connect();
        } catch (JSchException e) {
            // in case the channel failed, always close the parent session first
            try { session.disconnect(); } catch (Exception ex) { }
            session = null;
            throw new FileSystemException("Unable to create SFTP channel on SSH session: " + e.getMessage(), e);
        }

        // based on the URL, make a decision if we should attempt to change dirs
        if (getURL().getPath() != null) {
            logger.info("Changing SFTP directory to: " + getURL().getPath());
            try {
                channel.cd(getURL().getPath());
            } catch (SftpException e) {
                // make sure we disconnect
                try { disconnect(); } catch (Exception ex) { }
                session = null;
                throw new FileSystemException("Unable to change directory on SFTP channel to " + getURL().getPath(), e);
            }
        } else {
            // staying in whatever directory we were assigned by default
            // for information purposeds, let's try to print out that dir
            try {
                String currentDir = channel.pwd();
                logger.info("Current SFTP directory: " + currentDir);
            } catch (SftpException e) {
                // ignore this error
                logger.warn("Unable to get current directory -- safe to ignore");
            }
        }
    }