Don’t set insert URI of a Sone, let it be generated from the identity.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneInserter.java
index 46a3fe7..9f1f164 100644 (file)
@@ -17,7 +17,7 @@
 
 package net.pterodactylus.sone.core;
 
-import static com.google.common.base.Preconditions.checkArgument;
+import static java.lang.String.format;
 import static java.lang.System.currentTimeMillis;
 import static net.pterodactylus.sone.data.Album.NOT_EMPTY;
 
@@ -34,6 +34,7 @@ import java.util.logging.Logger;
 
 import net.pterodactylus.sone.core.Options.Option;
 import net.pterodactylus.sone.core.Options.OptionWatcher;
+import net.pterodactylus.sone.core.SoneModificationDetector.LockableFingerprintProvider;
 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
@@ -57,6 +58,7 @@ import net.pterodactylus.util.template.TemplateParser;
 import net.pterodactylus.util.template.XmlFilter;
 
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Optional;
 import com.google.common.collect.FluentIterable;
 import com.google.common.collect.Ordering;
 import com.google.common.eventbus.EventBus;
@@ -99,9 +101,8 @@ public class SoneInserter extends AbstractService {
        private final FreenetInterface freenetInterface;
 
        private final SoneModificationDetector soneModificationDetector;
-
-       /** The Sone to insert. */
-       private volatile Sone sone;
+       private final long delay;
+       private final String soneId;
 
        /**
         * Creates a new Sone inserter.
@@ -112,35 +113,46 @@ public class SoneInserter extends AbstractService {
         *            The event bus
         * @param freenetInterface
         *            The freenet interface
-        * @param sone
-        *            The Sone to insert
+        * @param soneId
+        *            The ID of the Sone to insert
         */
-       public SoneInserter(Core core, EventBus eventBus, FreenetInterface freenetInterface, Sone sone) {
-               super("Sone Inserter for “" + sone.getName() + "”", false);
+       public SoneInserter(final Core core, EventBus eventBus, FreenetInterface freenetInterface, final String soneId) {
+               this(core, eventBus, freenetInterface, soneId, new SoneModificationDetector(new LockableFingerprintProvider() {
+                       @Override
+                       public boolean isLocked() {
+                               final Optional<Sone> sone = core.getSone(soneId);
+                               if (!sone.isPresent()) {
+                                       return false;
+                               }
+                               return core.isLocked(sone.get());
+                       }
+
+                       @Override
+                       public String getFingerprint() {
+                               final Optional<Sone> sone = core.getSone(soneId);
+                               if (!sone.isPresent()) {
+                                       return null;
+                               }
+                               return sone.get().getFingerprint();
+                       }
+               }, insertionDelay), 1000);
+       }
+
+       @VisibleForTesting
+       SoneInserter(Core core, EventBus eventBus, FreenetInterface freenetInterface, String soneId, SoneModificationDetector soneModificationDetector, long delay) {
+               super("Sone Inserter for “" + soneId + "”", false);
                this.core = core;
                this.eventBus = eventBus;
                this.freenetInterface = freenetInterface;
-               this.sone = sone;
-               this.soneModificationDetector = new SoneModificationDetector(core, sone, insertionDelay);
+               this.soneId = soneId;
+               this.soneModificationDetector = soneModificationDetector;
+               this.delay = delay;
        }
 
        //
        // ACCESSORS
        //
 
-       /**
-        * Sets the Sone to insert.
-        *
-        * @param sone
-        *              The Sone to insert
-        * @return This Sone inserter
-        */
-       public SoneInserter setSone(Sone sone) {
-               checkArgument((this.sone == null) || sone.equals(this.sone), "Sone to insert can not be set to a different Sone");
-               this.sone = sone;
-               return this;
-       }
-
        @VisibleForTesting
        static AtomicInteger getInsertionDelay() {
                return insertionDelay;
@@ -199,9 +211,15 @@ public class SoneInserter extends AbstractService {
                while (!shouldStop()) {
                        try {
                                /* check every second. */
-                               sleep(1000);
+                               sleep(delay);
 
                                if (soneModificationDetector.isEligibleForInsert()) {
+                                       Optional<Sone> soneOptional = core.getSone(soneId);
+                                       if (!soneOptional.isPresent()) {
+                                               logger.log(Level.WARNING, format("Sone %s has disappeared, exiting inserter.", soneId));
+                                               return;
+                                       }
+                                       Sone sone = soneOptional.get();
                                        InsertInformation insertInformation = new InsertInformation(sone);
                                        logger.log(Level.INFO, String.format("Inserting Sone “%s”…", sone.getName()));
 
@@ -210,7 +228,7 @@ public class SoneInserter extends AbstractService {
                                                sone.setStatus(SoneStatus.inserting);
                                                long insertTime = currentTimeMillis();
                                                eventBus.post(new SoneInsertingEvent(sone));
-                                               FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri(), insertInformation.generateManifestEntries(), "index.html");
+                                               FreenetURI finalUri = freenetInterface.insertDirectory(sone.getInsertUri(), insertInformation.generateManifestEntries(), "index.html");
                                                eventBus.post(new SoneInsertedEvent(sone, currentTimeMillis() - insertTime));
                                                /* at this point we might already be stopped. */
                                                if (shouldStop()) {
@@ -265,7 +283,8 @@ public class SoneInserter extends AbstractService {
         *
         * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
         */
-       private class InsertInformation {
+       @VisibleForTesting
+       class InsertInformation {
 
                private final String fingerprint;
 
@@ -284,7 +303,6 @@ public class SoneInserter extends AbstractService {
                        soneProperties.put("name", sone.getName());
                        soneProperties.put("time", currentTimeMillis());
                        soneProperties.put("requestUri", sone.getRequestUri());
-                       soneProperties.put("insertUri", sone.getInsertUri());
                        soneProperties.put("profile", sone.getProfile());
                        soneProperties.put("posts", Ordering.from(Post.TIME_COMPARATOR).sortedCopy(sone.getPosts()));
                        soneProperties.put("replies", Ordering.from(Reply.TIME_COMPARATOR).reverse().sortedCopy(sone.getReplies()));
@@ -297,19 +315,11 @@ public class SoneInserter extends AbstractService {
                // ACCESSORS
                //
 
-               private String getFingerprint() {
+               @VisibleForTesting
+               String getFingerprint() {
                        return fingerprint;
                }
 
-               /**
-                * Returns the insert URI of the Sone.
-                *
-                * @return The insert URI of the Sone
-                */
-               public FreenetURI getInsertUri() {
-                       return (FreenetURI) soneProperties.get("insertUri");
-               }
-
                //
                // ACTIONS
                //