2 * jSite-remote - UpdateChecker.java -
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package de.todesbaum.jsite.application;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Properties;
28 import de.todesbaum.jsite.main.Main;
29 import de.todesbaum.jsite.main.Version;
30 import de.todesbaum.util.freenet.fcp2.Client;
31 import de.todesbaum.util.freenet.fcp2.ClientGet;
32 import de.todesbaum.util.freenet.fcp2.Connection;
33 import de.todesbaum.util.freenet.fcp2.Message;
34 import de.todesbaum.util.freenet.fcp2.Persistence;
35 import de.todesbaum.util.freenet.fcp2.ReturnType;
36 import de.todesbaum.util.freenet.fcp2.Verbosity;
37 import de.todesbaum.util.io.Closer;
40 * Checks for newer versions of jSite.
42 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
44 public class UpdateChecker implements Runnable {
46 /** Counter for connection names. */
47 private static int counter = 0;
49 /** The edition for the update check URL. */
50 private static final int UPDATE_EDITION = 0;
52 /** The URL for update checks. */
53 private static final String UPDATE_KEY = "USK@e3myoFyp5avg6WYN16ImHri6J7Nj8980Fm~aQe4EX1U,QvbWT0ImE0TwLODTl7EoJx2NBnwDxTbLTE6zkB-eGPs,AQACAAE";
55 /** Object used for synchronization. */
56 private final Object syncObject = new Object();
58 /** Update listeners. */
59 private final List<UpdateListener> updateListeners = new ArrayList<UpdateListener>();
61 /** Whether the main thread should stop. */
62 private boolean shouldStop = false;
64 /** Current last found edition of update key. */
65 private int lastUpdateEdition = UPDATE_EDITION;
67 /** Last found version. */
68 private Version lastVersion = Main.getVersion();
70 /** The freenet interface. */
71 private final Freenet7Interface freenetInterface;
74 * Creates a new update checker that uses the given frame as its parent and
75 * communications via the given freenet interface.
77 * @param freenetInterface
78 * The freenet interface
80 public UpdateChecker(Freenet7Interface freenetInterface) {
81 this.freenetInterface = freenetInterface;
85 // EVENT LISTENER MANAGEMENT
89 * Adds an update listener to the list of registered listeners.
91 * @param updateListener
92 * The update listener to add
94 public void addUpdateListener(UpdateListener updateListener) {
95 updateListeners.add(updateListener);
99 * Removes the given listener from the list of registered listeners.
101 * @param updateListener
102 * The update listener to remove
104 public void removeUpdateListener(UpdateListener updateListener) {
105 updateListeners.remove(updateListener);
109 * Notifies all listeners that a version was found.
111 * @param foundVersion
112 * The version that was found
113 * @param versionTimestamp
114 * The timestamp of the version
116 protected void fireUpdateFound(Version foundVersion, long versionTimestamp) {
117 for (UpdateListener updateListener : updateListeners) {
118 updateListener.foundUpdateData(foundVersion, versionTimestamp);
127 * Returns the latest version that was found.
129 * @return The latest found version
131 public Version getLatestVersion() {
140 * Starts the update checker.
142 public void start() {
143 new Thread(this).start();
147 * Stops the update checker.
150 synchronized (syncObject) {
152 syncObject.notifyAll();
161 * Returns whether the update checker should stop.
163 * @return <code>true</code> if the update checker should stop,
164 * <code>false</code> otherwise
166 private boolean shouldStop() {
167 synchronized (syncObject) {
173 * Creates the URI of the update file for the given edition.
177 * @return The URI for the update file for the given edition
179 private String constructUpdateKey(int edition) {
180 return UPDATE_KEY + "/jSite/" + edition + "/jSite.properties";
184 // INTERFACE Runnable
191 Connection connection = freenetInterface.getConnection("jSite-" + ++counter + "-UpdateChecker");
193 connection.connect();
194 } catch (IOException e1) {
195 e1.printStackTrace();
197 Client client = new Client(connection);
198 boolean checkNow = false;
199 int currentEdition = lastUpdateEdition;
200 while (!shouldStop()) {
202 System.out.println("Trying " + constructUpdateKey(currentEdition));
203 ClientGet clientGet = new ClientGet("get-update-key");
204 clientGet.setUri(constructUpdateKey(currentEdition));
205 clientGet.setPersistence(Persistence.CONNECTION);
206 clientGet.setReturnType(ReturnType.direct);
207 clientGet.setVerbosity(Verbosity.ALL);
209 client.execute(clientGet);
210 boolean stop = false;
212 Message message = client.readMessage();
213 System.out.println(message);
214 if ("GetFailed".equals(message.getName())) {
215 if ("27".equals(message.get("code"))) {
216 String editionString = message.get("redirecturi").split("/")[2];
217 int editionNumber = -1;
219 editionNumber = Integer.parseInt(editionString);
220 } catch (NumberFormatException nfe1) {
223 if (editionNumber != -1) {
224 System.out.println("Found new edition " + editionNumber);
225 currentEdition = editionNumber;
226 lastUpdateEdition = editionNumber;
232 if ("AllData".equals(message.getName())) {
233 System.out.println("Update data found.");
234 InputStream dataInputStream = null;
235 Properties properties = new Properties();
237 dataInputStream = message.getPayloadInputStream();
238 properties.load(dataInputStream);
240 Closer.close(dataInputStream);
243 String foundVersionString = properties.getProperty("jSite.Version");
244 if (foundVersionString != null) {
245 Version foundVersion = Version.parse(foundVersionString);
246 if (foundVersion != null) {
247 lastVersion = foundVersion;
248 String versionTimestampString = properties.getProperty("jSite.Date");
249 System.out.println(versionTimestampString);
250 long versionTimestamp = -1;
252 versionTimestamp = Long.parseLong(versionTimestampString);
253 } catch (NumberFormatException nfe1) {
256 fireUpdateFound(foundVersion, versionTimestamp);
264 } catch (IOException e) {
265 System.out.println("Got IOException: " + e.getMessage());
268 if (!checkNow && !shouldStop()) {
269 synchronized (syncObject) {
271 syncObject.wait(15 * 60 * 1000);
272 } catch (InterruptedException ie1) {