1 package net.sourceforge.cruisecontrol; 2 3 import java.io.BufferedReader ; 4 import java.io.File ; 5 import java.io.FileReader ; 6 import java.io.IOException ; 7 8 23 public class BuildStatus { 24 25 public static final int READ_ALL_LINES = -2; 26 27 31 public static final int READ_ONLY_STATUS_LINES = 2; 32 33 protected BuildStatus() { 34 } 35 36 56 public static String getStatusPlain(boolean isSingleProject, String dir, 57 String projectName, String statusFileName, int maxReadLines) { 58 String status = genStatus(isSingleProject, dir, projectName, 59 statusFileName, false, maxReadLines); 60 61 return status; 62 } 63 64 84 public static String getStatusHtml(boolean isSingleProject, String dir, 85 String projectName, String statusFileName, int maxReadLines) { 86 String status = genStatus(isSingleProject, dir, projectName, 87 statusFileName, true, maxReadLines); 88 89 return status; 90 } 91 92 115 private static String genStatus(boolean isSingleProject, String dir, 116 String projectName, String statusFileName, boolean insertBreaks, 117 int maxReadLines) { 118 119 File statusFile = getFile(isSingleProject, dir, projectName, 120 statusFileName); 121 122 String status; 123 if (statusFile.exists()) { 124 status = getStatus(statusFile, insertBreaks, maxReadLines); 125 } else { 126 status = "(build status file not found)"; 127 } 128 129 return status; 130 } 131 132 146 private static String getStatus(File statusFile, boolean insertBreaks, 147 int maxReadLines) { 148 StringBuffer sb = new StringBuffer (101); 149 BufferedReader br = null; 150 151 try { 152 br = new BufferedReader (new FileReader (statusFile)); 153 String line = br.readLine(); 154 int linesRead = 1; 155 156 while (line != null && readMoreLines(linesRead, maxReadLines)) { 157 158 if (line.indexOf("<br>") == -1) { 159 addLine(line, sb, insertBreaks); 160 } else { 161 int startIndex = 0; 162 for (int endIndex = line.indexOf("<br>"); 163 endIndex != -1 && readMoreLines(linesRead, maxReadLines); 164 linesRead++) { 165 String substring = line.substring(startIndex, endIndex); 166 if (substring.length() > 0) { 167 addLine(substring, sb, insertBreaks); 168 } else { 169 linesRead--; 170 } 171 startIndex = endIndex + "<br>".length(); 172 endIndex = line.indexOf("<br>", startIndex); 173 } 174 String substring = line.substring(startIndex); 175 if (substring.length() > 0 && readMoreLines(linesRead, maxReadLines)) { 176 addLine(substring, sb, insertBreaks); 177 } 178 } 179 180 line = br.readLine(); 181 linesRead++; 182 } 183 } catch (IOException e) { 184 throw new CruiseControlWebAppException( 185 "Error reading status file: " + statusFile.getName() + " : " 186 + e.getMessage(), e); 187 } finally { 188 try { 189 if (br != null) { 190 br.close(); 191 } 192 } catch (IOException e) { 193 } 195 br = null; 196 } 197 198 return sb.toString(); 199 } 200 201 private static boolean readMoreLines(int linesRead, int maxReadLines) { 202 return linesRead <= maxReadLines || maxReadLines == READ_ALL_LINES; 203 } 204 205 private static void addLine(String line, StringBuffer sb, boolean insertBreaks) { 206 sb.append(line); 207 sb.append('\n'); 208 if (insertBreaks) { 209 sb.append("<br/>"); 210 } 211 } 212 213 227 private static File getFile(boolean isSingleProject, String dir, 228 String projectName, String statusFileName) { 229 String statusFileDir = null; 230 231 if (isSingleProject) { 232 statusFileDir = dir; 233 } else { 234 statusFileDir = dir + System.getProperty("file.separator") 235 + projectName; 236 } 237 238 File statusFile = new File (statusFileDir, statusFileName); 239 240 if (statusFile.isDirectory()) { 241 final String msg = "CruiseControl: currentBuildStatusFile " 242 + statusFile.getAbsolutePath() + " is a directory." 243 + " Edit the web.xml to provide the path to the correct file."; 244 throw new CruiseControlWebAppException(msg); 245 } 246 247 return statusFile; 248 } 249 } 250 | Popular Tags |