2 * Sone - UpdateChecker.java - Copyright © 2011–2012 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 3 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, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.core;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.net.MalformedURLException;
24 import java.util.Date;
25 import java.util.Properties;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
29 import net.pterodactylus.sone.core.FreenetInterface.Fetched;
30 import net.pterodactylus.sone.main.SonePlugin;
31 import net.pterodactylus.util.io.Closer;
32 import net.pterodactylus.util.logging.Logging;
33 import net.pterodactylus.util.version.Version;
34 import freenet.keys.FreenetURI;
35 import freenet.support.api.Bucket;
38 * Watches the official Sone homepage for new releases.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class UpdateChecker {
45 private static final Logger logger = Logging.getLogger(UpdateChecker.class);
47 /** The key of the Sone homepage. */
48 private static final String SONE_HOMEPAGE = "USK@nwa8lHa271k2QvJ8aa0Ov7IHAV-DFOCFgmDt3X6BpCI,DuQSUZiI~agF8c-6tjsFFGuZ8eICrzWCILB60nT8KKo,AQACAAE/sone/";
50 /** The current latest known edition. */
51 private static final int LATEST_EDITION = 55;
53 /** The Freenet interface. */
54 private final FreenetInterface freenetInterface;
56 /** The update listener manager. */
57 private final UpdateListenerManager updateListenerManager = new UpdateListenerManager();
59 /** The current URI of the homepage. */
60 private FreenetURI currentUri;
62 /** The latest known edition. */
63 private long latestEdition;
65 /** The current latest known version. */
66 private Version currentLatestVersion = SonePlugin.VERSION;
68 /** The release date of the latest version. */
69 private long latestVersionDate;
72 * Creates a new update checker.
74 * @param freenetInterface
75 * The freenet interface to use
77 public UpdateChecker(FreenetInterface freenetInterface) {
78 this.freenetInterface = freenetInterface;
82 // EVENT LISTENER MANAGEMENT
86 * Adds the given listener to the list of registered listeners.
88 * @param updateListener
91 public void addUpdateListener(UpdateListener updateListener) {
92 updateListenerManager.addListener(updateListener);
96 * Removes the given listener from the list of registered listeners.
98 * @param updateListener
99 * The listener to remove
101 public void removeUpdateListener(UpdateListener updateListener) {
102 updateListenerManager.removeListener(updateListener);
110 * Returns whether a version that is later than the currently running
111 * version has been found.
113 * @return {@code true} if a new version was found
115 public boolean hasLatestVersion() {
116 return currentLatestVersion.compareTo(SonePlugin.VERSION) > 0;
120 * Returns the latest version. If no new latest version has been found, the
121 * current version is returned.
123 * @return The latest known version
125 public Version getLatestVersion() {
126 return currentLatestVersion;
130 * Returns the release time of the latest version. If no new latest version
131 * has been found, the returned value is undefined.
133 * @return The release time of the latest version, if a new version was
136 public long getLatestVersionDate() {
137 return latestVersionDate;
141 * Returns the latest known edition of the Sone homepage.
143 * @return The latest edition of the Sone homepage
145 public long getLatestEdition() {
146 return latestEdition;
154 * Starts the update checker.
156 public void start() {
158 currentUri = new FreenetURI(SONE_HOMEPAGE + LATEST_EDITION);
159 } catch (MalformedURLException mue1) {
160 /* this can not really happen unless I screw up. */
161 logger.log(Level.SEVERE, "Sone Homepage URI invalid!", mue1);
163 freenetInterface.registerUsk(currentUri, new FreenetInterface.Callback() {
166 @SuppressWarnings("synthetic-access")
167 public void editionFound(FreenetURI uri, long edition, boolean newKnownGood, boolean newSlot) {
168 logger.log(Level.FINEST, String.format("Found update for %s: %d, %s, %s", uri, edition, newKnownGood, newSlot));
169 if (newKnownGood || newSlot) {
170 Fetched uriResult = freenetInterface.fetchUri(uri.setMetaString(new String[] { "sone.properties" }));
171 if (uriResult == null) {
172 logger.log(Level.WARNING, String.format("Could not fetch properties of latest homepage: %s", uri));
175 Bucket resultBucket = uriResult.getFetchResult().asBucket();
177 parseProperties(resultBucket.getInputStream(), edition);
178 latestEdition = edition;
179 } catch (IOException ioe1) {
180 logger.log(Level.WARNING, String.format("Could not parse sone.properties of %s!", uri), ioe1);
190 * Stops the update checker.
193 freenetInterface.unregisterUsk(currentUri);
201 * Parses the properties of the latest version and fires events, if
204 * @see UpdateListener#updateFound(Version, long, long)
205 * @see UpdateListenerManager#fireUpdateFound(Version, long, long)
206 * @param propertiesInputStream
207 * The input stream to parse
209 * The latest edition of the Sone homepage
210 * @throws IOException
211 * if an I/O error occured
213 private void parseProperties(InputStream propertiesInputStream, long edition) throws IOException {
214 Properties properties = new Properties();
215 InputStreamReader inputStreamReader = null;
217 inputStreamReader = new InputStreamReader(propertiesInputStream, "UTF-8");
218 properties.load(inputStreamReader);
220 Closer.close(inputStreamReader);
222 String versionString = properties.getProperty("CurrentVersion/Version");
223 String releaseTimeString = properties.getProperty("CurrentVersion/ReleaseTime");
224 if ((versionString == null) || (releaseTimeString == null)) {
225 logger.log(Level.INFO, "Invalid data parsed from properties.");
228 Version version = Version.parse(versionString);
229 long releaseTime = 0;
231 releaseTime = Long.parseLong(releaseTimeString);
232 } catch (NumberFormatException nfe1) {
235 if ((version == null) || (releaseTime == 0)) {
236 logger.log(Level.INFO, "Could not parse data from properties.");
239 if (version.compareTo(currentLatestVersion) > 0) {
240 currentLatestVersion = version;
241 latestVersionDate = releaseTime;
242 logger.log(Level.INFO, String.format("Found new version: %s (%tc)", version, new Date(releaseTime)));
243 updateListenerManager.fireUpdateFound(version, releaseTime, edition);