KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > update > XJUpdateManager


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.xjlib.appkit.update;
33
34 import org.antlr.xjlib.appkit.app.XJApplication;
35 import org.antlr.xjlib.appkit.frame.XJDialog;
36 import org.antlr.xjlib.appkit.utils.XJAlert;
37 import org.antlr.xjlib.appkit.utils.XJDialogProgress;
38 import org.antlr.xjlib.appkit.utils.XJDialogProgressDelegate;
39 import org.antlr.xjlib.foundation.XJUtils;
40
41 import java.awt.*;
42 import java.beans.XMLDecoder JavaDoc;
43 import java.beans.XMLEncoder JavaDoc;
44 import java.io.*;
45 import java.net.MalformedURLException JavaDoc;
46 import java.net.URL JavaDoc;
47 import java.util.HashMap JavaDoc;
48 import java.util.Map JavaDoc;
49
50 public class XJUpdateManager {
51
52     public static final String JavaDoc KEY_VERSION = "KEY_VERSION";
53     public static final String JavaDoc KEY_APP_NAME = "KEY_APP_NAME";
54     public static final String JavaDoc KEY_DESCRIPTION = "KEY_DESCRIPTION";
55     public static final String JavaDoc KEY_DOWNLOAD_FILE_URL = "KEY_DOWNLOAD_FILE_URL";
56     public static final String JavaDoc KEY_DOWNLOAD_FILE_NAME = "KEY_DOWNLOAD_FILE_NAME";
57     public static final String JavaDoc KEY_DOWNLOAD_SIZE = "KEY_DOWNLOAD_SIZE";
58
59     protected Container parent;
60     protected XJUpdateManagerDelegate delegate;
61     protected Map JavaDoc updateInfoMap;
62
63     protected boolean cancelDownload = false;
64
65     public XJUpdateManager(Container parent, XJUpdateManagerDelegate delegate) {
66         this.parent = parent == null?XJApplication.getActiveContainer():parent;
67         this.delegate = delegate==null?new DefaultDownloadUpdateDelegate():delegate;
68     }
69
70     public Container getParentContainer() {
71         return parent;
72     }
73
74     public String JavaDoc getApplicationName() {
75         if(updateInfoMap == null)
76             return null;
77         else
78             return (String JavaDoc)updateInfoMap.get(KEY_APP_NAME);
79     }
80
81     public String JavaDoc getDownloadVersion() {
82         if(updateInfoMap == null)
83             return null;
84         else
85             return (String JavaDoc)updateInfoMap.get(KEY_VERSION);
86     }
87
88     public String JavaDoc getDownloadFileName() {
89         if(updateInfoMap == null)
90             return null;
91         else
92             return (String JavaDoc)updateInfoMap.get(KEY_DOWNLOAD_FILE_NAME);
93     }
94
95     public String JavaDoc getDownloadFileURL() {
96         if(updateInfoMap == null)
97             return null;
98
99         return (String JavaDoc)updateInfoMap.get(KEY_DOWNLOAD_FILE_URL);
100     }
101
102     public long getDownloadSize() {
103         if(updateInfoMap == null)
104             return 0;
105         else
106             return ((Long JavaDoc)updateInfoMap.get(KEY_DOWNLOAD_SIZE)).longValue();
107     }
108
109     public String JavaDoc getDescription() {
110         if(updateInfoMap == null)
111             return null;
112         else
113             return (String JavaDoc)updateInfoMap.get(KEY_DESCRIPTION);
114     }
115
116     public boolean writeUpdateXMLFile(String JavaDoc version, String JavaDoc appName, String JavaDoc description,
117                                       String JavaDoc downloadFileName, String JavaDoc downloadFileURL,
118                                       long downloadFileSize, String JavaDoc outputFile) {
119         XMLEncoder JavaDoc encoder;
120         try {
121             encoder = new XMLEncoder JavaDoc(new BufferedOutputStream(new FileOutputStream(outputFile)));
122         } catch (FileNotFoundException e) {
123             XJAlert.display(getParentContainer(), "Update Manager", "Cannot write the update xml file because:\n"+e);
124             return false;
125         }
126
127         Map JavaDoc<String JavaDoc,Serializable> update = new HashMap JavaDoc<String JavaDoc, Serializable>();
128         update.put(KEY_VERSION, version);
129         update.put(KEY_APP_NAME, appName);
130         update.put(KEY_DESCRIPTION, description);
131         update.put(KEY_DOWNLOAD_FILE_NAME, downloadFileName);
132         update.put(KEY_DOWNLOAD_FILE_URL, downloadFileURL);
133         update.put(KEY_DOWNLOAD_SIZE, new Long JavaDoc(downloadFileSize));
134
135         encoder.writeObject(update);
136         encoder.close();
137
138         return true;
139     }
140
141     public void downloadUpdateToLocalDisk(String JavaDoc localFile) {
142         downloadUpdateToLocalDisk(getDownloadFileURL(), localFile);
143     }
144
145     public void downloadUpdateToLocalDisk(String JavaDoc urlString, String JavaDoc localFile) {
146         new File(localFile).delete();
147
148         BufferedOutputStream localOutputStream;
149         try {
150             localOutputStream = new BufferedOutputStream(new FileOutputStream(localFile));
151         } catch (FileNotFoundException e) {
152             XJAlert.display(getParentContainer(), "Update Manager", "Cannot download the update because:\n"+e);
153             return;
154         }
155
156         URL JavaDoc url;
157         try {
158             url = new URL JavaDoc(urlString);
159         } catch (MalformedURLException JavaDoc e) {
160             XJAlert.display(getParentContainer(), "Update Manager", "Cannot download the update because:\n"+e);
161             return;
162         }
163
164         InputStream is;
165
166         try {
167             is = url.openStream();
168         } catch (IOException e) {
169             XJAlert.display(getParentContainer(), "Update Manager", "Cannot download the update because:\n"+e);
170             return;
171         }
172
173         delegate.umDownloadBegin();
174
175         new Thread JavaDoc(new BackgroundDownloader(is, localOutputStream, localFile)).start();
176     }
177
178     public void fetchRemoteUpdateInformation(String JavaDoc urlString, boolean silent) {
179         updateInfoMap = null;
180
181         URL JavaDoc url;
182         try {
183             url = new URL JavaDoc(urlString);
184         } catch (MalformedURLException JavaDoc e) {
185             if(!silent)
186                 XJAlert.display(getParentContainer(), "Update Manager", "Cannot check the update because:\n"+e);
187             return;
188         }
189
190         InputStream is;
191
192         try {
193             is = url.openStream();
194         } catch (IOException e) {
195             if(!silent)
196                 XJAlert.display(getParentContainer(), "Update Manager", "Cannot check the update because:\n"+e);
197             return;
198         }
199
200         XMLDecoder JavaDoc decoder = new XMLDecoder JavaDoc(new BufferedInputStream(is));
201         try {
202             updateInfoMap = (Map JavaDoc)decoder.readObject();
203         } catch(Exception JavaDoc e) {
204             XJAlert.display(getParentContainer(), "Update Manager", "Cannot check the update because:\n"+e);
205             return;
206         }
207         decoder.close();
208     }
209
210     public boolean isUpdateAvailable(String JavaDoc version) {
211         if(updateInfoMap == null)
212             return false;
213
214         return XJUtils.isVersionGreaterThan(getDownloadVersion(), version);
215     }
216
217     public void cancelDownload() {
218         setCancelDownload(true);
219     }
220
221     public synchronized void setCancelDownload(boolean flag) {
222         this.cancelDownload = flag;
223     }
224
225     public boolean isCancelDownload() {
226         return cancelDownload;
227     }
228
229     public void checkForUpdates(String JavaDoc version, String JavaDoc remoteUpdateFile, String JavaDoc localDownloadPath, boolean silent) {
230         fetchRemoteUpdateInformation(remoteUpdateFile, silent);
231         if(isUpdateAvailable(version)) {
232             if(new XJUpdateManagerDialogUpdateAvailable(this).runModal() == XJDialog.BUTTON_OK) {
233                 downloadUpdateToLocalDisk(XJUtils.concatPath(localDownloadPath, getDownloadFileName()));
234             }
235         } else if(updateInfoMap != null) {
236             if(!silent)
237                 XJAlert.display(getParentContainer(), "Check for Updates", "You already have the latest version.\nCheck again later.");
238         }
239     }
240
241     protected class DefaultDownloadUpdateDelegate extends XJUpdateManagerDelegate implements XJDialogProgressDelegate {
242
243         protected XJDialogProgress progress;
244
245         public void umDownloadBegin() {
246             progress = new XJDialogProgress(parent);
247             progress.setInfo("Downloading...");
248             progress.setProgress(0);
249             progress.setProgressMax(100);
250             progress.setDelegate(this);
251
252             progress.display();
253         }
254
255         public void umDownloadProgress(long current, long total) {
256             progress.setProgress((float)current/total*100);
257         }
258
259         public void umDownloadCancelled() {
260             progress.close();
261             XJAlert.display(getParentContainer(), "Check for Updates", "The download has been cancelled.");
262         }
263
264         public void umDownloadCompleted(String JavaDoc localDownloadFile) {
265             progress.close();
266             XJAlert.display(getParentContainer(), "Check for Updates", "The new version has been downloaded and is available here:\n"+localDownloadFile);
267         }
268
269         public void dialogDidCancel() {
270             cancelDownload();
271         }
272     }
273
274     protected class BackgroundDownloader implements Runnable JavaDoc {
275
276         protected InputStream is;
277         protected OutputStream os;
278         protected String JavaDoc localDownloadFile;
279
280         public BackgroundDownloader(InputStream is, OutputStream os, String JavaDoc localDownloadFile) {
281             this.is = is;
282             this.os = os;
283             this.localDownloadFile = localDownloadFile;
284         }
285
286         public void run() {
287             byte[] b = new byte[1024*8];
288             int r;
289             long current = 0;
290             long total = getDownloadSize();
291             try {
292                 while((r = is.read(b)) > 0 && !isCancelDownload()) {
293                     current += r;
294                     delegate.umDownloadProgress(current, total);
295                     os.write(b, 0, r);
296                 }
297             } catch (IOException e) {
298                 e.printStackTrace();
299             } finally {
300                 try {
301                     is.close();
302                 } catch (IOException e) {
303                     // ignore
304
}
305                 try {
306                     os.close();
307                 } catch (IOException e) {
308                     // ignore
309
}
310
311                 if(isCancelDownload())
312                     delegate.umDownloadCancelled();
313                 else
314                     delegate.umDownloadCompleted(localDownloadFile);
315             }
316         }
317     }
318 }
319
Popular Tags