2 * Sone - UpdateChecker.java - Copyright © 2011–2013 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 static java.util.logging.Logger.getLogger;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.net.MalformedURLException;
26 import java.util.Date;
27 import java.util.Properties;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
31 import net.pterodactylus.sone.core.FreenetInterface.Fetched;
32 import net.pterodactylus.sone.core.event.UpdateFoundEvent;
33 import net.pterodactylus.sone.main.SonePlugin;
34 import net.pterodactylus.util.io.Closer;
35 import net.pterodactylus.util.version.Version;
37 import com.google.common.eventbus.EventBus;
38 import com.google.inject.Inject;
40 import freenet.keys.FreenetURI;
41 import freenet.support.api.Bucket;
44 * Watches the official Sone homepage for new releases.
46 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48 public class UpdateChecker {
51 private static final Logger logger = getLogger("Sone.UpdateChecker");
54 private final EventBus eventBus;
56 /** The Freenet interface. */
57 private final FreenetInterface freenetInterface;
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.
76 * @param freenetInterface
77 * The freenet interface to use
80 public UpdateChecker(EventBus eventBus, FreenetInterface freenetInterface) {
81 this.eventBus = eventBus;
82 this.freenetInterface = freenetInterface;
90 * Returns whether a version that is later than the currently running
91 * version has been found.
93 * @return {@code true} if a new version was found
95 public boolean hasLatestVersion() {
96 return currentLatestVersion.compareTo(SonePlugin.VERSION) > 0;
100 * Returns the latest version. If no new latest version has been found, the
101 * current version is returned.
103 * @return The latest known version
105 public Version getLatestVersion() {
106 return currentLatestVersion;
110 * Returns the release time of the latest version. If no new latest version
111 * has been found, the returned value is undefined.
113 * @return The release time of the latest version, if a new version was
116 public long getLatestVersionDate() {
117 return latestVersionDate;
121 * Returns the latest known edition of the Sone homepage.
123 * @return The latest edition of the Sone homepage
125 public long getLatestEdition() {
126 return latestEdition;
134 * Starts the update checker.
136 public void start() {
138 currentUri = new FreenetURI(SonePlugin.getHomepage());
139 } catch (MalformedURLException mue1) {
140 /* this can not really happen unless I screw up. */
141 logger.log(Level.SEVERE, "Sone Homepage URI invalid!", mue1);
143 freenetInterface.registerUsk(currentUri, new FreenetInterface.Callback() {
146 @SuppressWarnings("synthetic-access")
147 public void editionFound(FreenetURI uri, long edition, boolean newKnownGood, boolean newSlot) {
148 logger.log(Level.FINEST, String.format("Found update for %s: %d, %s, %s", uri, edition, newKnownGood, newSlot));
149 if (newKnownGood || newSlot) {
150 Fetched uriResult = freenetInterface.fetchUri(uri.setMetaString(new String[] { "sone.properties" }));
151 if (uriResult == null) {
152 logger.log(Level.WARNING, String.format("Could not fetch properties of latest homepage: %s", uri));
155 Bucket resultBucket = uriResult.getFetchResult().asBucket();
157 parseProperties(resultBucket.getInputStream(), edition);
158 latestEdition = edition;
159 } catch (IOException ioe1) {
160 logger.log(Level.WARNING, String.format("Could not parse sone.properties of %s!", uri), ioe1);
170 * Stops the update checker.
173 freenetInterface.unregisterUsk(currentUri);
181 * Parses the properties of the latest version and fires events, if
184 * @see UpdateFoundEvent
185 * @param propertiesInputStream
186 * The input stream to parse
188 * The latest edition of the Sone homepage
189 * @throws IOException
190 * if an I/O error occured
192 private void parseProperties(InputStream propertiesInputStream, long edition) throws IOException {
193 Properties properties = new Properties();
194 InputStreamReader inputStreamReader = null;
196 inputStreamReader = new InputStreamReader(propertiesInputStream, "UTF-8");
197 properties.load(inputStreamReader);
199 Closer.close(inputStreamReader);
201 String versionString = properties.getProperty("CurrentVersion/Version");
202 String releaseTimeString = properties.getProperty("CurrentVersion/ReleaseTime");
203 if ((versionString == null) || (releaseTimeString == null)) {
204 logger.log(Level.INFO, "Invalid data parsed from properties.");
207 Version version = Version.parse(versionString);
208 long releaseTime = 0;
210 releaseTime = Long.parseLong(releaseTimeString);
211 } catch (NumberFormatException nfe1) {
214 if ((version == null) || (releaseTime == 0)) {
215 logger.log(Level.INFO, "Could not parse data from properties.");
218 if (version.compareTo(currentLatestVersion) > 0) {
219 currentLatestVersion = version;
220 latestVersionDate = releaseTime;
221 logger.log(Level.INFO, String.format("Found new version: %s (%tc)", version, new Date(releaseTime)));
222 eventBus.post(new UpdateFoundEvent(version, releaseTime, edition));