00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030  
00031 package org.objectweb.proactive.filetransfer;
00032 
00033 import java.io.File;
00034 import java.io.IOException;
00035 import java.util.Vector;
00036 
00037 import org.apache.log4j.Logger;
00038 import org.objectweb.proactive.core.ProActiveException;
00039 import org.objectweb.proactive.core.filetransfer.FileBlock;
00040 import org.objectweb.proactive.core.filetransfer.FileTransferEngine;
00041 import org.objectweb.proactive.core.filetransfer.FileTransferRequest;
00042 import org.objectweb.proactive.core.filetransfer.FileTransferService;
00043 import org.objectweb.proactive.core.filetransfer.OperationStatus;
00044 import org.objectweb.proactive.core.node.Node;
00045 import org.objectweb.proactive.core.util.log.Loggers;
00046 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00047 
00048 
00055 public class FileTransfer {
00056     static Logger logger = ProActiveLogger.getLogger(Loggers.FILETRANSFER);
00057 
00068     public static FileVector pullFile(Node node, File srcFile, File dstFile)
00069         throws IOException, ProActiveException {
00070         return pullFile(node, srcFile, dstFile, FileBlock.DEFAULT_BLOCK_SIZE,
00071             FileTransferService.DEFAULT_MAX_SIMULTANEOUS_BLOCKS);
00072     }
00073 
00074     public static FileVector pullFile(Node node, File srcFile, File dstFile,
00075         int bsize, int numFlyingBlocks) throws IOException, ProActiveException {
00076         File[] src = new File[1];
00077         File[] dst = new File[1];
00078         src[0] = srcFile;
00079         dst[0] = dstFile;
00080 
00081         return pullFiles(node, src, dst, bsize, numFlyingBlocks);
00082     }
00083 
00089     public static FileVector pullFiles(Node node, File[] srcFile, File[] dstFile)
00090         throws IOException, ProActiveException {
00091         return pullFiles(node, srcFile, dstFile, FileBlock.DEFAULT_BLOCK_SIZE,
00092             FileTransferService.DEFAULT_MAX_SIMULTANEOUS_BLOCKS);
00093     }
00094 
00095     public static FileVector pullFiles(Node node, File[] srcFile,
00096         File[] dstFile, int bsize, int numFlyingBlocks)
00097         throws IOException, ProActiveException {
00098         if (srcFile.length != dstFile.length) {
00099             throw new ProActiveException(
00100                 "Error, number destination and source file lists do not match in length");
00101         }
00102 
00103         FileVector fileWrapper = new FileVector();
00104         if (srcFile.length == 0) {
00105             return fileWrapper;
00106         }
00107 
00108         for (int i = 0; i < srcFile.length; i++) {
00109             
00110             if (dstFile[i].exists() && !dstFile[i].canWrite()) {
00111                 logger.error("Can't write to " + dstFile[i].getAbsoluteFile());
00112                 throw new IOException("Can't write to " +
00113                     dstFile[i].getAbsoluteFile());
00114             }
00115         }
00116         try {
00117             FileTransferService ftsRemote = FileTransferEngine.getFileTransferEngine()
00118                                                               .getFTS(node);
00119             FileTransferService ftsLocal = FileTransferEngine.getFileTransferEngine()
00120                                                              .getFTS();
00121 
00122             
00123             
00124             
00125             for (int i = 0; i < srcFile.length; i++) {
00126                 OperationStatus futureFile = ftsLocal.receiveFile(ftsRemote,
00127                         srcFile[i], dstFile[i], bsize, numFlyingBlocks);
00128                 FileTransferRequest ftr = new FileTransferRequest(srcFile[i],
00129                         dstFile[i], futureFile, ftsRemote, ftsLocal);
00130                 fileWrapper.add(ftr);
00131             }
00132 
00133             
00134             
00135             return fileWrapper;
00136         } catch (Exception e) {
00137             e.printStackTrace();
00138             throw new ProActiveException(
00139                 "Unable to connect or use ProActive Node: " + node);
00140         }
00141     }
00142 
00153     public static FileVector pushFile(Node node, File srcFile, File dstFile)
00154         throws IOException, ProActiveException {
00155         return pushFile(node, srcFile, dstFile, FileBlock.DEFAULT_BLOCK_SIZE,
00156             FileTransferService.DEFAULT_MAX_SIMULTANEOUS_BLOCKS);
00157     }
00158 
00159     public static FileVector pushFile(Node node, File srcFile, File dstFile,
00160         int bsize, int numFlyingBlocks) throws IOException, ProActiveException {
00161         File[] src = new File[1];
00162         File[] dst = new File[1];
00163         src[0] = srcFile;
00164         dst[0] = dstFile;
00165 
00166         return pushFiles(node, src, dst, bsize, numFlyingBlocks);
00167     }
00168 
00173     public static FileVector pushFiles(Node node, File[] srcFile, File[] dstFile)
00174         throws IOException, ProActiveException {
00175         return pushFiles(node, srcFile, dstFile, FileBlock.DEFAULT_BLOCK_SIZE,
00176             FileTransferService.DEFAULT_MAX_SIMULTANEOUS_BLOCKS);
00177     }
00178 
00179     public static FileVector pushFiles(Node node, File[] srcFile,
00180         File[] dstFile, int bsize, int numFlyingBlocks)
00181         throws IOException, ProActiveException {
00182         if (srcFile.length != dstFile.length) {
00183             throw new ProActiveException(
00184                 "Error, destination and source file lists do not match in length");
00185         }
00186 
00187         FileVector fileVector = new FileVector();
00188 
00189         if (srcFile.length == 0) {
00190             return fileVector;
00191         }
00192 
00193         for (int i = 0; i < srcFile.length; i++) {
00194             
00195             if (!srcFile[i].canRead()) {
00196                 logger.error("Can't read or doesn't exist: " +
00197                     srcFile[i].getAbsoluteFile());
00198                 throw new IOException("Can't read or doesn't exist: " +
00199                     srcFile[i].getAbsoluteFile());
00200             }
00201         }
00202         try {
00203             FileTransferService ftsLocal = FileTransferEngine.getFileTransferEngine()
00204                                                              .getFTS();
00205             FileTransferService ftsRemote = FileTransferEngine.getFileTransferEngine()
00206                                                               .getFTS(node);
00207 
00208             
00209             for (int i = 0; i < srcFile.length; i++) {
00210                 OperationStatus f = ftsLocal.sendFile(ftsRemote, srcFile[i],
00211                         dstFile[i], bsize, numFlyingBlocks); 
00212                 fileVector.add(new FileTransferRequest(srcFile[i], dstFile[i],
00213                         f, ftsLocal, ftsRemote));
00214             }
00215 
00216             
00217             
00218             return fileVector;
00219         } catch (Exception e) {
00220             e.printStackTrace();
00221             throw new ProActiveException(
00222                 "Unable to connect or use ProActive Node: " + node);
00223         }
00224     }
00225 
00226     static public FileVector pushFile(Node node, FileVector srcFile, File dst)
00227         throws IOException, ProActiveException {
00228         File[] f = new File[1];
00229         f[0] = dst;
00230 
00231         return pushFiles(node, srcFile, f);
00232     }
00233 
00244     static public FileVector pushFiles(Node node, FileVector srcFile, File[] dst)
00245         throws ProActiveException {
00246         if (srcFile.size() != dst.length) {
00247             throw new ProActiveException(
00248                 "Error, destination and source file lists do not match in length");
00249         }
00250 
00251         FileTransferService ftsLocal = FileTransferEngine.getFileTransferEngine()
00252                                                          .getFTS();
00253         FileTransferService ftsRemote = FileTransferEngine.getFileTransferEngine()
00254                                                           .getFTS(node);
00255 
00256         
00257         Vector requests = srcFile.getFilesRequest();
00258         FileVector fileWrapper = new FileVector();
00259         for (int i = 0; i < requests.size(); i++) {
00260             FileTransferRequest ftr = (FileTransferRequest) requests.get(i);
00261 
00262             
00263 
00264 
00265 
00266             FileTransferRequest newFTR = new FileTransferRequest(ftr.getDstFile(),
00267                     dst[i], null, ftr.getDestinationFTS(), ftsRemote);
00268             OperationStatus future = ftsLocal.submitFileTransferRequest(ftsLocal,
00269                     newFTR, ftr.getOperationFuture()); 
00270             newFTR.setDstFuture(future); 
00271             fileWrapper.add(newFTR);
00272         }
00273 
00274         return fileWrapper;
00275     }
00276 }