Search in sources :

Example 31 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project cdap by caskdata.

the class SFTPFileSystem method open.

@Override
public FSDataInputStream open(Path f, int bufferSize) throws IOException {
    ChannelSftp channel = connect();
    Path workDir;
    try {
        workDir = new Path(channel.pwd());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    Path absolute = makeAbsolute(workDir, f);
    FileStatus fileStat = getFileStatus(channel, absolute);
    if (fileStat.isDirectory()) {
        disconnect(channel);
        throw new IOException(String.format(E_PATH_DIR, f));
    }
    InputStream is;
    try {
        // the path could be a symbolic link, so get the real path
        absolute = new Path("/", channel.realpath(absolute.toUri().getPath()));
        is = channel.get(absolute.toUri().getPath());
    } catch (SftpException e) {
        throw new IOException(e);
    }
    FSDataInputStream fis = new FSDataInputStream(new SFTPInputStream(is, channel, statistics));
    return fis;
}
Also used : Path(org.apache.hadoop.fs.Path) ChannelSftp(com.jcraft.jsch.ChannelSftp) FileStatus(org.apache.hadoop.fs.FileStatus) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) InputStream(java.io.InputStream) SftpException(com.jcraft.jsch.SftpException) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) IOException(java.io.IOException)

Example 32 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project cdap by caskdata.

the class SFTPConnectionPool method shutdown.

/** Shutdown the connection pool and close all open connections. */
synchronized void shutdown() {
    if (this.con2infoMap == null) {
        // already shutdown in case it is called
        return;
    }
    LOG.info("Inside shutdown, con2infoMap size=" + con2infoMap.size());
    this.maxConnection = 0;
    Set<ChannelSftp> cons = con2infoMap.keySet();
    if (cons != null && cons.size() > 0) {
        // make a copy since we need to modify the underlying Map
        Set<ChannelSftp> copy = new HashSet<ChannelSftp>(cons);
        // Initiate disconnect from all outstanding connections
        for (ChannelSftp con : copy) {
            try {
                disconnect(con);
            } catch (IOException ioe) {
                ConnectionInfo info = con2infoMap.get(con);
                LOG.error("Error encountered while closing connection to " + info.getHost(), ioe);
            }
        }
    }
    // make sure no further connections can be returned.
    this.idleConnections = null;
    this.con2infoMap = null;
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 33 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project cdap by caskdata.

the class SFTPConnectionPool method getFromPool.

synchronized ChannelSftp getFromPool(ConnectionInfo info) throws IOException {
    Set<ChannelSftp> cons = idleConnections.get(info);
    ChannelSftp channel;
    if (cons != null && cons.size() > 0) {
        Iterator<ChannelSftp> it = cons.iterator();
        if (it.hasNext()) {
            channel = it.next();
            idleConnections.remove(info);
            return channel;
        } else {
            throw new IOException("Connection pool error.");
        }
    }
    return null;
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) IOException(java.io.IOException)

Example 34 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project cdap by caskdata.

the class SFTPFileSystem method getHomeDirectory.

@Override
public Path getHomeDirectory() {
    ChannelSftp channel = null;
    try {
        channel = connect();
        Path homeDir = new Path(channel.pwd());
        return homeDir;
    } catch (Exception ioe) {
        return null;
    } finally {
        try {
            disconnect(channel);
        } catch (IOException ioe) {
            return null;
        }
    }
}
Also used : Path(org.apache.hadoop.fs.Path) ChannelSftp(com.jcraft.jsch.ChannelSftp) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SftpException(com.jcraft.jsch.SftpException)

Example 35 with ChannelSftp

use of com.jcraft.jsch.ChannelSftp in project bamboobsc by billchen198318.

the class SFtpClientUtils method put.

/**
	 * 本地檔案放到遠端SFTP上
	 * 	
	 * @param user
	 * @param password
	 * @param addr
	 * @param port
	 * @param localFile
	 * @param remoteFile
	 * @throws JSchException
	 * @throws SftpException
	 * @throws Exception
	 */
public static void put(String user, String password, String addr, int port, List<String> localFile, List<String> remoteFile) throws JSchException, SftpException, Exception {
    Session session = getSession(user, password, addr, port);
    Channel channel = session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp) channel;
    try {
        for (int i = 0; i < localFile.size(); i++) {
            String rf = remoteFile.get(i);
            String lf = localFile.get(i);
            logger.info("put local file: " + lf + " write to " + addr + " :" + rf);
            sftpChannel.put(lf, rf);
            logger.info("success write to " + addr + " :" + rf);
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    } finally {
        sftpChannel.exit();
        channel.disconnect();
        session.disconnect();
    }
}
Also used : ChannelSftp(com.jcraft.jsch.ChannelSftp) Channel(com.jcraft.jsch.Channel) SftpException(com.jcraft.jsch.SftpException) JSchException(com.jcraft.jsch.JSchException) Session(com.jcraft.jsch.Session)

Aggregations

ChannelSftp (com.jcraft.jsch.ChannelSftp)42 SftpException (com.jcraft.jsch.SftpException)24 IOException (java.io.IOException)15 JSchException (com.jcraft.jsch.JSchException)12 Session (com.jcraft.jsch.Session)12 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)8 File (java.io.File)7 CoreException (org.eclipse.core.runtime.CoreException)7 IStatus (org.eclipse.core.runtime.IStatus)7 Status (org.eclipse.core.runtime.Status)7 Channel (com.jcraft.jsch.Channel)6 JSch (com.jcraft.jsch.JSch)6 Path (org.apache.hadoop.fs.Path)6 InputStream (java.io.InputStream)5 SftpATTRS (com.jcraft.jsch.SftpATTRS)4 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 LinkedList (java.util.LinkedList)3 LsEntry (com.jcraft.jsch.ChannelSftp.LsEntry)2 OutputStream (java.io.OutputStream)2