Insert known Sones, too.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneInserter.java
index 307037c..ba6f437 100644 (file)
@@ -20,12 +20,15 @@ package net.pterodactylus.sone.core;
 import java.io.InputStreamReader;
 import java.io.StringWriter;
 import java.nio.charset.Charset;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.freenet.StringBucket;
+import net.pterodactylus.util.filter.Filter;
+import net.pterodactylus.util.filter.Filters;
 import net.pterodactylus.util.io.Closer;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.service.AbstractService;
@@ -33,6 +36,7 @@ import net.pterodactylus.util.template.DefaultTemplateFactory;
 import net.pterodactylus.util.template.ReflectionAccessor;
 import net.pterodactylus.util.template.Template;
 import net.pterodactylus.util.template.TemplateException;
+import net.pterodactylus.util.template.XmlFilter;
 import freenet.client.async.ManifestElement;
 import freenet.keys.FreenetURI;
 
@@ -51,11 +55,15 @@ public class SoneInserter extends AbstractService {
 
        static {
                templateFactory.addAccessor(Object.class, new ReflectionAccessor());
+               templateFactory.addFilter("xml", new XmlFilter());
        }
 
        /** The UTF-8 charset. */
        private static final Charset utf8Charset = Charset.forName("UTF-8");
 
+       /** The core. */
+       private final Core core;
+
        /** The Freenet interface. */
        private final FreenetInterface freenetInterface;
 
@@ -65,13 +73,16 @@ public class SoneInserter extends AbstractService {
        /**
         * Creates a new Sone inserter.
         *
+        * @param core
+        *            The core
         * @param freenetInterface
         *            The freenet interface
         * @param sone
         *            The Sone to insert
         */
-       public SoneInserter(FreenetInterface freenetInterface, Sone sone) {
+       public SoneInserter(Core core, FreenetInterface freenetInterface, Sone sone) {
                super("Sone Inserter for “" + sone.getName() + "”");
+               this.core = core;
                this.freenetInterface = freenetInterface;
                this.sone = sone;
        }
@@ -86,11 +97,18 @@ public class SoneInserter extends AbstractService {
        @Override
        protected void serviceRun() {
                long modificationCounter = 0;
+               boolean restartNow = true;
                while (!shouldStop()) {
+                       if (!restartNow) {
+                               logger.log(Level.FINEST, "Waiting 60 seconds before checking Sone “" + sone.getName() + "”.");
+                               sleep(60 * 1000);
+                       }
+                       restartNow = false;
                        InsertInformation insertInformation = null;
                        synchronized (sone) {
                                modificationCounter = sone.getModificationCounter();
                                if (modificationCounter > 0) {
+                                       sone.setTime(System.currentTimeMillis());
                                        insertInformation = new InsertInformation(sone.getRequestUri(), sone.getInsertUri());
                                }
                        }
@@ -100,6 +118,7 @@ public class SoneInserter extends AbstractService {
                                boolean success = false;
                                try {
                                        FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri().setKeyType("USK").setDocName("Sone-" + sone.getName()).setSuggestedEdition(0), insertInformation.generateManifestEntries(), "index.html");
+                                       sone.updateUris(finalUri);
                                        success = true;
                                        logger.log(Level.INFO, "Inserted Sone “%s” at %s.", new Object[] { sone.getName(), finalUri });
                                } catch (SoneException se1) {
@@ -117,12 +136,11 @@ public class SoneInserter extends AbstractService {
                                                        sone.setModificationCounter(0);
                                                } else {
                                                        logger.log(Level.FINE, "Sone “%s” was modified since the insert started, starting another insert…", new Object[] { sone });
+                                                       restartNow = true;
                                                }
                                        }
                                }
                        }
-                       logger.log(Level.FINEST, "Waiting 60 seconds before checking Sone “" + sone.getName() + "” again.");
-                       sleep(60 * 1000);
                }
        }
 
@@ -225,7 +243,19 @@ public class SoneInserter extends AbstractService {
                        } finally {
                                Closer.close(templateInputStreamReader);
                        }
+                       Collection<Sone> knownSones = Filters.filteredCollection(core.getKnownSones(), new Filter<Sone>() {
+
+                               /**
+                                * {@inheritDoc}
+                                */
+                               @Override
+                               public boolean filterObject(Sone object) {
+                                       return !sone.isSoneBlocked(object.getId()) && !object.equals(sone);
+                               }
+                       });
+
                        template.set("currentSone", sone);
+                       template.set("knownSones", knownSones);
                        StringWriter writer = new StringWriter();
                        StringBucket bucket = null;
                        try {