Author: lahiru
Date: Mon Aug 19 16:40:23 2013
New Revision: 1515505
URL: http://svn.apache.org/r1515505
Log:
fixing build error and make scpTo work.
Added:
airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SCPTo.java
- copied, changed from r1514868,
airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SCPFrom.java
airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/SCPToTest.java
- copied, changed from r1514869,
airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/SCPFromTest.java
Removed:
airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SCPFrom.java
airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/SCPFromTest.java
Copied:
airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SCPTo.java
(from r1514868,
airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SCPFrom.java)
URL:
http://svn.apache.org/viewvc/airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SCPTo.java?p2=airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SCPTo.java&p1=airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SCPFrom.java&r1=1514868&r2=1515505&rev=1515505&view=diff
==============================================================================
---
airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SCPFrom.java
(original)
+++
airavata/sandbox/gsissh/src/main/java/org/apache/airavata/gsi/ssh/api/SCPTo.java
Mon Aug 19 16:40:23 2013
@@ -30,9 +30,14 @@ import java.io.*;
/**
* This class is going to be useful to SCP a file to a remote grid machine
using my proxy credentials
*/
-public class SCPFrom {
- private static final org.slf4j.Logger log =
LoggerFactory.getLogger(SCPFrom.class);
+public class SCPTo {
+ private static final org.slf4j.Logger log =
LoggerFactory.getLogger(SCPTo.class);
+ static {
+ JSch.setConfig("gssapi-with-mic.x509",
"org.apache.airavata.gsi.ssh.GSSContextX509");
+ JSch.setConfig("userauth.gssapi-with-mic",
"com.jcraft.jsch.UserAuthGSSAPIWithMICGSSCredentials");
+
+ }
private ServerInfo serverInfo;
private AuthenticationInfo authenticationInfo;
@@ -40,7 +45,7 @@ public class SCPFrom {
private ConfigReader configReader;
- public SCPFrom(ServerInfo serverInfo, AuthenticationInfo
authenticationInfo, String certificateLocation, ConfigReader configReader) {
+ public SCPTo(ServerInfo serverInfo, AuthenticationInfo authenticationInfo,
String certificateLocation, ConfigReader configReader) {
System.setProperty("X509_CERT_DIR", certificateLocation);
this.serverInfo = serverInfo;
this.authenticationInfo = authenticationInfo;
@@ -55,8 +60,8 @@ public class SCPFrom {
* @throws JSchException
* @throws SSHApiException
*/
- public void scpFrom(String rFile, String lFile) throws IOException,
JSchException, SSHApiException {
- FileOutputStream fos = null;
+ public void scpTo(String rFile, String lFile) throws IOException,
JSchException, SSHApiException {
+ FileInputStream fis = null;
String prefix = null;
if (new File(lFile).isDirectory()) {
prefix = lFile + File.separator;
@@ -94,8 +99,10 @@ public class SCPFrom {
+ serverInfo.getUserName(), e);
}
- // exec 'scp -f rFile' remotely
- String command = "scp -f " + rFile;
+ boolean ptimestamp = true;
+
+ // exec 'scp -t rfile' remotely
+ String command="scp " + (ptimestamp ? "-p" :"") +" -t "+rFile;
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
@@ -105,106 +112,83 @@ public class SCPFrom {
channel.connect();
- byte[] buf = new byte[1024];
-
- // send '\0'
- buf[0] = 0;
- out.write(buf, 0, 1);
- out.flush();
-
- while (true) {
- int c = checkAck(in);
- if (c != 'C') {
- break;
- }
-
- // read '0644 '
- in.read(buf, 0, 5);
-
- long filesize = 0L;
- while (true) {
- if (in.read(buf, 0, 1) < 0) {
- // error
- break;
- }
- if (buf[0] == ' ') break;
- filesize = filesize * 10L + (long) (buf[0] - '0');
- }
-
- String file = null;
- for (int i = 0; ; i++) {
- in.read(buf, i, 1);
- if (buf[i] == (byte) 0x0a) {
- file = new String(buf, 0, i);
- break;
- }
- }
-
- //System.out.println("filesize="+filesize+", file="+file);
-
- // send '\0'
- buf[0] = 0;
- out.write(buf, 0, 1);
- out.flush();
-
- // read a content of lFile
- fos = new FileOutputStream(prefix == null ? lFile : prefix + file);
- int foo;
- while (true) {
- if (buf.length < filesize) foo = buf.length;
- else foo = (int) filesize;
- foo = in.read(buf, 0, foo);
- if (foo < 0) {
- // error
- break;
- }
- fos.write(buf, 0, foo);
- filesize -= foo;
- if (filesize == 0L) break;
- }
- fos.close();
- fos = null;
-
- if (checkAck(in) != 0) {
+ if(checkAck(in)!=0){
+ System.exit(0);
+ }
+
+ File _lfile = new File(lFile);
+
+ if(ptimestamp){
+ command="T "+(_lfile.lastModified()/1000)+" 0";
+ // The access time should be sent here,
+ // but it is not accessible with JavaAPI ;-<
+ command+=(" "+(_lfile.lastModified()/1000)+" 0\n");
+ out.write(command.getBytes()); out.flush();
+ if(checkAck(in)!=0){
System.exit(0);
- }
-
- // send '\0'
- buf[0] = 0;
- out.write(buf, 0, 1);
- out.flush();
- }
-
- session.disconnect();
+ }
+ }
- System.exit(0);
- }
+ // send "C0644 filesize filename", where filename should not
include '/'
+ long filesize=_lfile.length();
+ command="C0644 "+filesize+" ";
+ if(lFile.lastIndexOf('/')>0){
+ command+=lFile.substring(lFile.lastIndexOf('/')+1);
+ }
+ else{
+ command+=lFile;
+ }
+ command+="\n";
+ out.write(command.getBytes()); out.flush();
+ if(checkAck(in)!=0){
+ System.exit(0);
+ }
+
+ // send a content of lFile
+ fis=new FileInputStream(lFile);
+ 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){
+ System.exit(0);
+ }
+ out.close();
- static int checkAck(InputStream in) throws IOException {
- int b = in.read();
- // b may be 0 for success,
- // 1 for error,
- // 2 for fatal error,
- // -1
- if (b == 0) return b;
- if (b == -1) return b;
-
- if (b == 1 || b == 2) {
- StringBuffer sb = new StringBuffer();
- int c;
- do {
- c = in.read();
- sb.append((char) c);
- }
- while (c != '\n');
- if (b == 1) { // error
- System.out.print(sb.toString());
- }
- if (b == 2) { // fatal error
- System.out.print(sb.toString());
- }
- }
- return b;
+ channel.disconnect();
+ session.disconnect();
}
+ static int checkAck(InputStream in) throws IOException{
+ int b=in.read();
+ // b may be 0 for success,
+ // 1 for error,
+ // 2 for fatal error,
+ // -1
+ if(b==0) return b;
+ if(b==-1) return b;
+
+ if(b==1 || b==2){
+ StringBuffer sb=new StringBuffer();
+ int c;
+ do {
+ c=in.read();
+ sb.append((char)c);
+ }
+ while(c!='\n');
+ if(b==1){ // error
+ System.out.print(sb.toString());
+ }
+ if(b==2){ // fatal error
+ System.out.print(sb.toString());
+ }
+ }
+ return b;
+ }
}
Copied:
airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/SCPToTest.java
(from r1514869,
airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/SCPFromTest.java)
URL:
http://svn.apache.org/viewvc/airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/SCPToTest.java?p2=airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/SCPToTest.java&p1=airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/SCPFromTest.java&r1=1514869&r2=1515505&rev=1515505&view=diff
==============================================================================
---
airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/SCPFromTest.java
(original)
+++
airavata/sandbox/gsissh/src/test/java/org/apache/airavata/gsi/ssh/config/SCPToTest.java
Mon Aug 19 16:40:23 2013
@@ -21,7 +21,7 @@
package org.apache.airavata.gsi.ssh.config;
import org.apache.airavata.gsi.ssh.api.AuthenticationInfo;
-import org.apache.airavata.gsi.ssh.api.SCPFrom;
+import org.apache.airavata.gsi.ssh.api.SCPTo;
import org.apache.airavata.gsi.ssh.api.ServerInfo;
import org.apache.airavata.gsi.ssh.impl.MyProxyAuthenticationInfo;
import org.testng.annotations.BeforeTest;
@@ -29,7 +29,7 @@ import org.testng.annotations.Test;
import java.io.File;
-public class SCPFromTest {
+public class SCPToTest {
private String myProxyUserName;
private String myProxyPassword;
private String certificateLocation;
@@ -69,8 +69,7 @@ public class SCPFromTest {
= new MyProxyAuthenticationInfo(myProxyUserName,
myProxyPassword, "myproxy.teragrid.org",
7512, 17280000);
ServerInfo serverInfo = new ServerInfo("ogce" ,"trestles.sdsc.edu");
- SCPFrom scpFrom = new
SCPFrom(serverInfo,authenticationInfo,this.certificateLocation,new
ConfigReader());
-
-// scpFrom.scpFrom(rFilePath, lFilePath);
+ SCPTo scpTo = new
SCPTo(serverInfo,authenticationInfo,this.certificateLocation,new
ConfigReader());
+ scpTo.scpTo(rFilePath, lFilePath);
}
}