diff options
author | Michael Valenta | 2003-09-10 17:30:37 +0000 |
---|---|---|
committer | Michael Valenta | 2003-09-10 17:30:37 +0000 |
commit | 89e7e21e84a69f35233d9e92f47c4c19f67ef841 (patch) | |
tree | 68e93faf5a281c830ff71eca856f7e0ff91765ba | |
parent | 6371383c39b40e82f6b0f43b43c3f9bd4fbc1e13 (diff) | |
download | eclipse.platform.team-89e7e21e84a69f35233d9e92f47c4c19f67ef841.tar.gz eclipse.platform.team-89e7e21e84a69f35233d9e92f47c4c19f67ef841.tar.xz eclipse.platform.team-89e7e21e84a69f35233d9e92f47c4c19f67ef841.zip |
Generalized RemoteSynchronizers
2 files changed, 223 insertions, 0 deletions
diff --git a/bundles/org.eclipse.team.core/src/org/eclipse/team/core/subscribers/RemoteBytesSynchronizer.java b/bundles/org.eclipse.team.core/src/org/eclipse/team/core/subscribers/RemoteBytesSynchronizer.java new file mode 100644 index 000000000..3a7c1e188 --- /dev/null +++ b/bundles/org.eclipse.team.core/src/org/eclipse/team/core/subscribers/RemoteBytesSynchronizer.java @@ -0,0 +1,160 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.team.core.subscribers; + +import java.util.HashSet; +import java.util.Set; + +import org.eclipse.core.resources.IResource; +import org.eclipse.core.resources.ISynchronizer; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.QualifiedName; +import org.eclipse.team.core.TeamException; +import org.eclipse.team.internal.core.Assert; + +/** + * A remote bytes sychronizer is a remote synchronizer that caches the + * remote sync bytes using the org.eclipse.core.resources.ISynchronizer. + * It also has API that differentiates the case of no existing remote for + * a local resource from that of the remote state never having been queried + * for that local resource. + */ +public abstract class RemoteBytesSynchronizer extends RemoteSynchronizer { + + private static final byte[] NO_REMOTE = new byte[0]; + + protected QualifiedName syncName; + protected Set changedResources = new HashSet(); + + public RemoteBytesSynchronizer(QualifiedName name) { + syncName = name; + getSynchronizer().add(syncName); + } + + /** + * Dispose of any cached sync bytes. + */ + public void dispose() { + getSynchronizer().remove(getSyncName()); + } + + protected ISynchronizer getSynchronizer() { + return ResourcesPlugin.getWorkspace().getSynchronizer(); + } + + protected QualifiedName getSyncName() { + return syncName; + } + + /** + * Return the remote sync bytes cached for the given local resource. + * A return value of <code>null</code> can mean either that the + * remote has never been fetched or that it doesn't exist. The method + * <code>isRemoteKnown(IResource)</code> should be used to differentiate + * these two cases. + */ + public byte[] getSyncBytes(IResource resource) throws TeamException { + byte[] syncBytes = internalGetSyncBytes(resource); + if (syncBytes != null && equals(syncBytes, NO_REMOTE)) { + // If it is known that there is no remote, return null + return null; + } + return syncBytes; + } + + private byte[] internalGetSyncBytes(IResource resource) throws TeamException { + try { + return getSynchronizer().getSyncInfo(getSyncName(), resource); + } catch (CoreException e) { + throw TeamException.asTeamException(e); + } + } + + /** + * Set the remote sync bytes for the given resource. The bytes should never be + * <code>null</code>. If it is known that the remote does not exist, + * <code>setRemoteDoesNotExist(IResource)</code> should be invoked. If the sync + * bytes for the remote are stale and should be removed, <code>removeSyncBytes()</code> + * should be called. + * @param resource + * @param bytes + * @throws TeamException + */ + public void setSyncBytes(IResource resource, byte[] bytes) throws TeamException { + Assert.isNotNull(bytes); + byte[] oldBytes = internalGetSyncBytes(resource); + if (oldBytes != null && equals(oldBytes, bytes)) return; + try { + getSynchronizer().setSyncInfo(getSyncName(), resource, bytes); + } catch (CoreException e) { + throw TeamException.asTeamException(e); + } + changedResources.add(resource); + } + + /** + * Remove the remote bytes cached for the given local resource. After this + * operation <code>isRemoteKnown(resource)</code> will return <code>false</code> + * and <code>getSyncBytes(resource)</code> will return <code>null</code> for the + * resource (and potentially it's children depending on the value of the depth parameter. + */ + public void removeSyncBytes(IResource resource, int depth, boolean silent) throws TeamException { + if (resource.exists() || resource.isPhantom()) { + try { + getSynchronizer().flushSyncInfo(getSyncName(), resource, depth); + } catch (CoreException e) { + throw TeamException.asTeamException(e); + } + if(silent == false) { + changedResources.add(resource); + } + } + } + + /** + * Return true if the remote resources associated with the given local + * resource has been fetched. This method is useful for those cases when + * there are no sync bytes for a remote resource and the client wants to + * know if this means that the remote does exist (i.e. this method returns + * <code>true</code>) or the remote has not been fetched (i.e. this method returns + * <code>false</code>). + */ + public boolean isRemoteKnown(IResource resource) throws TeamException { + return internalGetSyncBytes(resource) != null; + } + + /** + * This method should be invoked by a client to indicate that it is known that + * there is no remote resource associated with the local resource. After this method + * is invoked, <code>isRemoteKnown(resource)</code> will return <code>true</code> and + * <code>getSyncBytes(resource)</code> will return <code>null</code>. + */ + protected void setRemoteDoesNotExist(IResource resource) throws TeamException { + setSyncBytes(resource, NO_REMOTE); + } + + private boolean equals(byte[] syncBytes, byte[] oldBytes) { + if (syncBytes.length != oldBytes.length) return false; + for (int i = 0; i < oldBytes.length; i++) { + if (oldBytes[i] != syncBytes[i]) return false; + } + return true; + } + + /* (non-Javadoc) + * @see org.eclipse.team.core.subscribers.RemoteSynchronizer#hasRemote(org.eclipse.core.resources.IResource) + */ + public boolean hasRemote(IResource resource) throws TeamException { + return getSyncBytes(resource) != null; + } + +}
\ No newline at end of file diff --git a/bundles/org.eclipse.team.core/src/org/eclipse/team/core/subscribers/RemoteSynchronizer.java b/bundles/org.eclipse.team.core/src/org/eclipse/team/core/subscribers/RemoteSynchronizer.java new file mode 100644 index 000000000..aaa98c711 --- /dev/null +++ b/bundles/org.eclipse.team.core/src/org/eclipse/team/core/subscribers/RemoteSynchronizer.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2000, 2003 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.team.core.subscribers; + +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.team.core.TeamException; +import org.eclipse.team.core.sync.IRemoteResource; + +/** + * A remote synchronizer provides API to access a handle for a resource resource + * associated with a local resource (that may or may not exist locally). + * API is also provided to trigger a refresh in order to cache the remote state + * for later retrieval. + */ +public abstract class RemoteSynchronizer { + + /** + * Return a remote resource handle created from the remote sync bytes associated + * with the local resource for this synchronizer. + * + * @param resource the local resource + * @return the IRemoteResource handle for a remote resource + * @throws TeamException + */ + public abstract IRemoteResource getRemoteResource(IResource resource) throws TeamException; + + /** + * Return whether the given resource has a corresponding remote resource that + * is known to exist (at the last point in time that a refresh was performed). + * + * @param resource the local resource handle + * @return <code>true</code> if a corrrespondin remote resource is know to exist + * @throws TeamException + */ + public abstract boolean hasRemote(IResource resource) throws TeamException; + + /** + * Refreshes the contents of the resource synchronizer and returns the list + * of resources whose remote synchronization state changed since the last refresh. + * The <code>cacheFileContentsHint</code> indicates that the user of this synchronizer + * will be using the file contents. Subclasses can decide + * whether to cache file contents during the refresh or to + * allow them to be fetched when request. + * + * @param resources the resources to refresh + * @param depth the depth of the operation + * @param cacheFileContentsHint a hint which indicates whether file contents will be used + * @param monitor the progress monitor + * @return the resources whose remote has changed since the last refresh + * @throws TeamException + */ + public abstract IResource[] refresh(IResource[] resources, int depth, boolean cacheFileContentsHint, IProgressMonitor monitor) throws TeamException; + +} |