public class ResourceTracker
extends java.lang.Object
The resource tracker can be configured to prefetch resources, which are downloaded in the order added to the media tracker.
Multiple threads are used to download and cache resources that are actively being waited for (blocking a caller) or those that have been started downloading by calling the startDownload method. Resources that are prefetched are downloaded one at a time and only if no other trackers have requested downloads. This allows the tracker to start downloading many items without using many system resources, but still quickly download items as needed.
Modifier and Type | Class and Description |
---|---|
static class |
ResourceTracker.RequestMethods |
Constructor and Description |
---|
ResourceTracker()
Creates a resource tracker that does not prefetch resources.
|
ResourceTracker(boolean prefetch)
Creates a resource tracker.
|
Modifier and Type | Method and Description |
---|---|
void |
addDownloadListener(DownloadListener listener)
Adds the listener to the list of objects interested in
receivind DownloadEvents.
|
void |
addResource(java.net.URL location,
Version version,
DownloadOptions options,
UpdatePolicy updatePolicy)
Add a resource identified by the specified location and
version.
|
boolean |
checkResource(java.net.URL location)
Returns whether a resource is available for use (ie, can be
accessed with the getCacheFile method).
|
protected void |
fireDownloadEvent(Resource resource)
Fires the download event corresponding to the resource's
state.
|
long |
getAmountRead(java.net.URL location)
Returns the number of bytes downloaded for a resource.
|
java.io.File |
getCacheFile(java.net.URL location)
Returns a file containing the downloaded resource.
|
java.net.URL |
getCacheURL(java.net.URL location)
Returns a URL pointing to the cached location of the
resource, or the resource itself if it is a non-cacheable
resource.
|
long |
getTotalSize(java.net.URL location)
Returns the number of total size in bytes of a resource, or
-1 it the size is not known.
|
void |
removeDownloadListener(DownloadListener listener)
Removes a download listener.
|
void |
removeResource(java.net.URL location)
Removes a resource from the tracker.
|
protected void |
startDownloadThread(Resource resource)
Start a new download thread.
|
boolean |
startResource(java.net.URL location)
Starts loading the resource if it is not already being
downloaded or already cached.
|
boolean |
waitForResource(java.net.URL location,
long timeout)
Wait for a particular resource to be downloaded and made
available.
|
boolean |
waitForResources(java.net.URL[] urls,
long timeout)
Wait for a group of resources to be downloaded and made
available locally.
|
public ResourceTracker()
public ResourceTracker(boolean prefetch)
prefetch
- whether to download resources before requested.public void addResource(java.net.URL location, Version version, DownloadOptions options, UpdatePolicy updatePolicy)
location
- the location of the resourceversion
- the resource versionoptions
- options to control downloadupdatePolicy
- whether to check for updates if already in cachepublic void removeResource(java.net.URL location)
location
- location of resource to be removedIllegalResourceDescriptorException
- if the resource is not being trackedpublic void addDownloadListener(DownloadListener listener)
listener
- the listener to add.public void removeDownloadListener(DownloadListener listener)
listener
- the listener to remove.protected void fireDownloadEvent(Resource resource)
resource
- resource on which event is firedpublic java.net.URL getCacheURL(java.net.URL location)
If the resource has not downloaded yet, the method will block until it has been transferred to the cache.
location
- the resource locationIllegalResourceDescriptorException
- if the resource is not being trackedCacheUtil.isCacheable(java.net.URL, net.sourceforge.jnlp.Version)
public java.io.File getCacheFile(java.net.URL location)
If the resource has not downloaded yet, the method will block until it has been transferred to the cache.
location
- the resource locationIllegalResourceDescriptorException
- if the resource is not being trackedCacheUtil.isCacheable(java.net.URL, net.sourceforge.jnlp.Version)
public boolean waitForResources(java.net.URL[] urls, long timeout) throws java.lang.InterruptedException
urls
- the resources to wait fortimeout
- the time in ms to wait before returning, 0 for no timeoutjava.lang.InterruptedException
- if thread is interruptedIllegalResourceDescriptorException
- if the resource is not being trackedpublic boolean waitForResource(java.net.URL location, long timeout) throws java.lang.InterruptedException
location
- the resource to wait fortimeout
- the timeout, or 0 to wait until completedjava.lang.InterruptedException
- if another thread interrupted the waitIllegalResourceDescriptorException
- if the resource is not being trackedpublic long getAmountRead(java.net.URL location)
location
- the resource locationIllegalResourceDescriptorException
- if the resource is not being trackedpublic boolean checkResource(java.net.URL location)
location
- the resource locationIllegalResourceDescriptorException
- if the resource is not being trackedpublic boolean startResource(java.net.URL location)
location
- the resource locationIllegalResourceDescriptorException
- if the resource is not being trackedpublic long getTotalSize(java.net.URL location)
location
- the resource locationIllegalResourceDescriptorException
- if the resource is not being trackedprotected void startDownloadThread(Resource resource)
Calls to this method should be synchronized on lock.
resource
- resource to be download