KickJava   Java API By Example, From Geeks To Geeks.

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


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;
38
39 import java.io.File JavaDoc;
40 import java.io.FileWriter JavaDoc;
41 import java.io.IOException JavaDoc;
42
43 import java.util.HashMap JavaDoc;
44 import java.util.Map JavaDoc;
45
46 import org.apache.log4j.Logger;
47
48 import org.jdom.Element;
49
50 import net.sourceforge.cruisecontrol.CruiseControlException;
51 import net.sourceforge.cruisecontrol.Publisher;
52 import net.sourceforge.cruisecontrol.util.XMLLogHelper;
53 import net.sourceforge.cruisecontrol.util.ValidationHelper;
54
55 import net.sourceforge.cruisecontrol.publishers.rss.CruiseControlFeed;
56 import net.sourceforge.cruisecontrol.publishers.rss.CruiseControlItem;
57 import net.sourceforge.cruisecontrol.publishers.rss.Item;
58
59
60 /**
61  * Publisher Plugin which publishes the most recent build information to an
62  * RSS (Really Simple Syndication) Feed.
63  *
64  * Copyright (c) 2005 Hewlett-Packard Development Company, L.P.
65  * @author Patrick Conant
66  */

67 public class RSSPublisher implements Publisher {
68
69     private static final Logger LOG = Logger.getLogger(RSSPublisher.class);
70
71     //Map to hold all RSSFeed instances. The RSSFeeds are keyed according to
72
// filename so that multiple projects can share a single RSSFeed.
73
private static Map JavaDoc rssFeeds = new HashMap JavaDoc();
74
75     private String JavaDoc fileName;
76     private String JavaDoc buildresultsurl;
77     private String JavaDoc channelLinkURL;
78     private int maxLength = 10;
79
80     private CruiseControlFeed rssFeed;
81
82
83     /**
84      * Static method that allows multiple project RSSPublishers to share a
85      * single RSSFeed instance.
86      */

87     public static CruiseControlFeed getRSSFeed(File JavaDoc publishToFile) {
88
89         // FIXME not thread safe
90

91         String JavaDoc pathToPublishFile;
92         try {
93             pathToPublishFile = publishToFile.getCanonicalPath();
94         } catch (IOException JavaDoc ioe) {
95             pathToPublishFile = publishToFile.getAbsolutePath().toLowerCase();
96         }
97         CruiseControlFeed rssfeed = (CruiseControlFeed) rssFeeds.get(pathToPublishFile);
98
99         if (rssfeed == null) {
100             //Create a new RSS Feed and add it to the collection.
101
rssfeed = new CruiseControlFeed(publishToFile);
102             rssFeeds.put(pathToPublishFile, rssfeed);
103         }
104
105         rssfeed.incrementProjectCount();
106         return rssfeed;
107     }
108
109
110     /**
111      * Define the publishing.
112      *
113      * @param cruisecontrolLog JDOM Element representation of the main cruisecontrol build log
114      */

115     public void publish(Element cruisecontrolLog) throws CruiseControlException {
116
117         XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog);
118
119
120         // Get a reference to the RSSFeed
121
if (this.rssFeed == null) {
122             this.rssFeed = getRSSFeed(new File JavaDoc(this.fileName));
123
124             // Ensure that the rssFeed matches the config properties of the publisher.
125
this.rssFeed.setProjectName(helper.getProjectName());
126             this.rssFeed.setMaxLength(this.maxLength);
127             this.rssFeed.setLink(this.channelLinkURL);
128         }
129
130         // Create the RSSFeedItem
131
Item rssItem = new CruiseControlItem(helper, this.buildresultsurl);
132         rssFeed.addItem(rssItem);
133
134         // Publish the feed.
135
publishFeed();
136     }
137
138
139     protected void publishFeed() throws CruiseControlException {
140
141         FileWriter JavaDoc fw = null;
142         try {
143             fw = new FileWriter JavaDoc(fileName);
144             this.rssFeed.write(fw);
145         } catch (IOException JavaDoc ioe) {
146             throw new CruiseControlException("Error writing file: " + fileName, ioe);
147         } finally {
148             if (fw != null) {
149                 try {
150                     fw.close();
151                 } catch (IOException JavaDoc ignore) {
152                 }
153             }
154         }
155     }
156
157     /**
158      * Called after the configuration is read to make sure that all the mandatory parameters
159      * were specified..
160      *
161      * @throws CruiseControlException if there was a configuration error.
162      */

163     public void validate() throws CruiseControlException {
164         ValidationHelper.assertIsSet(fileName, "filename", this.getClass());
165         ValidationHelper.assertIsSet(buildresultsurl, "buildresultsurl", this.getClass());
166     }
167
168     /**
169      * Set the name of the file to which the RSS feed should be pushed.
170      */

171     public void setFile(String JavaDoc fileName) {
172         // FIXME do we need to trim() here? isn't it enforced by the API
173
this.fileName = fileName.trim();
174     }
175
176     /**
177      * Set the build results URL. This should be of the format:
178      * http://[SERVER]/cruisecontrol/buildresults/[OPTIONAL_PROJECT_NAME]
179      */

180     public void setBuildResultsURL(String JavaDoc buildResultsURL) {
181         this.buildresultsurl = buildResultsURL.trim();
182     }
183
184
185     /**
186      * Set the channel link URL. In many newsreaders, this is the URL linked
187      * from the feed title.
188      */

189     public void setChannelLinkURL(String JavaDoc channelLinkURL) {
190         this.channelLinkURL = channelLinkURL.trim();
191     }
192
193     /**
194      * The maximum number of entries to include in the RSS feed. Default is 10.
195      */

196     public void setMaxLength(int max) {
197         if (max > 0) {
198             this.maxLength = max;
199         } else {
200             LOG.warn("ignoring command to set maxRecords to invalud value (" + max + ");");
201         }
202     }
203 }
204
Popular Tags