1 package net.sourceforge.cruisecontrol.listeners; 2 3 import java.io.BufferedReader ; 4 import java.io.File ; 5 import java.io.FileReader ; 6 import java.io.FileWriter ; 7 import java.io.IOException ; 8 import java.text.DateFormat ; 9 import java.util.ArrayList ; 10 import java.util.Date ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import net.sourceforge.cruisecontrol.CruiseControlException; 15 import net.sourceforge.cruisecontrol.DateFormatFactory; 16 import net.sourceforge.cruisecontrol.Listener; 17 import net.sourceforge.cruisecontrol.ProjectEvent; 18 import net.sourceforge.cruisecontrol.ProjectState; 19 import net.sourceforge.cruisecontrol.util.CurrentBuildFileWriter; 20 import net.sourceforge.cruisecontrol.util.ValidationHelper; 21 22 import org.apache.log4j.Logger; 23 24 43 public class CurrentBuildStatusPageListener 44 implements Listener { 45 private static final Logger LOG = Logger.getLogger(CurrentBuildStatusPageListener.class); 46 47 private String dstFileName; 48 49 private File sourceFile = null; 50 51 private List sourceText = new ArrayList (); 52 53 private static final String DEFAULT_TEXT = "{Project}: {State.Date} - {State.Name}: {State.Description}"; 54 55 private List history = new ArrayList (); 56 57 private static final String KEY_PROJECT = "{project}"; 58 private static final String KEY_NAME = "{state.name}"; 59 private static final String KEY_DESC = "{state.description}"; 60 private static final String KEY_DATE = "{state.date}"; 61 private static final String KEY_DURATION = "{state.duration}"; 62 private static final String KEY_HISTORY = "{history}"; 63 64 69 private class HistoryItem { 70 public HistoryItem(ProjectState projstate) { 71 state = projstate.getName(); 72 desc = projstate.getDescription(); 73 when = System.currentTimeMillis(); 74 } 75 76 public String state; 77 public String desc; 78 public long when; 79 } 80 81 82 85 public CurrentBuildStatusPageListener() { 86 sourceText.add(DEFAULT_TEXT); 87 } 88 89 90 public void handleEvent(ProjectEvent event) 91 throws CruiseControlException { 92 if (!(event instanceof ProjectStateChangedEvent)) { 93 LOG.debug("ignoring event " + event.getClass().getName() + " for project " + event.getProjectName()); 95 return; 96 } 97 98 final ProjectStateChangedEvent stateChanged = (ProjectStateChangedEvent) event; 99 final ProjectState newState = stateChanged.getNewState(); 100 LOG.debug("updating status to " + newState.getName() + " for project " + stateChanged.getProjectName()); 101 102 HistoryItem hist = new HistoryItem(newState); 103 String result = substituteText(hist, stateChanged.getProjectName()); 104 history.add(0, hist); 105 writeFile(result); 106 } 107 108 109 114 private String substituteText(HistoryItem current, String projectName) { 115 StringBuffer result = new StringBuffer (); 116 Iterator lineIter = sourceText.iterator(); 117 118 while (lineIter.hasNext()) { 119 String src = (String ) lineIter.next(); 120 121 if (src.toLowerCase().startsWith(KEY_HISTORY)) { 123 src = src.substring(KEY_HISTORY.length()); 124 Iterator histIter = history.iterator(); 125 long prevtime = current.when; 126 127 while (histIter.hasNext()) { 128 HistoryItem hist = (HistoryItem) histIter.next(); 129 result.append(substituteItems(src, projectName, hist, prevtime)); 130 result.append('\n'); 131 prevtime = hist.when; 132 } 133 } else { 134 result.append(substituteItems(src, projectName, current, 0)); 136 result.append('\n'); 137 } 138 139 } 140 141 return result.toString(); 142 } 143 144 145 152 private String substituteItems(String src, String projectName, HistoryItem current, long prevtime) { 153 int idx; 154 StringBuffer result = new StringBuffer (); 155 DateFormat dateFmt = DateFormatFactory.getDateFormat(); 156 157 while ((idx = src.indexOf('{')) != -1) { 159 if (idx > 0) { 160 result.append(src.substring(0, idx)); 161 } 162 163 src = src.substring(idx); 165 int skiplen; 166 167 if (src.toLowerCase().startsWith(KEY_PROJECT)) { 168 result.append(projectName); 169 skiplen = KEY_PROJECT.length(); 170 } else if (src.toLowerCase().startsWith(KEY_NAME)) { 171 result.append(current.state); 172 skiplen = KEY_NAME.length(); 173 } else if (src.toLowerCase().startsWith(KEY_DESC)) { 174 result.append(current.desc); 175 skiplen = KEY_DESC.length(); 176 } else if (src.toLowerCase().startsWith(KEY_DATE)) { 177 result.append(dateFmt.format(new Date (current.when))); 178 skiplen = KEY_DATE.length(); 179 } else if (src.toLowerCase().startsWith(KEY_DURATION)) { 180 result.append(formatDuration(prevtime - current.when)); 181 skiplen = KEY_DURATION.length(); 182 } else { 183 result.append('{'); 184 skiplen = 1; 185 } 186 187 if (skiplen > 0) { 188 src = src.substring(skiplen); } 190 } 191 192 result.append(src); 194 return (result.toString()); 195 } 196 197 198 204 private String formatDuration(long msecs) { 205 StringBuffer buf = new StringBuffer (); 206 long hours = msecs / (60 * 60 * 1000); 207 msecs %= (60 * 60 * 1000); 208 long mins = msecs / (60 * 1000); 209 msecs %= (60 * 1000); 210 long secs = msecs / 1000; 211 msecs %= 1000; 212 213 if (hours > 0) { 214 buf.append(hours); 215 buf.append(':'); 216 } 217 218 if ((mins > 0) || (hours > 0)) { 219 if (mins < 10) { 220 buf.append('0'); 221 } 222 buf.append(mins); 223 buf.append(':'); 224 } 225 226 if ((secs < 10) && ((mins > 0) || (hours > 0))) { 227 buf.append('0'); 228 } 229 230 buf.append(secs); 231 buf.append('.'); 232 233 if (msecs < 100) { 234 buf.append('0'); 235 } 236 237 if (msecs < 10) { 238 buf.append('0'); 239 } 240 241 buf.append(msecs); 242 243 return buf.toString(); 244 } 245 246 247 public void validate() 248 throws CruiseControlException { 249 250 ValidationHelper.assertIsSet(dstFileName, "file", this.getClass()); 251 CurrentBuildFileWriter.validate(dstFileName); 252 253 if (sourceFile != null) { 254 ValidationHelper.assertTrue(sourceFile.exists(), dstFileName + " does not exist: " 255 + sourceFile.getAbsolutePath()); 256 ValidationHelper.assertTrue(sourceFile.isFile(), dstFileName + " is not a file: " 257 + sourceFile.getAbsolutePath()); 258 259 sourceText = readSource(); 260 } 261 } 262 263 264 270 private void writeFile(String content) 271 throws CruiseControlException { 272 273 FileWriter fw = null; 274 try { 275 fw = new FileWriter (dstFileName); 276 fw.write(content); 277 } catch (IOException ioe) { 278 throw new CruiseControlException("Error writing file: " + dstFileName, ioe); 279 } finally { 280 if (fw != null) { 281 try { 282 fw.close(); 283 } catch (IOException ignore) { 284 285 } 286 } 287 } 288 } 289 290 291 297 private List readSource() 298 throws CruiseControlException { 299 300 List result = new ArrayList (); 301 BufferedReader reader = null; 302 303 try { 304 reader = new BufferedReader (new FileReader (sourceFile)); 305 String line; 306 307 while ((line = reader.readLine()) != null) { 308 result.add(line); 309 } 310 } catch (IOException ioe) { 311 throw new CruiseControlException("Error reading file: " + sourceFile, ioe); 312 } finally { 313 if (reader != null) { 314 try { 315 reader.close(); 316 } catch (IOException ignore) { 317 318 } 319 } 320 } 321 322 return result; 323 } 324 325 326 public void setFile(String fileName) { 327 this.dstFileName = fileName.trim(); 328 LOG.debug("set fileName = " + fileName); 329 } 330 331 332 public void setSourceFile(String fileName) { 333 sourceFile = new File (fileName); 334 LOG.debug("set sourceFile = " + fileName); 335 } 336 } 337 | Popular Tags |