KickJava   Java API By Example, From Geeks To Geeks.

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


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, 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.ArrayList JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Properties JavaDoc;
44
45 import org.apache.log4j.Logger;
46 import org.jdom.Element;
47
48 import net.sourceforge.cruisecontrol.Publisher;
49 import net.sourceforge.cruisecontrol.util.XMLLogHelper;
50
51 /**
52  * Provides an abstract base class to handle the functionality common to all CM
53  * Synergy publishers.
54  *
55  * @author <a HREF="mailto:rjmpsmith@gmail.com">Robert J. Smith </a>
56  */

57 public abstract class CMSynergyPublisher implements Publisher {
58
59     private static final Logger LOG = Logger.getLogger(CMSynergyPublisher.class);
60
61     /**
62      * The file which contains the mapping between CM Synergy session names
63      * and IDs.
64      */

65     private File JavaDoc sessionFile;
66     
67     /**
68      * The given name of the CM Synergy session to use.
69      */

70     private String JavaDoc sessionName;
71     
72     /**
73      * The CM Synergy project spec (2 part name) of the project we will
74      * be referencing.
75      */

76     private String JavaDoc projectSpec;
77
78     /**
79      * The CM Synergy executable used for executing commands. If not set,
80      * we will use the default value "ccm".
81      */

82     private String JavaDoc ccmExe;
83     
84     /**
85      * Sets the file which contains the mapping between CM Synergy session names
86      * and IDs. This file should be in the standard properties file format. Each
87      * line should map one name to a CM Synergy session ID (as returned by the
88      * "ccm status" command).
89      * <p>
90      * example:
91      * <br><br>
92      * session1=localhost:65024:192.168.1.17
93      *
94      * @param sessionFile
95      * The session file
96      */

97     public void setSessionFile(String JavaDoc sessionFile) {
98         this.sessionFile = new File JavaDoc(sessionFile);
99     }
100     
101     /**
102      * Returns the session file which maps CM Synergy session names to CM
103      * Synergy session IDs.
104      *
105      * @return The session file.
106      */

107     public File JavaDoc getSessionFile() {
108         return this.sessionFile;
109     }
110     
111     /**
112      * Sets the name of the CM Synergy session to use with this plugin. This
113      * name should appear in the specified session file.
114      *
115      * @param sessionName
116      * The session name
117      *
118      * @see #setSessionFile(String)
119      */

120     public void setSessionName(String JavaDoc sessionName) {
121         this.sessionName = sessionName;
122     }
123     
124     /**
125      * Returns the name of the CM Synergy session used with this publisher.
126      *
127      * @return The CM Synergy session name.
128      */

129     public String JavaDoc getSessionName() {
130         return this.sessionName;
131     }
132  
133     /**
134      * Sets the CM Synergy project to be used with this publisher.
135      *
136      * @param projectSpec
137      * The project spec (in 2 part name format).
138      */

139     public void setProject(String JavaDoc projectSpec) {
140         this.projectSpec = projectSpec;
141     }
142     
143     /**
144      * Gets the CM Synergy project to be used with this publisher.
145      *
146      * @return The CM Synergy project (in 2 part name format), or
147      * <code>null</code> if it was not set.
148      */

149     public String JavaDoc getProject() {
150         return this.projectSpec;
151     }
152     
153     /**
154      * Sets the name of the CM Synergy executable to use when issuing
155      * commands.
156      *
157      * @param ccmExe the name of the CM Synergy executable
158      */

159     public void setCcmExe(String JavaDoc ccmExe) {
160         this.ccmExe = ccmExe;
161     }
162     
163     /**
164      * Gets the full path of the ccm command line executable.
165      *
166      * @return The full path of the ccm command line executable, or
167      * <code>null</code> if it was not set.
168      */

169     public String JavaDoc getCcmExe() {
170         return this.ccmExe;
171     }
172     
173     /**
174      * Uses the log to determine if the build was successful.
175      *
176      * @param log
177      * The Cruise Control log (as a JDOM Element).
178      *
179      * @return <code>true</code> if the build was successful,
180      * <code>false</code> otherwise.
181      */

182     private boolean isBuildSuccessful(Element log) {
183         XMLLogHelper helper = new XMLLogHelper(log);
184         return helper.isBuildSuccessful();
185     }
186     
187     /**
188      * Extracts the build properties from the Cruise Control log
189      *
190      * @param log The log
191      *
192      * @return The properties set within the current build.
193      */

194     public Properties JavaDoc getBuildProperties(Element log) {
195         Properties JavaDoc buildProperties = new Properties JavaDoc();
196         
197         Iterator JavaDoc propertyIterator = log.getChild("info")
198                 .getChildren("property").iterator();
199         while (propertyIterator.hasNext()) {
200             Element property = (Element) propertyIterator.next();
201             buildProperties.put(property.getAttributeValue("name"), property
202                     .getAttributeValue("value"));
203
204         }
205         
206         return buildProperties;
207     }
208     
209     /**
210      * Extract a list of CM Synergy modifications from the Cruise Control log
211      *
212      * @param log
213      * The Cruise Control log (as a JDOM element).
214      *
215      * @return A <code>List</code> of new CM Synergy tasks
216      */

217     public List JavaDoc getNewTasks(Element log) {
218         List JavaDoc taskList = new ArrayList JavaDoc();
219
220         // Get the modification list from the log
221
Element modifications = log.getChild("modifications");
222         if (modifications != null) {
223             // From this list, extract all CM Synergy modifications (tasks)
224
for (Iterator JavaDoc modificationList = modifications.getChildren(
225                     "modification").iterator(); modificationList.hasNext();) {
226                 Element modification = (Element) modificationList.next();
227                 String JavaDoc type = modification.getAttributeValue("type");
228                 if (type != null && type.equals("ccmtask")) {
229                     String JavaDoc task = modification.getChild("task").getText();
230                     if (task != null) {
231                         taskList.add(task.trim());
232                     }
233                 }
234             }
235         }
236
237         return taskList;
238     }
239     
240     /**
241      * Determines if the publish should take place.
242      *
243      * @param log
244      * The Cruise Control log (as a JDOM element).
245      * @return true if the build was successful and new CM Synergy tasks were
246      * found, false otherwise.
247      */

248     public boolean shouldPublish(Element log) {
249         // Only publish upon a successful build.
250
if (!isBuildSuccessful(log)) {
251             LOG.info("Build failed. Skipping publisher.");
252             return false;
253         }
254
255         // Do not publish if no new tasks were found
256
List JavaDoc newTasks = getNewTasks(log);
257         if (newTasks.size() < 1) {
258             LOG.info("No new CM Synergy tasks in build. Skipping publisher.");
259             return false;
260         }
261         
262         return true;
263     }
264 }
265
Popular Tags