Skip to content

Commit

Permalink
Fixed JumpMind#716
Browse files Browse the repository at this point in the history
  • Loading branch information
gwilmer committed May 7, 2017
1 parent 47a6eef commit b894d1e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class BinaryFileWriter extends AbstractFileWriter {

public static final String TYPE = "Binary File Writer";

IDirectory streamable;

@Override
public void start() {
init();
Expand All @@ -24,15 +26,15 @@ public void start() {
@Override
public void handle(Message inputMessage, ISendMessageCallback callback, boolean unitOfWorkBoundaryReached) {

IDirectory streamable = (IDirectory) getResourceReference();
streamable = (IDirectory) getResourceReference();
if (inputMessage instanceof BinaryMessage) {
BinaryMessage message = (BinaryMessage) inputMessage;
String fileName = getFileName(inputMessage);
if (!append) {
streamable.delete(fileName);
streamable.delete(fileName, false);
}

OutputStream fos = streamable.getOutputStream(fileName, mustExist);
OutputStream fos = streamable.getOutputStream(fileName, mustExist, false, false);

try {
fos.write(message.getPayload());
Expand All @@ -54,5 +56,10 @@ public void handle(Message inputMessage, ISendMessageCallback callback, boolean
public boolean supportsStartupMessages() {
return false;
}

@Override
public void stop() {
streamable.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,9 @@ private boolean fileExists(ChannelSftp sftp, String filePath) throws SftpExcepti

@Override
public void close() {
close(true);
}

@Override
public void close(boolean success) {
Session session = threadSession.get();
Expand All @@ -220,12 +221,12 @@ public void close(boolean success) {
ChannelSftp channel = entry.getValue();
if (channel != null) {
channel.disconnect();
channels.remove(entry.getKey());
}
}
channels.clear();
threadChannels.set(null);
}
}
}

@Override
public boolean requiresContentLength() {
Expand Down Expand Up @@ -283,6 +284,11 @@ protected ChannelSftp openConnectedChannel() throws JSchException {
Session session = openSession();
ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
channel.connect();
try {
channel.cd(basePath);
} catch (SftpException e) {
throw new IoException(e);
}
return channel;
}

Expand All @@ -306,6 +312,11 @@ protected ChannelSftp openConnectedChannel(int channelId) throws JSchException {
}
if (!channel.isConnected()) {
channel.connect();
try {
channel.cd(basePath);
} catch (SftpException e) {
throw new IoException(e);
}
}
channels.put(channelId, channel);
threadChannels.set(channels);
Expand Down Expand Up @@ -482,7 +493,6 @@ public OutputStream getOutputStream(String relativePath, boolean mustExist, bool
session = openSession();
// Get a reusable channel if the session is not auto closed.
sftp = (closeSession) ? openConnectedChannel() : openConnectedChannel(CHANNEL_OUT);
sftp.cd(basePath);
createRelativePathDirectoriesIfNecessary(sftp, relativePath, mustExist);
return new CloseableOutputStream(sftp.put(relativePath, ChannelSftp.OVERWRITE), session, sftp, closeSession);
} catch (Exception e) {
Expand Down

0 comments on commit b894d1e

Please sign in to comment.