15 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
182 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
15 |
ilm |
5 |
*
|
|
|
6 |
* The contents of this file are subject to the terms of the GNU General Public License Version 3
|
|
|
7 |
* only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
|
|
|
8 |
* copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
|
|
|
9 |
* language governing permissions and limitations under the License.
|
|
|
10 |
*
|
|
|
11 |
* When distributing the software, include this License Header Notice in each file.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
package org.openconcerto.ftp.updater;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.ftp.FTPUtils;
|
|
|
17 |
import org.openconcerto.ftp.IFtp;
|
|
|
18 |
import org.openconcerto.utils.FileUtils;
|
|
|
19 |
|
67 |
ilm |
20 |
import java.awt.AWTException;
|
|
|
21 |
import java.awt.SystemTray;
|
|
|
22 |
import java.awt.TrayIcon;
|
15 |
ilm |
23 |
import java.io.BufferedReader;
|
|
|
24 |
import java.io.File;
|
|
|
25 |
import java.io.FileInputStream;
|
|
|
26 |
import java.io.FileReader;
|
|
|
27 |
import java.io.IOException;
|
65 |
ilm |
28 |
import java.io.InputStream;
|
15 |
ilm |
29 |
import java.io.InputStreamReader;
|
|
|
30 |
import java.util.Properties;
|
|
|
31 |
|
67 |
ilm |
32 |
import javax.swing.ImageIcon;
|
15 |
ilm |
33 |
import javax.swing.JOptionPane;
|
|
|
34 |
|
|
|
35 |
public class UpdateManager implements Runnable {
|
|
|
36 |
private Thread thread;
|
|
|
37 |
private String login;
|
|
|
38 |
private String pass;
|
|
|
39 |
private String server;
|
132 |
ilm |
40 |
private String port;
|
15 |
ilm |
41 |
private String file;
|
|
|
42 |
private static boolean stop = false;
|
|
|
43 |
|
|
|
44 |
private static final int UPDATE_COUNT = 2;
|
|
|
45 |
private static int counter = UPDATE_COUNT;
|
|
|
46 |
private boolean enabled;
|
|
|
47 |
|
|
|
48 |
UpdateManager() {
|
93 |
ilm |
49 |
// Solve Windows Firewall issue
|
|
|
50 |
System.setProperty("java.net.preferIPv4Stack", "true");
|
|
|
51 |
//
|
15 |
ilm |
52 |
final Properties props = new Properties();
|
|
|
53 |
final File f = new File("Configuration/update.properties");
|
|
|
54 |
if (f.exists()) {
|
|
|
55 |
try {
|
|
|
56 |
props.load(new FileInputStream("Configuration/update.properties"));
|
|
|
57 |
this.login = props.getProperty("login");
|
|
|
58 |
this.pass = props.getProperty("pass");
|
|
|
59 |
this.server = props.getProperty("ftpserver");
|
|
|
60 |
this.file = props.getProperty("file");
|
132 |
ilm |
61 |
this.port = props.getProperty("port");
|
15 |
ilm |
62 |
this.enabled = Boolean.parseBoolean(props.getProperty("enabled"));
|
|
|
63 |
if (!this.enabled) {
|
|
|
64 |
System.out.println("Mise à jour désactivées");
|
|
|
65 |
} else {
|
|
|
66 |
this.thread = new Thread(this);
|
|
|
67 |
this.thread.setDaemon(true);
|
|
|
68 |
this.thread.setPriority(Thread.MIN_PRIORITY);
|
|
|
69 |
}
|
|
|
70 |
} catch (IOException e) {
|
|
|
71 |
e.printStackTrace();
|
|
|
72 |
}
|
|
|
73 |
} else {
|
|
|
74 |
System.out.println("Mise à jour désactivées (fichier de configuration manquant)");
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
public static synchronized void start() {
|
|
|
80 |
UpdateManager u = new UpdateManager();
|
|
|
81 |
if (!u.isStarted()) {
|
|
|
82 |
u.startWatcher();
|
|
|
83 |
} else {
|
|
|
84 |
throw new RuntimeException("UpdateManager already started");
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
public static synchronized void stop() {
|
|
|
89 |
stop = true;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
public static synchronized void forceUpdate() {
|
|
|
93 |
counter = UPDATE_COUNT;
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
private boolean isStarted() {
|
|
|
97 |
if (thread == null) {
|
|
|
98 |
return false;
|
|
|
99 |
}
|
|
|
100 |
return this.thread.isAlive();
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
private void startWatcher() {
|
|
|
104 |
if (this.enabled) {
|
|
|
105 |
this.thread.start();
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
@Override
|
|
|
110 |
public void run() {
|
|
|
111 |
System.err.println("UpdateManager started");
|
|
|
112 |
|
|
|
113 |
while (!stop) {
|
|
|
114 |
if (counter >= UPDATE_COUNT) {
|
|
|
115 |
counter = 0;
|
|
|
116 |
// Update
|
|
|
117 |
final IFtp ftp = new IFtp();
|
|
|
118 |
BufferedReader bReaderRemote = null;
|
|
|
119 |
try {
|
132 |
ilm |
120 |
if (this.port != null && this.port.trim().length() > 0) {
|
|
|
121 |
ftp.connect(this.server, Integer.parseInt(this.port));
|
|
|
122 |
} else {
|
|
|
123 |
ftp.connect(this.server);
|
|
|
124 |
}
|
91 |
ilm |
125 |
System.err.println("UpdateManager connected to '" + this.server + "'");
|
15 |
ilm |
126 |
boolean logged = ftp.login(this.login, this.pass);
|
|
|
127 |
|
|
|
128 |
if (!logged) {
|
|
|
129 |
throw new IllegalStateException("Identifiants refusés");
|
91 |
ilm |
130 |
} else {
|
|
|
131 |
System.err.println("UpdateManager authenticated with '" + this.login + "' '" + pass + "'");
|
15 |
ilm |
132 |
}
|
65 |
ilm |
133 |
if (this.file == null) {
|
|
|
134 |
throw new IllegalStateException("Fichier de version non spécifié");
|
|
|
135 |
} else {
|
|
|
136 |
System.err.println("UpdateManager downloading '" + this.file + "'");
|
|
|
137 |
}
|
|
|
138 |
final InputStream retrieveFileStream = ftp.retrieveFileStream(this.file);
|
91 |
ilm |
139 |
if (retrieveFileStream == null) {
|
|
|
140 |
throw new IllegalStateException("Téléchargement de " + this.file + " impossible");
|
|
|
141 |
}
|
65 |
ilm |
142 |
bReaderRemote = new BufferedReader(new InputStreamReader(retrieveFileStream));
|
15 |
ilm |
143 |
|
|
|
144 |
int newVersion = Integer.parseInt(bReaderRemote.readLine());
|
|
|
145 |
|
|
|
146 |
BufferedReader bReaderLocal = null;
|
|
|
147 |
int currentVersion = -1;
|
|
|
148 |
try {
|
|
|
149 |
bReaderLocal = new BufferedReader(new FileReader(".version"));
|
|
|
150 |
currentVersion = Integer.parseInt(bReaderLocal.readLine());
|
|
|
151 |
} catch (Exception e) {
|
|
|
152 |
System.err.println(".version manquant");
|
|
|
153 |
} finally {
|
|
|
154 |
if (bReaderLocal != null) {
|
|
|
155 |
bReaderLocal.close();
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
// / marche pas: ftp.quit();
|
65 |
ilm |
160 |
System.err.println("UpdateManager current version: " + currentVersion + " new version: " + newVersion);
|
15 |
ilm |
161 |
if (newVersion > currentVersion) {
|
|
|
162 |
Object[] options = { "Maintenant", "Plus tard" };
|
|
|
163 |
int res = JOptionPane.showOptionDialog(null, "Une mise à jour est disponible. Installer la mise à jour:", "Mises à jour automatiques", JOptionPane.DEFAULT_OPTION,
|
|
|
164 |
JOptionPane.WARNING_MESSAGE, null, options, options[0]);
|
|
|
165 |
if (res == 0) {
|
|
|
166 |
update(newVersion);
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
} catch (Exception e) {
|
|
|
172 |
Object[] options = { "Réessayer dans 1 minute", "Abandonner" };
|
|
|
173 |
int res = JOptionPane.showOptionDialog(null, "Impossible de se connecter au serveur de mises à jour.\n" + e.getMessage(), "Mises à jour automatiques", JOptionPane.DEFAULT_OPTION,
|
|
|
174 |
JOptionPane.WARNING_MESSAGE, null, options, options[0]);
|
|
|
175 |
if (res == 0) {
|
|
|
176 |
counter--;
|
|
|
177 |
} else {
|
|
|
178 |
stop = true;
|
|
|
179 |
}
|
|
|
180 |
e.printStackTrace();
|
|
|
181 |
|
|
|
182 |
} finally {
|
|
|
183 |
try {
|
|
|
184 |
System.err.println("Disconnect start");
|
|
|
185 |
if (bReaderRemote != null) {
|
|
|
186 |
bReaderRemote.close();
|
|
|
187 |
}
|
|
|
188 |
// ftp.abort();
|
|
|
189 |
// ftp.logout();
|
|
|
190 |
// ftp.disconnect();
|
|
|
191 |
ftp.quit();
|
|
|
192 |
System.err.println("Disconnected start");
|
|
|
193 |
} catch (Exception e) {
|
|
|
194 |
e.printStackTrace();
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
}
|
|
|
199 |
if (!stop) {
|
|
|
200 |
try {
|
|
|
201 |
// Sleep 1 minute
|
|
|
202 |
Thread.sleep(60 * 1000);
|
|
|
203 |
} catch (InterruptedException e) {
|
|
|
204 |
e.printStackTrace();
|
|
|
205 |
}
|
|
|
206 |
}
|
|
|
207 |
counter++;
|
|
|
208 |
}
|
|
|
209 |
System.err.println("UpdateManager stopped");
|
|
|
210 |
}
|
|
|
211 |
|
|
|
212 |
private void update(int toVersion) {
|
67 |
ilm |
213 |
|
|
|
214 |
try {
|
182 |
ilm |
215 |
if (SystemTray.isSupported()) {
|
|
|
216 |
SystemTray.getSystemTray().add(new TrayIcon(new ImageIcon(UpdateManager.class.getResource("tray.png")).getImage()));
|
|
|
217 |
}
|
67 |
ilm |
218 |
} catch (AWTException e2) {
|
|
|
219 |
e2.printStackTrace();
|
|
|
220 |
}
|
|
|
221 |
displayMessage("Préparation de la mise à jour");
|
15 |
ilm |
222 |
System.out.println("Update application");
|
|
|
223 |
File tempDir = new File("Update");
|
|
|
224 |
if (tempDir.exists()) {
|
|
|
225 |
FileUtils.rmR(tempDir);
|
|
|
226 |
|
|
|
227 |
}
|
|
|
228 |
tempDir.mkdir();
|
65 |
ilm |
229 |
if (!tempDir.exists()) {
|
|
|
230 |
JOptionPane.showMessageDialog(null, "Impossible de créer le dossier Update\nVérifiez les permissions des fichiers ou lancez le logiciel en tant qu'administrateur");
|
|
|
231 |
return;
|
|
|
232 |
}
|
15 |
ilm |
233 |
File f = new File("Update/.version");
|
|
|
234 |
try {
|
67 |
ilm |
235 |
FileUtils.write(String.valueOf(toVersion) + "\n", f);
|
|
|
236 |
} catch (IOException e1) {
|
65 |
ilm |
237 |
JOptionPane.showMessageDialog(null, "Impossible de créer le .version\nVérifiez les permissions des fichiers ou lancez le logiciel en tant qu'administrateur");
|
|
|
238 |
return;
|
15 |
ilm |
239 |
}
|
|
|
240 |
final IFtp ftp = new IFtp();
|
|
|
241 |
try {
|
67 |
ilm |
242 |
displayMessage("Connexion au serveur");
|
132 |
ilm |
243 |
if (this.port != null && this.port.trim().length() > 0) {
|
|
|
244 |
ftp.connect(this.server, Integer.parseInt(this.port));
|
|
|
245 |
} else {
|
|
|
246 |
ftp.connect(this.server);
|
|
|
247 |
}
|
15 |
ilm |
248 |
boolean logged = ftp.login(this.login, this.pass);
|
|
|
249 |
if (!logged) {
|
|
|
250 |
JOptionPane.showMessageDialog(null, "Impossible d'accéder au serveur FTP pour récupérer les fichiers");
|
|
|
251 |
ftp.disconnect();
|
|
|
252 |
return;
|
|
|
253 |
}
|
|
|
254 |
String dir = this.file;
|
|
|
255 |
int i = dir.lastIndexOf('.');
|
|
|
256 |
if (i > 0) {
|
|
|
257 |
dir = dir.substring(0, i);
|
|
|
258 |
}
|
|
|
259 |
|
|
|
260 |
boolean cwdOk = ftp.changeWorkingDirectory(dir);
|
|
|
261 |
if (!cwdOk) {
|
|
|
262 |
JOptionPane.showMessageDialog(null, "Impossible d'accéder au dossier " + dir + " du serveur FTP");
|
|
|
263 |
ftp.disconnect();
|
|
|
264 |
return;
|
|
|
265 |
}
|
67 |
ilm |
266 |
displayMessage("Téléchargement des fichiers");
|
15 |
ilm |
267 |
FTPUtils.saveR(ftp, tempDir);
|
|
|
268 |
|
|
|
269 |
if (tempDir.getAbsolutePath().contains("workspace") && !tempDir.getAbsolutePath().contains("dist")) {
|
|
|
270 |
JOptionPane.showMessageDialog(null, "Mise à jour desactivée en version non 'dist'");
|
|
|
271 |
ftp.disconnect();
|
|
|
272 |
return;
|
|
|
273 |
}
|
65 |
ilm |
274 |
|
28 |
ilm |
275 |
final String updaterFilename = "Update" + File.separator + "update.jar";
|
15 |
ilm |
276 |
if (!new File(updaterFilename).exists()) {
|
28 |
ilm |
277 |
JOptionPane.showMessageDialog(null, "Le fichier 'Update/update.jar' est manquant.");
|
15 |
ilm |
278 |
ftp.disconnect();
|
|
|
279 |
return;
|
|
|
280 |
}
|
67 |
ilm |
281 |
displayMessage("Copie des fichiers");
|
|
|
282 |
try {
|
|
|
283 |
ftp.disconnect();
|
|
|
284 |
} catch (Throwable e) {
|
|
|
285 |
e.printStackTrace();
|
|
|
286 |
}
|
83 |
ilm |
287 |
String jHome = System.getProperty("java.home");
|
|
|
288 |
jHome += File.separatorChar + "bin" + File.separatorChar + "java";
|
151 |
ilm |
289 |
ProcessBuilder process = new ProcessBuilder(jHome, "-jar", updaterFilename);
|
|
|
290 |
process.start();
|
83 |
ilm |
291 |
JOptionPane.showMessageDialog(null, "Mise à jour terminée");
|
15 |
ilm |
292 |
System.exit(0);
|
|
|
293 |
|
|
|
294 |
} catch (Exception e) {
|
|
|
295 |
e.printStackTrace();
|
|
|
296 |
JOptionPane.showMessageDialog(null, "Impossible de récupérer les fichiers");
|
|
|
297 |
|
|
|
298 |
}
|
|
|
299 |
|
|
|
300 |
}
|
67 |
ilm |
301 |
|
|
|
302 |
void displayMessage(String s) {
|
|
|
303 |
try {
|
182 |
ilm |
304 |
if (SystemTray.isSupported()) {
|
|
|
305 |
SystemTray.getSystemTray().getTrayIcons()[0].displayMessage("Mise à jour en cours", s, TrayIcon.MessageType.INFO);
|
|
|
306 |
}
|
67 |
ilm |
307 |
} catch (Throwable e) {
|
|
|
308 |
e.printStackTrace();
|
|
|
309 |
}
|
|
|
310 |
}
|
15 |
ilm |
311 |
}
|