1 37 package net.sourceforge.cruisecontrol.sourcecontrols; 38 39 import net.sourceforge.cruisecontrol.CruiseControlException; 40 import net.sourceforge.cruisecontrol.Modification; 41 import net.sourceforge.cruisecontrol.SourceControl; 42 import net.sourceforge.cruisecontrol.util.Commandline; 43 import net.sourceforge.cruisecontrol.util.StreamPumper; 44 import net.sourceforge.cruisecontrol.util.ValidationHelper; 45 import org.apache.log4j.Logger; 46 import org.jdom.Element; 47 48 import java.io.BufferedReader ; 49 import java.io.IOException ; 50 import java.io.InputStreamReader ; 51 import java.io.PrintWriter ; 52 import java.text.DateFormat ; 53 import java.text.ParseException ; 54 import java.text.SimpleDateFormat ; 55 import java.util.ArrayList ; 56 import java.util.Date ; 57 import java.util.HashMap ; 58 import java.util.Hashtable ; 59 import java.util.Iterator ; 60 import java.util.List ; 61 import java.util.Map ; 62 import java.util.StringTokenizer ; 63 64 70 public class UCM implements SourceControl { 71 72 private static final Logger LOG = Logger.getLogger(UCM.class); 73 74 private String stream; 75 private String viewPath; 76 private String property; 77 private boolean contributors = true; 78 79 80 private final SimpleDateFormat inDateFormatter = 81 new SimpleDateFormat ("dd-MMMM-yyyy.HH:mm:ss"); 82 83 84 private final SimpleDateFormat outDateFormatter = 85 new SimpleDateFormat ("yyyyMMdd.HHmmss"); 86 87 private Hashtable properties = new Hashtable (); 88 89 94 public Map getProperties() { 95 return properties; 96 } 97 98 103 public void validate() throws CruiseControlException { 104 ValidationHelper.assertIsSet(stream, "stream", this.getClass()); 105 ValidationHelper.assertIsSet(viewPath, "viewpath", this.getClass()); 106 } 107 108 113 public String getStream() { 114 return stream; 115 } 116 117 122 public void setStream(String stream) { 123 this.stream = stream; 124 } 125 126 131 public String getViewPath() { 132 return viewPath; 133 } 134 135 140 public void setViewPath(String viewPath) { 141 this.viewPath = viewPath; 142 } 143 144 149 public boolean isContributors() { 150 return contributors; 151 } 152 153 158 public void setContributors(boolean contributors) { 159 this.contributors = contributors; 160 } 161 162 167 public void setProperty(String property) { 168 this.property = property; 169 } 170 171 176 public String getProperty() { 177 return this.property; 178 } 179 180 191 public List getModifications(Date lastBuild, Date now) { 192 String lastBuildDate = inDateFormatter.format(lastBuild); 193 String nowDate = inDateFormatter.format(now); 194 properties.put("ucmlastbuild", lastBuildDate); 195 properties.put("ucmnow", nowDate); 196 List mods = new ArrayList (); 197 try { 198 HashMap activityNames = collectActivitiesSinceLastBuild(lastBuildDate); 199 if (activityNames.size() == 0) { 200 return mods; 201 } 202 mods = describeAllActivities(activityNames); 203 } catch (Exception e) { 204 LOG.error("Command failed to execute succesfully", e); 205 } 206 207 if (!mods.isEmpty() && getProperty() != null) { 209 properties.put(getProperty(), "true"); 210 } 211 212 return mods; 213 } 214 215 218 private HashMap collectActivitiesSinceLastBuild(String lastBuildDate) { 219 220 LOG.debug("Last build time was: " + lastBuildDate); 221 222 HashMap activityMap = new HashMap (); 223 224 Commandline commandLine = buildListStreamCommand(lastBuildDate); 225 LOG.debug("Executing: " + commandLine); 226 227 try { 228 Process p = Runtime.getRuntime().exec(commandLine.getCommandline()); 229 StreamPumper errorPumper = 230 new StreamPumper(p.getErrorStream(), new PrintWriter (System.err, true)); 231 new Thread (errorPumper).start(); 232 InputStreamReader isr = new InputStreamReader (p.getInputStream()); 233 BufferedReader br = new BufferedReader (isr); 234 String line; 235 236 while (((line = br.readLine()) != null) && (!br.equals(""))) { 237 String [] details = getDetails(line); 238 if (details[0].equals("mkbranch")) { 239 } else { 241 String activityName = details[1]; 242 String activityDate = details[2]; 243 if (!activityMap.containsKey(activityName)) { 245 LOG.debug("Found activity name: " + activityName + "; date: " + activityDate); 246 activityMap.put(activityName, activityDate); 247 } 248 } 249 } 250 251 p.waitFor(); 252 p.getInputStream().close(); 253 p.getOutputStream().close(); 254 p.getErrorStream().close(); 255 } catch (IOException e) { 256 LOG.error("IO Error executing ClearCase lshistory command", e); 257 } catch (InterruptedException e) { 258 LOG.error("Interrupt Error executing ClearCase lshistory command", e); 259 } 260 261 return activityMap; 262 } 263 264 private String [] getDetails(String line) { 265 ArrayList details = new ArrayList (); 267 String delimiter = "~#~"; 268 int startIndex = 0; 269 int index = 0; 270 while (index != -1) { 271 String detail; 272 index = line.indexOf(delimiter, startIndex); 273 if (index == -1) { 274 detail = line.substring(startIndex, line.length()); 275 } else { 276 detail = line.substring(startIndex, index); 277 } 278 details.add(detail); 279 startIndex = index + delimiter.length(); 280 } 281 282 return (String []) details.toArray(new String []{}); 283 } 284 285 288 public Commandline buildListStreamCommand(String lastBuildDate) { 289 Commandline commandLine = new Commandline(); 290 commandLine.setExecutable("cleartool"); 291 commandLine.createArgument().setValue("lshistory"); 292 commandLine.createArgument().setValue("-branch"); 293 commandLine.createArgument().setValue(getStream()); 294 commandLine.createArgument().setValue("-r"); 295 commandLine.createArgument().setValue("-nco"); 296 commandLine.createArgument().setValue("-since"); 297 commandLine.createArgument().setValue(lastBuildDate); 298 commandLine.createArgument().setValue("-fmt"); 299 commandLine.createArgument().setValue("%o~#~%[activity]Xp~#~%Nd\n"); 300 commandLine.createArgument().setValue(getViewPath()); 301 return commandLine; 302 } 303 304 307 private List describeAllActivities(HashMap activityNames) { 308 309 ArrayList activityList = new ArrayList (); 310 311 Iterator it = activityNames.entrySet().iterator(); 312 while (it.hasNext()) { 313 Map.Entry activity = (Map.Entry ) it.next(); 314 String activityID = activity.getKey().toString(); 315 String activityDate = activity.getValue().toString(); 316 UCMModification activityMod = describeActivity(activityID, activityDate); 317 activityList.add(activityMod); 318 319 if (activityMod.comment.startsWith("deliver ") && isContributors()) { 321 List contribList; 322 contribList = describeContributors(activityID); 323 Iterator contribIter = contribList.iterator(); 324 while (contribIter.hasNext()) { 325 String contribName = contribIter.next().toString(); 326 UCMModification contribMod = describeActivity(contribName, activityDate); 327 contribMod.type = "contributor"; 329 LOG.debug("Found contributor name: " + contribName + "; date: " + activityDate); 330 activityList.add(contribMod); 331 } 332 } 333 } 334 335 return activityList; 336 } 337 338 341 private UCMModification describeActivity(String activityID, String activityDate) { 342 343 UCMModification mod = new UCMModification(); 344 345 Commandline commandLine = buildDescribeActivityCommand(activityID); 346 LOG.debug("Executing: " + commandLine); 347 348 try { 349 Process p = Runtime.getRuntime().exec(commandLine.getCommandline()); 350 StreamPumper errorPumper = 351 new StreamPumper(p.getErrorStream(), new PrintWriter (System.err, true)); 352 new Thread (errorPumper).start(); 353 InputStreamReader isr = new InputStreamReader (p.getInputStream()); 354 BufferedReader br = new BufferedReader (isr); 355 String line; 356 357 while (((line = br.readLine()) != null) && (!br.equals(""))) { 358 String [] details = getDetails(line); 359 try { 360 mod.modifiedTime = outDateFormatter.parse(activityDate); 361 } catch (ParseException e) { 362 LOG.error("Error parsing modification date"); 363 } 364 mod.type = "activity"; 365 if (details[0].equals("")) { 367 mod.revision = details[3]; 368 } else { 369 mod.revision = details[0]; 370 } 371 mod.crmtype = details[1]; 372 mod.userName = details[2]; 373 mod.comment = details[3]; 374 } 375 376 p.waitFor(); 377 p.getInputStream().close(); 378 p.getOutputStream().close(); 379 p.getErrorStream().close(); 380 } catch (IOException e) { 381 LOG.error("IO Error executing ClearCase describe command", e); 382 } catch (InterruptedException e) { 383 LOG.error("Interrupt error executing ClearCase describe command", e); 384 } 385 386 return mod; 387 } 388 389 390 393 public Commandline buildDescribeActivityCommand(String activityID) { 394 Commandline commandLine = new Commandline(); 395 commandLine.setExecutable("cleartool"); 396 commandLine.createArgument().setValue("describe"); 397 commandLine.createArgument().setValue("-fmt"); 398 commandLine.createArgument().setValue("%[crm_record_id]p~#~%[crm_record_type]p~#~%u~#~%[headline]p~#~"); 399 commandLine.createArgument().setValue(activityID); 400 return commandLine; 401 } 402 403 406 private List describeContributors(String activityName) { 407 408 ArrayList contribList = new ArrayList (); 409 Commandline commandLine = buildListContributorsCommand(activityName); 410 LOG.debug("Executing: " + commandLine); 411 412 try { 413 Process p = Runtime.getRuntime().exec(commandLine.getCommandline()); 414 StreamPumper errorPumper = 415 new StreamPumper(p.getErrorStream(), new PrintWriter (System.err, true)); 416 new Thread (errorPumper).start(); 417 418 InputStreamReader isr = new InputStreamReader (p.getInputStream()); 419 BufferedReader br = new BufferedReader (isr); 420 String line; 421 422 while ((line = br.readLine()) != null) { 423 String [] contribs = splitOnSpace(line); 424 for (int i = 0; i < contribs.length; i++) { 425 contribList.add(contribs[i]); 426 } 427 } 428 429 p.waitFor(); 430 p.getInputStream().close(); 431 p.getOutputStream().close(); 432 p.getErrorStream().close(); 433 } catch (IOException e) { 434 LOG.error("IO Error executing ClearCase describe contributors command", e); 435 } catch (InterruptedException e) { 436 LOG.error("Interrupt Error executing ClearCase describe contributors command", e); 437 } 438 439 return contribList; 440 } 441 442 private String [] splitOnSpace(String string) { 443 ArrayList parts = new ArrayList (); 445 StringTokenizer tokenizer = new StringTokenizer (string, " "); 446 while (tokenizer.hasMoreTokens()) { 447 parts.add(tokenizer.nextToken()); 448 } 449 return (String []) parts.toArray(new String []{}); 450 } 451 452 455 public Commandline buildListContributorsCommand(String activityID) { 456 Commandline commandLine = new Commandline(); 457 commandLine.setExecutable("cleartool"); 458 commandLine.createArgument().setValue("describe"); 459 commandLine.createArgument().setValue("-fmt"); 460 commandLine.createArgument().setValue("\"%[contrib_acts]Xp\""); 461 commandLine.createArgument().setValue(activityID); 462 return commandLine; 463 } 464 465 468 private static class UCMModification extends Modification { 469 private static final String TAGNAME_CRMTYPE = "crmtype"; 470 471 public String crmtype; 472 473 public int compareTo(Object o) { 474 UCMModification modification = (UCMModification) o; 475 return getActivitityNumber() - modification.getActivitityNumber(); 476 } 477 478 public boolean equals(Object o) { 479 if (o == null || !(o instanceof UCMModification)) { 480 return false; 481 } 482 483 UCMModification modification = (UCMModification) o; 484 return getActivitityNumber() == modification.getActivitityNumber(); 485 } 486 487 public int hashCode() { 488 return getActivitityNumber(); 489 } 490 491 private int getActivitityNumber() { 492 return Integer.parseInt(revision); 493 } 494 495 UCMModification() { 496 super("ucm"); 497 } 498 499 public Element toElement(DateFormat formatter) { 500 Element modificationElement = super.toElement(formatter); 501 Element crmtypeElement = new Element(TAGNAME_CRMTYPE); 502 crmtypeElement.addContent(crmtype); 503 modificationElement.addContent(crmtypeElement); 504 return modificationElement; 505 } 506 507 } 508 509 512 public static void main(String [] args) { 513 UCM ucmtest = new UCM(); 514 ucmtest.setStream("RatlBankModel_Int"); 515 ucmtest.setViewPath("/view/RatlBankModel_int/vobs/RatlBankSources/model"); 517 List changes = new ArrayList (); 519 520 try { 521 changes = ucmtest.getModifications(new SimpleDateFormat ("yyyyMMdd.HHmmss").parse("20050822.095914"), 522 new Date ()); 523 } catch (ParseException e) { 524 e.printStackTrace(); 526 } 527 528 System.out.println(changes.toString()); 529 } 530 531 } 532 | Popular Tags |