Don‘t download a Sone more than once.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneDownloader.java
index e11ded8..34f48d9 100644 (file)
@@ -111,6 +111,9 @@ public class SoneDownloader extends AbstractService {
         *            The Sone to fetch
         */
        public void fetchSone(Sone sone) {
+               if (core.getSoneStatus(sone) == SoneStatus.downloading) {
+                       return;
+               }
                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" });
                core.setSoneStatus(sone, SoneStatus.downloading);
@@ -150,7 +153,16 @@ public class SoneDownloader extends AbstractService {
                try {
                        xmlBucket = fetchResult.asBucket();
                        xmlInputStream = xmlBucket.getInputStream();
-                       Document document = XML.transformToDocument(xmlInputStream);
+                       Document document;
+                       /* XML parsing is not thread-safe. */
+                       synchronized (this) {
+                               document = XML.transformToDocument(xmlInputStream);
+                       }
+                       if (document == null) {
+                               /* TODO - mark Sone as bad. */
+                               logger.log(Level.WARNING, "Could not parse XML for Sone %s at %s!", new Object[] { originalSone, requestUri });
+                               return null;
+                       }
                        SimpleXML soneXml;
                        try {
                                soneXml = SimpleXML.fromDocument(document);