1 37 package net.sourceforge.cruisecontrol.sourcecontrols; 38 39 import java.io.BufferedReader ; 40 import java.io.File ; 41 import java.io.FileReader ; 42 import java.io.IOException ; 43 import java.text.ParseException ; 44 import java.text.SimpleDateFormat ; 45 import java.util.ArrayList ; 46 import java.util.Date ; 47 import java.util.HashMap ; 48 import java.util.List ; 49 import java.util.Map ; 50 import java.util.StringTokenizer ; 51 52 import net.sourceforge.cruisecontrol.CruiseControlException; 53 import net.sourceforge.cruisecontrol.Modification; 54 import net.sourceforge.cruisecontrol.SourceControl; 55 import net.sourceforge.cruisecontrol.util.StreamPumper; 56 import net.sourceforge.cruisecontrol.util.ValidationHelper; 57 58 import org.apache.log4j.Logger; 59 60 66 public class PVCS implements SourceControl { 67 68 private static final Logger LOG = Logger.getLogger(PVCS.class); 69 private static final String DOUBLE_QUOTE = "\""; 70 71 private Map properties = new HashMap (); 72 private Date lastBuild; 73 74 private String archiveFileSuffix = "-arc"; 75 private String pvcsbin; 76 private String pvcsExecCommand; 77 private String pvcsProject; 78 private String pvcsSubProject; 81 private String pvcsVersionLabel; 82 private String loginId; 83 84 87 private SimpleDateFormat inDateFormat = new SimpleDateFormat ("MM/dd/yyyy h:mma"); 88 private SimpleDateFormat outDateFormatSub = new SimpleDateFormat ("dd MMM yyyy HH:mm:ss"); 89 90 93 private SimpleDateFormat outDateFormat = new SimpleDateFormat ("MMM dd yyyy HH:mm:ss"); 94 95 private static final String PVCS_RESULTS_FILE = "vlog.txt"; 96 97 101 public String getPvcsbin() { 102 return pvcsbin; 103 } 104 105 public void setPvcsExecCommand(String command) { 106 pvcsExecCommand = command; 107 } 108 109 public String getPvcsExecCommand() { 110 return pvcsExecCommand; 111 } 112 113 117 public void setPvcsbin(String bin) { 118 this.pvcsbin = bin; 119 } 120 121 public void setPvcsproject(String project) { 122 pvcsProject = project; 123 } 124 125 public void setPvcssubproject(String subproject) { 126 pvcsSubProject = subproject; 127 } 128 129 public void setPvcsversionlabel(String versionlabel) { 130 pvcsVersionLabel = versionlabel; 131 } 132 133 public void setInDateFormat(String inDateFormat) { 134 this.inDateFormat = new SimpleDateFormat (inDateFormat); 135 } 136 137 public void setOutDateFormat(String outDateFormat) { 138 this.outDateFormat = new SimpleDateFormat (outDateFormat); 139 } 140 141 public Map getProperties() { 142 return properties; 143 } 144 145 public void validate() throws CruiseControlException { 146 ValidationHelper.assertIsSet(pvcsProject, "pvcsproject", this.getClass()); 147 ValidationHelper.assertIsSet(pvcsSubProject, "pvcssubproject", this.getClass()); 148 } 149 150 161 public List getModifications(Date lastBuild, Date now) { 162 this.lastBuild = lastBuild; 163 String lastBuildDate = inDateFormat.format(lastBuild); 165 String nowDate = inDateFormat.format(now); 166 167 try { 168 setPvcsExecCommand(getExecutable("pcli") + " " + buildExecCommand(lastBuildDate, nowDate)); 169 exec(pvcsExecCommand); 170 } catch (Exception e) { 171 LOG.error("Error in executing the PVCS command : ", e); 172 return new ArrayList (); 173 } 174 175 return makeModificationsList(new File (PVCS_RESULTS_FILE)); 176 } 177 178 String getExecutable(String exe) { 179 StringBuffer correctedExe = new StringBuffer (); 180 if (getPvcsbin() != null) { 181 if (getPvcsbin().endsWith(File.separator)) { 182 correctedExe.append(getPvcsbin()); 183 } else { 184 correctedExe.append(getPvcsbin()).append(File.separator); 185 } 186 } 187 return correctedExe.append(exe).toString(); 188 } 189 190 protected void exec(String command) throws IOException , InterruptedException { 191 LOG.debug("Command to execute: " + command); 192 Process p = Runtime.getRuntime().exec(command); 193 StreamPumper errorPumper = new StreamPumper(p.getErrorStream()); 194 new Thread (errorPumper).start(); 195 p.getInputStream(); 196 p.waitFor(); 197 p.getOutputStream(); 198 p.getInputStream(); 199 p.getErrorStream(); 200 } 201 202 207 List makeModificationsList(File inputFile) { 208 List theList; 209 BufferedReader brIn; 210 ModificationBuilder modificationBuilder = new ModificationBuilder(pvcsProject); 211 try { 212 brIn = new BufferedReader (new FileReader (inputFile)); 213 String line; 214 while ((line = brIn.readLine()) != null) { 215 modificationBuilder.addLine(line); 216 } 217 brIn.close(); 218 } catch (IOException e) { 219 LOG.error("Error in reading vlog file of PVCS modifications : ", e); 220 } 221 theList = modificationBuilder.getList(); 222 223 if (theList == null) { 224 theList = new ArrayList (); 225 } 226 227 return theList; 228 } 229 230 238 String buildExecCommand(String lastBuild, String now) { 239 String command = 240 "run -ns -q -xo" + DOUBLE_QUOTE + PVCS_RESULTS_FILE + DOUBLE_QUOTE 241 + " -xe" + DOUBLE_QUOTE + PVCS_RESULTS_FILE + DOUBLE_QUOTE 242 + " vlog "; 243 244 if (loginId != null && !loginId.trim().equals("")) { 245 command += "-id" + DOUBLE_QUOTE + loginId + DOUBLE_QUOTE + " "; 246 } 247 248 command += "-ds" + DOUBLE_QUOTE + lastBuild + DOUBLE_QUOTE 249 + " -de" + DOUBLE_QUOTE + now + DOUBLE_QUOTE 250 + " -pr" + DOUBLE_QUOTE + pvcsProject + DOUBLE_QUOTE; 251 252 if (pvcsVersionLabel != null && !pvcsVersionLabel.equals("")) { 253 command += " -v" + DOUBLE_QUOTE + pvcsVersionLabel + DOUBLE_QUOTE; 254 } 255 256 command += " -z " + pvcsSubProject; 257 return command; 258 } 259 260 264 class ModificationBuilder { 265 private String proj; 266 private Modification modification; 267 private ArrayList modificationList; 268 private boolean firstModifiedTime = true; 269 private boolean firstUserName = true; 270 private boolean nextLineIsComment = false; 271 private boolean waitingForNextValidStart = false; 272 273 public ModificationBuilder(String proj) { 274 this.proj = proj; 275 } 276 277 public ArrayList getList() { 278 return modificationList; 279 } 280 281 private void initializeModification() { 282 if (modificationList == null) { 283 modificationList = new ArrayList (); 284 } 285 modification = new Modification("pvcs"); 286 firstModifiedTime = true; 287 firstUserName = true; 288 nextLineIsComment = false; 289 waitingForNextValidStart = false; 290 } 291 292 public void addLine(String line) { 293 if (line.startsWith("Archive:")) { 294 initializeModification(); 295 String fileName; 296 297 int startIndex = (line.indexOf(proj) + proj.length()); 298 int endIndex = line.indexOf(archiveFileSuffix); 299 if (endIndex == -1) { 300 endIndex = line.length(); 301 } 302 fileName = line.substring(startIndex, endIndex); 303 if (fileName.startsWith("/") || fileName.startsWith("\\")) { 304 fileName = fileName.substring(1); 305 } 306 if (fileName.startsWith("archives")) { 307 fileName = fileName.substring("archives".length()); 308 } 309 310 311 modification.createModifiedFile(fileName, null); 312 313 } else if (waitingForNextValidStart) { 314 } else if (line.startsWith("Workfile:")) { 319 modification.createModifiedFile(line.substring(18), null); 320 } else if (line.startsWith("Archive created:")) { 321 try { 322 String createdDate = line.substring(18); 323 Date createTime; 324 try { 325 createTime = outDateFormat.parse(createdDate); 326 } catch (ParseException e) { 327 createTime = outDateFormatSub.parse(createdDate); 328 } 329 if (createTime.after(lastBuild)) { 330 modification.type = "added"; 331 } else { 332 modification.type = "modified"; 333 } 334 } catch (ParseException e) { 335 LOG.error("Error parsing create date: " + e.getMessage(), e); 336 } 337 } else if (line.startsWith("Last modified:")) { 338 if (firstModifiedTime) { 340 firstModifiedTime = false; 341 String lastMod = null; 342 try { 343 lastMod = line.substring(16); 344 modification.modifiedTime = outDateFormat.parse(lastMod); 345 } catch (ParseException e) { 346 try { 347 modification.modifiedTime = outDateFormatSub.parse(lastMod); 348 } catch (ParseException pe) { 349 modification.modifiedTime = null; 350 LOG.error("Error parsing modification time : ", e); 351 } 352 } 353 } 354 } else if (nextLineIsComment) { 355 modification.comment = line; 357 modificationList.add(modification); 360 waitingForNextValidStart = true; 361 } else if (line.startsWith("Author id:")) { 362 if (firstUserName) { 364 String sub = line.substring(11); 365 StringTokenizer st = new StringTokenizer (sub, " "); 366 modification.userName = st.nextToken().trim(); 367 firstUserName = false; 368 nextLineIsComment = true; 369 } 370 } 372 } 374 } 376 379 public String getLoginid() { 380 return loginId; 381 } 382 383 386 public void setLoginid(String loginId) { 387 this.loginId = loginId; 388 } 389 390 void setLastBuild(Date date) { 391 this.lastBuild = date; 392 } 393 394 public void setArchiveFileSuffix(String archiveSuffix) { 395 this.archiveFileSuffix = archiveSuffix; 396 } 397 398 } | Popular Tags |