1 37 38 package net.sourceforge.cruisecontrol.sourcecontrols; 39 40 import java.io.File ; 41 import java.io.FilenameFilter ; 42 import java.util.ArrayList ; 43 import java.util.Date ; 44 import java.util.Hashtable ; 45 import java.util.List ; 46 import java.util.Map ; 47 48 import org.apache.log4j.Logger; 49 import net.sourceforge.cruisecontrol.CruiseControlException; 50 import net.sourceforge.cruisecontrol.Modification; 51 import net.sourceforge.cruisecontrol.SourceControl; 52 import net.sourceforge.cruisecontrol.Log; 53 import net.sourceforge.cruisecontrol.util.ValidationHelper; 54 55 import net.sourceforge.cruisecontrol.util.DateUtil; 56 import net.sourceforge.cruisecontrol.util.XMLLogHelper; 57 import org.jdom.input.SAXBuilder; 58 import org.jdom.Document; 59 import org.jdom.Element; 60 import org.jdom.JDOMException; 61 import java.io.IOException ; 62 63 70 public class BuildStatus implements SourceControl { 71 72 private static final Logger LOG = Logger.getLogger(BuildStatus.class); 73 74 75 public static final String MOST_RECENT_LOGDIR_KEY = "most.recent.logdir"; 76 77 79 public static final String MOST_RECENT_LOGFILE_KEY = "most.recent.logfile"; 80 81 83 public static final String MOST_RECENT_LOGTIME_KEY = "most.recent.logtime"; 84 85 89 public static final String MOST_RECENT_LOGLABEL_KEY = "most.recent.loglabel"; 90 91 private Hashtable properties = new Hashtable (); 92 private String logDir; 93 94 101 public Map getProperties() { 102 return properties; 103 } 104 105 110 public void setLogDir(String logDir) { 111 this.logDir = logDir; 112 properties.put(MOST_RECENT_LOGDIR_KEY, logDir); 113 } 114 115 118 public void validate() throws CruiseControlException { 119 ValidationHelper.assertIsSet(logDir, "logdir", this.getClass()); 120 121 File logDirectory = new File (logDir); 122 ValidationHelper.assertTrue(logDirectory.exists(), 123 "Log directory does not exist: " + logDirectory.getAbsolutePath()); 124 ValidationHelper.assertTrue(logDirectory.isDirectory(), 125 "Log directory is not a directory: " + logDirectory.getAbsolutePath()); 126 } 127 128 138 public List getModifications(Date lastBuild, Date unused) { 139 List modifications = new ArrayList (); 140 File logDirectory = new File (logDir); 141 final String filename = Log.formatLogFileName(lastBuild); 142 143 try { 144 File [] newLogs = logDirectory.listFiles(new FilenameFilter () { 145 public boolean accept(File dir, String name) { 146 return name.compareTo(filename) > 0 && Log.wasSuccessfulBuild(name); 147 } 148 }); 149 150 Modification mostRecent = null; 151 152 for (int i = 0; i < newLogs.length; i++) { 153 Modification modification = new Modification("buildstatus"); 154 String name = newLogs[i].getName(); 155 156 modification.modifiedTime = Log.parseDateFromLogFileName(name); 157 modification.userName = "cc-" + getProjectFromLog(newLogs[i]); 158 modification.comment = logDir.substring(logDir.lastIndexOf('/') + 1); 159 modification.revision = Log.parseLabelFromLogFileName(name); 160 161 Modification.ModifiedFile modfile = modification.createModifiedFile(name, null); 162 modfile.revision = modification.revision; 163 modfile.action = "add"; 164 165 if (mostRecent == null || modification.modifiedTime.after(mostRecent.modifiedTime)) { 166 mostRecent = modification; 167 } 168 169 modifications.add(modification); 170 } 171 172 if (mostRecent != null) { 175 properties.put(MOST_RECENT_LOGFILE_KEY, ((Modification.ModifiedFile) mostRecent.files.get(0)).fileName); 176 properties.put(MOST_RECENT_LOGTIME_KEY, DateUtil.getFormattedTime(mostRecent.modifiedTime)); 177 properties.put(MOST_RECENT_LOGLABEL_KEY, mostRecent.revision); 178 } 179 180 } catch (Exception e) { 181 LOG.error("Error checking for modifications", e); 182 } 183 return modifications; 184 } 185 186 private String getProjectFromLog(File f) { 187 LOG.info("Getting project from file: " + f.getName()); 188 try { 189 Document doc = readDocFromFile(f); 190 LOG.info("Loaded xml document for BuildStatus"); 191 Element root = doc.getRootElement(); 192 XMLLogHelper log = new XMLLogHelper(root); 193 return log.getProjectName(); 194 } catch (JDOMException ex) { 195 LOG.info("Failed to load BuildStatus xml document" + ex); 196 } catch (IOException ex) { 197 LOG.info("Failed to load BuildStatus xml document" + ex); 198 } catch (CruiseControlException ex) { 199 LOG.info("Could load BuildStatus xml log document, but generated exception anyway" + ex); 200 } 201 return "Unknown"; 202 } 203 204 private Document readDocFromFile(File f) throws JDOMException, IOException { 205 SAXBuilder sxb = new SAXBuilder(); 206 return sxb.build(f); 207 } 208 } 209 210 | Popular Tags |