org/objectweb/proactive/core/process/filetransfer/FileTransferDefinition.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.io.Serializable;
00034 import java.util.ArrayList;
00035 
00036 import org.apache.log4j.Logger;
00037 import org.objectweb.proactive.core.util.log.Loggers;
00038 import org.objectweb.proactive.core.util.log.ProActiveLogger;
00039 
00040 
00048 public class FileTransferDefinition implements Serializable {
00049     protected static Logger logger = ProActiveLogger.getLogger(Loggers.DEPLOYMENT_FILETRANSFER);
00050     private ArrayList<FileDescription> all;
00051     private ArrayList<FileDescription> files;
00052     private ArrayList<FileDescription> homonymousFiles;
00053     private ArrayList<FileDescription> heteronymousFiles;
00054     private ArrayList<DirectoryDescription> directories;
00055     private ArrayList<DirectoryDescription> homonymousDirs;
00056     private ArrayList<DirectoryDescription> heteronymousDirs;
00057     String name;
00058 
00059     public FileTransferDefinition(String name) {
00060         all = new ArrayList<FileDescription>();
00061         files = new ArrayList<FileDescription>();
00062         directories = new ArrayList<DirectoryDescription>();
00063         homonymousDirs = new ArrayList<DirectoryDescription>();
00064         homonymousFiles = new ArrayList<FileDescription>();
00065         heteronymousDirs = new ArrayList<DirectoryDescription>();
00066         heteronymousFiles = new ArrayList<FileDescription>();
00067 
00068         this.name = name;
00069     }
00070 
00071     public FileTransferDefinition() {
00072         this("");
00073     }
00074 
00075     public void addFile(String srcname, String destname) {
00076         if ((srcname == null) || (srcname.length() < 0) || (destname == null) ||
00077                 (destname.length() < 0)) {
00078             logger.debug("Discarding empty or null filename");
00079 
00080             return;
00081         }
00082 
00083         FileDescription newFile = new FileDescription(srcname, destname);
00084 
00085         if (newFile.isHomonymous()) {
00086             homonymousFiles.add(newFile);
00087         } else {
00088             heteronymousFiles.add(newFile);
00089         }
00090 
00091         files.add(newFile);
00092         all.add(newFile);
00093     }
00094 
00095     public void addDir(String srcname, String destname, String includes,
00096         String excludes) {
00097         if ((srcname == null) || (srcname.length() < 0) || (destname == null) ||
00098                 (destname.length() < 0)) {
00099             logger.debug("Discarding empty or null directory name");
00100 
00101             return;
00102         }
00103 
00104         DirectoryDescription newDir = new DirectoryDescription(srcname,
00105                 destname, includes, excludes);
00106 
00107         if (newDir.isHomonymous()) {
00108             homonymousDirs.add(newDir);
00109         } else {
00110             heteronymousDirs.add(newDir);
00111         }
00112 
00113         directories.add(newDir);
00114         all.add(newDir);
00115     }
00116 
00117     public void addDir(String srcname, String destname) {
00118         addDir(srcname, destname, "", "");
00119     }
00120 
00124     public FileDescription[] getAll() {
00125         return all.toArray(new FileDescription[0]);
00126     }
00127 
00128     public FileDescription[] getAllFiles() {
00129         return files.toArray(new FileDescription[0]);
00130     }
00131 
00132     public DirectoryDescription[] getAllDirectories() {
00133         return directories.toArray(new DirectoryDescription[0]);
00134     }
00135 
00136     public FileDescription[] getHomonymousFile() {
00137         return homonymousFiles.toArray(new FileDescription[0]);
00138     }
00139 
00140     public DirectoryDescription[] getHomonymousDir() {
00141         return homonymousDirs.toArray(new DirectoryDescription[0]);
00142     }
00143 
00144     public FileDescription[] getHeteronymousFile() {
00145         return heteronymousFiles.toArray(new FileDescription[0]);
00146     }
00147 
00148     public DirectoryDescription[] getHeteronymousDir() {
00149         return heteronymousDirs.toArray(new DirectoryDescription[0]);
00150     }
00151 
00152     public String getId() {
00153         return name;
00154     }
00155 
00156     public void setId(String name) {
00157         this.name = name;
00158     }
00159 
00160     public String toString() {
00161         StringBuilder sb = new StringBuilder();
00162 
00163         for (int i = 0; i < files.size(); i++)
00164             sb.append(files.get(i)).append("\n");
00165 
00166         for (int i = 0; i < directories.size(); i++)
00167             sb.append(directories.get(i)).append("\n");
00168 
00169         while ((sb.length() > 0) && (sb.charAt(sb.length() - 1) == '\n'))
00170             sb.deleteCharAt(sb.length() - 1);
00171 
00172         return sb.toString();
00173     }
00174 
00175     public boolean equals(FileTransferDefinition fileTransfer) {
00176         return (name.length() > 0) && (fileTransfer.getId().length() > 0) &&
00177         name.equalsIgnoreCase(fileTransfer.getId());
00178     }
00179 
00180     public boolean isEmpty() {
00181         return ((files.size() == 0) && (directories.size() == 0));
00182     }
00183 
00184     public class FileDescription implements Serializable {
00185         String srcName;
00186         String destName;
00187 
00188         public FileDescription(String srcname, String destname) {
00189             //Trim white spaces
00190             srcname = srcname.trim();
00191             destname = destname.trim();
00192 
00193             /* Delete ending slashes
00194              * Note: This can give trouble when copying to the root "/" !!!
00195              */
00196             if (srcname.endsWith("/") || srcname.endsWith("\\")) {
00197                 this.srcName = srcname.substring(0, srcname.length() - 1);
00198             } else {
00199                 this.srcName = srcname;
00200             }
00201 
00202             if (destname.endsWith("/") || destname.endsWith("\\")) {
00203                 this.destName = destname.substring(0, destname.length() - 1);
00204             } else {
00205                 this.destName = destname;
00206             }
00207         }
00208 
00209         public String toString() {
00210             return "src:" + srcName + " dest:" + destName;
00211         }
00212 
00213         public boolean isHomonymous() {
00214             return srcName.equals(destName) || (destName.length() <= 0);
00215         }
00216 
00220         public String getDestName() {
00221             return destName;
00222         }
00223 
00227         public String getSrcName() {
00228             return srcName;
00229         }
00230 
00231         public boolean isDir() {
00232             return false;
00233         }
00234     }
00235 
00236     public class DirectoryDescription extends FileDescription {
00237         String includes;
00238         String excludes;
00239 
00240         public DirectoryDescription(String srcname, String destname,
00241             String includes, String excludes) {
00242             super(srcname, destname);
00243 
00244             this.includes = includes;
00245             this.excludes = excludes;
00246         }
00247 
00248         public String toString() {
00249             return super.toString() + " inc:" + includes + " exc:" + excludes;
00250         }
00251 
00252         public boolean isDir() {
00253             return true;
00254         }
00255     }
00256 }

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