2 * jSite - UpdateChecker.java - Copyright © 2008–2011 David Roden
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 package de.todesbaum.jsite.application;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Properties;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
29 import de.todesbaum.jsite.main.Main;
30 import de.todesbaum.jsite.main.Version;
31 import de.todesbaum.util.freenet.fcp2.Client;
32 import de.todesbaum.util.freenet.fcp2.ClientGet;
33 import de.todesbaum.util.freenet.fcp2.Connection;
34 import de.todesbaum.util.freenet.fcp2.Message;
35 import de.todesbaum.util.freenet.fcp2.Persistence;
36 import de.todesbaum.util.freenet.fcp2.ReturnType;
37 import de.todesbaum.util.freenet.fcp2.Verbosity;
38 import de.todesbaum.util.io.Closer;
41 * Checks for newer versions of jSite.
43 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
45 public class UpdateChecker implements Runnable {
48 private static final Logger logger = Logger.getLogger(UpdateChecker.class.getName());
50 /** Counter for connection names. */
51 private static int counter = 0;
53 /** The edition for the update check URL. */
54 private static final int UPDATE_EDITION = 11;
56 /** The URL for update checks. */
57 private static final String UPDATE_KEY = "USK@e3myoFyp5avg6WYN16ImHri6J7Nj8980Fm~aQe4EX1U,QvbWT0ImE0TwLODTl7EoJx2NBnwDxTbLTE6zkB-eGPs,AQACAAE";
59 /** Object used for synchronization. */
60 private final Object syncObject = new Object();
62 /** Update listeners. */
63 private final List<UpdateListener> updateListeners = new ArrayList<UpdateListener>();
65 /** Whether the main thread should stop. */
66 private boolean shouldStop = false;
68 /** Current last found edition of update key. */
69 private int lastUpdateEdition = UPDATE_EDITION;
71 /** Last found version. */
72 private Version lastVersion = Main.getVersion();
74 /** The freenet interface. */
75 private final Freenet7Interface freenetInterface;
78 * Creates a new update checker that uses the given frame as its parent and
79 * communications via the given freenet interface.
81 * @param freenetInterface
82 * The freenet interface
84 public UpdateChecker(Freenet7Interface freenetInterface) {
85 this.freenetInterface = freenetInterface;
89 // EVENT LISTENER MANAGEMENT
93 * Adds an update listener to the list of registered listeners.
95 * @param updateListener
96 * The update listener to add
98 public void addUpdateListener(UpdateListener updateListener) {
99 updateListeners.add(updateListener);
103 * Removes the given listener from the list of registered listeners.
105 * @param updateListener
106 * The update listener to remove
108 public void removeUpdateListener(UpdateListener updateListener) {
109 updateListeners.remove(updateListener);
113 * Notifies all listeners that a version was found.
115 * @param foundVersion
116 * The version that was found
117 * @param versionTimestamp
118 * The timestamp of the version
120 protected void fireUpdateFound(Version foundVersion, long versionTimestamp) {
121 for (UpdateListener updateListener : updateListeners) {
122 updateListener.foundUpdateData(foundVersion, versionTimestamp);
131 * Returns the latest version that was found.
133 * @return The latest found version
135 public Version getLatestVersion() {
144 * Starts the update checker.
146 public void start() {
147 new Thread(this).start();
151 * Stops the update checker.
154 synchronized (syncObject) {
156 syncObject.notifyAll();
165 * Returns whether the update checker should stop.
167 * @return <code>true</code> if the update checker should stop,
168 * <code>false</code> otherwise
170 private boolean shouldStop() {
171 synchronized (syncObject) {
177 * Creates the URI of the update file for the given edition.
181 * @return The URI for the update file for the given edition
183 private String constructUpdateKey(int edition) {
184 return UPDATE_KEY + "/jSite/" + edition + "/jSite.properties";
188 // INTERFACE Runnable
195 Connection connection = freenetInterface.getConnection("jSite-" + ++counter + "-UpdateChecker");
197 connection.connect();
198 } catch (IOException e1) {
199 e1.printStackTrace();
201 Client client = new Client(connection);
202 boolean checkNow = false;
203 int currentEdition = lastUpdateEdition;
204 while (!shouldStop()) {
206 logger.log(Level.FINE, "Trying " + constructUpdateKey(currentEdition));
207 ClientGet clientGet = new ClientGet("get-update-key");
208 clientGet.setUri(constructUpdateKey(currentEdition));
209 clientGet.setPersistence(Persistence.CONNECTION);
210 clientGet.setReturnType(ReturnType.direct);
211 clientGet.setVerbosity(Verbosity.ALL);
213 client.execute(clientGet);
214 boolean stop = false;
216 Message message = client.readMessage();
217 logger.log(Level.FINEST, "Received message: " + message);
218 if (message == null) {
221 if ("GetFailed".equals(message.getName())) {
222 if ("27".equals(message.get("code"))) {
223 String editionString = message.get("redirecturi").split("/")[2];
224 int editionNumber = -1;
226 editionNumber = Integer.parseInt(editionString);
227 } catch (NumberFormatException nfe1) {
230 if (editionNumber != -1) {
231 logger.log(Level.INFO, "Found new edition " + editionNumber);
232 currentEdition = editionNumber;
233 lastUpdateEdition = editionNumber;
239 if ("AllData".equals(message.getName())) {
240 logger.log(Level.FINE, "Update data found.");
241 InputStream dataInputStream = null;
242 Properties properties = new Properties();
244 dataInputStream = message.getPayloadInputStream();
245 properties.load(dataInputStream);
247 Closer.close(dataInputStream);
250 String foundVersionString = properties.getProperty("jSite.Version");
251 if (foundVersionString != null) {
252 Version foundVersion = Version.parse(foundVersionString);
253 if (foundVersion != null) {
254 lastVersion = foundVersion;
255 String versionTimestampString = properties.getProperty("jSite.Date");
256 logger.log(Level.FINEST, "Version timestamp: " + versionTimestampString);
257 long versionTimestamp = -1;
259 versionTimestamp = Long.parseLong(versionTimestampString);
260 } catch (NumberFormatException nfe1) {
263 fireUpdateFound(foundVersion, versionTimestamp);
271 } catch (IOException e) {
272 logger.log(Level.INFO, "Got IOException: " + e.getMessage());
275 if (!checkNow && !shouldStop()) {
276 synchronized (syncObject) {
278 syncObject.wait(15 * 60 * 1000);
279 } catch (InterruptedException ie1) {