KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
40 import java.util.List JavaDoc;
41
42 import net.sourceforge.cruisecontrol.CruiseControlException;
43 import net.sourceforge.cruisecontrol.sourcecontrols.CMSynergy;
44 import net.sourceforge.cruisecontrol.util.ManagedCommandline;
45 import net.sourceforge.cruisecontrol.util.ValidationHelper;
46
47 import org.apache.log4j.Logger;
48 import org.jdom.Element;
49
50 /**
51  * After a successful build, this class copies newly checked in tasks to a given
52  * CM Synergy folder.
53  *
54  * @author <a HREF="mailto:rjmpsmith@gmail.com">Robert J. Smith </a>
55  */

56 public class CMSynergyTaskPublisher extends CMSynergyPublisher {
57     
58     private static final Logger LOG = Logger.getLogger(CMSynergyTaskPublisher.class);
59     private String JavaDoc folderNumber;
60     private String JavaDoc folderName;
61
62     /**
63      * Sets the name (or a substring of the name) of the folder which will
64      * recieve the new tasks. This folder must exist within the reconfigure
65      * properties of the given project.
66      *
67      * @param folderName The folder name
68      */

69     public void setFolderName(String JavaDoc folderName) {
70         this.folderName = folderName;
71     }
72     
73     /**
74      * The number of the folder which will recieve the new tasks.
75      *
76      * @param folderNumber The folder number
77      */

78     public void setFolderNumber(String JavaDoc folderNumber) {
79         this.folderNumber = folderNumber;
80     }
81     
82     /* (non-Javadoc)
83      * @see net.sourceforge.cruisecontrol.Publisher#publish(org.jdom.Element)
84      */

85     public void publish(Element log) throws CruiseControlException {
86
87         // Only publish upon a successful build which includes new tasks.
88
if (!shouldPublish(log)) {
89             return;
90         }
91         
92         // If need be, look up the folder number
93
if (folderNumber == null) {
94             folderNumber = getFolderNumber(folderName, getProject());
95         }
96
97         // Get a string based list of new tasks
98
StringBuffer JavaDoc tasks = new StringBuffer JavaDoc();
99         List JavaDoc newTasksList = getNewTasks(log);
100         Iterator JavaDoc newTasks = newTasksList.iterator();
101         for (int index = 0; newTasks.hasNext(); index++) {
102             if (index > 0) {
103                 tasks.append(",");
104             }
105             tasks.append(newTasks.next());
106         }
107         
108         LOG.info("Copying " + newTasksList.size() + " task(s) into folder "
109                 + folderNumber + ".");
110
111         // Create our CM Synergy command
112
ManagedCommandline cmd = CMSynergy.createCcmCommand(
113                 getCcmExe(), getSessionName(), getSessionFile());
114         cmd.createArgument().setValue("folder");
115         cmd.createArgument().setValue("-modify");
116         cmd.createArgument().setValue("-add_tasks");
117         cmd.createArgument().setValue(tasks.toString());
118         cmd.createArgument().setValue(folderNumber);
119
120         try {
121             cmd.execute();
122             cmd.assertExitCode(0);
123         } catch (Exception JavaDoc e) {
124             throw new CruiseControlException(
125                     "Failed to copy new tasks to folder " + folderNumber, e);
126         }
127     }
128     
129     /*
130      * (non-Javadoc)
131      *
132      * @see net.sourceforge.cruisecontrol.Publisher#validate()
133      */

134     public void validate() throws CruiseControlException {
135         ValidationHelper.assertFalse(folderNumber == null && folderName == null,
136             "Must specify either 'folderName' or 'folderNumber'");
137
138         ValidationHelper.assertFalse(folderName != null && getProject() == null,
139             "'project' attribute must be set when using the 'folderName' attribute.");
140     }
141     
142     /**
143      * Get the CM synergy folder number matching the folder with the given name
144      * in the given project.
145      *
146      * @param folderName
147      * A case sensitive substring of the folder name.
148      * @param project
149      * The CM Synergy project in which the folder exists (in 2 part
150      * name format).
151      *
152      * @return The folder number.
153      *
154      * @throws CruiseControlException
155      * if the folder number can not be found.
156      */

157     private String JavaDoc getFolderNumber(String JavaDoc folderName, String JavaDoc project) throws CruiseControlException {
158         // Get a list of folders in the project
159
ManagedCommandline cmd = CMSynergy.createCcmCommand(
160                 getCcmExe(), getSessionName(), getSessionFile());
161         cmd.createArgument().setValue("reconfigure_properties");
162         cmd.createArgument().setValue("-u");
163         cmd.createArgument().setValue("-f");
164         cmd.createArgument().setValue(
165                 "%description" + CMSynergy.CCM_ATTR_DELIMITER + "%name");
166         cmd.createArgument().setValue("-show");
167         cmd.createArgument().setValue("folders");
168         cmd.createArgument().setValue(project);
169
170         try {
171             cmd.execute();
172             cmd.assertExitCode(0);
173         } catch (Exception JavaDoc e) {
174             throw new CruiseControlException("Could not get a list of folders in project " + project, e);
175         }
176
177         // Try to find a matching folder name
178
for (Iterator JavaDoc folders = cmd.getStdoutAsList().iterator(); folders
179                 .hasNext();) {
180             String JavaDoc line = (String JavaDoc) folders.next();
181             if (line.indexOf(folderName) > -1) {
182                 int index = line.indexOf(CMSynergy.CCM_ATTR_DELIMITER);
183                 if (index == -1) {
184                     LOG.warn("Bad format in result: \"" + line + "\"");
185                     continue;
186                 }
187                 index += CMSynergy.CCM_ATTR_DELIMITER.length();
188                 return line.substring(index).trim();
189             }
190         }
191
192         // If we've gotten this far, no such folder exists in the project
193
throw new CruiseControlException("Could not find a folder matching \""
194                 + folderName + "\" in project \"" + project + "\".");
195     }
196
197 }
198
Popular Tags