1 12 13 package org.eclipse.team.internal.ccvs.core; 14 15 import java.io.File ; 16 import java.net.URI ; 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.HashMap ; 20 import java.util.HashSet ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.Set ; 24 import java.util.StringTokenizer ; 25 26 import org.eclipse.core.resources.*; 27 import org.eclipse.core.runtime.*; 28 import org.eclipse.core.runtime.jobs.ISchedulingRule; 29 import org.eclipse.core.runtime.jobs.MultiRule; 30 import org.eclipse.team.core.*; 31 import org.eclipse.team.internal.ccvs.core.client.*; 32 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption; 33 import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation; 34 import org.eclipse.team.internal.ccvs.core.connection.CVSServerException; 35 import org.eclipse.team.internal.ccvs.core.filesystem.CVSURI; 36 import org.eclipse.team.internal.ccvs.core.resources.*; 37 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo; 38 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories; 39 40 48 public class CVSProjectSetCapability extends ProjectSetCapability { 49 50 55 public String [] asReference( 56 IProject[] projects, 57 ProjectSetSerializationContext context, 58 IProgressMonitor monitor) 59 throws TeamException { 60 61 String [] result = new String [projects.length]; 62 for (int i = 0; i < projects.length; i++) 63 result[i] = asReference(projects[i]); 64 return result; 65 } 66 67 74 private String asReference(IProject project) throws TeamException { 75 CVSTeamProvider provider = (CVSTeamProvider)RepositoryProvider.getProvider(project); 76 CVSWorkspaceRoot root = provider.getCVSWorkspaceRoot(); 77 CVSRepositoryLocation location = CVSRepositoryLocation.fromString(root.getRemoteLocation().getLocation(false)); 78 location.setUserMuteable(true); 79 ICVSFolder folder = root.getLocalRoot(); 80 return asReference(location, folder, project); 81 } 82 83 private String asReference(CVSRepositoryLocation location, ICVSFolder folder, IProject project) throws TeamException { 84 StringBuffer buffer = new StringBuffer (); 85 buffer.append("1.0,"); 87 String repoLocation = location.getLocation(); 88 buffer.append(repoLocation); 89 buffer.append(","); 91 FolderSyncInfo syncInfo = folder.getFolderSyncInfo(); 92 String module = syncInfo.getRepository(); 93 buffer.append(module); 94 buffer.append(","); 96 String projectName = project.getName(); 97 buffer.append(projectName); 98 CVSTag tag = syncInfo.getTag(); 99 if (tag != null) { 100 if (tag.getType() != CVSTag.DATE) { 101 buffer.append(","); String tagName = tag.getName(); 103 buffer.append(tagName); 104 } 105 } 106 return buffer.toString(); 107 } 108 109 114 public IProject[] addToWorkspace( 115 String [] referenceStrings, 116 ProjectSetSerializationContext context, 117 IProgressMonitor monitor) 118 throws TeamException { 119 120 monitor = Policy.monitorFor(monitor); 121 Policy.checkCanceled(monitor); 122 123 Map infoMap = new HashMap (referenceStrings.length); 125 IProject[] projects = asProjects(referenceStrings, infoMap); 126 projects = confirmOverwrite(context, projects); 127 if (projects == null) 128 throw new OperationCanceledException(); 129 130 return checkout(projects, infoMap, monitor); 132 } 133 134 142 private IProject[] asProjects(String [] referenceStrings, Map infoMap) throws CVSException { 143 Collection result = new ArrayList (); 144 for (int i = 0; i < referenceStrings.length; i++) { 145 StringTokenizer tokenizer = new StringTokenizer (referenceStrings[i], ","); String version = tokenizer.nextToken(); 147 if (!version.equals("1.0")) continue; 150 LoadInfo info = new LoadInfo(tokenizer); 151 IProject proj = info.getProject(); 152 result.add(proj); 153 infoMap.put(proj, info); 154 } 155 return (IProject[]) result.toArray(new IProject[result.size()]); 156 } 157 158 165 private IProject[] checkout( 166 final IProject[] projects, 167 final Map infoMap, 168 IProgressMonitor monitor) 169 throws TeamException { 170 171 final List result = new ArrayList (); 172 try { 173 ResourcesPlugin.getWorkspace().run(new IWorkspaceRunnable() { 174 public void run(IProgressMonitor monitor) throws CoreException { 175 monitor.beginTask("", 1000 * projects.length); try { 177 for (int i = 0; i < projects.length; i++) { 178 if (monitor.isCanceled()) 179 break; 180 IProject project = projects[i]; 181 LoadInfo info = (LoadInfo) infoMap.get(project); 182 if (info != null && info.checkout(new SubProgressMonitor(monitor, 1000))) 183 result.add(project); 184 } 185 } 186 finally { 187 monitor.done(); 188 } 189 } 190 }, getCheckoutRule(projects), IResource.NONE, monitor); 191 } catch (CoreException e) { 192 throw TeamException.asTeamException(e); 193 } 194 return (IProject[])result.toArray(new IProject[result.size()]); 195 } 196 197 200 class LoadInfo { 201 private final ICVSRepositoryLocation repositoryLocation; 202 private final String module; 203 private final IProject project; 204 private final CVSTag tag; 205 206 211 LoadInfo(StringTokenizer tokenizer) throws CVSException { 212 String repo = tokenizer.nextToken(); 213 repositoryLocation = getRepositoryLocationFromString(repo); 214 module = tokenizer.nextToken(); 215 String projectName = tokenizer.nextToken(); 216 project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); 217 if (tokenizer.hasMoreTokens()) { 218 String tagName = tokenizer.nextToken(); 219 tag = new CVSTag(tagName, CVSTag.BRANCH); 220 } 221 else { 222 tag = null; 223 } 224 } 225 226 232 private IProject getProject() { 233 return project; 234 } 235 236 243 boolean checkout(IProgressMonitor monitor) throws TeamException { 244 if (repositoryLocation == null) 245 return false; 246 CVSProjectSetCapability.checkout( 247 repositoryLocation, 248 project, 249 module, 250 tag, 251 monitor); 252 return true; 253 } 254 } 255 256 263 private static ICVSRepositoryLocation getRepositoryLocationFromString(String repo) throws CVSException { 264 ICVSRepositoryLocation newLocation = CVSRepositoryLocation.fromString(repo); 266 if (newLocation.getUsername() == null || newLocation.getUsername().length() == 0) { 267 ICVSRepositoryLocation[] locations = CVSProviderPlugin.getPlugin().getKnownRepositories(); 269 for (int i = 0; i < locations.length; i++) { 270 ICVSRepositoryLocation location = locations[i]; 271 if (location.getMethod() == newLocation.getMethod() 272 && location.getHost().equals(newLocation.getHost()) 273 && location.getPort() == newLocation.getPort() 274 && location.getRootDirectory().equals(newLocation.getRootDirectory())) 275 return location; 276 } 277 } 278 KnownRepositories.getInstance().addRepository(newLocation, true); 280 return newLocation; 281 } 282 283 298 public static void checkout( 299 ICVSRepositoryLocation repository, 300 IProject project, 301 String sourceModule, 302 CVSTag tag, 303 IProgressMonitor monitor) 304 throws TeamException { 305 306 if (sourceModule == null) 307 sourceModule = project.getName(); 308 checkout(new ICVSRemoteFolder[] { new RemoteFolder(null, repository, sourceModule, tag)}, 309 new IProject[] { project }, monitor); 310 } 311 312 320 public static void checkout(final ICVSRemoteFolder[] resources, final IProject[] projects, final IProgressMonitor monitor) throws TeamException { 321 final TeamException[] eHolder = new TeamException[1]; 322 try { 323 IWorkspaceRunnable workspaceRunnable = new IWorkspaceRunnable() { 324 public void run(IProgressMonitor pm) throws CoreException { 325 try { 326 pm.beginTask(null, 1000 * resources.length); 327 328 ICVSFolder root = CVSWorkspaceRoot.getCVSFolderFor(ResourcesPlugin.getWorkspace().getRoot()); 330 331 for (int i=0;i<resources.length;i++) { 332 IProject project = null; 333 RemoteFolder resource = (RemoteFolder)resources[i]; 334 335 if (projects != null) 337 project = projects[i]; 338 339 String moduleName; 341 if (resource instanceof RemoteModule) { 342 moduleName = ((RemoteModule)resource).getName(); 343 } else { 344 moduleName = resource.getRepositoryRelativePath(); 345 } 346 347 ICVSRepositoryLocation repository = resource.getRepository(); 349 Session session = new Session(repository, root); 350 try { 351 session.open(Policy.subMonitorFor(pm, 50), false ); 352 353 final Set targetProjects = new HashSet (); 355 if (project == null) { 356 357 IStatus status = Request.EXPAND_MODULES.execute(session, new String [] {moduleName}, Policy.subMonitorFor(pm, 50)); 359 if (status.getCode() == CVSStatus.SERVER_ERROR) { 360 throw new CVSServerException(status); 361 } 362 363 String [] expansions = session.getModuleExpansions(); 365 for (int j = 0; j < expansions.length; j++) { 366 targetProjects.add(ResourcesPlugin.getWorkspace().getRoot().getProject(new Path(null, expansions[j]).segment(0))); 367 } 368 369 } else { 370 targetProjects.add(project); 371 } 372 373 root.run(new ICVSRunnable() { 375 public void run(IProgressMonitor monitor) throws CVSException { 376 scrubProjects((IProject[]) targetProjects.toArray(new IProject[targetProjects.size()]), monitor); 377 } 378 }, Policy.subMonitorFor(pm, 100)); 379 380 List localOptions = new ArrayList (); 382 if (project != null) { 384 localOptions.add(Checkout.makeDirectoryNameOption(project.getName())); 385 } 386 if (CVSProviderPlugin.getPlugin().getPruneEmptyDirectories()) 388 localOptions.add(Command.PRUNE_EMPTY_DIRECTORIES); 389 CVSTag tag = resource.getTag(); 391 if (tag == null) { 392 tag = CVSTag.DEFAULT; 394 } 395 localOptions.add(Update.makeTagOption(tag)); 396 397 IStatus status = Command.CHECKOUT.execute(session, 399 Command.NO_GLOBAL_OPTIONS, 400 (LocalOption[])localOptions.toArray(new LocalOption[localOptions.size()]), 401 new String []{moduleName}, 402 null, 403 Policy.subMonitorFor(pm, 800)); 404 if (status.getCode() == CVSStatus.SERVER_ERROR) { 405 throw new CVSServerException(status); 407 } 408 409 refreshProjects((IProject[])targetProjects.toArray(new IProject[targetProjects.size()]), Policy.subMonitorFor(pm, 100)); 411 412 } finally { 413 session.close(); 414 } 415 } 416 } 417 catch (TeamException e) { 418 eHolder[0] = e; 420 } finally { 421 pm.done(); 422 } 423 } 425 }; 426 ResourcesPlugin.getWorkspace().run(workspaceRunnable, getCheckoutRule(projects), 0, monitor); 427 } catch (CoreException e) { 428 throw CVSException.wrapException(e); 429 } finally { 430 monitor.done(); 431 } 432 if (eHolder[0] != null) { 434 throw eHolder[0]; 435 } 436 } 437 438 private static ISchedulingRule getCheckoutRule(final IProject[] projects) { 439 if (projects.length == 1) { 440 return ResourcesPlugin.getWorkspace().getRuleFactory().modifyRule(projects[0]); 441 } else { 442 Set rules = new HashSet (); 443 for (int i = 0; i < projects.length; i++) { 444 ISchedulingRule modifyRule = ResourcesPlugin.getWorkspace().getRuleFactory().modifyRule(projects[i]); 445 if (modifyRule instanceof IResource && ((IResource)modifyRule).getType() == IResource.ROOT) { 446 return modifyRule; 449 } 450 rules.add(modifyRule); 451 } 452 return new MultiRule((ISchedulingRule[]) rules.toArray(new ISchedulingRule[rules.size()])); 453 } 454 } 455 458 static void refreshProjects(IProject[] projects, IProgressMonitor monitor) throws CoreException, TeamException { 459 monitor.beginTask(CVSMessages.CVSProvider_Creating_projects_2, projects.length * 100); 460 try { 461 for (int i = 0; i < projects.length; i++) { 462 IProject project = projects[i]; 463 RepositoryProvider.map(project, CVSProviderPlugin.getTypeId()); 465 CVSTeamProvider provider = (CVSTeamProvider)RepositoryProvider.getProvider(project, CVSProviderPlugin.getTypeId()); 466 provider.setWatchEditEnabled(CVSProviderPlugin.getPlugin().isWatchEditEnabled()); 467 } 468 469 } finally { 470 monitor.done(); 471 } 472 } 473 474 477 static void scrubProjects(IProject[] projects, IProgressMonitor monitor) throws CVSException { 478 if (projects == null) { 479 monitor.done(); 480 return; 481 } 482 monitor.beginTask(CVSMessages.CVSProvider_Scrubbing_projects_1, projects.length * 100); 483 try { 484 for (int i=0;i<projects.length;i++) { 485 IProject project = projects[i]; 486 if (project != null && project.exists()) { 487 if(!project.isOpen()) { 488 project.open(Policy.subMonitorFor(monitor, 10)); 489 } 490 monitor.subTask(CVSMessages.CVSProvider_Scrubbing_local_project_1); 493 if (RepositoryProvider.getProvider(project) != null) 495 RepositoryProvider.unmap(project); 496 IResource[] children = project.members(IContainer.INCLUDE_TEAM_PRIVATE_MEMBERS); 497 IProgressMonitor subMonitor = Policy.subMonitorFor(monitor, 80); 498 subMonitor.beginTask(null, children.length * 100); 499 try { 500 for (int j = 0; j < children.length; j++) { 501 if ( ! children[j].getName().equals(".project")) { children[j].delete(true , Policy.subMonitorFor(subMonitor, 100)); 503 } 504 } 505 } finally { 506 subMonitor.done(); 507 } 508 } else if (project != null) { 509 File location = new File (project.getParent().getLocation().toFile(), project.getName()); 511 if (location.exists()) { 512 deepDelete(location); 513 } 514 } 515 } 516 } catch (CoreException e) { 517 throw CVSException.wrapException(e); 518 } finally { 519 monitor.done(); 520 } 521 } 522 523 private static void deepDelete(File resource) { 524 if (resource.isDirectory()) { 525 File [] fileList = resource.listFiles(); 526 for (int i = 0; i < fileList.length; i++) { 527 deepDelete(fileList[i]); 528 } 529 } 530 resource.delete(); 531 } 532 533 public String getProject(String referenceString) { 534 StringTokenizer tokenizer = new StringTokenizer (referenceString, ","); String version = tokenizer.nextToken(); 537 if (!version.equals("1.0")) return null; 540 try { 541 LoadInfo info = new LoadInfo(tokenizer); 542 return info.getProject().getName(); 543 } catch (CVSException e) { 544 CVSProviderPlugin.log(e); 545 return null; 546 } 547 } 548 549 public URI getURI(String referenceString) { 550 StringTokenizer tokenizer = new StringTokenizer (referenceString, ","); String version = tokenizer.nextToken(); 553 if (!version.equals("1.0")) return null; 556 try { 557 LoadInfo info = new LoadInfo(tokenizer); 558 CVSURI cvsURI = new CVSURI(info.repositoryLocation,new Path(info.module),info.tag); 559 return cvsURI.toURI(); 560 } catch (CVSException e) { 561 CVSProviderPlugin.log(e); 562 return null; 563 } 564 } 565 566 569 public String asReference(URI uri, String projectName) { 570 try { 571 CVSURI cvsURI = CVSURI.fromUri(uri); 572 ICVSRepositoryLocation location = cvsURI.getRepository(); 573 ICVSFolder folder = cvsURI.toFolder(); 574 IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); 575 return asReference((CVSRepositoryLocation)location, folder, project); 576 } catch (TeamException e) { 577 CVSProviderPlugin.log(e); 578 return null; 579 } 580 } 581 582 } 583 | Popular Tags |