KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > XMLLogHelper


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.util;
38
39 import java.text.DateFormat JavaDoc;
40 import java.text.SimpleDateFormat JavaDoc;
41 import java.util.HashSet JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.Set JavaDoc;
44
45 import net.sourceforge.cruisecontrol.CruiseControlException;
46 import net.sourceforge.cruisecontrol.DateFormatFactory;
47 import net.sourceforge.cruisecontrol.Modification;
48
49 import org.jdom.Element;
50
51 /**
52  * Wrapper for the cruisecontrol build log. This class serves two purposes:<br>
53  * <ul>
54  * <li>Provide a convenient way to get relevant information about the build</li>
55  * <li>Abstract the build information so that the XML for the build log can change easily</li>
56  * </ul>
57  * <p>
58  * The CruiseControl log is expected to be in the following format: <p>
59  *
60  * <pre>
61  * <cruisecontrol>
62  * <info>
63  * <property name="label" value=""/>
64  * <property name="lastbuildtime" value=""/>
65  * <property name="lastgoodbuildtime" value=""/>
66  * <property name="lastbuildsuccessful" value=""/>
67  * <property name="buildfile" value=""/>
68  * <property name="buildtarget" value=""/>
69  * </info>
70  * <build error="">
71  * <properties>
72  * </properties>
73  * </build>
74  * <modifications>
75  * </modifications>
76  * </cruisecontrol>
77  * </pre>
78  *
79  * Note: buildtarget is only present when a target is forced via the JMX interface.
80  *
81  * @author Alden Almagro
82  * @author Jonny Boman
83  * @version $Id: XMLLogHelper.java,v 1.18 2006/01/21 02:02:48 pauljulius Exp $
84  */

85 public class XMLLogHelper {
86
87     private Element log;
88     private DateFormat JavaDoc dateFormat;
89
90     public XMLLogHelper(Element log) {
91         this(log, DateFormatFactory.getDateFormat());
92     }
93
94     // deprecated? not used...
95
public XMLLogHelper(Element log, String JavaDoc dateFormatString) {
96         this(log, new SimpleDateFormat JavaDoc(dateFormatString));
97     }
98
99     public XMLLogHelper(Element log, DateFormat JavaDoc dateFormat) {
100         this.log = log;
101         this.dateFormat = dateFormat;
102     }
103     
104     /**
105      * @return the build log name
106      */

107     public String JavaDoc getLogFileName() throws CruiseControlException {
108         return getCruiseControlInfoProperty("logfile");
109     }
110
111     /**
112      * @return the label for this build
113      */

114     public String JavaDoc getLabel() throws CruiseControlException {
115         return getCruiseControlInfoProperty("label");
116     }
117
118     public String JavaDoc getBuildTimestamp() throws CruiseControlException {
119         return getCruiseControlInfoProperty("cctimestamp");
120     }
121
122     /**
123      * @return true if the previous build was successful, false if it was not
124      */

125     public boolean wasPreviousBuildSuccessful() throws CruiseControlException {
126         return getCruiseControlInfoProperty("lastbuildsuccessful").equals("true");
127     }
128
129     /**
130      * @return true if the build was necessary
131      */

132     public boolean isBuildNecessary() {
133         if (log.getChild("build") != null && log.getChild("build").getAttributeValue("error") != null) {
134
135         return !log.getChild("build").getAttributeValue("error").equals("No Build Necessary"); }
136         return true;
137     }
138
139     /**
140      * @return project name as defined in the ant build file
141      */

142     public String JavaDoc getProjectName() throws CruiseControlException {
143         return getCruiseControlInfoProperty("projectname");
144     }
145
146     /**
147      * @return true if the build was successful, false otherwise
148      */

149     public boolean isBuildSuccessful() {
150         return (log.getChild("build").getAttribute("error") == null);
151     }
152
153     /**
154      * Looks in modifications/changelist/ or modifications/modification/user depending on SouceControl implementation.
155      * @return <code>Set</code> of usernames that have modified code since the last build
156      */

157     public Set JavaDoc getBuildParticipants() {
158         Set JavaDoc results = new HashSet JavaDoc();
159         if (isP4Modifications()) {
160
161             Iterator JavaDoc changelistIterator = log.getChild("modifications").getChildren("changelist").iterator();
162             while (changelistIterator.hasNext()) {
163                 Element changelistElement = (Element) changelistIterator.next();
164                 String JavaDoc val = changelistElement.getAttributeValue("email");
165                  if ((val == null) || (val.length() == 0)) {
166                    val = changelistElement.getAttributeValue("user");
167                  }
168                  results.add(val);
169             }
170         } else {
171             Iterator JavaDoc modificationIterator = log.getChild("modifications").getChildren("modification")
172                     .iterator();
173             while (modificationIterator.hasNext()) {
174                 Element modification = (Element) modificationIterator.next();
175                 Element emailElement = modification.getChild("email");
176                 if (emailElement == null) {
177                     emailElement = modification.getChild("user");
178                 }
179                 results.add(emailElement.getText());
180             }
181         }
182         return results;
183     }
184
185     private boolean isP4Modifications() {
186         return log.getChild("modifications").getChildren("changelist") != null
187                 && !log.getChild("modifications").getChildren("changelist").isEmpty();
188     }
189
190     /**
191      * @param propertyName the name of the ant property
192      * @return the value of the ant property
193      */

194     public String JavaDoc getAntProperty(String JavaDoc propertyName) throws CruiseControlException {
195         Iterator JavaDoc propertyIterator = log.getChild("build").getChild("properties").getChildren("property")
196                 .iterator();
197         while (propertyIterator.hasNext()) {
198             Element property = (Element) propertyIterator.next();
199             if (property.getAttributeValue("name").equals(propertyName)) { return property
200                     .getAttributeValue("value"); }
201         }
202         throw new CruiseControlException("Property: " + propertyName + " not found.");
203     }
204
205     public String JavaDoc getCruiseControlInfoProperty(String JavaDoc propertyName) throws CruiseControlException {
206         Iterator JavaDoc propertyIterator = log.getChild("info").getChildren("property").iterator();
207         while (propertyIterator.hasNext()) {
208             Element property = (Element) propertyIterator.next();
209             if (property.getAttributeValue("name").equals(propertyName)) { return property
210                     .getAttributeValue("value"); }
211         }
212         throw new CruiseControlException("Property: " + propertyName + " not found.");
213     }
214
215     public Set JavaDoc getModifications() {
216         Set JavaDoc results = new HashSet JavaDoc();
217         if (isP4Modifications()) {
218             //TODO: implement this
219
} else {
220             Iterator JavaDoc modificationIterator = log.getChild("modifications").getChildren("modification")
221                     .iterator();
222             while (modificationIterator.hasNext()) {
223                 Element modification = (Element) modificationIterator.next();
224                 Modification mod = new Modification();
225                 mod.fromElement(modification, dateFormat);
226                 results.add(mod);
227             }
228         }
229         return results;
230     }
231
232     public boolean isBuildFix() throws CruiseControlException {
233         return !this.wasPreviousBuildSuccessful() && this.isBuildSuccessful();
234     }
235     
236 }
237
Popular Tags