1 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 ; 43 import java.beans.XMLEncoder ; 44 import java.io.*; 45 import java.net.MalformedURLException ; 46 import java.net.URL ; 47 import java.util.HashMap ; 48 import java.util.Map ; 49 50 public class XJUpdateManager { 51 52 public static final String KEY_VERSION = "KEY_VERSION"; 53 public static final String KEY_APP_NAME = "KEY_APP_NAME"; 54 public static final String KEY_DESCRIPTION = "KEY_DESCRIPTION"; 55 public static final String KEY_DOWNLOAD_FILE_URL = "KEY_DOWNLOAD_FILE_URL"; 56 public static final String KEY_DOWNLOAD_FILE_NAME = "KEY_DOWNLOAD_FILE_NAME"; 57 public static final String KEY_DOWNLOAD_SIZE = "KEY_DOWNLOAD_SIZE"; 58 59 protected Container parent; 60 protected XJUpdateManagerDelegate delegate; 61 protected Map 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 getApplicationName() { 75 if(updateInfoMap == null) 76 return null; 77 else 78 return (String )updateInfoMap.get(KEY_APP_NAME); 79 } 80 81 public String getDownloadVersion() { 82 if(updateInfoMap == null) 83 return null; 84 else 85 return (String )updateInfoMap.get(KEY_VERSION); 86 } 87 88 public String getDownloadFileName() { 89 if(updateInfoMap == null) 90 return null; 91 else 92 return (String )updateInfoMap.get(KEY_DOWNLOAD_FILE_NAME); 93 } 94 95 public String getDownloadFileURL() { 96 if(updateInfoMap == null) 97 return null; 98 99 return (String )updateInfoMap.get(KEY_DOWNLOAD_FILE_URL); 100 } 101 102 public long getDownloadSize() { 103 if(updateInfoMap == null) 104 return 0; 105 else 106 return ((Long )updateInfoMap.get(KEY_DOWNLOAD_SIZE)).longValue(); 107 } 108 109 public String getDescription() { 110 if(updateInfoMap == null) 111 return null; 112 else 113 return (String )updateInfoMap.get(KEY_DESCRIPTION); 114 } 115 116 public boolean writeUpdateXMLFile(String version, String appName, String description, 117 String downloadFileName, String downloadFileURL, 118 long downloadFileSize, String outputFile) { 119 XMLEncoder encoder; 120 try { 121 encoder = new XMLEncoder (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 <String ,Serializable> update = new HashMap <String , 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 (downloadFileSize)); 134 135 encoder.writeObject(update); 136 encoder.close(); 137 138 return true; 139 } 140 141 public void downloadUpdateToLocalDisk(String localFile) { 142 downloadUpdateToLocalDisk(getDownloadFileURL(), localFile); 143 } 144 145 public void downloadUpdateToLocalDisk(String urlString, String 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 url; 157 try { 158 url = new URL (urlString); 159 } catch (MalformedURLException 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 (new BackgroundDownloader(is, localOutputStream, localFile)).start(); 176 } 177 178 public void fetchRemoteUpdateInformation(String urlString, boolean silent) { 179 updateInfoMap = null; 180 181 URL url; 182 try { 183 url = new URL (urlString); 184 } catch (MalformedURLException 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 decoder = new XMLDecoder (new BufferedInputStream(is)); 201 try { 202 updateInfoMap = (Map )decoder.readObject(); 203 } catch(Exception 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 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 version, String remoteUpdateFile, String 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 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 { 275 276 protected InputStream is; 277 protected OutputStream os; 278 protected String localDownloadFile; 279 280 public BackgroundDownloader(InputStream is, OutputStream os, String 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 } 305 try { 306 os.close(); 307 } catch (IOException e) { 308 } 310 311 if(isCancelDownload()) 312 delegate.umDownloadCancelled(); 313 else 314 delegate.umDownloadCompleted(localDownloadFile); 315 } 316 } 317 } 318 } 319 | Popular Tags |