KickJava   Java API By Example, From Geeks To Geeks.

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


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2006, 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.ManagedCommandline;
42 import net.sourceforge.cruisecontrol.util.ValidationHelper;
43 import net.sourceforge.cruisecontrol.util.XMLLogHelper;
44 import org.apache.log4j.Logger;
45 import org.jdom.CDATA;
46 import org.jdom.Element;
47
48 import java.util.ArrayList JavaDoc;
49 import java.util.Iterator JavaDoc;
50 import java.util.List JavaDoc;
51
52 /**
53  * Creates a ClearCase UCM baseline for the specified view's integration stream.
54  * Uses the value of the CruiseControl generated ${label} property as well as the
55  * value of the baselineprefix attribute (if specified) to name the baseline.
56  * A baseline is only created if UCM modifications are recorded in the build log.
57  * By default an incremental baseline is created although a full baseline can be
58  * created too (incremental baselines are recommended for Continuous Integration).
59  *
60  * @author <a HREF="mailto:kevin.lee@buildmeister.com">Kevin Lee</a>
61  */

62 public class ClearCaseBaselinePublisher implements Publisher {
63     private boolean full = false;
64     private String JavaDoc baselineprefix;
65     private String JavaDoc viewtag;
66     private String JavaDoc component;
67
68     private static final Logger LOG = Logger.getLogger(ClearCaseBaselinePublisher.class);
69
70     /**
71      * Set the baselineprefix flag
72      *
73      * @param baselineprefix the status to set the flag to
74      */

75     public void setBaselineprefix(String JavaDoc baselineprefix) {
76         this.baselineprefix = baselineprefix;
77     } // setBaselineprefix
78

79     /**
80      * Get baselineprefix flag status
81      *
82      * @return String containing status of baselineprefix flag
83      */

84     public String JavaDoc getBaselineprefix() {
85         return this.baselineprefix;
86     } // getBaselineprefix
87

88     /**
89      * Set the viewtag status flag
90      *
91      * @param viewtag the status to set the flag to
92      */

93     public void setViewtag(String JavaDoc viewtag) {
94         this.viewtag = viewtag;
95     } // setViewtag
96

97     /**
98      * Get viewtag flag status
99      *
100      * @return String containing status of viewtag flag
101      */

102     public String JavaDoc getViewtag() {
103         return this.viewtag;
104     } // getViewtag
105

106     /**
107      * Set the full flag
108      *
109      * @param full the status to set the flag to
110      */

111     public void setFull(boolean full) {
112         this.full = full;
113     } // setFull
114

115     /**
116      * Get full flag status
117      *
118      * @return boolean containing status of full flag
119      */

120     public boolean getFull() {
121         return this.full;
122     } // getFull
123

124     /**
125      * Set the component to generate the baseline for
126      *
127      * @param comp the name of the component
128      */

129     public void setComponent(String JavaDoc comp) {
130         this.component = comp;
131     } // setComponent
132

133     /**
134      * Get the component flag status
135      *
136      * @return the component the baseline is to be applied to
137      */

138     public String JavaDoc getComponent() {
139         return this.component;
140     } // getComponent
141

142     /*
143     * check whether the appropriate attributes have been set
144     */

145     public void validate() throws CruiseControlException {
146         ValidationHelper.assertIsSet(viewtag, "viewtag", this.getClass());
147     } // validate
148

149     /**
150      * extract the list of UCM modifications from the CruiseControl log
151      * (assumes the UCM sourcecontrol was used)
152      *
153      * @param log The Cruise Control log (as a JDOM element).
154      * @return a <code>List</code> of UCM activities
155      */

156     public List JavaDoc getActivities(Element log) {
157         List JavaDoc activityList = new ArrayList JavaDoc();
158
159         // get the modification list from the log
160
if (log != null) {
161             Element modifications = log.getChild("modifications");
162             if (modifications != null) {
163                 // from this list, extract all UCM activities
164
for (Iterator JavaDoc modificationList = modifications.getChildren(
165                         "modification").iterator(); modificationList.hasNext();) {
166                     Element modification = (Element) modificationList.next();
167                     String JavaDoc type = modification.getAttributeValue("type");
168                     if (type != null && type.equals("activity")) {
169                         String JavaDoc activity = modification.getChild("revision").getText();
170
171                         if (activity != null) {
172                             activityList.add(activity.trim());
173                         }
174                     }
175                 }
176             }
177         }
178         return activityList;
179
180     } // getActivities
181

182     /**
183      * determines if the publish should take place
184      *
185      * @param log the CruiseControl log (as a JDOM element).
186      * @return true if the build was successful and new activities were found
187      */

188     public boolean shouldPublish(Element log) {
189         // do not publish if no activities were found
190
List JavaDoc newActivities = getActivities(log);
191         if (newActivities.size() < 1) {
192             LOG.info("No UCM activities in build. Skipping publisher.");
193             return false;
194         }
195         return true;
196     } // shouldPublish
197

198     /* (non-Javadoc)
199     * @see net.sourceforge.cruisecontrol.Publisher#publish(org.jdom.Element)
200     */

201     public void publish(Element log) throws CruiseControlException {
202         XMLLogHelper helper = new XMLLogHelper(log);
203
204         // only publish if the build includes UCM activities
205
if (!shouldPublish(log)) {
206             return;
207         }
208
209         // should the baselinename include the prefix?
210
String JavaDoc baselinename;
211         if (getBaselineprefix() != null) {
212             baselinename = getBaselineprefix() + helper.getLabel();
213         } else {
214             baselinename = helper.getLabel();
215         }
216
217         // create the "cleartool mkbl" command line
218
ManagedCommandline cmd = new ManagedCommandline();
219         cmd.setExecutable("cleartool");
220         cmd.createArgument().setValue("mkbl");
221         cmd.createArgument().setValue("-view");
222         cmd.createArgument().setValue(getViewtag());
223         if (getFull()) {
224             cmd.createArgument().setValue("-full");
225         }
226         if (getComponent() != null) {
227             cmd.createArgument().setValue("-component");
228             cmd.createArgument().setValue(getComponent());
229         }
230         cmd.createArgument().setValue(baselinename);
231
232         // execute it
233
try {
234             cmd.execute();
235             cmd.assertExitCode(0);
236         } catch (Exception JavaDoc e) {
237             StringBuffer JavaDoc error = new StringBuffer JavaDoc("Failed to create baseline: ");
238             error.append(baselinename);
239             throw new CruiseControlException(error.toString(), e);
240         }
241
242         // log success
243
StringBuffer JavaDoc message = new StringBuffer JavaDoc("Created baseline: ");
244         message.append(baselinename);
245         LOG.info(message.toString());
246
247         // TODO: update ClearQuest records/activities with build number
248

249     } // publish
250

251     /**
252      * for testing
253      */

254     public static void main(String JavaDoc[] args) {
255
256         // create a fake cruisecontrol log
257
Element logElement = new Element("cruisecontrol");
258         // add a single modification
259
Element mods = new Element("modifications");
260         logElement.addContent(mods);
261         Element mod = new Element("modification");
262         mod.setAttribute("type", "activity");
263         mods.addContent(mod);
264         Element rev = new Element("revision");
265         rev.addContent(new CDATA("Some activitiy"));
266         mod.addContent(rev);
267         // and a build label
268
Element info = new Element("info");
269         logElement.addContent(info);
270         Element prop = new Element("property");
271         prop.setAttribute("name", "label");
272         prop.setAttribute("value", "1_TST");
273         info.addContent(prop);
274
275         ClearCaseBaselinePublisher baseline = new ClearCaseBaselinePublisher();
276         baseline.setViewtag("RatlBankModel_int");
277         //baseline.setFull(true);
278
//baseline.setComponent("RATLBANKMODEL_REL@\\RatlBankProjects");
279
//baseline.setbaselineprefix("testbl");
280
try {
281             baseline.publish(logElement);
282         } catch (CruiseControlException ex) {
283             ex.printStackTrace();
284         }
285     }
286
287 } // ClearCaseBaselinePublisher
288
Popular Tags