-
Notifications
You must be signed in to change notification settings - Fork 17
/
Updater.java
576 lines (477 loc) · 18.2 KB
/
Updater.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
* Copyright 2011 Tyler Blair. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and contributors and should not be interpreted as representing official policies,
* either expressed or implied, of anybody else.
*/
package com.griefcraft.util;
import com.griefcraft.lwc.LWC;
import com.griefcraft.lwc.LWCInfo;
import com.griefcraft.scripting.ModuleLoader;
import com.griefcraft.sql.Database;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.logging.Logger;
public class Updater {
public enum UpdateMethod {
/**
* Updating will be performed automatically by LWC
*/
AUTOMATIC,
/**
* Updating will be performed manually by the admin
*/
MANUAL;
/**
* Match an input to an update method
*
* @param input
* @return
*/
public static UpdateMethod match(String input) {
for (UpdateMethod method : values()) {
if (method.toString().equalsIgnoreCase(input)) {
return method;
}
}
return null;
}
}
public enum UpdateBranch {
/**
* Long release schedules and is recommended on any server that wishes to have stable LWC features
* but does not necessarily want the very latest LWC has to offer in features, bug fixes, and
* performance.
*/
STABLE("stable"),
/**
* The most volatile branch. Should not be used on production servers but offers the most up to date features
* but testing may not have been performed all around. Performance may also not have been rounded out and
* any features may be removed at any time. Versioning for this branch is based off of the latest build number.
*/
BLEEDING_EDGE("bleeding");
/**
* The branch name to use on the update site
*/
private String branch;
UpdateBranch(String branch) {
this.branch = branch;
}
/**
* @return
*/
public String getBranch() {
return branch;
}
/**
* Match an input to a branch
*
* @param input
* @return
*/
public static UpdateBranch match(String input) {
for (UpdateBranch branch : values()) {
if (branch.toString().equalsIgnoreCase(input)) {
return branch;
}
}
return null;
}
}
/**
* The logging object for this class
*/
private final Logger logger = Logger.getLogger("LWC");
/**
* URL to the base update site
*/
public final static String UPDATE_SITE = "http://update.griefcraft.com";
/**
* Location of the plugin on the website
*/
public final static String PLUGIN_LOCATION = "/lwc/";
/**
* URL to the Jenkins job for LWC
*/
public final static String JENKINS = "http://ci.griefcraft.com/job/LWC/";
/**
* The folder where libraries are stored
*/
public final static String DEST_LIBRARY_FOLDER = "plugins/LWC/lib/";
/**
* The queue of files that need to be downloaded
*/
private final Queue<UpdaterFile> fileQueue = new ConcurrentLinkedQueue<UpdaterFile>();
/**
* The update branch to use
*/
private UpdateBranch updateBranch;
/**
* The update method to use
*/
private UpdateMethod updateMethod;
/**
* The latest plugin version
*/
private Version latestVersion;
public void init() {
LWC lwc = LWC.getInstance();
updateBranch = UpdateBranch.match(lwc.getConfiguration().getString("updater.branch", "STABLE"));
updateMethod = UpdateMethod.match(lwc.getConfiguration().getString("updater.method", "MANUAL"));
if (updateMethod == UpdateMethod.AUTOMATIC) {
this.loadVersions(true, new Runnable() {
public void run() {
tryAutoUpdate(false);
logger.info("LWC: Latest version: " + latestVersion);
}
});
}
// verify we have local files (e.g sqlite.jar, etc)
this.verifyFiles();
this.downloadFiles();
}
/**
* Download all the files in the queue
*/
public void downloadFiles() {
synchronized (fileQueue) {
UpdaterFile updaterFile = null;
while ((updaterFile = fileQueue.poll()) != null) {
try {
File local = new File(updaterFile.getLocalLocation());
String remote = updaterFile.getRemoteLocation();
logger.info("LWC: Downloading file " + local.getName());
// check for LWC folder
File folder = new File("plugins/LWC/");
if (!folder.exists()) {
folder.mkdir();
}
// check native folders
folder = new File(getOSSpecificFolder());
if (!folder.exists()) {
folder.mkdirs();
}
if (local.exists()) {
local.delete();
}
// create the local file
local.createNewFile();
// open the file
OutputStream outputStream = new FileOutputStream(local);
// Connect to the server
URL url = new URL(remote);
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
// hopefully, the content length provided isn't -1
int contentLength = connection.getContentLength();
// Keep a running tally
int bytesTransffered = 0;
long lastUpdate = 0L;
// begin transferring
byte[] buffer = new byte[1024];
int read = 0;
while ((read = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, read);
bytesTransffered += read;
if (contentLength > 0) {
if (System.currentTimeMillis() - lastUpdate > 500L) {
int percentTransferred = (int) (((float) bytesTransffered / contentLength) * 100);
lastUpdate = System.currentTimeMillis();
// omit 100% ..
if (percentTransferred != 100) {
logger.info(percentTransferred + "%");
}
}
}
}
// ok!
outputStream.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Try and automatically update, if automatic updating is enabled.
*
* @param forceUpdate
* @return true if LWC was updated
*/
public boolean tryAutoUpdate(boolean forceUpdate) {
if (!forceUpdate) {
// do we qualify for an automatic update?
if (!shouldAutoUpdate()) {
return false;
}
}
// we shouldn't update if the current version is the same as the latest, or their build numbers are equal
Version current = LWCInfo.FULL_VERSION;
if (current.equals(latestVersion) || (current.getBuildNumber() > 0 && latestVersion.getBuildNumber() > 0 && current.getBuildNumber() == latestVersion.getBuildNumber())) {
return false;
}
logger.info(Colors.Red + "LWC update found!");
// make sure all class files are loaded in our jar file before we overwrite it
loadAllClasses();
// update_site/version/LWC.jar
UpdaterFile file = new UpdaterFile("plugins/LWC.jar", getLatestDownloadURL());
// queue it
fileQueue.offer(file);
// immediately download
downloadFiles();
return true;
}
/**
* Load all the classes in the current jar file so it can be overwritten
*/
private void loadAllClasses() {
LWC lwc = LWC.getInstance();
try {
// Load the jar
JarFile jar = new JarFile(lwc.getPlugin().getFile());
// Walk through all of the entries
Enumeration<JarEntry> enumeration = jar.entries();
while (enumeration.hasMoreElements()) {
JarEntry entry = enumeration.nextElement();
String name = entry.getName();
// is it a class file?
if (name.endsWith(".class")) {
// convert to package
String path = name.replaceAll("/", ".");
path = path.substring(0, path.length() - ".class".length());
// Load it
this.getClass().getClassLoader().loadClass(path);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
/**
* Verify all required files exist
*/
private void verifyFiles() {
// SQLite libraries
if (Database.DefaultType == Database.Type.SQLite) {
// sqlite.jar
this.verifyFile(new UpdaterFile(DEST_LIBRARY_FOLDER + "sqlite.jar", UPDATE_SITE + "/shared/lib/sqlite.jar"));
// Native library
this.verifyFile(new UpdaterFile(getFullNativeLibraryPath(), UPDATE_SITE + "/shared/lib/" + getFullNativeLibraryPath().replaceAll(DEST_LIBRARY_FOLDER, "")));
}
}
/**
* Verify a file and if it does not exist, download it
*
* @param updaterFile
* @return true if the file was queued to be downloaded
*/
private boolean verifyFile(UpdaterFile updaterFile) {
if (updaterFile == null) {
return false;
}
File file = new File(updaterFile.getLocalLocation());
// Does it exist on the FS?
if (file.exists()) {
// So it does!
return false;
}
// It does not exist ..
fileQueue.offer(updaterFile);
return true;
}
/**
* Download a config file
*
* @param config
*/
public void downloadConfig(String config) {
File file = new File(ModuleLoader.ROOT_PATH + config); // where to save to
UpdaterFile updaterFile = new UpdaterFile(file.getPath(), UPDATE_SITE + PLUGIN_LOCATION + "config/" + config);
this.verifyFile(updaterFile);
this.downloadFiles();
}
/**
* Get the latest download URL. Could vary depending upon which update branch is used
*
* @return
*/
public String getLatestDownloadURL() {
if (updateBranch == null) {
return "";
}
switch (updateBranch) {
case BLEEDING_EDGE:
return JENKINS + latestVersion.getBuildNumber() + "/artifact/build/LWC.jar";
default:
return UPDATE_SITE + PLUGIN_LOCATION + "branch/" + updateBranch.getBranch() + "/" + latestVersion.getRawVersion() + "/LWC.jar";
}
}
/**
* Check if we should try to automatically update
*
* @return
*/
public boolean shouldAutoUpdate() {
if (updateBranch == null) {
return false;
}
if (updateMethod != UpdateMethod.AUTOMATIC) {
return false;
}
Version current = LWCInfo.FULL_VERSION;
switch (updateBranch) {
// We only want to compare the build number for bleeding edge
case BLEEDING_EDGE:
return latestVersion.getBuildNumber() > current.getBuildNumber() && (latestVersion.getBuildNumber() > 0 && current.getBuildNumber() > 0);
// For everything else, we are fine with the default routine
default:
return latestVersion.newerThan(current);
}
}
/**
* Load the latest version
*
* @param background
*/
public void loadVersions(boolean background) {
this.loadVersions(background, null);
}
/**
* Load the latest version
*
* @param background if true, will be run in the background
* @param callback The callback will be ran after the version is loaded
*/
public void loadVersions(boolean background, final Runnable callback) {
class Background_Check_Thread implements Runnable {
public void run() {
switch (updateBranch) {
/**
* The bleeding edge branch instead reads the latest build number from Jenkins and relies
* upon that. The nature of bleeding edge is to stay up to the latest build.
*/
case BLEEDING_EDGE:
try {
URL url = new URL(JENKINS + "lastSuccessfulBuild/buildNumber");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
latestVersion = new Version("b" + reader.readLine());
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
default:
// by default, use the LATEST file
try {
URL url = new URL(UPDATE_SITE + PLUGIN_LOCATION + "branch/" + updateBranch.getBranch() + "/LATEST");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
// read in the first line
String line = reader.readLine();
reader.close();
// parse it and we are done
latestVersion = new Version(line);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
// run the callback
if (callback != null) {
callback.run();
}
}
}
Background_Check_Thread worker = new Background_Check_Thread();
if (background) {
new Thread(worker).start();
} else {
worker.run();
}
}
/**
* @return the full path to the native library for sqlite
*/
public String getFullNativeLibraryPath() {
return getOSSpecificFolder() + getOSSpecificFileName();
}
/**
* @return the latest plugin version
*/
public Version getLatestVersion() {
return latestVersion;
}
/**
* @return the update branch that is being used
*/
public UpdateBranch getUpdateBranch() {
return updateBranch;
}
/**
* @return the os/arch specific file name for sqlite's native library
*/
public String getOSSpecificFileName() {
String osname = System.getProperty("os.name").toLowerCase();
if (osname.contains("windows")) {
return "sqlitejdbc.dll";
} else if (osname.contains("mac")) {
return "libsqlitejdbc.jnilib";
} else { /* We assume linux/unix */
return "libsqlitejdbc.so";
}
}
/**
* @return the os/arch specific folder location for SQLite's native library
*/
public String getOSSpecificFolder() {
String osname = System.getProperty("os.name").toLowerCase();
String arch = System.getProperty("os.arch").toLowerCase();
if (osname.contains("windows")) {
return DEST_LIBRARY_FOLDER + "native/Windows/" + arch + "/";
} else if (osname.contains("mac")) {
return DEST_LIBRARY_FOLDER + "native/Mac/" + arch + "/";
} else { /* We assume linux/unix */
return DEST_LIBRARY_FOLDER + "native/Linux/" + arch + "/";
}
}
}