KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > publishers > FTPPublisher


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.publishers;
38
39 import java.io.File JavaDoc;
40 import java.util.Vector JavaDoc;
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 /**
53  * Publishes the XML log file and the published artifacts via FTP to
54  * a remote host. The ArtifactsPublisher must be listed before this
55  * task in order to publish the artifacts.
56  *
57  * @author <a HREF="groboclown@users.sourceforge.net">Matt Albrecht</a>
58  */

59 public class FTPPublisher extends AbstractFTPClass implements Publisher {
60
61     private static final Logger LOG = Logger.getLogger(FTPPublisher.class);
62     
63     private String JavaDoc destdir;
64     private String JavaDoc srcdir;
65     private boolean deleteArtifacts = false;
66
67
68     /**
69      * The remote directory to put the artifacts into.
70      */

71     public void setDestDir(String JavaDoc dir) {
72         this.destdir = dir;
73     }
74
75
76     public void setSrcDir(String JavaDoc 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         // put the log files
98
XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog);
99         String JavaDoc uniqueDir = helper.getBuildTimestamp();
100         
101         File JavaDoc logDir = new File JavaDoc(srcdir + File.separator + uniqueDir);
102         
103         Vector JavaDoc knownDirs = new Vector JavaDoc();
104         FTPClient ftp = null;
105         try {
106             ftp = openFTP();
107             
108             // maybe text, maybe bin. Set it to bin!
109
setBinary(ftp);
110             
111             // get the log file
112
String JavaDoc logName = getLogFileName(srcdir, uniqueDir);
113             String JavaDoc lname = destdir + File.separator + logName;
114             File JavaDoc lf = new File JavaDoc(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             // prevent exceptions if the directory doesn't exist.
126
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 JavaDoc basedir, FTPClient ftp, String JavaDoc destdir,
142             Vector JavaDoc knownDirs)
143             throws CruiseControlException {
144         String JavaDoc[] fileList = basedir.list();
145         if (fileList == null) {
146             return;
147         }
148         for (int i = 0; i < fileList.length; ++i) {
149             String JavaDoc fname = destdir + File.separator + fileList[i];
150             File JavaDoc f = new File JavaDoc(basedir, fileList[i]);
151             if (f.exists()) {
152                 if (f.isDirectory()) {
153                     // recursively add the file.
154
// Since we delete after this, removing the directory
155
// should work.
156
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     /**
170      * Since build failures mark the log file as "log[date].xml", and
171      * successes mark the log file as "log[date]L[label].xml", we
172      * need a good way to track down which log file to use.
173      */

174     private String JavaDoc getLogFileName(String JavaDoc srcdir, String JavaDoc uniqueDir) {
175         File JavaDoc dir = new File JavaDoc(srcdir);
176         String JavaDoc basename = "log" + uniqueDir;
177         if (dir.exists() && dir.isDirectory()) {
178             String JavaDoc[] 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         // no logfile with the uniquedir was found, so consider the
186
// build as failed.
187
return basename + ".xml";
188     }
189 }
190
Popular Tags