Don’t register shutdown hooks for all Services.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneDownloader.java
index da33b0b..9b48d7d 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import net.pterodactylus.sone.core.Core.SoneStatus;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Reply;
@@ -68,7 +69,7 @@ public class SoneDownloader extends AbstractService {
         *            The Freenet interface
         */
        public SoneDownloader(Core core, FreenetInterface freenetInterface) {
-               super("Sone Downloader");
+               super("Sone Downloader", false);
                this.core = core;
                this.freenetInterface = freenetInterface;
        }
@@ -111,11 +112,20 @@ public class SoneDownloader extends AbstractService {
        public void fetchSone(Sone sone) {
                logger.log(Level.FINE, "Starting fetch for Sone “%s” from %s…", new Object[] { sone, sone.getRequestUri().setMetaString(new String[] { "sone.xml" }) });
                FreenetURI requestUri = sone.getRequestUri().setMetaString(new String[] { "sone.xml" });
-               FetchResult fetchResult = freenetInterface.fetchUri(requestUri);
-               logger.log(Level.FINEST, "Got %d bytes back.", fetchResult.size());
-               Sone parsedSone = parseSone(sone, fetchResult, requestUri);
-               if (parsedSone != null) {
-                       core.addSone(parsedSone);
+               core.setSoneStatus(sone, SoneStatus.downloading);
+               try {
+                       FetchResult fetchResult = freenetInterface.fetchUri(requestUri);
+                       if (fetchResult == null) {
+                               /* TODO - mark Sone as bad. */
+                               return;
+                       }
+                       logger.log(Level.FINEST, "Got %d bytes back.", fetchResult.size());
+                       Sone parsedSone = parseSone(sone, fetchResult, requestUri);
+                       if (parsedSone != null) {
+                               core.addSone(parsedSone);
+                       }
+               } finally {
+                       core.setSoneStatus(sone, (sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
                }
        }
 
@@ -171,6 +181,20 @@ public class SoneDownloader extends AbstractService {
                        }
                        sone.setName(soneName);
 
+                       String soneTime = soneXml.getValue("time", null);
+                       if (soneTime == null) {
+                               /* TODO - mark Sone as bad. */
+                               logger.log(Level.WARNING, "Downloaded time for Sone %s was null!", new Object[] { sone });
+                               return null;
+                       }
+                       try {
+                               sone.setTime(Long.parseLong(soneTime));
+                       } catch (NumberFormatException nfe1) {
+                               /* TODO - mark Sone as bad. */
+                               logger.log(Level.WARNING, "Downloaded Sone %s with invalid time: %s", new Object[] { sone, soneTime });
+                               return null;
+                       }
+
                        SimpleXML profileXml = soneXml.getNode("profile");
                        if (profileXml == null) {
                                /* TODO - mark Sone as bad. */