|
| 1 | +package org.petschko.lib.update; |
| 2 | + |
| 3 | +import org.jetbrains.annotations.Nullable; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.net.MalformedURLException; |
| 9 | +import java.net.URL; |
| 10 | + |
| 11 | +/** |
| 12 | + * Author: Peter Dragicevic [peter@petschko.org] |
| 13 | + * Authors-Website: http://petschko.org/ |
| 14 | + * Date: 05.10.2017 |
| 15 | + * Time: 14:37 |
| 16 | + * Update: - |
| 17 | + * Version: 0.0.1 |
| 18 | + * |
| 19 | + * Notes: Update Class |
| 20 | + */ |
| 21 | +public class Update { |
| 22 | + private UpdateCache updateCache; |
| 23 | + private URL checkVersionUrl; |
| 24 | + private URL whatsNewUrl = null; |
| 25 | + private URL downloadURL = null; |
| 26 | + private Version currentVersion; |
| 27 | + private Version newestVersion = null; |
| 28 | + private boolean hasNewVersion = false; |
| 29 | + private boolean ignoreCache = false; |
| 30 | + |
| 31 | + /** |
| 32 | + * Update Constructor |
| 33 | + * |
| 34 | + * @param checkVersionUrl - URL to get the newest Version-Number |
| 35 | + * @param currentVersion - Current Version |
| 36 | + * @throws IOException - IO-Exception |
| 37 | + */ |
| 38 | + public Update(String checkVersionUrl, String currentVersion) throws IOException { |
| 39 | + this.updateCache = new UpdateCache(); |
| 40 | + this.checkVersionUrl = new URL(checkVersionUrl); |
| 41 | + this.currentVersion = new Version(currentVersion); |
| 42 | + |
| 43 | + this.init(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Update Constructor |
| 48 | + * |
| 49 | + * @param checkVersionUrl - URL to get the newest Version-Number |
| 50 | + * @param currentVersion - Current Version |
| 51 | + * @param cacheTime - Cache-Time in Sec |
| 52 | + * @throws IOException - IO-Exception |
| 53 | + */ |
| 54 | + public Update(String checkVersionUrl, String currentVersion, long cacheTime) throws IOException { |
| 55 | + this.updateCache = new UpdateCache(cacheTime); |
| 56 | + this.checkVersionUrl = new URL(checkVersionUrl); |
| 57 | + this.currentVersion = new Version(currentVersion); |
| 58 | + |
| 59 | + this.init(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Update Constructor |
| 64 | + * |
| 65 | + * @param checkVersionUrl - URL to get the newest Version-Number |
| 66 | + * @param currentVersion - Current Version |
| 67 | + * @param ignoreCache - Should the Cache be ignored? |
| 68 | + * @throws IOException - IO-Exception |
| 69 | + */ |
| 70 | + public Update(String checkVersionUrl, String currentVersion, boolean ignoreCache) throws IOException { |
| 71 | + this.updateCache = new UpdateCache(); |
| 72 | + this.checkVersionUrl = new URL(checkVersionUrl); |
| 73 | + this.currentVersion = new Version(currentVersion); |
| 74 | + this.ignoreCache = ignoreCache; |
| 75 | + |
| 76 | + this.init(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Gets the Whats-New URL |
| 81 | + * |
| 82 | + * @return - Whats-New URL |
| 83 | + */ |
| 84 | + public URL getWhatsNewUrl() { |
| 85 | + return whatsNewUrl; |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Initiates this instance (may loads cache) |
| 90 | + */ |
| 91 | + private void init() throws IOException { |
| 92 | + if(this.ignoreCache) { |
| 93 | + this.getUpdateInfo(); |
| 94 | + } else { |
| 95 | + if(this.updateCache.loadCache()) { |
| 96 | + this.newestVersion = this.updateCache.newestVersionCache; |
| 97 | + this.downloadURL = this.updateCache.cachedDownloadUrl; |
| 98 | + this.whatsNewUrl = this.updateCache.cachedWhatsNewUrl; |
| 99 | + } else { |
| 100 | + this.getUpdateInfo(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + this.checkVersion(); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Get all information from the File for the Update process |
| 109 | + */ |
| 110 | + private void getUpdateInfo() throws IOException { |
| 111 | + // Read the Update-URL |
| 112 | + InputStream content = this.checkVersionUrl.openStream(); |
| 113 | + |
| 114 | + |
| 115 | + // Convert the read Content to Strings |
| 116 | + int c; |
| 117 | + int currentString = 0; |
| 118 | + StringBuilder version = new StringBuilder(); |
| 119 | + StringBuilder downloadUrl = new StringBuilder(); |
| 120 | + StringBuilder whatsNewUrl = new StringBuilder(); |
| 121 | + |
| 122 | + while(true) { |
| 123 | + try { |
| 124 | + c = content.read(); |
| 125 | + |
| 126 | + // Exit loop if file reaches end |
| 127 | + if(c == -1) |
| 128 | + break; |
| 129 | + |
| 130 | + if(c == (int) ';') |
| 131 | + currentString++; |
| 132 | + else { |
| 133 | + switch(currentString) { |
| 134 | + case 0: |
| 135 | + version.append((char) c); |
| 136 | + break; |
| 137 | + case 1: |
| 138 | + downloadUrl.append((char) c); |
| 139 | + break; |
| 140 | + case 2: |
| 141 | + whatsNewUrl.append((char) c); |
| 142 | + break; |
| 143 | + default: |
| 144 | + } |
| 145 | + } |
| 146 | + } catch(IOException e) { |
| 147 | + e.printStackTrace(); |
| 148 | + break; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + this.newestVersion = new Version(version.toString().trim()); |
| 153 | + |
| 154 | + try { |
| 155 | + this.downloadURL = new URL(downloadUrl.toString().trim()); |
| 156 | + this.whatsNewUrl = new URL(whatsNewUrl.toString().trim()); |
| 157 | + } catch(MalformedURLException e) { |
| 158 | + e.printStackTrace(); |
| 159 | + } |
| 160 | + |
| 161 | + this.savesCache(); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Saves new Data to the Cache |
| 166 | + */ |
| 167 | + private void savesCache() { |
| 168 | + this.updateCache.newestVersionCache = this.newestVersion; |
| 169 | + this.updateCache.cachedDownloadUrl = this.downloadURL; |
| 170 | + this.updateCache.cachedWhatsNewUrl = this.whatsNewUrl; |
| 171 | + |
| 172 | + this.updateCache.saveCache(); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Checks if the Version is the newest |
| 177 | + */ |
| 178 | + private void checkVersion() { |
| 179 | + if(this.newestVersion == null) |
| 180 | + return; |
| 181 | + |
| 182 | + if(! this.currentVersion.versionsEqual(this.newestVersion)) |
| 183 | + this.hasNewVersion = this.currentVersion.thisIsLowerThan(this.newestVersion); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Shows if the current Version is the newest |
| 188 | + * |
| 189 | + * @return - Is newest Version |
| 190 | + */ |
| 191 | + public boolean isHasNewVersion() { |
| 192 | + return this.hasNewVersion; |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Get the newest Version-Number |
| 197 | + * |
| 198 | + * @return - Newest Version-Number or null if could not read Update-URL |
| 199 | + */ |
| 200 | + public String getNewestVersion() { |
| 201 | + return newestVersion.getVersion(); |
| 202 | + } |
| 203 | + |
| 204 | + /** |
| 205 | + * Get the current Version |
| 206 | + * |
| 207 | + * @return - Current Version |
| 208 | + */ |
| 209 | + public String getCurrentVersion() { |
| 210 | + return currentVersion.getVersion(); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Starts the updater but not relaunch after update |
| 215 | + * |
| 216 | + * @param targetJar - Target jar which should be updated |
| 217 | + * @param gui - Should the Updater show a GUI window |
| 218 | + */ |
| 219 | + public void runUpdate(String targetJar, boolean gui) throws UpdateException { |
| 220 | + this.runUpdate(targetJar, gui, false, null); |
| 221 | + } |
| 222 | + |
| 223 | + |
| 224 | + /** |
| 225 | + * Starts the updater |
| 226 | + * |
| 227 | + * @param targetJar - Target jar which should be updated |
| 228 | + * @param gui - Should the Updater show a GUI window |
| 229 | + * @param relaunch - Relaunch this Program after Update |
| 230 | + * @param relaunchArgs - Args for relaunch, can be null if none |
| 231 | + */ |
| 232 | + public void runUpdate(String targetJar, boolean gui, boolean relaunch, @Nullable String[] relaunchArgs) throws UpdateException { |
| 233 | + File updaterFile = new File("update.jar"); |
| 234 | + File targetJarFile = new File(targetJar); |
| 235 | + |
| 236 | + if(this.newestVersion == null) |
| 237 | + throw new UpdateException("Newest Version is not set!", this.currentVersion); |
| 238 | + |
| 239 | + if(this.newestVersion.versionsEqual(new Version(""))) |
| 240 | + throw new UpdateException("Newest Version is empty...", this.currentVersion); |
| 241 | + |
| 242 | + if(this.newestVersion.versionsEqual(this.currentVersion)) |
| 243 | + throw new UpdateException("This Program is already up to date!", this.currentVersion, this.newestVersion); |
| 244 | + |
| 245 | + if(! targetJarFile.exists() || targetJarFile.isDirectory()) |
| 246 | + throw new UpdateException("Can not find the Target-Jar", this.currentVersion); |
| 247 | + |
| 248 | + if(! updaterFile.exists() || updaterFile.isDirectory()) |
| 249 | + throw new UpdateException("Updater not found!", this.currentVersion); |
| 250 | + |
| 251 | + String[] run = { |
| 252 | + "java", |
| 253 | + "-jar", |
| 254 | + "update.jar", |
| 255 | + "\"" + targetJar + "\"", |
| 256 | + "\"" + this.downloadURL.toString() + "\"", |
| 257 | + gui ? "true" : "false", |
| 258 | + relaunch ? "true" : "false" |
| 259 | + }; |
| 260 | + |
| 261 | + // Add args |
| 262 | + if(relaunchArgs != null) { |
| 263 | + String[] tmp = new String[run.length + relaunchArgs.length]; |
| 264 | + int i; |
| 265 | + for(i = 0; i < run.length; i++) |
| 266 | + tmp[i] = run[i]; |
| 267 | + |
| 268 | + int n = i; |
| 269 | + for(; i < tmp.length; i++) |
| 270 | + tmp[i] = relaunchArgs[i - n]; |
| 271 | + |
| 272 | + run = tmp; |
| 273 | + } |
| 274 | + |
| 275 | + try { |
| 276 | + Runtime.getRuntime().exec(run); |
| 277 | + } catch (Exception e) { |
| 278 | + throw new UpdateException(e.getMessage(), this.currentVersion, e); |
| 279 | + } |
| 280 | + System.exit(0); |
| 281 | + } |
| 282 | +} |
0 commit comments