Merge branch 'master' into next
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneInserter.java
index 4bb4868..05e3600 100644 (file)
@@ -57,6 +57,9 @@ public class SoneInserter extends AbstractService {
        /** The logger. */
        private static final Logger logger = Logging.getLogger(SoneInserter.class);
 
+       /** The insertion delay (in seconds). */
+       private static volatile int insertionDelay = 60;
+
        /** The template factory used to create the templates. */
        private static final DefaultTemplateFactory templateFactory = new DefaultTemplateFactory();
 
@@ -95,6 +98,21 @@ public class SoneInserter extends AbstractService {
        }
 
        //
+       // ACCESSORS
+       //
+
+       /**
+        * Changes the insertion delay, i.e. the time the Sone inserter waits after
+        * it has noticed a Sone modification before it starts the insert.
+        *
+        * @param insertionDelay
+        *            The insertion delay (in seconds)
+        */
+       public static void setInsertionDelay(int insertionDelay) {
+               SoneInserter.insertionDelay = insertionDelay;
+       }
+
+       //
        // SERVICE METHODS
        //
 
@@ -115,10 +133,10 @@ public class SoneInserter extends AbstractService {
                                        modificationCounter = sone.getModificationCounter();
                                        lastModificationTime = System.currentTimeMillis();
                                        sone.setTime(lastModificationTime);
-                                       logger.log(Level.FINE, "Sone %s has been modified, waiting 60 seconds before inserting.", new Object[] { sone.getName() });
-                                       if ((System.currentTimeMillis() - lastModificationTime) > (60 * 1000)) {
-                                               insertInformation = new InsertInformation(sone);
-                                       }
+                                       logger.log(Level.FINE, "Sone %s has been modified, waiting %d seconds before inserting.", new Object[] { sone.getName(), insertionDelay });
+                               }
+                               if ((lastModificationTime > 0) && ((System.currentTimeMillis() - lastModificationTime) > (insertionDelay * 1000))) {
+                                       insertInformation = new InsertInformation(sone);
                                }
                        }
 
@@ -128,8 +146,8 @@ public class SoneInserter extends AbstractService {
                                boolean success = false;
                                try {
                                        core.setSoneStatus(sone, SoneStatus.inserting);
-                                       FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri().setKeyType("USK").setDocName("Sone-" + sone.getName()).setSuggestedEdition(0), insertInformation.generateManifestEntries(), "index.html");
-                                       sone.updateUris(finalUri);
+                                       FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri().setKeyType("USK").setSuggestedEdition(0), insertInformation.generateManifestEntries(), "index.html");
+                                       sone.updateUris(finalUri.getEdition());
                                        success = true;
                                        logger.log(Level.INFO, "Inserted Sone “%s” at %s.", new Object[] { sone.getName(), finalUri });
                                } catch (SoneException se1) {
@@ -148,6 +166,7 @@ public class SoneInserter extends AbstractService {
                                                        logger.log(Level.FINE, "Sone “%s” was not modified further, resetting counter…", new Object[] { sone });
                                                        sone.setModificationCounter(0);
                                                        modificationCounter = 0;
+                                                       lastModificationTime = 0;
                                                }
                                        }
                                }