1 37 package net.sourceforge.cruisecontrol.publishers; 38 39 import java.io.File ; 40 import java.util.Vector ; 41 42 import net.sourceforge.cruisecontrol.CruiseControlException; 43 import net.sourceforge.cruisecontrol.Publisher; 44 import net.sourceforge.cruisecontrol.util.AbstractFTPClass; 45 import net.sourceforge.cruisecontrol.util.ValidationHelper; 46 import net.sourceforge.cruisecontrol.util.XMLLogHelper; 47 48 import org.apache.commons.net.ftp.FTPClient; 49 import org.apache.log4j.Logger; 50 import org.jdom.Element; 51 52 59 public class FTPPublisher extends AbstractFTPClass implements Publisher { 60 61 private static final Logger LOG = Logger.getLogger(FTPPublisher.class); 62 63 private String destdir; 64 private String srcdir; 65 private boolean deleteArtifacts = false; 66 67 68 71 public void setDestDir(String dir) { 72 this.destdir = dir; 73 } 74 75 76 public void setSrcDir(String dir) { 77 this.srcdir = dir; 78 } 79 80 81 public void setDeleteArtifacts(boolean shouldDelete) { 82 deleteArtifacts = shouldDelete; 83 } 84 85 86 public void validate() throws CruiseControlException { 87 ValidationHelper.assertIsSet(destdir, "destdir", this.getClass()); 88 ValidationHelper.assertIsSet(srcdir, "srcdir", this.getClass()); 89 super.validate(); 90 } 91 92 93 94 public void publish(Element cruisecontrolLog) 95 throws CruiseControlException { 96 97 XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog); 99 String uniqueDir = helper.getBuildTimestamp(); 100 101 File logDir = new File (srcdir + File.separator + uniqueDir); 102 103 Vector knownDirs = new Vector (); 104 FTPClient ftp = null; 105 try { 106 ftp = openFTP(); 107 108 setBinary(ftp); 110 111 String logName = getLogFileName(srcdir, uniqueDir); 113 String lname = destdir + File.separator + logName; 114 File lf = new File (srcdir, logName); 115 if (lf.exists()) { 116 makeDirsForFile(ftp, lname, knownDirs); 117 sendFile(ftp, lf, lname); 118 if (deleteArtifacts) { 119 lf.delete(); 120 } 121 } else { 122 LOG.info("Could not find build log file " + lf + "."); 123 } 124 125 if (logDir.exists()) { 127 LOG.info("Sending log dir " + logDir + "."); 128 ftpDir(logDir, ftp, destdir + File.separator + uniqueDir, knownDirs); 129 LOG.info("Done sending log dir " + logDir + "."); 130 } else { 131 LOG.info("Could not find artifacts directory " + logDir + "."); 132 } 133 } catch (CruiseControlException e) { 134 LOG.error("FTPPublisher.publish()", e); 135 } finally { 136 closeFTP(ftp); 137 } 138 } 139 140 141 private void ftpDir(File basedir, FTPClient ftp, String destdir, 142 Vector knownDirs) 143 throws CruiseControlException { 144 String [] fileList = basedir.list(); 145 if (fileList == null) { 146 return; 147 } 148 for (int i = 0; i < fileList.length; ++i) { 149 String fname = destdir + File.separator + fileList[i]; 150 File f = new File (basedir, fileList[i]); 151 if (f.exists()) { 152 if (f.isDirectory()) { 153 ftpDir(f, ftp, fname, knownDirs); 157 } else { 158 makeDirsForFile(ftp, fname, knownDirs); 159 sendFile(ftp, f, fname); 160 } 161 if (deleteArtifacts) { 162 f.delete(); 163 } 164 } 165 } 166 } 167 168 169 174 private String getLogFileName(String srcdir, String uniqueDir) { 175 File dir = new File (srcdir); 176 String basename = "log" + uniqueDir; 177 if (dir.exists() && dir.isDirectory()) { 178 String [] list = dir.list(); 179 for (int i = 0; i < list.length; ++i) { 180 if (list[i].startsWith(basename) && list[i].endsWith(".xml")) { 181 return list[i]; 182 } 183 } 184 } 185 return basename + ".xml"; 188 } 189 } 190 | Popular Tags |