diff options
author | Omair Majid <[email protected]> | 2011-02-10 17:25:18 -0500 |
---|---|---|
committer | Omair Majid <[email protected]> | 2011-02-10 17:25:18 -0500 |
commit | bd77d9b6fd61b02fda0e0c3bba478caf7dfe8258 (patch) | |
tree | e46d6648f8b07edbeef40f95c2f6aec0bb45a217 /netx/net/sourceforge/jnlp/cache/ResourceTracker.java | |
parent | d0496a267170fb065b28352281a7b70a66e94e2f (diff) |
Fix RH669942: Add support for packEnabled and versionEnabled
This changeset adds support for explicitly using jars with pack200 compression
and versioning. This requires no server side support, but expects the client
to try and find the right jar and fallback to using uncompressed/unversioned
jars if the versioned/compressed ones can not be found.
2011-02-10 Omair Majid <[email protected]>
Fix RH669942; Add support for packEnabled and versionEnabled.
* NEWS: Update with bugfix.
* netx/net/sourceforge/jnlp/DownloadOptions.java: New file.
* netx/net/sourceforge/jnlp/JNLPFile.java
(openURL): Use null for DownloadOptions.
(getResourceDescs): New method.
(getResourceDescs(Locale,String,String)): New method.
* netx/net/sourceforge/jnlp/Launcher.java
(launchApplication): Add image to downloader with null DownloadOptions.
* netx/net/sourceforge/jnlp/cache/CacheUtil.java
(getCachedResource): Add resource with null DownloadOptions.
* netx/net/sourceforge/jnlp/cache/Resource.java: Add new field
downloadLocation.
(Resource): Initialize downloadLocation.
(getDownloadLocation): New method.
(setDownloadLocation): New method.
* netx/net/sourceforge/jnlp/cache/ResourceTracker.java: Add new field
downloadOptions.
(addResource(URL,Version,UpdatePolicy)): Renamed to...
(addResource(URL,Version,DownloadOptions,UpdatePolicy)): New method.
(downloadResource): Add support for explicit downloading of packed jars as
well as content-encoded packed jars.
(initializeResource): Invokde findBestUrl to find the best url. Set that
as the download location for the resource.
(getVersionedResourceURL): Remove.
(findBestUrl): New method. Use ResourceUrlCreator to get a list of all
possible urls that can be used to download this resource. Try them one by
one until one works and return that.
* netx/net/sourceforge/jnlp/cache/ResourceUrlCreator.java: New file.
* netx/net/sourceforge/jnlp/runtime/JNLPClassLoader.java
(initializeResources): Add resource with appropriate download options.
(activateJars): Likewise.
(loadClass): Likewise.
(getDownloadOptionsForJar): New method.
Diffstat (limited to 'netx/net/sourceforge/jnlp/cache/ResourceTracker.java')
-rw-r--r-- | netx/net/sourceforge/jnlp/cache/ResourceTracker.java | 146 |
1 files changed, 90 insertions, 56 deletions
diff --git a/netx/net/sourceforge/jnlp/cache/ResourceTracker.java b/netx/net/sourceforge/jnlp/cache/ResourceTracker.java index 8db4a4b..93e763e 100644 --- a/netx/net/sourceforge/jnlp/cache/ResourceTracker.java +++ b/netx/net/sourceforge/jnlp/cache/ResourceTracker.java @@ -29,12 +29,15 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.jar.JarOutputStream; import java.util.jar.Pack200; import java.util.jar.Pack200.Unpacker; import java.util.zip.GZIPInputStream; +import net.sourceforge.jnlp.DownloadOptions; import net.sourceforge.jnlp.Version; import net.sourceforge.jnlp.event.DownloadEvent; import net.sourceforge.jnlp.event.DownloadListener; @@ -117,6 +120,9 @@ public class ResourceTracker { /** resources requested to be downloaded */ private static ArrayList<Resource> queue = new ArrayList<Resource>(); + private static Map<Resource, DownloadOptions> downloadOptions = + new HashMap<Resource, DownloadOptions>(); + /** resource trackers threads are working for (used for load balancing across multi-tracker downloads) */ private static ArrayList<ResourceTracker> active = new ArrayList<ResourceTracker>(); // @@ -163,7 +169,7 @@ public class ResourceTracker { * @param version the resource version * @param updatePolicy whether to check for updates if already in cache */ - public void addResource(URL location, Version version, UpdatePolicy updatePolicy) { + public void addResource(URL location, Version version, DownloadOptions options, UpdatePolicy updatePolicy) { if (location == null) throw new IllegalArgumentException("location==null"); @@ -177,6 +183,11 @@ public class ResourceTracker { resources.add(resource); } + if (options == null) { + options = new DownloadOptions(false, false); + } + downloadOptions.put(resource, options); + // checkCache may take a while (loads properties file). this // should really be synchronized on resources, but the worst // case should be that the resource will be updated once even @@ -621,7 +632,8 @@ public class ResourceTracker { try { // create out second in case in does not exist - URLConnection con = getVersionedResourceURL(resource).openConnection(); + URL realLocation = resource.getDownloadLocation(); + URLConnection con = realLocation.openConnection(); con.addRequestProperty("Accept-Encoding", "pack200-gzip, gzip"); con.connect(); @@ -636,16 +648,16 @@ public class ResourceTracker { String contentEncoding = con.getContentEncoding(); if (JNLPRuntime.isDebug()) { - System.err.println("Content encoding for " + resource.location + ": " - + contentEncoding); + System.err.println("Downloading" + resource.location + " using " + + realLocation + " (encoding : " + contentEncoding + ")"); + } - if (contentEncoding != null) { - if (contentEncoding.equals("gzip")) { - downloadLocation = new URL(downloadLocation.toString() + ".gz"); - } else if (contentEncoding.equals("pack200-gzip")) { - downloadLocation = new URL(downloadLocation.toString() + ".pack.gz"); - } + if ("gzip".equals(contentEncoding)) { + downloadLocation = new URL(downloadLocation.toString() + ".gz"); + } else if ("pack200-gzip".equals(contentEncoding) || + realLocation.getPath().endsWith(".pack.gz")) { + downloadLocation = new URL(downloadLocation.toString() + ".pack.gz"); } InputStream in = new BufferedInputStream(con.getInputStream()); @@ -668,41 +680,42 @@ public class ResourceTracker { /* * If the file was compressed, uncompress it. */ - if (contentEncoding != null) { - if (contentEncoding.equals("gzip")) { - GZIPInputStream gzInputStream = new GZIPInputStream(new FileInputStream(CacheUtil - .getCacheFile(downloadLocation, resource.downloadVersion))); - InputStream inputStream = new BufferedInputStream(gzInputStream); - - BufferedOutputStream outputStream = new BufferedOutputStream( - new FileOutputStream(CacheUtil.getCacheFile(resource.location, - resource.downloadVersion))); - - while (-1 != (rlen = inputStream.read(buf))) { - outputStream.write(buf, 0, rlen); - } - outputStream.close(); - inputStream.close(); - gzInputStream.close(); + if ("gzip".equals(contentEncoding)) { + GZIPInputStream gzInputStream = new GZIPInputStream(new FileInputStream(CacheUtil + .getCacheFile(downloadLocation, resource.downloadVersion))); + InputStream inputStream = new BufferedInputStream(gzInputStream); - } else if (contentEncoding.equals("pack200-gzip")) { - GZIPInputStream gzInputStream = new GZIPInputStream(new FileInputStream( - CacheUtil.getCacheFile(downloadLocation, resource.downloadVersion))); - InputStream inputStream = new BufferedInputStream(gzInputStream); + BufferedOutputStream outputStream = new BufferedOutputStream( + new FileOutputStream(CacheUtil.getCacheFile(resource.location, + resource.downloadVersion))); - JarOutputStream outputStream = new JarOutputStream(new FileOutputStream( - CacheUtil.getCacheFile(resource.location, resource.downloadVersion))); + while (-1 != (rlen = inputStream.read(buf))) { + outputStream.write(buf, 0, rlen); + } - Unpacker unpacker = Pack200.newUnpacker(); - unpacker.unpack(inputStream, outputStream); + outputStream.close(); + inputStream.close(); + gzInputStream.close(); - outputStream.close(); - inputStream.close(); - gzInputStream.close(); - } + } else if ("pack200-gzip".equals(contentEncoding) || + realLocation.getPath().endsWith(".pack.gz")) { + GZIPInputStream gzInputStream = new GZIPInputStream(new FileInputStream( + CacheUtil.getCacheFile(downloadLocation, resource.downloadVersion))); + InputStream inputStream = new BufferedInputStream(gzInputStream); + + JarOutputStream outputStream = new JarOutputStream(new FileOutputStream( + CacheUtil.getCacheFile(resource.location, resource.downloadVersion))); + + Unpacker unpacker = Pack200.newUnpacker(); + unpacker.unpack(inputStream, outputStream); + + outputStream.close(); + inputStream.close(); + gzInputStream.close(); } + resource.changeStatus(DOWNLOADING, DOWNLOADED); synchronized (lock) { lock.notifyAll(); // wake up wait's to check for completion @@ -731,7 +744,9 @@ public class ResourceTracker { File localFile = CacheUtil.getCacheFile(resource.location, resource.downloadVersion); // connect - URLConnection connection = getVersionedResourceURL(resource).openConnection(); // this won't change so should be okay unsynchronized + URL finalLocation = findBestUrl(resource); + resource.setDownloadLocation(finalLocation); + URLConnection connection = finalLocation.openConnection(); // this won't change so should be okay unsynchronized connection.addRequestProperty("Accept-Encoding", "pack200-gzip, gzip"); int size = connection.getContentLength(); @@ -777,27 +792,46 @@ public class ResourceTracker { } /** - * Returns the versioned url for a resource - * @param resource the resource to get the url for + * Returns the best URL to use for downloading the resource + * + * @param resource the resource + * @return a URL or null */ - private URL getVersionedResourceURL(Resource resource) { - String actualLocation = resource.location.getProtocol() + "://" - + resource.location.getHost(); - if (resource.location.getPort() != -1) { - actualLocation += ":" + resource.location.getPort(); + private URL findBestUrl(Resource resource) { + DownloadOptions options = downloadOptions.get(resource); + if (options == null) { + options = new DownloadOptions(false, false); } - actualLocation += resource.location.getPath(); - if (resource.requestVersion != null - && resource.requestVersion.isVersionId()) { - actualLocation += "?version-id=" + resource.requestVersion; + + List<URL> urls = new ResourceUrlCreator(resource, options).getUrls(); + if (JNLPRuntime.isDebug()) { + System.err.println("All possible urls for " + + resource.toString() + " : " + urls); } - URL versionedURL; - try { - versionedURL = new URL(actualLocation); - } catch (MalformedURLException e) { - return resource.location; + URL bestUrl = null; + for (int i = 0; i < urls.size(); i++) { + URL url = urls.get(i); + try { + URLConnection connection = url.openConnection(); + connection.addRequestProperty("Accept-Encoding", "pack200-gzip, gzip"); + if (connection instanceof HttpURLConnection) { + HttpURLConnection con = (HttpURLConnection)connection; + int responseCode = con.getResponseCode(); + if (responseCode == -1 || responseCode < 200 || responseCode >= 300) { + continue; + } + } + if (JNLPRuntime.isDebug()) { + System.err.println("best url for " + resource.toString() + " is " + url.toString()); + } + bestUrl = url; + break; + } catch (IOException e) { + // continue + } } - return versionedURL; + + return bestUrl; } /** |