KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > publishers > rss > CruiseControlItem


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2005, 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.rss;
38
39 import java.io.File JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Collections JavaDoc;
42 import java.util.Comparator JavaDoc;
43 import java.util.Date JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import java.util.List JavaDoc;
46 import net.sourceforge.cruisecontrol.Modification;
47 import net.sourceforge.cruisecontrol.CruiseControlException;
48 import net.sourceforge.cruisecontrol.util.XMLLogHelper;
49 import net.sourceforge.cruisecontrol.util.DateUtil;
50 import org.apache.log4j.Logger;
51
52 /**
53  * A generic RSS Feed Item (includes the contents of an RSS feed between the
54  * &lt:item> and </item> tags).
55  *
56  * Copyright (c) 2005 Hewlett-Packard Development Company, L.P.
57  * @author Patrick Conant
58  */

59 public class CruiseControlItem extends Item {
60
61     private static final Logger LOG = Logger.getLogger(CruiseControlItem.class);
62
63     /**
64      * Construct from an XMLLogHelper.
65      */

66     public CruiseControlItem(XMLLogHelper logHelper, String JavaDoc buildResultsURL) throws CruiseControlException {
67         super();
68         this.setTitle(createTitle(logHelper));
69         this.setLink(createLink(logHelper, buildResultsURL));
70         this.setDescription(createDescription(logHelper));
71         try {
72             this.setPublishDate(DateUtil.parseFormattedTime(logHelper.getBuildTimestamp(), "cctimestamp"));
73         } catch (CruiseControlException ccex) {
74             // default to the current date -- won't be too far off.
75
this.setPublishDate(new Date JavaDoc());
76         }
77     }
78
79
80
81
82     /**
83      * Create a title based on the contents of an XML log file. This method
84      * is largely copied from the e-mail publisher classes.
85      */

86     private String JavaDoc createTitle(XMLLogHelper logHelper) throws CruiseControlException {
87
88         StringBuffer JavaDoc title = new StringBuffer JavaDoc();
89         title.append(logHelper.getProjectName());
90         if (logHelper.isBuildSuccessful()) {
91             String JavaDoc label = logHelper.getLabel();
92             if (label.length() > 0) {
93                 title.append(" ");
94                 title.append(label);
95             }
96
97             // Anytime the build is "fixed" the title line
98
// should read "fixed".
99
if (logHelper.isBuildFix()) {
100                 title.append(" Build Fixed");
101             } else {
102                 title.append(" Build Successful");
103             }
104         } else {
105             title.append(" Build Failed");
106         }
107         return title.toString();
108     }
109
110     /**
111      * Create a link to the build results URL based on the contents of the
112      * XML log file. THis method is borrowed from the email publisher classes.
113      */

114     private String JavaDoc createLink(XMLLogHelper logHelper, String JavaDoc buildResultsURL) throws CruiseControlException {
115
116         if (buildResultsURL == null) {
117             return "";
118         }
119         String JavaDoc logFileName = logHelper.getLogFileName();
120
121         int startName = logFileName.lastIndexOf(File.separator) + 1;
122         int endName = logFileName.lastIndexOf(".");
123         String JavaDoc baseLogFileName = logFileName.substring(startName, endName);
124         StringBuffer JavaDoc url = new StringBuffer JavaDoc(buildResultsURL);
125
126         if (buildResultsURL.indexOf("?") == -1) {
127             url.append("?");
128         } else {
129             url.append("&");
130         }
131         url.append("log=");
132         url.append(baseLogFileName);
133
134         return url.toString();
135     }
136
137     /**
138      * To compare modifications happening in the same project.
139      */

140     static class ModificationComparator implements Comparator JavaDoc {
141         public int compare(Object JavaDoc o1, Object JavaDoc o2) {
142             Modification mod1 = (Modification) o1;
143             Modification mod2 = (Modification) o2;
144             long modifiedTimeDifference = mod1.modifiedTime.getTime() - mod2.modifiedTime.getTime();
145             if (modifiedTimeDifference != 0) {
146                 return modifiedTimeDifference > 0 ? +1 : -1;
147             }
148             return mod1.getFileName().compareTo(mod2.getFileName());
149         }
150     }
151
152     private String JavaDoc createDescription(XMLLogHelper logHelper) throws CruiseControlException {
153         StringBuffer JavaDoc description = new StringBuffer JavaDoc();
154
155         // Write out the build time and label
156
description.append("<em>Build Time:</em> ");
157         try {
158             description.append(DateUtil.parseFormattedTime(logHelper.getBuildTimestamp(), "cctimestamp"));
159         } catch (CruiseControlException ccex) {
160             LOG.error("exception trying to resolve cctimestamp", ccex);
161             description.append("not available");
162         } catch (NullPointerException JavaDoc npe) { // FIXME why is that possible?
163
LOG.error("NPE trying to resolve cctimestamp", npe);
164             description.append("not available");
165         }
166
167         description.append("<br/>");
168
169         description.append("<em>Label:</em> ");
170         if (logHelper.getLabel() != null) {
171             description.append(logHelper.getLabel());
172         }
173         description.append("<br/>");
174
175         // Write out all of the modifications...
176
description.append("<em>Modifications: </em>");
177         try {
178             final List JavaDoc modifications = new ArrayList JavaDoc(logHelper.getModifications());
179             Collections.sort(modifications, new ModificationComparator());
180             description.append(modifications.size());
181             Iterator JavaDoc it = modifications.iterator();
182             while (it.hasNext()) {
183                 Modification mod = (Modification) it.next();
184                 description.append("<li>");
185                 description.append(mod.getFileName());
186                 description.append(" by ");
187                 if (mod.userName != null) {
188                     description.append(mod.userName);
189                 } else {
190                     description.append("[no user]");
191                 }
192                 description.append(" (");
193                 if (mod.comment != null) {
194                     description.append(mod.comment);
195                 } else {
196                     description.append("[no comment]");
197                 }
198                 description.append(")</li>");
199             }
200             description.append("</ul>");
201         } catch (NullPointerException JavaDoc npe) { // FIXME
202
LOG.error("NPE trying to build String representation of modifications in description", npe);
203             //throws NullPointerException during tests for XMLLogHelpers
204
// generated from scratch...
205
description.append("0");
206         }
207         description.append("<br/>\n<ul>");
208
209         return description.toString();
210     }
211 }
212
Popular Tags