Decoupling between SCP*Clients and SCPEngine

Encapsulate listener inside SCPEngine.
Remove string path in UploadClient.
This commit is contained in:
Cyril Ledru
2011-03-16 17:17:36 +08:00
committed by Shikhar Bhushan
parent aa9f4e192f
commit 286a22270b
3 changed files with 37 additions and 74 deletions

View File

@@ -148,7 +148,7 @@ public final class SCPDownloadClient {
final String dirname = dMsgParts[2];
if (length != 0)
throw new IOException("Remote SCP command sent strange directory length: " + length);
engine.listener.startedDir(dirname);
engine.startedDir(dirname);
{
f = FileTransferUtil.getTargetDirectory(f, dirname);
engine.signal("ACK: D");
@@ -157,15 +157,15 @@ public final class SCPDownloadClient {
setAttributes(f, parsePermissions(dMsgParts[0]), tMsg);
engine.signal("ACK: E");
}
engine.listener.finishedDir();
engine.finishedDir();
}
private void processFile(String cMsg, String tMsg, File f)
private void processFile(String cMsg, String tMsg, File f)
throws IOException {
final String[] cMsgParts = tokenize(cMsg, 3); // C<perms> <size> <filename>
final long length = parseLong(cMsgParts[1], "length");
final String filename = cMsgParts[2];
engine.listener.startedFile(filename, length);
engine.startedFile(length, filename);
{
f = FileTransferUtil.getTargetFile(f, filename);
engine.signal("Remote can start transfer");
@@ -179,10 +179,10 @@ public final class SCPDownloadClient {
setAttributes(f, parsePermissions(cMsgParts[0]), tMsg);
engine.signal("Transfer done");
}
engine.listener.finishedFile();
engine.finishedFile();
}
private void setAttributes(File f, int perms, String tMsg)
private void setAttributes(File f, int perms, String tMsg)
throws IOException {
modeSetter.setPermissions(f, perms);
if (tMsg != null && modeSetter.preservesTimes()) {

View File

@@ -24,6 +24,7 @@ import net.schmizz.sshj.common.IOUtils;
import net.schmizz.sshj.common.SSHException;
import net.schmizz.sshj.connection.channel.direct.Session.Command;
import net.schmizz.sshj.connection.channel.direct.SessionFactory;
import net.schmizz.sshj.xfer.LocalFile;
import net.schmizz.sshj.xfer.TransferListener;
import org.slf4j.Logger;
@@ -59,7 +60,7 @@ class SCPEngine {
final Logger log = LoggerFactory.getLogger(getClass());
final SessionFactory host;
final TransferListener listener;
private final TransferListener listener;
Command scp;
int exitStatus;
@@ -184,4 +185,27 @@ class SCPEngine {
throw new IOException("Had EOF before transfer completed");
}
void startedDir(final String dirname) {
listener.startedDir(dirname);
}
void startedDir(LocalFile f) {
listener.startedDir(f.getName());
}
void finishedDir() {
listener.finishedDir();
}
void startedFile(final long length, final String filename) {
listener.startedFile(filename, length);
}
void startedFile(LocalFile f) {
listener.startedFile(f.getName(), f.length());
}
void finishedFile() {
listener.finishedFile();
}
}

View File

@@ -15,9 +15,7 @@
*/
package net.schmizz.sshj.xfer.scp;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
@@ -36,9 +34,7 @@ public final class SCPUploadClient {
private final ModeGetter modeGetter;
private FileFilter fileFilter;
private SCPEngine engine;
private SCPEngine engine;
SCPUploadClient(SessionFactory host, TransferListener listener, ModeGetter modeGetter) {
engine = new SCPEngine(host, listener);
@@ -58,7 +54,6 @@ public final class SCPUploadClient {
}
public void setFileFilter(FileFilter fileFilter) {
this.fileFilter = fileFilter;
}
private synchronized void startCopy(LocalFile sourceFile, String targetPath)
@@ -68,14 +63,6 @@ public final class SCPUploadClient {
process(sourceFile);
}
private File[] getChildren(File f)
throws IOException {
File[] files = fileFilter == null ? f.listFiles() : f.listFiles(fileFilter);
if (files == null)
throw new IOException("Error listing files in directory: " + f);
return files;
}
private void init(String target)
throws SSHException {
List<Arg> args = new LinkedList<Arg>();
@@ -86,44 +73,21 @@ public final class SCPUploadClient {
engine.execSCPWith(args, target);
}
private void process(File f)
throws IOException {
if (f.isDirectory()) {
engine.listener.startedDir(f.getName());
sendDirectory(f);
engine.listener.finishedDir();
} else if (f.isFile()) {
engine.listener.startedFile(f.getName(), f.length());
sendFile(f);
engine.listener.finishedFile();
} else
throw new IOException(f + " is not a regular file or directory");
}
private void process(LocalFile f)
throws IOException {
if (f.isDirectory()) {
engine.listener.startedDir(f.getName());
engine.startedDir(f);
sendDirectory(f);
engine.listener.finishedDir();
engine.finishedDir();
} else if (f.isFile()) {
engine.listener.startedFile(f.getName(), f.length());
engine.startedFile(f);
sendFile(f);
engine.listener.finishedFile();
engine.finishedFile();
} else
throw new IOException(f + " is not a regular file or directory");
}
private void sendDirectory(File f)
throws IOException {
preserveTimeIfPossible(f);
engine.sendMessage("D0" + getPermString(f) + " 0 " + f.getName());
for (File child : getChildren(f))
process(child);
engine.sendMessage("E");
}
private void sendDirectory(LocalFile f)
private void sendDirectory(LocalFile f)
throws IOException {
preserveTimeIfPossible(f);
engine.sendMessage("D0" + getPermString(f) + " 0 " + f.getName());
@@ -132,20 +96,6 @@ public final class SCPUploadClient {
engine.sendMessage("E");
}
private void sendFile(File f)
throws IOException {
preserveTimeIfPossible(f);
final InputStream src = new FileInputStream(f);
try {
engine.sendMessage("C0" + getPermString(f) + " " + f.length() + " " + f.getName());
engine.transfer(src, engine.scp.getOutputStream(), engine.scp.getRemoteMaxPacketSize(), f.length());
engine.signal("Transfer done");
engine.check("Remote agrees transfer done");
} finally {
IOUtils.closeQuietly(src);
}
}
private void sendFile(LocalFile f)
throws IOException {
preserveTimeIfPossible(f);
@@ -160,23 +110,12 @@ public final class SCPUploadClient {
}
}
private void preserveTimeIfPossible(File f)
throws IOException {
if (modeGetter.preservesTimes())
engine.sendMessage("T" + modeGetter.getLastModifiedTime(f) + " 0 " + modeGetter.getLastAccessTime(f) + " 0");
}
private void preserveTimeIfPossible(LocalFile f)
throws IOException {
if (modeGetter.preservesTimes())
engine.sendMessage("T" + modeGetter.getLastModifiedTime(f) + " 0 " + modeGetter.getLastAccessTime(f) + " 0");
}
private String getPermString(File f)
throws IOException {
return Integer.toOctalString(modeGetter.getPermissions(f) & 07777);
}
private String getPermString(LocalFile f)
throws IOException {
return Integer.toOctalString(modeGetter.getPermissions(f) & 07777);