Example usage for org.springframework.integration.sftp.session SftpSession SftpSession

List of usage examples for org.springframework.integration.sftp.session SftpSession SftpSession

Introduction

In this page you can find the example usage for org.springframework.integration.sftp.session SftpSession SftpSession.

Prototype

public SftpSession(JSchSessionWrapper wrapper) 

Source Link

Usage

From source file:org.springframework.integration.sftp.session.DefaultSftpSessionFactory.java

@Override
public SftpSession getSession() {
    Assert.hasText(this.host, "host must not be empty");
    Assert.hasText(this.user, "user must not be empty");
    Assert.isTrue(StringUtils.hasText(this.userInfoWrapper.getPassword()) || this.privateKey != null,
            "either a password or a private key is required");
    try {/* w  w  w.  ja v  a 2  s.c o m*/
        JSchSessionWrapper jschSession;
        if (this.isSharedSession) {
            this.sharedSessionLock.readLock().lock();
            try {
                if (this.sharedJschSession == null || !this.sharedJschSession.isConnected()) {
                    this.sharedSessionLock.readLock().unlock();
                    this.sharedSessionLock.writeLock().lock();
                    try {
                        if (this.sharedJschSession == null || !this.sharedJschSession.isConnected()) {
                            this.sharedJschSession = new JSchSessionWrapper(initJschSession());
                            try {
                                this.sharedJschSession.getSession().connect();
                            } catch (JSchException e) {
                                throw new IllegalStateException("failed to connect", e);
                            }
                        }
                    } finally {
                        this.sharedSessionLock.readLock().lock();
                        this.sharedSessionLock.writeLock().unlock();
                    }
                }
            } finally {
                this.sharedSessionLock.readLock().unlock();
            }
            jschSession = this.sharedJschSession;
        } else {
            jschSession = new JSchSessionWrapper(initJschSession());
        }
        SftpSession sftpSession = new SftpSession(jschSession);
        sftpSession.connect();
        jschSession.addChannel();
        return sftpSession;
    } catch (Exception e) {
        throw new IllegalStateException("failed to create SFTP Session", e);
    }
}