use of com.jcraft.jsch.ChannelSftp in project gradle by gradle.
the class SftpResourceUploader method upload.
@Override
public void upload(LocalResource resource, URI destination) throws IOException {
LockableSftpClient client = sftpClientFactory.createSftpClient(destination, credentials);
try {
ChannelSftp channel = client.getSftpClient();
ensureParentDirectoryExists(channel, destination);
InputStream sourceStream = resource.open();
try {
channel.put(sourceStream, destination.getPath());
} finally {
sourceStream.close();
}
} catch (com.jcraft.jsch.SftpException e) {
throw ResourceExceptions.putFailed(destination, e);
} finally {
sftpClientFactory.releaseSftpClient(client);
}
}
use of com.jcraft.jsch.ChannelSftp in project hadoop by apache.
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 hadoop by apache.
the class SFTPFileSystem method create.
/**
* A stream obtained via this call must be closed before using other APIs of
* this class or else the invocation will block.
*/
@Override
public FSDataOutputStream create(Path f, FsPermission permission, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress) throws IOException {
final ChannelSftp client = connect();
Path workDir;
try {
workDir = new Path(client.pwd());
} catch (SftpException e) {
throw new IOException(e);
}
Path absolute = makeAbsolute(workDir, f);
if (exists(client, f)) {
if (overwrite) {
delete(client, f, false);
} else {
disconnect(client);
throw new IOException(String.format(E_FILE_EXIST, f));
}
}
Path parent = absolute.getParent();
if (parent == null || !mkdirs(client, parent, FsPermission.getDefault())) {
parent = (parent == null) ? new Path("/") : parent;
disconnect(client);
throw new IOException(String.format(E_CREATE_DIR, parent));
}
OutputStream os;
try {
client.cd(parent.toUri().getPath());
os = client.put(f.getName());
} catch (SftpException e) {
throw new IOException(e);
}
FSDataOutputStream fos = new FSDataOutputStream(os, statistics) {
@Override
public void close() throws IOException {
super.close();
disconnect(client);
}
};
return fos;
}
use of com.jcraft.jsch.ChannelSftp in project GNS by MobilityFirst.
the class SFTPUpload method localFileNewer.
/**
*
* @param user
* @param host
* @param keyFile
* @param fileToTransfer
* @param sftpWorkingDirectory
* @return true if the local file is newer
*/
public static boolean localFileNewer(String user, String host, File keyFile, String fileToTransfer, String sftpWorkingDirectory) {
if (verbose) {
System.out.println("Local File Newer Check " + fileToTransfer + " to " + host + "@" + user + " " + sftpWorkingDirectory);
}
try {
ChannelSftp channelSftp = authenticateSftp(user, host, keyFile);
Path paths = Paths.get(fileToTransfer);
String localDir = paths.getParent().toString();
channelSftp.cd(sftpWorkingDirectory);
channelSftp.lcd(localDir);
SftpATTRS remoteAttributes = channelSftp.stat(paths.getFileName().toString());
long localTime = new File(fileToTransfer).lastModified();
long remoteTime = remoteAttributes.getMTime() * 1000L;
if (verbose) {
System.out.println("L: " + localDir + " R: " + sftpWorkingDirectory + "\n" + "Local time = " + localTime + " Remote time = " + remoteTime);
}
if (verbose) {
System.out.println("Result " + (localTime > remoteTime));
}
return localTime > remoteTime;
} catch (JSchException | SftpException e) {
System.out.println("Exception while checking for file newer:" + e);
return false;
}
}
use of com.jcraft.jsch.ChannelSftp in project GNS by MobilityFirst.
the class SFTPUpload method uploadFile.
/**
*
* @param user
* @param host
* @param keyFile
* @param fileToTransfer
* @param sftpWorkingDirectory
*/
public static void uploadFile(String user, String host, File keyFile, String fileToTransfer, String sftpWorkingDirectory) {
if (verbose) {
System.out.println("Upload file from " + fileToTransfer + " to " + host + "@" + user + " " + sftpWorkingDirectory);
}
try {
ChannelSftp channelSftp = authenticateSftp(user, host, keyFile);
File f = new File(fileToTransfer);
channelSftp.put(new FileInputStream(f), f.getName());
} catch (JSchException | SftpException | FileNotFoundException e) {
System.out.println("Exception while uploading file:" + e);
}
}
Aggregations