1 37 package net.sourceforge.cruisecontrol.sourcecontrols; 38 39 import com.starbase.starteam.File; 40 import com.starbase.starteam.Folder; 41 import com.starbase.starteam.Item; 42 import com.starbase.starteam.TypeNotFoundException; 43 import com.starbase.starteam.PropertyNames; 44 import com.starbase.starteam.Server; 45 import com.starbase.starteam.ServerException; 46 import com.starbase.starteam.StarTeamFinder; 47 import com.starbase.starteam.TypeNames; 48 import com.starbase.starteam.User; 49 import com.starbase.starteam.UserAccount; 50 import com.starbase.starteam.View; 51 import com.starbase.starteam.ViewConfiguration; 52 import com.starbase.util.OLEDate; 53 import net.sourceforge.cruisecontrol.CruiseControlException; 54 import net.sourceforge.cruisecontrol.Modification; 55 import net.sourceforge.cruisecontrol.SourceControl; 56 import net.sourceforge.cruisecontrol.util.ValidationHelper; 57 import org.apache.log4j.Logger; 58 59 import java.util.ArrayList ; 60 import java.util.Date ; 61 import java.util.HashMap ; 62 import java.util.Hashtable ; 63 import java.util.Iterator ; 64 import java.util.List ; 65 import java.util.Map ; 66 67 76 public class StarTeam implements SourceControl { 77 78 private static final Logger LOG = Logger.getLogger(StarTeam.class); 79 80 private String userName; 81 private String password; 82 private String folder; 83 private String url; 84 private List modifications = new ArrayList (); 85 private OLEDate nowDate; 86 private Folder lastBuildRoot; 87 private PropertyNames stPropertyNames; 88 private Server server; 89 private TypeNames stTypeNames; 90 91 private Hashtable properties = new Hashtable (); 92 private String property; 93 private String propertyOnDelete; 94 95 private boolean preloadFileInformation = true; 96 private boolean canLookupEmails = true; 97 private boolean canLookupDeletions = true; 98 99 102 public void setUsername(String userName) { 103 this.userName = userName; 104 } 105 106 109 public void setPassword(String password) { 110 this.password = password; 111 } 112 113 116 public void setFolder(String folder) { 117 this.folder = folder; 118 } 119 120 public void setPreloadFileInformation(boolean preloadFileInformation) { 121 this.preloadFileInformation = preloadFileInformation; 122 } 123 124 public void setStarteamurl(String url) { 125 this.url = url; 126 } 127 128 public void setProperty(String property) { 129 this.property = property; 130 } 131 132 public void setPropertyOnDelete(String propertyOnDelete) { 133 this.propertyOnDelete = propertyOnDelete; 134 } 135 136 public Map getProperties() { 137 return properties; 138 } 139 140 public void validate() throws CruiseControlException { 141 ValidationHelper.assertIsSet(folder, "folder", this.getClass()); 142 ValidationHelper.assertIsSet(url, "url", this.getClass()); 143 ValidationHelper.assertIsSet(userName, "username", this.getClass()); 144 ValidationHelper.assertIsSet(password, "password", this.getClass()); 145 146 } 149 150 154 public List getModifications(Date lastBuild, Date now) { 155 modifications.clear(); 160 161 nowDate = new OLEDate(now.getTime()); 163 OLEDate lastBuildDate = new OLEDate(lastBuild.getTime()); 164 165 server = null; 166 try { 167 View view = StarTeamFinder.openView(userName + ":" + password + "@" + url); 169 server = view.getServer(); 170 171 View snapshotAtNow = new View(view, ViewConfiguration.createFromTime(nowDate)); 172 View snapshotAtLastBuild = 173 new View(view, ViewConfiguration.createFromTime(lastBuildDate)); 174 175 Map nowFiles = new HashMap (); 176 Map lastBuildFiles = new HashMap (); 177 178 Folder nowRoot = StarTeamFinder.findFolder(snapshotAtNow.getRootFolder(), folder); 179 180 stPropertyNames = server.getPropertyNames(); 181 stTypeNames = server.getTypeNames(); 182 final String [] propertiesToCache = 184 new String [] { 185 stPropertyNames.FILE_CONTENT_REVISION, 186 stPropertyNames.MODIFIED_TIME, 187 stPropertyNames.FILE_FILE_TIME_AT_CHECKIN, 188 stPropertyNames.COMMENT, 189 stPropertyNames.MODIFIED_USER_ID, 190 stPropertyNames.FILE_NAME }; 191 192 if (preloadFileInformation) { 193 nowRoot.populateNow(stTypeNames.FILE, propertiesToCache, -1); 195 } 196 197 addFolderModsToList(nowFiles, nowRoot); 199 200 try { 201 lastBuildRoot = 202 StarTeamFinder.findFolder(snapshotAtLastBuild.getRootFolder(), folder); 203 204 if (preloadFileInformation) { 205 lastBuildRoot.populateNow(stTypeNames.FILE, propertiesToCache, -1); 207 } 208 209 addFolderModsToList(lastBuildFiles, lastBuildRoot); 210 } catch (ServerException se) { 211 LOG.error(url + ": Server Exception occurred visiting last build view: ", se); 212 } 213 214 compareFileLists(nowFiles, lastBuildFiles); 215 216 snapshotAtNow.getRootFolder().discardItems(stTypeNames.FILE, -1); 218 219 try { 220 snapshotAtLastBuild.getRootFolder().discardItems(stTypeNames.FILE, -1); 221 } catch (ServerException se) { 222 LOG.error(url + ": Server Exception occurred discarding last build file cache: ", se); 223 } 224 225 LOG.info(url + ": " + modifications.size() + " modifications in " + nowRoot.getFolderHierarchy()); 226 return modifications; 227 } catch (Exception e) { 228 LOG.error(url + ": Problem looking up modifications in StarTeam.", e); 229 modifications.clear(); 230 return modifications; 231 } finally { 232 if (server != null) { 233 server.disconnect(); 234 } 235 } 236 } 237 238 241 private void compareFileLists(Map nowFiles, Map lastBuildFiles) { 242 for (Iterator iter = nowFiles.keySet().iterator(); iter.hasNext();) { 243 Integer currentItemID = (Integer ) iter.next(); 244 File currentFile = (File) nowFiles.get(currentItemID); 245 246 if (lastBuildFiles.containsKey(currentItemID)) { 247 File lastBuildFile = (File) lastBuildFiles.get(currentItemID); 248 249 if (fileHasBeenModified(currentFile, lastBuildFile)) { 250 addRevision(currentFile, "modified"); 251 } else if (fileHasBeenMoved(currentFile, lastBuildFile)) { 252 addRevision(currentFile, "moved"); 253 } 254 lastBuildFiles.remove(currentItemID); 258 } else { 259 addRevision(currentFile, "new"); 261 } 262 } 263 examineOldFiles(lastBuildFiles); 264 } 265 266 270 private void examineOldFiles(Map lastBuildFiles) { 271 272 if (canLookupDeletions && preloadFileInformation && !lastBuildFiles.isEmpty()) { 274 try { 275 final String [] propertiesToCacheForDeletes = 277 new String [] { 278 stPropertyNames.AUDIT_CLASS_ID, 279 stPropertyNames.AUDIT_OBJECT_ID, 280 stPropertyNames.AUDIT_EVENT_ID, 281 stPropertyNames.MODIFIED_TIME, 282 stPropertyNames.AUDIT_USER_ID }; 283 284 lastBuildRoot.populateNow(stTypeNames.AUDIT, propertiesToCacheForDeletes, -1); 285 } catch (TypeNotFoundException tnfx) { 286 LOG.debug(url + ": Error caching Audit information (StarTeam 4.2 SP2 SDK or older in use)." 287 + " Deletions will be reported as by Unknown.", tnfx); 288 canLookupDeletions = false; 289 } 290 } 291 293 for (Iterator iter = lastBuildFiles.values().iterator(); iter.hasNext();) { 294 File currentLastBuildFile = (File) iter.next(); 295 addRevision((File) currentLastBuildFile.getFromHistoryByDate(nowDate), "deleted"); 296 } 297 } 298 299 private boolean fileHasBeenModified(File currentFile, File lastBuildFile) { 300 return currentFile.getContentVersion() != lastBuildFile.getContentVersion(); 301 } 302 303 private boolean fileHasBeenMoved(File currentFile, File lastBuildFile) { 304 return !currentFile.getParentFolder().getFolderHierarchy().equals( 305 lastBuildFile.getParentFolder().getFolderHierarchy()); 306 } 307 308 private void addFolderModsToList(Map fileList, Folder folder) { 309 313 Item[] files = folder.getItems(stTypeNames.FILE); 314 for (int i = 0; i < files.length; i++) { 315 File file = (File) files[i]; 316 fileList.put(new Integer (file.getItemID()), file); 317 } 318 319 Folder[] folders = folder.getSubFolders(); 320 for (int i = 0; i < folders.length; i++) { 321 addFolderModsToList(fileList, folders[i]); 322 } 323 } 324 325 330 private void addRevision(File revision, String status) { 331 332 Modification mod = new Modification(); 333 mod.type = "StarTeam"; 334 String fileName = revision.getName(); 335 Folder parentFolder = revision.getParentFolder(); 336 String folderName = parentFolder.getFolderHierarchy(); 337 if (folderName.length() > 0 && (folderName.endsWith("\\") || folderName.endsWith("/"))) { 338 folderName = folderName.substring(0, folderName.length() - 1); 339 } 340 Modification.ModifiedFile modFile = mod.createModifiedFile(fileName, folderName); 341 modFile.action = status; 342 343 User user = null; 344 if (property != null) { 345 properties.put(property, "true"); 346 } 347 if (status.equals("deleted")) { 348 if (propertyOnDelete != null) { 349 properties.put(propertyOnDelete, "true"); 350 } 351 352 boolean foundAudit = false; 353 354 if (canLookupDeletions) { 356 try { 357 Item[] audits = parentFolder.getItems(stTypeNames.AUDIT); 358 for (int i = 0; i < audits.length && !foundAudit; i++) { 359 com.starbase.starteam.Audit audit = (com.starbase.starteam.Audit) (audits[i]); 360 if (audit.getItemDescriptor().equals(fileName) && (audit.getEnumDisplayName( 361 stPropertyNames.AUDIT_EVENT_ID, audit.getInt(stPropertyNames.AUDIT_EVENT_ID)).equals( 362 "Deleted"))) { 363 foundAudit = true; 364 mod.modifiedTime = audit.getModifiedTime().createDate(); 365 user = server.getUser(audit.getInt(stPropertyNames.AUDIT_USER_ID)); 366 mod.userName = user.getName(); 367 } 368 } 369 } catch (TypeNotFoundException tnfx) { 370 LOG.debug(url + ": Error looking up Audit information (StarTeam 4.2 SP2 SDK or older in use)." 371 + " Deletions will be reported as by Unknown.", tnfx); 372 canLookupDeletions = false; 373 } 374 } 375 377 if (!foundAudit) { 378 mod.modifiedTime = revision.getModifiedTime().createDate(); 379 mod.userName = "Unknown"; 380 } 381 } else { 382 mod.modifiedTime = revision.getModifiedTime().createDate(); 383 user = server.getUser(revision.getModifiedBy()); 384 mod.userName = user.getName(); 385 mod.comment = revision.getComment(); 386 mod.revision = "" + revision.getContentVersion(); 387 } 388 389 if (user != null && canLookupEmails) { 391 392 try { 395 UserAccount useracct = 397 server.getAdministration().findUserAccount(user.getID()); 398 if (useracct == null) { 399 LOG.warn(url + ": User account for " + user.getName() + " with ID " + user.getID() + " not found."); 400 } else { 401 mod.emailAddress = useracct.getEmailAddress(); 402 } 403 } catch (ServerException sx) { 404 LOG.debug(url + ": Error looking up user email address.", sx); 409 canLookupEmails = false; 410 } 411 } 412 413 modifications.add(mod); 414 } 415 416 } 417 | Popular Tags |