1 11 package org.eclipse.team.internal.ccvs.core.syncinfo; 12 13 import java.util.*; 14 15 import org.eclipse.core.resources.*; 16 import org.eclipse.core.runtime.*; 17 import org.eclipse.core.runtime.jobs.Job; 18 import org.eclipse.osgi.util.NLS; 19 import org.eclipse.team.core.RepositoryProvider; 20 import org.eclipse.team.core.TeamException; 21 import org.eclipse.team.core.variants.*; 22 import org.eclipse.team.internal.ccvs.core.*; 23 import org.eclipse.team.internal.ccvs.core.resources.*; 24 25 28 public class CVSResourceVariantTree extends ResourceVariantTree { 29 30 private CVSTag tag; 31 private boolean cacheFileContentsHint; 32 private CVSSyncTreeSubscriber subscriber; 33 34 public CVSResourceVariantTree(ResourceVariantByteStore cache, CVSTag tag, boolean cacheFileContentsHint) { 35 super(cache); 36 this.tag = tag; 37 this.cacheFileContentsHint = cacheFileContentsHint; 38 } 39 40 43 public ResourceVariantByteStore getByteStore() { 44 return super.getByteStore(); 45 } 46 47 50 protected byte[] getBytes(IResource local, IResourceVariant remote) throws TeamException { 51 if (remote != null) { 52 return super.getBytes(local, remote); 53 } else { 54 if (local.getType() == IResource.FOLDER) { 55 return getBaseBytes((IContainer)local, getTag(local)); 57 } 58 return null; 59 } 60 } 61 62 65 protected IResourceVariant[] fetchMembers(IResourceVariant remote, IProgressMonitor progress) throws TeamException { 66 ICVSRemoteResource[] children = remote != null ? (ICVSRemoteResource[])((RemoteResource)remote).members(progress) : new ICVSRemoteResource[0]; 67 IResourceVariant[] result = new IResourceVariant[children.length]; 68 for (int i = 0; i < children.length; i++) { 69 result[i] = (IResourceVariant)children[i]; 70 } 71 return result; 72 } 73 74 77 protected IResourceVariant fetchVariant(IResource resource, int depth, IProgressMonitor monitor) throws TeamException { 78 return (IResourceVariant)CVSWorkspaceRoot.getRemoteTree(resource, getTag(resource), isCacheFileContentsHint(), depth, monitor); 79 } 80 81 84 public IResource[] collectChanges(IResource local, 85 IResourceVariant remote, int depth, IProgressMonitor monitor) 86 throws TeamException { 87 return super.collectChanges(local, remote, depth, monitor); 88 } 89 90 public IResource[] members(IResource resource) throws TeamException { 91 if (resource.getType() == IResource.FILE) { 92 return new IResource[0]; 93 } 94 Set members = new HashSet(); 96 members.addAll(Arrays.asList(super.members(resource))); 97 try { 98 IResource[] localMembers = EclipseSynchronizer.getInstance().members((IContainer) resource); for (int i = 0; i < localMembers.length; i++) { 100 IResource local = localMembers[i]; 101 if (local.getType() != IResource.FILE) { 102 ICVSFolder folder = CVSWorkspaceRoot.getCVSFolderFor((IContainer)local); 103 if (folder.isCVSFolder()) { 104 members.add(local); 105 } 106 } 107 } 108 } catch (CoreException e) { 109 throw CVSException.wrapException(e); 110 } 111 return (IResource[]) members.toArray(new IResource[members.size()]); 112 } 113 114 117 public IResource[] roots() { 118 return subscriber.roots(); 119 } 120 121 124 public IResourceVariant getResourceVariant(IResource resource) throws TeamException { 125 byte[] remoteBytes = getByteStore().getBytes(resource); 126 if (remoteBytes == null) { 127 return null; 129 } else { 130 if (resource.getType() == IResource.FILE) { 131 byte[] parentBytes = getParentBytes(resource); 132 if (parentBytes == null) { 133 IProject project = resource.getProject(); 134 if (project.exists() && RepositoryProvider.getProvider(project, CVSProviderPlugin.getTypeId()) != null) { 135 IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.ERROR,NLS.bind(CVSMessages.ResourceSynchronizer_missingParentBytesOnGet, new String [] { getSyncName(getByteStore()).toString(), resource.getFullPath().toString() }),resource); 136 CVSProviderPlugin.log(status); 137 } 139 return null; 140 } 141 return RemoteFile.fromBytes(resource, remoteBytes, parentBytes); 142 } else { 143 return RemoteFolder.fromBytes(resource, remoteBytes); 144 } 145 } 146 } 147 148 private String getSyncName(ResourceVariantByteStore cache) { 149 if (cache instanceof PersistantResourceVariantByteStore) { 150 return ((PersistantResourceVariantByteStore)cache).getSyncName().toString(); 151 } 152 return cache.getClass().getName(); 153 } 154 155 156 private byte[] getParentBytes(IResource resource) throws TeamException { 157 IContainer parent = resource.getParent(); 158 byte[] bytes = getByteStore().getBytes(parent); 159 if (bytes == null ) { 160 bytes = getBaseBytes(parent, getTag(resource)); 161 } 162 return bytes; 163 } 164 165 private byte[] getBaseBytes(IContainer parent, CVSTag tag) throws CVSException { 166 byte[] bytes; 167 ICVSFolder local = CVSWorkspaceRoot.getCVSFolderFor(parent); 169 FolderSyncInfo info = local.getFolderSyncInfo(); 170 if (info == null) { 171 bytes = null; 172 } else { 173 MutableFolderSyncInfo newInfo = info.cloneMutable(); 175 newInfo.setTag(tag); 176 newInfo.setStatic(false); 177 bytes = newInfo.getBytes(); 178 } 179 return bytes; 180 } 181 182 private boolean hasLocalSyncInfo(IContainer folder) { 183 ICVSFolder local = CVSWorkspaceRoot.getCVSFolderFor(folder); 184 try { 185 return local.getFolderSyncInfo() != null; 186 } catch (CVSException e) { 187 return true; 189 } 190 } 191 192 public CVSTag getTag(IResource resource) { 193 return tag; 194 } 195 196 199 public void dispose() { 200 getByteStore().dispose(); 201 } 202 203 206 protected boolean setVariant(IResource local, IResourceVariant remote) throws TeamException { 207 if (local.getType() == IResource.FOLDER && remote != null 208 && !hasLocalSyncInfo((IFolder)local) 209 && hasLocalSyncInfo(local.getParent())) { 210 CVSTag tag = CVSWorkspaceRoot.getCVSFolderFor(local.getParent()).getFolderSyncInfo().getTag(); 213 FolderSyncInfo info = null; 214 try{ 215 info = FolderSyncInfo.getFolderSyncInfo(remote.asBytes()); 216 } catch (CVSException e){ 217 Status status = new Status(Status.ERROR, CVSProviderPlugin.ID, 218 NLS.bind(CVSMessages.CVSResourceVariantTree_GettingSyncInfoError, local.getProjectRelativePath().toString()), 219 e); 220 throw new CVSException(status); 221 } 222 MutableFolderSyncInfo newInfo = info.cloneMutable(); 223 newInfo.setTag(tag); 224 ICVSFolder cvsFolder = CVSWorkspaceRoot.getCVSFolderFor((IFolder)local); 225 cvsFolder.setFolderSyncInfo(newInfo); 226 } 227 if (remote == null && !isManaged(local)) { 228 boolean changed = getByteStore().getBytes(local) != null; 231 flushVariants(local, IResource.DEPTH_ZERO); 232 return changed; 233 } else { 234 boolean changed = super.setVariant(local, remote); 235 if (local.getType() == IResource.FILE && getByteStore().getBytes(local) != null && !parentHasSyncBytes(local)) { 236 CVSProviderPlugin.log(new TeamException(NLS.bind(CVSMessages.ResourceSynchronizer_missingParentBytesOnSet, new String [] { getSyncName(getByteStore()), local.getFullPath().toString() }))); 239 } 240 return changed; 241 } 242 } 243 244 private boolean isManaged(IResource local) { 245 try { 246 return CVSWorkspaceRoot.getCVSResourceFor(local).isManaged(); 247 } catch (CVSException e) { 248 return false; 249 } 250 } 251 252 private boolean parentHasSyncBytes(IResource resource) throws TeamException { 253 if (resource.getType() == IResource.PROJECT) return true; 254 return getParentBytes(resource) != null; 255 } 256 257 260 protected IResource[] collectedMembers(IResource local, IResource[] members) throws TeamException { 261 IResource[] resources = getStoredMembers(local); 263 List children = new ArrayList(); 264 List changedResources = new ArrayList(); 265 children.addAll(Arrays.asList(members)); 266 for (int i = 0; i < resources.length; i++) { 267 IResource resource = resources[i]; 268 if (!children.contains(resource)) { 269 flushVariants(resource, IResource.DEPTH_INFINITE); 271 changedResources.add(resource); 272 } 273 } 274 return (IResource[]) changedResources.toArray(new IResource[changedResources.size()]); 275 } 276 277 286 private IResource[] getStoredMembers(IResource local) throws TeamException { 287 try { 288 if (local.getType() != IResource.FILE && (local.exists() || local.isPhantom())) { 289 IResource[] allChildren = ((IContainer)local).members(true ); 290 List childrenWithSyncBytes = new ArrayList(); 291 for (int i = 0; i < allChildren.length; i++) { 292 IResource resource = allChildren[i]; 293 if (getByteStore().getBytes(resource) != null) { 294 childrenWithSyncBytes.add(resource); 295 } 296 } 297 return (IResource[]) childrenWithSyncBytes.toArray( 298 new IResource[childrenWithSyncBytes.size()]); 299 } 300 } catch (CoreException e) { 301 throw TeamException.asTeamException(e); 302 } 303 return new IResource[0]; 304 } 305 306 309 protected IResource[] refresh(IResource resource, int depth, IProgressMonitor monitor) throws TeamException { 310 IResource[] changedResources = null; 311 monitor.beginTask(null, 100); 312 int count = 0; 316 int waitTime = 10; 317 while (count < 15 318 && (isJobInFamilyRunning(ResourcesPlugin.FAMILY_AUTO_BUILD) 319 || isJobInFamilyRunning(ResourcesPlugin.FAMILY_MANUAL_BUILD))) { 320 try { 321 Thread.sleep(waitTime); 322 } catch (InterruptedException e) { 323 } 325 count++; 326 if (count >= 10) { 327 waitTime = 1000; 328 } else if (count >= 5) { 329 waitTime = 100; 330 } 331 Policy.checkCanceled(monitor); 332 } 333 try { 334 changedResources = super.refresh(resource, depth, Policy.subMonitorFor(monitor, 99)); 335 } catch (TeamException e) { 336 if (!resource.getProject().isAccessible()) { 339 return new IResource[0]; 341 } 342 throw e; 343 } finally { 344 monitor.done(); 345 } 346 if (changedResources == null) return new IResource[0]; 347 return changedResources; 348 } 349 350 private boolean isJobInFamilyRunning(Object family) { 351 Job[] jobs = Job.getJobManager().find(family); 352 if (jobs != null && jobs.length > 0) { 353 for (int i = 0; i < jobs.length; i++) { 354 Job job = jobs[i]; 355 if (job.getState() != Job.NONE) { 356 return true; 357 } 358 } 359 } 360 return false; 361 } 362 363 364 public ICVSRemoteResource buildTree(RemoteFolderTree parent, IResource resource, boolean immutable, IProgressMonitor monitor) throws TeamException { 365 366 Policy.checkCanceled(monitor); 367 368 byte[] remoteBytes = getByteStore().getBytes(resource); 369 if (remoteBytes == null) { 370 return null; 372 } 373 374 if (resource.getType() == IResource.FILE) { 375 if (immutable) { 376 remoteBytes = ResourceSyncInfo.setTag(remoteBytes, new CVSTag(ResourceSyncInfo.getRevision(remoteBytes), CVSTag.VERSION)); 377 } 378 if (parent == null) { 379 return (ICVSRemoteResource)getResourceVariant(resource); 380 } 381 return new RemoteFile(parent, remoteBytes); 382 } else { 383 RemoteFolderTree remote = RemoteFolderTree.fromBytes(parent, resource, remoteBytes); 384 IResource[] members = members(resource); 385 List children = new ArrayList(); 386 for (int i = 0; i < members.length; i++) { 387 IResource member = members[i]; 388 ICVSRemoteResource child = buildTree(remote, member, immutable, monitor); 389 if (child != null) 390 children.add(child); 391 } 392 393 remote.setChildren((ICVSRemoteResource[])children.toArray(new ICVSRemoteResource[children.size()])); 395 return remote; 396 } 397 } 398 399 public boolean isCacheFileContentsHint() { 400 return cacheFileContentsHint; 401 } 402 } 403 | Popular Tags |