Don’t register shutdown hooks for all Services.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneDownloader.java
index 9f6ce5e..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;
@@ -37,6 +38,7 @@ import net.pterodactylus.util.xml.XML;
 import org.w3c.dom.Document;
 
 import freenet.client.FetchResult;
+import freenet.keys.FreenetURI;
 import freenet.support.api.Bucket;
 
 /**
@@ -67,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;
        }
@@ -109,11 +111,21 @@ 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" }) });
-               FetchResult fetchResult = freenetInterface.fetchUri(sone.getRequestUri().setMetaString(new String[] { "sone.xml" }));
-               logger.log(Level.FINEST, "Got %d bytes back.", fetchResult.size());
-               Sone parsedSone = parseSone(sone, fetchResult);
-               if (parsedSone != null) {
-                       core.addSone(parsedSone);
+               FreenetURI requestUri = sone.getRequestUri().setMetaString(new String[] { "sone.xml" });
+               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);
                }
        }
 
@@ -124,9 +136,11 @@ public class SoneDownloader extends AbstractService {
         *            The sone to parse, or {@code null} if the Sone is yet unknown
         * @param fetchResult
         *            The fetch result
+        * @param requestUri
+        *            The requested URI
         * @return The parsed Sone, or {@code null} if the Sone could not be parsed
         */
-       public Sone parseSone(Sone originalSone, FetchResult fetchResult) {
+       public Sone parseSone(Sone originalSone, FetchResult fetchResult, FreenetURI requestUri) {
                logger.log(Level.FINEST, "Persing FetchResult (%d bytes, %s) for %s…", new Object[] { fetchResult.size(), fetchResult.getMimeType(), originalSone });
                /* TODO - impose a size limit? */
                InputStream xmlInputStream = null;
@@ -156,7 +170,7 @@ public class SoneDownloader extends AbstractService {
                        /* load Sone from core. */
                        sone = originalSone;
                        if (sone == null) {
-                               sone = core.getSone(soneId);
+                               sone = core.getSone(soneId).setRequestUri(requestUri.setMetaString(new String[] {}));
                        }
 
                        String soneName = soneXml.getValue("name", null);
@@ -165,6 +179,21 @@ public class SoneDownloader extends AbstractService {
                                logger.log(Level.WARNING, "Downloaded name for Sone %s was null!", new Object[] { sone });
                                return null;
                        }
+                       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) {