Always download Sones in an own thread.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index eb517fc..6225087 100644 (file)
@@ -19,6 +19,7 @@ package net.pterodactylus.sone.core;
 
 import java.net.MalformedURLException;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
@@ -141,6 +142,15 @@ public class Core extends AbstractService {
        }
 
        /**
+        * Returns all known sones.
+        *
+        * @return All known sones
+        */
+       public Collection<Sone> getKnownSones() {
+               return soneCache.values();
+       }
+
+       /**
         * Creates a new post.
         *
         * @param sone
@@ -216,6 +226,7 @@ public class Core extends AbstractService {
         *            The Sone to watch for updates
         */
        public void addSone(Sone sone) {
+               soneCache.put(sone.getId(), sone);
                soneDownloader.addSone(sone);
        }
 
@@ -297,13 +308,24 @@ public class Core extends AbstractService {
         * @param requestUri
         *            The request URI to load the Sone from
         */
-       public void loadSone(String requestUri) {
-               try {
-                       FetchResult fetchResult = freenetInterface.fetchUri(new FreenetURI(requestUri).setMetaString(new String[] { "sone.xml" }));
-                       soneDownloader.parseSone(null, fetchResult);
-               } catch (MalformedURLException mue1) {
-                       logger.log(Level.INFO, "Could not create URI from “" + requestUri + "”.", mue1);
-               }
+       public void loadSone(final String requestUri) {
+               new Thread(new Runnable() {
+
+                       @Override
+                       @SuppressWarnings("synthetic-access")
+                       public void run() {
+                               try {
+                                       FreenetURI realRequestUri = new FreenetURI(requestUri).setMetaString(new String[] { "sone.xml" });
+                                       FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
+                                       Sone parsedSone = soneDownloader.parseSone(null, fetchResult, realRequestUri);
+                                       if (parsedSone != null) {
+                                               addSone(parsedSone);
+                                       }
+                               } catch (MalformedURLException mue1) {
+                                       logger.log(Level.INFO, "Could not create URI from “" + requestUri + "”.", mue1);
+                               }
+                       }
+               }, "Sone Downloader").start();
        }
 
        /**