org/objectweb/proactive/core/process/filetransfer/SecureCopyProtocol.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.core.process.filetransfer;
00032 
00033 import java.util.ArrayList;
00034 
00035 import org.objectweb.proactive.core.process.filetransfer.FileTransferDefinition.*;
00036 
00037 
00046 public class SecureCopyProtocol extends AbstractCopyProtocol {
00047     protected String COMMAND = "scp";
00048     protected String PARAMS = "-pr";
00049 
00050     public SecureCopyProtocol(String name) {
00051         super(name);
00052     }
00053 
00057     public boolean startFileTransfer() {
00058         int globalReturnCode = 1; //default is unsuccess
00059 
00060         String homocommand = buildHomonymousTransfer();
00061         String[] heterocommand = buildHeteronymousTransfer();
00062 
00063         //Put homonymous and heteronymous on the same array
00064         String[] allCommands = new String[(homocommand.length() > 0)
00065             ? (heterocommand.length + 1) : heterocommand.length];
00066 
00067         if (homocommand.length() > 0) {
00068             allCommands[allCommands.length - 1] = homocommand;
00069         }
00070         for (int i = 0; i < heterocommand.length; i++)
00071             allCommands[i] = heterocommand[i];
00072 
00073         Process[] allProcess = new Process[allCommands.length];
00074 
00075         // DEBUG
00076         if (logger.isDebugEnabled()) {
00077             logger.debug(homocommand);
00078             for (int i = 0; i < heterocommand.length; i++)
00079                 logger.debug(heterocommand[i]);
00080         }
00081 
00082         /* TODO improove :
00083          *                 -The SDTOUT, STDIN, SDTERR handling and implement flag to close the sockets
00084          *                 -Make this a parallel copy?? (Does it make sense?)
00085          */
00086         for (int i = 0; i < allProcess.length; i++) {
00087             try {
00088                 int returnCode;
00089                 allProcess[i] = Runtime.getRuntime().exec(allCommands[i]);
00090                 returnCode = allProcess[i].waitFor();
00091                 if (returnCode != 0) { //it's safe to call this method because the stream is closed
00092                     logger.error(getErrorMessage(allProcess[i].getErrorStream()));
00093                 } else {
00094                     globalReturnCode = returnCode; //One succes is good enough to return global success
00095                 }
00096             } catch (Exception e) {
00097                 logger.error("Error forking scp");
00098                 e.printStackTrace();
00099             }
00100         }
00101 
00102         return globalReturnCode == 0; //0 if forked external process returned succesfully
00103     }
00104 
00105     private String buildHomonymousTransfer() {
00106         FileDescription[] files = getHomonymousAll();
00107 
00108         //No files to transfer
00109         if (files.length <= 0) {
00110             return "";
00111         }
00112 
00113         //command
00114         StringBuilder sb = new StringBuilder();
00115         sb.append(COMMAND);
00116 
00117         //arguments
00118         sb.append(" ").append(PARAMS);
00119 
00120         //files
00121         for (int i = 0; i < files.length; i++) {
00122             // prefix/filename
00123             String fullfilename = FileTransferWorkShop.buildFilePathString(srcInfoParams,
00124                     files[i].getSrcName());
00125 
00126             //Skip unreadable file
00127             if (!FileTransferWorkShop.isLocalReadable(fullfilename)) {
00128                 //Skip unreadable file          
00129                 if (logger.isDebugEnabled()) {
00130                     logger.debug("Skipping: " + fullfilename);
00131                 }
00132                 continue;
00133             }
00134 
00135             sb.append(" ");
00136             sb.append(fullfilename);
00137         }
00138 
00139         sb.append(" ");
00140         if (dstInfoParams.username.length() > 0) {
00141             sb.append(dstInfoParams.username).append("@");
00142         }
00143         sb.append(dstInfoParams.hostname).append(":");
00144         sb.append(dstInfoParams.getPrefix());
00145 
00146         return sb.toString();
00147     }
00148 
00149     private String[] buildHeteronymousTransfer() {
00150         FileDescription[] files = getHeteronymousAll();
00151         ArrayList<String> command = new ArrayList<String>();
00152 
00153         //Files & Dirs are the same
00154         for (int i = 0; i < files.length; i++) {
00155             String fullfilename = FileTransferWorkShop.buildFilePathString(srcInfoParams,
00156                     files[i].getSrcName());
00157 
00158             //Skip unreadable file
00159             if (!FileTransferWorkShop.isLocalReadable(fullfilename)) {
00160                 if (logger.isDebugEnabled()) {
00161                     logger.debug("Skipping: " + fullfilename);
00162                 }
00163                 continue;
00164             }
00165 
00166             StringBuilder sb = new StringBuilder();
00167 
00168             //command
00169             sb.append(COMMAND).append(" ").append(PARAMS).append(" ");
00170 
00171             //srcprefix/srcfilename
00172             sb.append(fullfilename);
00173 
00174             //hostname:destprefix/destfilename
00175             sb.append(" ");
00176             if (dstInfoParams.username.length() > 0) {
00177                 sb.append(dstInfoParams.username).append("@");
00178             }
00179             sb.append(dstInfoParams.hostname).append(":");
00180 
00181             sb.append(FileTransferWorkShop.buildFilePathString(dstInfoParams,
00182                     files[i].getDestName()));
00183 
00184             command.add(sb.toString());
00185         }
00186 
00187         return command.toArray(new String[0]);
00188     }
00189 
00193     public boolean checkProtocol() {
00194         boolean retval = true;
00195         if (dstInfoParams.hostname.length() <= 0) {
00196             logger.error("Destination hostname not specified");
00197             retval = false;
00198         }
00199 
00200         return retval;
00201     }
00202 
00203     static public void main(String[] args) {
00204         //Default is SCP_COMMAND
00205         FileTransferWorkShop fts = new FileTransferWorkShop("scp");
00206 
00207         FileTransferDefinition ft1 = new FileTransferDefinition("1");
00208         FileTransferDefinition ft2 = new FileTransferDefinition("2");
00209 
00210         ft1.addFile("homofile1", "homofile1");
00211         ft1.addFile("heterofile1A", "heterofile1B");
00212         ft1.addDir("homodir1", "homodir1");
00213         ft1.addDir("heterodir1A", "heterodir1B");
00214 
00215         ft2.addFile("homofile2", "homofile2");
00216         ft2.addFile("heterofile2A", "heterofile2B");
00217         ft2.addDir("homodir2", "homodir2");
00218         ft2.addDir("heterodir2A", "heterodir2B");
00219 
00220         fts.addFileTransfer(ft1);
00221         fts.addFileTransfer(ft2);
00222         fts.setFileTransferCopyProtocol("processDefault");
00223         fts.dstInfoParams.setInfoParameter("username", "mleyton");
00224         fts.dstInfoParams.setInfoParameter("hostname", "plugrid1.inria.fr");
00225         fts.dstInfoParams.setInfoParameter("prefix",
00226             "/auto/owenii/u/owenii/home/mleyton");
00227         fts.srcInfoParams.setInfoParameter("prefix", "/home/USER");
00228 
00229         CopyProtocol[] cp = fts.getCopyProtocols();
00230 
00231         for (int i = 0; i < cp.length; i++)
00232             cp[i].startFileTransfer();
00233     }
00234 }

Generated on Mon Jan 22 15:16:08 2007 for ProActive by  doxygen 1.5.1