1 37 package net.sourceforge.cruisecontrol; 38 39 import java.io.File ; 40 import java.io.Serializable ; 41 import java.text.DateFormat ; 42 import java.text.ParseException ; 43 import java.text.SimpleDateFormat ; 44 import java.util.ArrayList ; 45 import java.util.Collections ; 46 import java.util.Date ; 47 import java.util.List ; 48 49 import net.sourceforge.cruisecontrol.taglib.CruiseControlLogFileFilter; 50 import net.sourceforge.cruisecontrol.taglib.CruiseControlSuccessfulLogFileFilter; 51 52 60 public class BuildInfo implements Comparable , Serializable { 61 public static final String LOG_PREFIX = "log"; 62 public static final char LABEL_SEPARATOR = 'L'; 63 public static final String LOG_DATE_PATTERN = "yyyyMMddHHmmss"; 64 private final Date buildDate; 65 private final String dateStamp; 66 private final String label; 67 private final LogFile logFile; 68 69 BuildInfo(String infoText) throws ParseException { 71 this(new File (infoText)); 72 } 73 public BuildInfo(File logFile) throws ParseException { 74 this(new LogFile(logFile)); 75 } 76 public BuildInfo(LogFile logFile) throws ParseException { 77 this.logFile = logFile; 78 dateStamp = deriveDateStamp(); 79 buildDate = deriveDate(); 80 label = deriveLabel(); 81 } 82 83 private String deriveLabel() { 84 String infoText = logFile.getName(); 85 boolean buildSuccessful = new CruiseControlSuccessfulLogFileFilter().isSuccessful(infoText); 86 String theLabel; 87 if (buildSuccessful) { 88 int labelStartIndex = (LOG_PREFIX + LOG_DATE_PATTERN + LABEL_SEPARATOR).length(); 89 theLabel = infoText.substring(labelStartIndex); 90 } else { 91 theLabel = null; 92 } 93 return theLabel; 94 } 95 96 private String deriveDateStamp() throws ParseException { 97 String infoText = logFile.getName(); 98 try { 99 return infoText.substring(LOG_PREFIX.length(), LOG_PREFIX.length() + LOG_DATE_PATTERN.length()); 100 } catch (StringIndexOutOfBoundsException e) { 101 throw new IllegalStateException ("infoText has wrong format: " + infoText + " " + e.getMessage()); 102 } 103 } 104 105 private Date deriveDate() throws ParseException { 106 String infoText = logFile.getName(); 107 Date theDate; 108 final DateFormat logDateFormat = new SimpleDateFormat (LOG_DATE_PATTERN); 109 try { 110 theDate = logDateFormat.parse(dateStamp); 111 } catch (ParseException e) { 112 throw new ParseException ("Invalid format: " + infoText + ". Format must be logyyyyMMddHHmmSS.xml or " 113 + "logyyyyMMddHHmmSSLlabel.xml", e.getErrorOffset()); 114 } 115 return theDate; 116 } 117 118 public Date getBuildDate() { 119 return buildDate; 120 } 121 122 126 public String getDateStamp() { 127 return dateStamp; 128 } 129 130 public String getLabel() { 131 return label; 132 } 133 134 public boolean isSuccessful() { 135 return getLabel() != null; 136 } 137 138 141 public String getLogName() { 142 return logFile.getName(); 143 } 144 145 public static BuildInfoSummary loadFromDir(File logDir) throws CruiseControlWebAppException { 146 File [] logFileNames = logDir.listFiles(new CruiseControlLogFileFilter()); 147 if (logFileNames == null) { 148 throw new CruiseControlWebAppException("Could not access the directory " + logDir.getAbsolutePath()); 149 } else if (logFileNames.length == 0) { 150 throw new CruiseControlWebAppException("Configuration problem? No logs found in logDir: " 151 + logDir.getAbsolutePath()); 152 } 153 List buildInfoList = new ArrayList (logFileNames.length); 154 for (int i = 0; i < logFileNames.length; i++) { 155 File file = logFileNames[i]; 156 try { 157 buildInfoList.add(new BuildInfo(file)); 158 } catch (ParseException e) { 159 throw new CruiseControlWebAppException("Could not parse log file name " + file.getName() 160 + ". Is the filter broken?", e); 161 } 162 } 163 Collections.sort(buildInfoList); 164 return new BuildInfoSummary(buildInfoList); 165 } 166 167 171 public int compareTo(Object arg0) { 172 BuildInfo other = (BuildInfo) arg0; 173 return this.buildDate.compareTo(other.buildDate); 174 } 175 } 176 | Popular Tags |