public abstract class CachedResourceVariant extends PlatformObject implements IResourceVariant
Overriding subclasses need to provide a cache Id for all their resource variants
and a cache path for each resource variant that uniquely identifies it. In addition,
they must implement fetchContents
to retrieve the contents of the
resource variant and then call setContents
to place these contents in the cache.
Subclasses may also call cacheHandle
in order to place the handle in the
cache so that it can be retrieved later by calling getCachedHandle
on any
resource variant whose cache path is the same as the cached handle. This allows subclasses to
cache additional resource variant properties such as author, comment, etc.
The IStorage
instance returned by this class will be
an IEncodedStorage
.
The cache in which the resource variants reside will occasionally clear cached entries if they have not been accessed for a certain amount of time.
Constructor and Description |
---|
CachedResourceVariant() |
Modifier and Type | Method and Description |
---|---|
protected void |
cacheHandle()
Cache this handle in the cache, replacing any previously cached handle.
|
protected abstract void |
fetchContents(IProgressMonitor monitor)
Method that is invoked when the contents of the resource variant need to
be fetched.
|
protected InputStream |
getCachedContents()
Return the cached contents for this resource variant or
null
if the contents have not been cached. |
protected CachedResourceVariant |
getCachedHandle()
Return the cached handle for this resource variant if there is
one.
|
protected abstract String |
getCacheId()
Return the ID that uniquely identifies the cache in which this resource variant
is to be cache.
|
protected abstract String |
getCachePath()
Get the path that uniquely identifies the remote resource
variant.
|
IPath |
getDisplayPath()
Return the full path of this resource that should be displayed to the
user.
|
long |
getSize()
Return the size (in bytes) of the contents of this resource variant.
|
IStorage |
getStorage(IProgressMonitor monitor)
Return an instance of IStorage or
null if the remote resource
does not have contents (i.e. is a folder). |
boolean |
isContentsCached()
Return whether there are already contents cached for this resource variant.
|
protected boolean |
isHandleCached()
Return
true if the cache contains an entry for this resource
variant. |
protected void |
setContents(InputStream stream,
IProgressMonitor monitor)
This method should be invoked by subclasses from within their
fetchContents
method in order to cache the contents for this resource variant. |
getAdapter
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
asBytes, equals, getContentIdentifier, getName, isContainer
public IStorage getStorage(IProgressMonitor monitor) throws TeamException
IResourceVariant
null
if the remote resource
does not have contents (i.e. is a folder). Since the ISorage#getContents()
method does not accept an IProgressMonitor
, this method must ensure that the contents
access by the resulting IStorage
is cached locally (hence the IProgressMonitor
argument to this method). Implementations of this method should
ensure that the resulting IStorage
is accessing locally cached contents and is not
contacting the server.
The returned storage object may be an instance of (@link org.eclipse.core.resources.IEncodedStorage} in which case clients can determine the character encoding of the contents.
getStorage
in interface IResourceVariant
monitor
- a progress monitorIStorage
that provides access to the contents of
the remote resource or null
if the remote resource is a container.TeamException
protected abstract void fetchContents(IProgressMonitor monitor) throws TeamException
isContainer()
returns false
.
Subclasses should override this method and invoke setContents
with a stream containing the fetched contents.monitor
- a progress monitorTeamException
protected void setContents(InputStream stream, IProgressMonitor monitor) throws TeamException
fetchContents
method in order to cache the contents for this resource variant.
This method is not intended to be overridden by clients.
stream
- the stream containing the contents of the resource variantmonitor
- a progress monitorTeamException
public boolean isContentsCached()
false
even if the contents are currently
being cached by another thread. The consequence of this is that the contents
may be fetched twice in the rare case where two threads request the same contents
concurrently. For containers, this method will always return false
.
This method is not intended to be overridden by clients.
protected InputStream getCachedContents() throws TeamException
null
if the contents have not been cached.
For containers, this method will always return null
.
This method is not intended to be overridden by clients.
null
TeamException
protected boolean isHandleCached()
true
if the cache contains an entry for this resource
variant. It is possible that another instance of this variant is cached.
To get the cached instance, call getCachedHandle()
. Note that
cached contents can be retrieved from any handle to a resource variant whose
cache path (as returned by getCachePath()
) match but other
state information may only be accessible from the cached copy.protected abstract String getCachePath()
IStorage
returned from this variant so the path could be converted to an
IPath
and displayed to the user.public long getSize()
protected abstract String getCacheId()
protected CachedResourceVariant getCachedHandle()
null
is returned.
If there is no cached handle and one is desired, then cacheHandle()
should be called.null
protected void cacheHandle()
getCachePath()
identifies an immutable
resource version (or revision). The ability to replace the handle itself
is provided so that additional state may be cached before or after the contents
are fetched.public IPath getDisplayPath()
IStorage
that
is returned by this instance.
Subclasses may override.
Copyright (c) 2000, 2014 Eclipse Contributors and others. All rights reserved.Guidelines for using Eclipse APIs.