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;
}
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;
}
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;
}
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;
}
}
}
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();
}
}
Aggregations