01: package com.xoetrope.deploy;
02:
03: import java.net.URL;
04:
05: import javax.jnlp.DownloadService;
06: import javax.jnlp.DownloadServiceListener;
07: import javax.jnlp.ServiceManager;
08: import javax.jnlp.UnavailableServiceException;
09:
10: /**
11: * A dynamic loader of resources via the JNLP API.
12: * <p> Copyright (c) Xoetrope Ltd., 2001-2007, This software is licensed under
13: * the GNU Public License (GPL), please see license.txt for more details. If
14: * you make commercial use of this software you must purchase a commercial
15: * license from Xoetrope.</p>
16: */
17: public class XJnlpResourceLoader {
18: private DownloadService downloadService;
19: private URL baseUrl;
20:
21: /**
22: * Creates a new instance of XJnlpResourceLoader
23: * @param base the basic URL to which resoruce names wil be appended
24: */
25: public XJnlpResourceLoader(URL base) {
26: baseUrl = base;
27: try {
28: downloadService = (DownloadService) ServiceManager
29: .lookup("javax.jnlp.DownloadService");
30: } catch (UnavailableServiceException e) {
31: downloadService = null;
32: }
33: }
34:
35: /**
36: * Set the base URL for the download service.
37: * @param base the basic URL to which resoruce names wil be appended
38: */
39: public void setBaseUrl(URL base) {
40: baseUrl = base;
41: }
42:
43: /**
44: * Load the specified resource, blocking till the download is complete
45: * @param res the resource name. The name will be appended to the base url to
46: * get the complete download URL.
47: * @param version the version to download or null if the version is not used
48: * @return true if the download completes successfully.
49: */
50: public boolean loadResource(String res, String version) {
51: return loadResource(res, version, false);
52: }
53:
54: /**
55: * Load the specified resource, blocking till the download is complete
56: * @param res the resource name. The name will be appended to the base url to
57: * get the complete download URL.
58: * @param version the version to download or null if the version is not used
59: * @param clear clear the cache before downloading. If this is true then the
60: * resource will always be downloded, otherwise the cachced resource will be
61: * returned if it is present.
62: * @return true if the download completes successfully.
63: */
64: public boolean loadResource(String res, String version,
65: boolean clear) {
66: if (downloadService != null) {
67: try {
68: // Determine if a particular resource is cached
69: URL url = new URL(baseUrl, res);
70: boolean cached = downloadService.isResourceCached(url,
71: version);
72: if (cached) {
73: if (clear)
74: downloadService.removeResource(url, version);
75: else
76: return true;
77: }
78:
79: // Load the resource into the cache
80: DownloadServiceListener dsl = downloadService
81: .getDefaultProgressWindow();
82: downloadService.loadResource(url, version, dsl);
83: return true;
84: } catch (Exception e) {
85: e.printStackTrace();
86: }
87: }
88:
89: return false;
90: }
91: }
|