1 12 package org.eclipse.team.internal.ccvs.core.resources; 13 14 import java.io.ByteArrayInputStream ; 15 import java.io.InputStream ; 16 import java.util.*; 17 18 import org.eclipse.core.resources.IFile; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.runtime.*; 21 import org.eclipse.team.core.TeamException; 22 import org.eclipse.team.core.history.IFileRevision; 23 import org.eclipse.team.core.variants.CachedResourceVariant; 24 import org.eclipse.team.internal.ccvs.core.*; 25 import org.eclipse.team.internal.ccvs.core.client.*; 26 import org.eclipse.team.internal.ccvs.core.client.Command.*; 27 import org.eclipse.team.internal.ccvs.core.client.listeners.ILogEntryListener; 28 import org.eclipse.team.internal.ccvs.core.client.listeners.LogListener; 29 import org.eclipse.team.internal.ccvs.core.connection.CVSServerException; 30 import org.eclipse.team.internal.ccvs.core.filehistory.CVSResourceVariantFileRevision; 31 import org.eclipse.team.internal.ccvs.core.filesystem.CVSURI; 32 import org.eclipse.team.internal.ccvs.core.syncinfo.*; 33 34 38 public class RemoteFile extends RemoteResource implements ICVSRemoteFile { 39 40 43 private final class LogEntryListener implements ILogEntryListener { 44 private final List entries = new ArrayList(); 45 public void handleLogEntryReceived(ILogEntry entry) { 46 if (entry.getRemoteFile().getRepositoryRelativePath().equals(getRepositoryRelativePath())) { 47 entries.add(entry); 48 } 49 } 50 public ILogEntry[] getEntries() { 51 return (ILogEntry[])entries.toArray(new ILogEntry[entries.size()]); 52 } 53 } 54 55 private byte[] syncBytes; 57 private ILogEntry entry; 59 private boolean fetching = false; 61 private boolean executable = false; 63 64 72 public static RemoteFile getBase(RemoteFolder parent, ICVSFile managed) throws CVSException { 73 Assert.isNotNull(parent, "A parent folder must be provided for file " + managed.getName()); byte[] syncBytes = managed.getSyncBytes(); 75 if ((syncBytes == null) || ResourceSyncInfo.isAddition(syncBytes)) { 76 return null; 78 } 79 if (ResourceSyncInfo.isDeletion(syncBytes)) { 80 syncBytes = ResourceSyncInfo.convertFromDeletion(syncBytes); 81 } 82 RemoteFile file = new RemoteFile(parent, syncBytes); 83 parent.setChildren(new ICVSRemoteResource[] {file}); 84 return file; 85 } 86 87 90 public static RemoteFile fromBytes(IResource local, byte[] bytes, byte[] parentBytes) throws CVSException { 91 Assert.isNotNull(bytes); 92 Assert.isTrue(local.getType() == IResource.FILE); 93 RemoteFolder parent = RemoteFolder.fromBytes(local.getParent(), parentBytes); 94 RemoteFile file = new RemoteFile(parent, bytes); 95 parent.setChildren(new ICVSRemoteResource[] {file}); 96 return file; 97 } 98 99 103 public static RemoteFile create(String filePath, ICVSRepositoryLocation location) { 104 return create(filePath, location, null, null); 105 } 106 107 111 public static RemoteFile create(String filePath, ICVSRepositoryLocation location, CVSTag tag, String revision) { 112 Assert.isNotNull(filePath); 113 Assert.isNotNull(location); 114 IPath path = new Path(null, filePath); 115 RemoteFolder parent = new RemoteFolder(null , location, path.removeLastSegments(1).toString(), tag ); 116 RemoteFile file = new RemoteFile(parent, Update.STATE_NONE, path.lastSegment(), revision , null , tag ); 117 parent.setChildren(new ICVSRemoteResource[] {file}); 118 return file; 119 } 120 121 131 public RemoteFile(RemoteFolder parent, int workspaceSyncState, String name, String revision, KSubstOption keywordMode, CVSTag tag) { 132 this(parent, name, workspaceSyncState, getSyncBytes(name, revision, keywordMode, tag)); 133 } 134 135 private static byte[] getSyncBytes(String name, String revision, KSubstOption keywordMode, CVSTag tag) { 136 if (revision == null) { 137 revision = ResourceSyncInfo.ADDED_REVISION; 138 } 139 if (keywordMode == null) { 140 keywordMode = KSubstOption.fromMode(""); } 145 MutableResourceSyncInfo newInfo = new MutableResourceSyncInfo(name, revision); 146 newInfo.setKeywordMode(keywordMode); 147 newInfo.setTag(tag); 148 return newInfo.getBytes(); 149 } 150 151 public RemoteFile(RemoteFolder parent, byte[] syncBytes) throws CVSException { 152 this(parent, Update.STATE_NONE, syncBytes); 153 } 154 155 RemoteFile(RemoteFolder parent, int workspaceSyncState, byte[] syncBytes) throws CVSException { 156 this(parent, ResourceSyncInfo.getName(syncBytes), workspaceSyncState, syncBytes); 157 } 158 159 private RemoteFile(RemoteFolder parent, String name, int workspaceSyncState, byte[] syncBytes) { 160 super(parent, name); 161 this.syncBytes = syncBytes; 162 setWorkspaceSyncState(workspaceSyncState); 163 } 164 165 168 public void accept(ICVSResourceVisitor visitor) throws CVSException { 169 visitor.visitFile(this); 170 } 171 172 175 public void accept(ICVSResourceVisitor visitor, boolean recurse) throws CVSException { 176 visitor.visitFile(this); 177 } 178 179 182 public InputStream getContents(IProgressMonitor monitor) throws CVSException { 183 try { 184 return getStorage(monitor).getContents(); 185 } catch (CoreException e) { 186 throw CVSException.wrapException(e); 187 } 188 } 189 190 protected void fetchContents(IProgressMonitor monitor) throws TeamException { 191 try { 192 aboutToReceiveContents(getSyncBytes()); 193 internalFetchContents(monitor); 194 if (!isContentsCached()) { 197 setContents(new ByteArrayInputStream (new byte[0]), monitor); 198 } 199 } finally { 200 doneReceivingContents(); 201 } 202 } 203 204 private void internalFetchContents(IProgressMonitor monitor) throws CVSException { 205 monitor.beginTask(CVSMessages.RemoteFile_getContents, 100); 206 monitor.subTask(CVSMessages.RemoteFile_getContents); 207 if (getRevision().equals(ResourceSyncInfo.ADDED_REVISION)) { 208 CVSTag tag = getSyncInfo().getTag(); 210 if (tag == null) tag = CVSTag.DEFAULT; 211 RemoteFolderMemberFetcher fetcher = new RemoteFolderMemberFetcher((RemoteFolder)getParent(), tag); 212 fetcher.updateFileRevisions(new ICVSFile[] { this }, Policy.subMonitorFor(monitor, 10)); 213 } 214 Session session = new Session(getRepository(), parent, false ); 215 session.open(Policy.subMonitorFor(monitor, 10), false ); 216 try { 217 IStatus status = Command.UPDATE.execute( 218 session, 219 Command.NO_GLOBAL_OPTIONS, 220 new LocalOption[] { 221 Update.makeTagOption(new CVSTag(getRevision(), CVSTag.VERSION)), 222 Update.IGNORE_LOCAL_CHANGES }, 223 new ICVSResource[] { this }, 224 null, 225 Policy.subMonitorFor(monitor, 80)); 226 if (status.getCode() == CVSStatus.SERVER_ERROR) { 227 throw new CVSServerException(status); 228 } 229 } finally { 230 session.close(); 231 monitor.done(); 232 } 233 } 234 235 238 public ILogEntry getLogEntry(IProgressMonitor monitor) throws CVSException { 239 if (entry == null) { 240 monitor = Policy.monitorFor(monitor); 241 monitor.beginTask(CVSMessages.RemoteFile_getLogEntries, 100); 242 Session session = new Session(getRepository(), parent, false ); 243 session.open(Policy.subMonitorFor(monitor, 10), false ); 244 try { 245 try { 246 LogEntryListener listener = new LogEntryListener(); 247 IStatus status = Command.LOG.execute( 248 session, 249 Command.NO_GLOBAL_OPTIONS, 250 new LocalOption[] { 251 Log.makeRevisionOption(getRevision())}, 252 new ICVSResource[] { RemoteFile.this }, 253 new LogListener(RemoteFile.this, listener), 254 Policy.subMonitorFor(monitor, 90)); 255 ILogEntry[] entries = listener.getEntries(); 256 if (entries.length == 1) { 257 entry = entries[0]; 258 } 259 if (status.getCode() == CVSStatus.SERVER_ERROR) { 260 throw new CVSServerException(status); 261 } 262 } finally { 263 monitor.done(); 264 } 265 } finally { 266 session.close(); 267 } 268 } 269 return entry; 270 } 271 272 275 public ILogEntry[] getLogEntries(IProgressMonitor monitor) throws CVSException { 276 monitor = Policy.monitorFor(monitor); 277 monitor.beginTask(CVSMessages.RemoteFile_getLogEntries, 100); 278 Session session = new Session(getRepository(), parent, false ); 279 session.open(Policy.subMonitorFor(monitor, 10), false ); 280 try { 281 QuietOption quietness = CVSProviderPlugin.getPlugin().getQuietness(); 282 try { 283 CVSProviderPlugin.getPlugin().setQuietness(Command.VERBOSE); 284 LogEntryListener listener = new LogEntryListener(); 285 IStatus status = Command.LOG.execute( 286 session, 287 Command.NO_GLOBAL_OPTIONS, Command.NO_LOCAL_OPTIONS, 288 new ICVSResource[] { RemoteFile.this }, new LogListener(RemoteFile.this, listener), 289 Policy.subMonitorFor(monitor, 90)); 290 if (status.getCode() == CVSStatus.SERVER_ERROR) { 291 throw new CVSServerException(status); 292 } 293 return listener.getEntries(); 294 } finally { 295 CVSProviderPlugin.getPlugin().setQuietness(quietness); 296 monitor.done(); 297 } 298 } finally { 299 session.close(); 300 } 301 } 302 303 306 public String getRevision() { 307 try { 308 return ResourceSyncInfo.getRevision(syncBytes); 309 } catch (CVSException e) { 310 CVSProviderPlugin.log(e); 311 return ResourceSyncInfo.ADDED_REVISION; 312 } 313 } 314 315 private KSubstOption getKeywordMode() { 316 try { 317 return ResourceSyncInfo.getKeywordMode(syncBytes); 318 } catch (CVSException e) { 319 CVSProviderPlugin.log(e); 320 return KSubstOption.getDefaultTextMode(); 321 } 322 } 323 324 330 public RemoteFile toRevision(String revision) { 331 RemoteFolder newParent = new RemoteFolder(null, parent.getRepository(), parent.getRepositoryRelativePath(), parent.getTag()); 332 RemoteFile file = new RemoteFile(newParent, getWorkspaceSyncState(), getName(), revision, getKeywordMode(), CVSTag.DEFAULT); 333 newParent.setChildren(new ICVSRemoteResource[] {file}); 334 return file; 335 } 336 337 340 public ResourceSyncInfo getSyncInfo() { 341 try { 342 return new ResourceSyncInfo(syncBytes); 343 } catch (CVSException e) { 344 CVSProviderPlugin.log(e); 345 return null; 346 } 347 } 348 349 352 public String getRemoteLocation(ICVSFolder stopSearching) throws CVSException { 353 return parent.getRemoteLocation(stopSearching) + Session.SERVER_SEPARATOR + getName(); 354 } 355 356 359 public String getRepositoryRelativePath() { 360 String parentPath = parent.getRepositoryRelativePath(); 361 return parentPath + Session.SERVER_SEPARATOR + getName(); 362 } 363 364 367 public ICVSRepositoryLocation getRepository() { 368 return parent.getRepository(); 369 } 370 371 374 public void setSyncInfo(ResourceSyncInfo fileInfo, int modificationState) { 375 setSyncBytes(fileInfo.getBytes(),modificationState); 376 } 377 378 383 public void setRevision(String revision) throws CVSException { 384 syncBytes = ResourceSyncInfo.setRevision(syncBytes, revision); 385 } 386 387 public InputStream getContents() throws CVSException { 388 if (!fetching) { 389 if (isContentsCached()) { 391 try { 392 InputStream cached = getCachedContents(); 393 if (cached != null) { 394 return cached; 395 } 396 } catch (TeamException e) { 397 throw CVSException.wrapException(e); 398 } 399 } 400 } 401 return new ByteArrayInputStream (new byte[0]); 405 } 406 407 protected InputStream getCachedContents() throws TeamException { 408 if (isHandleCached()) { 409 RemoteFile file = (RemoteFile)getCachedHandle(); 410 if (file != null) { 411 byte[] newSyncBytes = file.getSyncBytes(); 412 if (newSyncBytes != null) { 413 syncBytes = newSyncBytes; 415 } 416 } 417 } 418 return super.getCachedContents(); 419 } 420 421 public void setContents(InputStream stream, int responseType, boolean keepLocalHistory, IProgressMonitor monitor) throws CVSException { 422 try { 423 setContents(stream, monitor); 424 } catch (TeamException e) { 425 throw CVSException.wrapException(e); 426 } 427 } 428 429 432 public void setReadOnly(boolean readOnly) { 433 } 435 436 439 public boolean isReadOnly() { 440 return true; 441 } 442 443 446 public Date getTimeStamp() { 447 return getSyncInfo().getTimeStamp(); 448 } 449 450 453 public void setTimeStamp(Date date) { 454 } 456 457 460 public void copyTo(String mFile) { 461 } 463 464 467 public ICVSRemoteResource[] members(IProgressMonitor progress) { 468 return new ICVSRemoteResource[0]; 469 } 470 471 474 public boolean isContainer() { 475 return false; 476 } 477 478 481 public boolean isFolder() { 482 return false; 483 } 484 485 490 public IStatus tag(final CVSTag tag, final LocalOption[] localOptions, IProgressMonitor monitor) throws CVSException { 491 monitor = Policy.monitorFor(monitor); 492 monitor.beginTask(null, 100); 493 Session session = new Session(getRepository(), getParent(), true ); 494 session.open(Policy.subMonitorFor(monitor, 10), true ); 495 try { 496 return Command.RTAG.execute( 497 session, 498 Command.NO_GLOBAL_OPTIONS, 499 localOptions, 500 new CVSTag(getRevision(), CVSTag.VERSION), 501 tag, 502 new ICVSRemoteResource[] { RemoteFile.this }, 503 Policy.subMonitorFor(monitor, 90)); 504 } finally { 505 session.close(); 506 } 507 } 508 509 public boolean equals(Object target) { 510 if (this == target) 511 return true; 512 if (!(target instanceof RemoteFile)) 513 return false; 514 RemoteFile remote = (RemoteFile) target; 515 return super.equals(target) && remote.getRevision().equals(getRevision()); 516 } 517 518 521 public void edit(int notifications, boolean notifyForWritable, IProgressMonitor monitor) { 522 } 524 525 528 public void unedit(IProgressMonitor monitor) { 529 } 531 532 535 public void notificationCompleted() { 536 } 538 539 542 public NotifyInfo getPendingNotification() { 543 return null; 544 } 545 546 549 public ICVSRemoteResource forTag(ICVSRemoteFolder parent, CVSTag tag) { 550 return new RemoteFile((RemoteFolder)parent, getWorkspaceSyncState(), getName(), getRevision(), getKeywordMode(), tag); 551 } 552 553 556 public ICVSRemoteResource forTag(CVSTag tag) { 557 RemoteFolderTree remoteFolder = new RemoteFolderTree(null, getRepository(), 558 ((ICVSRemoteFolder)getParent()).getRepositoryRelativePath(), 559 tag); 560 RemoteFile remoteFile = (RemoteFile)forTag(remoteFolder, tag); 561 remoteFolder.setChildren(new ICVSRemoteResource[] { remoteFile }); 562 return remoteFile; 563 } 564 567 public void checkedIn(String info, boolean commit) { 568 } 570 573 public boolean isEdited() { 574 return false; 575 } 576 579 public byte[] getSyncBytes() { 580 return syncBytes; 581 } 582 585 public void setSyncBytes(byte[] syncBytes, int modificationState) { 586 if (fetching) { 587 RemoteFile file = (RemoteFile)getCachedHandle(); 588 if (file == null) { 589 cacheHandle(); 590 } else if (file != this) { 591 file.setSyncBytes(syncBytes, modificationState); 592 } 593 } 594 this.syncBytes = syncBytes; 595 } 596 597 public String toString() { 598 return super.toString() + " " + getRevision(); } 600 601 604 public String getContentIdentifier() { 605 return getRevision(); 606 } 607 608 612 public void aboutToReceiveContents(byte[] entryLine) { 613 setSyncBytes(entryLine, ICVSFile.CLEAN); 614 fetching = true; 615 } 616 617 620 public void doneReceivingContents() { 621 fetching = false; 622 } 623 624 627 public boolean isContentsCached() { 628 return super.isContentsCached(); 630 } 631 632 640 public void setContents(IFile file, IProgressMonitor monitor) throws TeamException, CoreException { 641 setContents(file.getContents(), monitor); 642 } 643 644 647 public void setExecutable(boolean executable) throws CVSException { 648 this.executable = executable; 650 RemoteFile file = (RemoteFile)getCachedHandle(); 651 if (file != this) { 652 file.setExecutable(executable); 653 } 654 } 655 656 659 public boolean isExecutable() throws CVSException { 660 return executable; 662 } 663 664 public CachedResourceVariant getCachedHandle() { 665 return super.getCachedHandle(); 666 } 667 668 public Object getAdapter(Class adapter) { 669 if (adapter == IFileRevision.class) 670 return new CVSResourceVariantFileRevision(this); 671 return super.getAdapter(adapter); 672 } 673 674 public CVSURI toCVSURI() { 675 ResourceSyncInfo info = getSyncInfo(); 676 return new CVSURI(getRepository(), new Path(getRepositoryRelativePath()), info.getTag(), info.getRevision()); 677 } 678 } 679 | Popular Tags |