}
// exec 'scp -t -p hadoop.jar' remotely
String command = "scp -p -t " + remoteFile;
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out = channel.getOutputStream();
final InputStream in = channel.getInputStream();
channel.connect();
if (checkAck(in) != 0) {
throw new CoreException(SSH_FAILED_STATUS1);
}
// send "C0644 filesize filename", where filename should not
// include '/'
long filesize = (new File(jarFile)).length();
command = "C0644 " + filesize + " ";
if (jarFile.lastIndexOf('/') > 0) {
command += jarFile.substring(jarFile.lastIndexOf('/') + 1);
} else {
command += jarFile;
}
command += "\n";
out.write(command.getBytes());
out.flush();
if (checkAck(in) != 0) {
throw new CoreException(SSH_FAILED_STATUS2);
}
// send a content of jarFile
fis = new FileInputStream(jarFile);
byte[] buf = new byte[1024];
while (true) {
int len = fis.read(buf, 0, buf.length);
if (len <= 0) {
break;
}
out.write(buf, 0, len); // out.flush();
}
fis.close();
fis = null;
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
if (checkAck(in) != 0) {
throw new CoreException(SSH_FAILED_STATUS3);
}
out.close();
channel.disconnect();
// move the jar file to a temp directory
String jarDir = "/tmp/hadoopjar"
+ new VMID().toString().replace(':', '_');
command = "mkdir " + jarDir + ";mv " + remoteFile + " " + jarDir;
channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.connect();
channel.disconnect();
session.disconnect();
// we create a new session with a zero timeout to prevent the
// console stream