-
Notifications
You must be signed in to change notification settings - Fork 17
/
Updater.java
280 lines (235 loc) · 9.57 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
/*
* 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.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.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class Updater {
/**
* 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/";
/**
* 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>();
public void init() {
// verify we have local files (e.g sqlite.jar, etc)
verifyFiles();
downloadFiles();
final LWC lwc = LWC.getInstance();
lwc.getPlugin().getServer().getScheduler().scheduleAsyncDelayedTask(lwc.getPlugin(), new Runnable() {
public void run() {
Version latest = getLatestVersion();
if (latest.newerThan(LWCInfo.FULL_VERSION)) {
lwc.log("An update is available. You are on version \"" + LWCInfo.FULL_VERSION.toString() + "\". The latest version is: \"" + latest.toString() + "\"");
lwc.log("LWC updates can be found on the Bukkit Dev page at: http://dev.bukkit.org/server-mods/lwc/");
}
}
});
}
/**
* 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;
}
/**
* @return the full path to the native library for sqlite
*/
public String getFullNativeLibraryPath() {
return getOSSpecificFolder() + getOSSpecificFileName();
}
/**
* @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 + "/";
}
}
/**
* Load the latest version
*/
public Version getLatestVersion() {
// by default, use the LATEST file
try {
URL url = new URL(UPDATE_SITE + PLUGIN_LOCATION + "branch/stable/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
return new Version(line);
} catch (MalformedURLException e) {
exceptionCaught(e);
} catch (IOException e) {
exceptionCaught(e);
}
return null;
}
/**
* Download all the files in the queue
*/
public void downloadFiles() {
synchronized (fileQueue) {
UpdaterFile updaterFile = null;
LWC lwc = LWC.getInstance();
while ((updaterFile = fileQueue.poll()) != null) {
try {
File local = new File(updaterFile.getLocalLocation());
String remote = updaterFile.getRemoteLocation();
lwc.log("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;
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) {
lwc.log(percentTransferred + "%");
}
}
}
}
// ok!
outputStream.close();
inputStream.close();
} catch (IOException e) {
exceptionCaught(e);
}
}
}
}
/**
* Called when an exception is caught
*
* @param e
*/
private void exceptionCaught(Exception e) {
LWC lwc = LWC.getInstance();
lwc.log("[LWC] The updater ran into a minor issue: " + e.getMessage());
lwc.log("[LWC] This can probably be ignored.");
}
}