Don’t modify a Sone when the sone inserter was stopped before the insert finished.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneInserter.java
index ea85f2d..5b281f6 100644 (file)
@@ -143,9 +143,12 @@ public class SoneInserter extends AbstractService {
                                try {
                                        core.setSoneStatus(sone, SoneStatus.inserting);
                                        FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri().setKeyType("USK").setSuggestedEdition(0), insertInformation.generateManifestEntries(), "index.html");
+                                       /* at this point we might already be stopped. */
+                                       if (shouldStop()) {
+                                               /* if so, bail out, don’t change anything. */
+                                               break;
+                                       }
                                        sone.setLatestEdition(finalUri.getEdition());
-                                       /* TODO - better encapsulation? */
-                                       core.saveSone(sone);
                                        success = true;
                                        logger.log(Level.INFO, "Inserted Sone “%s” at %s.", new Object[] { sone.getName(), finalUri });
                                } catch (SoneException se1) {
@@ -163,6 +166,7 @@ public class SoneInserter extends AbstractService {
                                                if (sone.getModificationCounter() == modificationCounter) {
                                                        logger.log(Level.FINE, "Sone “%s” was not modified further, resetting counter…", new Object[] { sone });
                                                        sone.setModificationCounter(0);
+                                                       core.saveSone(sone);
                                                        modificationCounter = 0;
                                                        lastModificationTime = 0;
                                                }