X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneInserter.java;h=9fad0533f5ad04f457143ff91080cb38586c7120;hb=1c0a2b5e67dda41e75d2315fd0f6f1cfecf26fa6;hp=60eebecd1690a70c04778cee79cf9ec896a8afa4;hpb=909ee198273c541c42b411bd4f169aee939e5ca4;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index 60eebec..9fad053 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -18,6 +18,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; @@ -32,6 +33,9 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; 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; @@ -54,6 +58,8 @@ import net.pterodactylus.util.template.TemplateException; 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; @@ -96,9 +102,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. @@ -109,33 +114,49 @@ 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 = core.getSone(soneId); + if (!sone.isPresent()) { + return false; + } + return core.isLocked(sone.get()); + } + + @Override + public String getFingerprint() { + final Optional 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; } /** @@ -191,9 +212,15 @@ public class SoneInserter extends AbstractService { while (!shouldStop()) { try { /* check every second. */ - sleep(1000); + sleep(delay); if (soneModificationDetector.isEligibleForInsert()) { + Optional 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())); @@ -241,6 +268,15 @@ public class SoneInserter extends AbstractService { } } + static class SetInsertionDelay implements OptionWatcher { + + @Override + public void optionChanged(Option option, Integer oldValue, Integer newValue) { + setInsertionDelay(newValue); + } + + } + /** * Container for information that are required to insert a Sone. This * container merely exists to copy all relevant data without holding a lock @@ -248,7 +284,8 @@ public class SoneInserter extends AbstractService { * * @author David ‘Bombe’ Roden */ - private class InsertInformation { + @VisibleForTesting + class InsertInformation { private final String fingerprint; @@ -280,7 +317,8 @@ public class SoneInserter extends AbstractService { // ACCESSORS // - private String getFingerprint() { + @VisibleForTesting + String getFingerprint() { return fingerprint; }