1 37 package net.sourceforge.cruisecontrol.publishers; 38 39 import java.io.File ; 40 import java.io.FileWriter ; 41 import java.io.IOException ; 42 43 import java.util.HashMap ; 44 import java.util.Map ; 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 67 public class RSSPublisher implements Publisher { 68 69 private static final Logger LOG = Logger.getLogger(RSSPublisher.class); 70 71 private static Map rssFeeds = new HashMap (); 74 75 private String fileName; 76 private String buildresultsurl; 77 private String channelLinkURL; 78 private int maxLength = 10; 79 80 private CruiseControlFeed rssFeed; 81 82 83 87 public static CruiseControlFeed getRSSFeed(File publishToFile) { 88 89 91 String pathToPublishFile; 92 try { 93 pathToPublishFile = publishToFile.getCanonicalPath(); 94 } catch (IOException ioe) { 95 pathToPublishFile = publishToFile.getAbsolutePath().toLowerCase(); 96 } 97 CruiseControlFeed rssfeed = (CruiseControlFeed) rssFeeds.get(pathToPublishFile); 98 99 if (rssfeed == null) { 100 rssfeed = new CruiseControlFeed(publishToFile); 102 rssFeeds.put(pathToPublishFile, rssfeed); 103 } 104 105 rssfeed.incrementProjectCount(); 106 return rssfeed; 107 } 108 109 110 115 public void publish(Element cruisecontrolLog) throws CruiseControlException { 116 117 XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog); 118 119 120 if (this.rssFeed == null) { 122 this.rssFeed = getRSSFeed(new File (this.fileName)); 123 124 this.rssFeed.setProjectName(helper.getProjectName()); 126 this.rssFeed.setMaxLength(this.maxLength); 127 this.rssFeed.setLink(this.channelLinkURL); 128 } 129 130 Item rssItem = new CruiseControlItem(helper, this.buildresultsurl); 132 rssFeed.addItem(rssItem); 133 134 publishFeed(); 136 } 137 138 139 protected void publishFeed() throws CruiseControlException { 140 141 FileWriter fw = null; 142 try { 143 fw = new FileWriter (fileName); 144 this.rssFeed.write(fw); 145 } catch (IOException ioe) { 146 throw new CruiseControlException("Error writing file: " + fileName, ioe); 147 } finally { 148 if (fw != null) { 149 try { 150 fw.close(); 151 } catch (IOException ignore) { 152 } 153 } 154 } 155 } 156 157 163 public void validate() throws CruiseControlException { 164 ValidationHelper.assertIsSet(fileName, "filename", this.getClass()); 165 ValidationHelper.assertIsSet(buildresultsurl, "buildresultsurl", this.getClass()); 166 } 167 168 171 public void setFile(String fileName) { 172 this.fileName = fileName.trim(); 174 } 175 176 180 public void setBuildResultsURL(String buildResultsURL) { 181 this.buildresultsurl = buildResultsURL.trim(); 182 } 183 184 185 189 public void setChannelLinkURL(String channelLinkURL) { 190 this.channelLinkURL = channelLinkURL.trim(); 191 } 192 193 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 |