KickJava   Java API By Example, From Geeks To Geeks.

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


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 net.sourceforge.cruisecontrol.CruiseControlException;
40 import net.sourceforge.cruisecontrol.Publisher;
41 import net.sourceforge.cruisecontrol.util.ValidationHelper;
42 import net.sourceforge.cruisecontrol.util.XMLLogHelper;
43 import org.apache.tools.ant.Project;
44 import org.apache.tools.ant.taskdefs.Copy;
45 import org.apache.tools.ant.types.FileSet;
46 import org.apache.tools.ant.util.FileUtils;
47 import org.jdom.Element;
48
49 import java.io.File JavaDoc;
50 import java.io.IOException JavaDoc;
51
52 public class ArtifactsPublisher implements Publisher {
53
54     private Copy copier = new Copy();
55
56     private String JavaDoc destDir;
57     private String JavaDoc targetDirectory;
58     private String JavaDoc targetFile;
59     private String JavaDoc subdirectory;
60     private boolean publishOnFailure = true;
61
62     public void setDest(String JavaDoc dir) {
63         destDir = dir;
64     }
65
66     public void setDir(String JavaDoc pDir) {
67         targetDirectory = pDir;
68     }
69
70     public void setFile(String JavaDoc file) {
71         targetFile = file;
72     }
73
74     public void setPublishOnFailure(boolean shouldPublish) {
75         publishOnFailure = shouldPublish;
76     }
77
78     public void publish(Element cruisecontrolLog)
79             throws CruiseControlException {
80         XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog);
81         if (shouldPublish(helper.isBuildSuccessful())) {
82             Project project = new Project();
83             String JavaDoc timestamp = helper.getBuildTimestamp();
84             File JavaDoc destinationDirectory = getDestinationDirectory(timestamp);
85
86             if (targetDirectory != null) {
87                 publishDirectory(project, destinationDirectory);
88             }
89             if (targetFile != null) {
90                 publishFile(destinationDirectory);
91             }
92         }
93     }
94
95     protected boolean shouldPublish(final boolean buildSuccessful) {
96         return buildSuccessful || publishOnFailure;
97     }
98
99     File JavaDoc getDestinationDirectory(String JavaDoc timestamp) {
100         String JavaDoc targetDir = timestamp;
101         if (subdirectory != null) {
102             targetDir = timestamp + File.separatorChar + subdirectory;
103         }
104         return new File JavaDoc(destDir, targetDir);
105     }
106
107     void publishFile(File JavaDoc uniqueDest) throws CruiseControlException {
108         File JavaDoc file = new File JavaDoc(targetFile);
109         if (!file.exists()) {
110             throw new CruiseControlException("target file " + file.getAbsolutePath() + " does not exist");
111         }
112         FileUtils utils = FileUtils.newFileUtils();
113         try {
114             utils.copyFile(file, new File JavaDoc(uniqueDest, file.getName()));
115         } catch (IOException JavaDoc e) {
116             throw new CruiseControlException(e);
117         }
118     }
119
120     void publishDirectory(Project project, File JavaDoc uniqueDest) throws CruiseControlException {
121         File JavaDoc directory = new File JavaDoc(targetDirectory);
122         if (!directory.exists()) {
123             throw new CruiseControlException("target directory " + directory.getAbsolutePath() + " does not exist");
124         }
125         if (!directory.isDirectory()) {
126             throw new CruiseControlException("target directory " + directory.getAbsolutePath() + " is not a directory");
127         }
128         FileSet set = new FileSet();
129         set.setDir(directory);
130         copier.addFileset(set);
131         copier.setTodir(uniqueDest);
132         copier.setProject(project);
133         try {
134             copier.execute();
135         } catch (Exception JavaDoc e) {
136             throw new CruiseControlException(e);
137         }
138     }
139
140     public void validate() throws CruiseControlException {
141         ValidationHelper.assertIsSet(destDir, "dest", this.getClass());
142
143         ValidationHelper.assertFalse(targetDirectory == null && targetFile == null,
144             "'dir' or 'file' must be specified in configuration file.");
145
146         ValidationHelper.assertFalse(targetDirectory != null && targetFile != null,
147             "only one of 'dir' or 'file' may be specified.");
148     }
149
150     public void setSubdirectory(String JavaDoc subdir) {
151         subdirectory = subdir;
152     }
153 }
154
Popular Tags