001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2006, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.listeners;
037:
038: import net.sourceforge.cruisecontrol.CruiseControlException;
039: import net.sourceforge.cruisecontrol.Listener;
040: import net.sourceforge.cruisecontrol.ProjectEvent;
041: import net.sourceforge.cruisecontrol.ProjectState;
042: import net.sourceforge.cruisecontrol.util.CurrentBuildFileWriter;
043: import net.sourceforge.cruisecontrol.util.ValidationHelper;
044: import net.sourceforge.cruisecontrol.util.IO;
045:
046: import org.apache.log4j.Logger;
047:
048: import java.util.Date;
049: import java.util.List;
050: import java.io.File;
051:
052: /**
053: * Writes a text snippet in a file (typically in a location where the reporting module can read it), indicating
054: * the current build status.
055: *
056: * <p>{@link net.sourceforge.cruisecontrol.DateFormatFactory} for the dateformat
057: *
058: * @see net.sourceforge.cruisecontrol.DateFormatFactory
059: * @author jfredrick
060: */
061: public class CurrentBuildStatusListener implements Listener {
062: private static final Logger LOG = Logger
063: .getLogger(CurrentBuildStatusListener.class);
064: private String fileName;
065:
066: public static final String MSG_PREFIX_PROGRESS = "progress: ";
067:
068: public void handleEvent(ProjectEvent event)
069: throws CruiseControlException {
070: if (event instanceof ProjectStateChangedEvent) {
071: final ProjectStateChangedEvent stateChanged = (ProjectStateChangedEvent) event;
072: final ProjectState newState = stateChanged.getNewState();
073: LOG.debug("updating status to " + newState.getName()
074: + " for project " + stateChanged.getProjectName());
075: final String text = newState.getDescription() + " since\n";
076: CurrentBuildFileWriter
077: .writefile(text, new Date(), fileName);
078: } else if (event instanceof ProgressChangedEvent) {
079: final ProgressChangedEvent progressChanged = (ProgressChangedEvent) event;
080: final String msgProgress = progressChanged.getProgress()
081: .getValue();
082: LOG.debug("updating progress to " + msgProgress
083: + " for project "
084: + progressChanged.getProjectName());
085: final String text = getStatusTextPrefix()
086: + MSG_PREFIX_PROGRESS + msgProgress;
087: IO.write(fileName, text);
088: } else {
089: // ignore other ProjectEvents
090: LOG.debug("ignoring event " + event.getClass().getName()
091: + " for project " + event.getProjectName());
092: }
093: }
094:
095: private String getStatusTextPrefix() throws CruiseControlException {
096: String statusPrefix = "";
097:
098: final File statusFile = new File(fileName);
099: if (!statusFile.exists()) {
100: return statusPrefix;
101: }
102:
103: final List lines = IO.readLines(statusFile);
104: // look for Progress Text Prefix (might not exist)
105: String line;
106: for (int i = 0; i < lines.size(); i++) {
107: line = (String) lines.get(i);
108: if (line.startsWith(MSG_PREFIX_PROGRESS)) {
109: break;
110: } else {
111: statusPrefix += line + "\n";
112: }
113: }
114: return statusPrefix;
115: }
116:
117: public void validate() throws CruiseControlException {
118: ValidationHelper.assertIsSet(fileName, "file", this .getClass());
119: CurrentBuildFileWriter.validate(fileName);
120: }
121:
122: public void setFile(String fileName) {
123: this .fileName = fileName.trim();
124: LOG.debug("set fileName = " + fileName);
125: }
126: }
|