- 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);
- }
- }
- if (insertInformation != null) {
- logger.log(Level.INFO, "Inserting Sone “%s”…", new Object[] { sone.getName() });
-
- 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);
- success = true;
- logger.log(Level.INFO, "Inserted Sone “%s” at %s.", new Object[] { sone.getName(), finalUri });
- } catch (SoneException se1) {
- logger.log(Level.WARNING, "Could not insert Sone “" + sone.getName() + "”!", se1);
- } finally {
- core.setSoneStatus(sone, SoneStatus.idle);
- }
+ try {
+ /* check every second. */
+ sleep(delay);
+
+ if (soneModificationDetector.isEligibleForInsert()) {
+ Sone sone = core.getSone(soneId);
+ if (sone == null) {
+ logger.log(Level.WARNING, format("Sone %s has disappeared, exiting inserter.", soneId));
+ return;
+ }
+ InsertInformation insertInformation = new InsertInformation(sone);
+ logger.log(Level.INFO, String.format("Inserting Sone “%s”…", sone.getName()));
+
+ boolean success = false;
+ try {
+ sone.setStatus(SoneStatus.inserting);
+ long insertTime = currentTimeMillis();
+ eventBus.post(new SoneInsertingEvent(sone));
+ FreenetURI finalUri = freenetInterface.insertDirectory(sone.getInsertUri(), insertInformation.generateManifestEntries(), "index.html");
+ eventBus.post(new SoneInsertedEvent(sone, currentTimeMillis() - insertTime, insertInformation.getFingerprint()));
+ /* at this point we might already be stopped. */
+ if (shouldStop()) {
+ /* if so, bail out, don’t change anything. */
+ break;
+ }
+ sone.setTime(insertTime);
+ sone.setLatestEdition(finalUri.getEdition());
+ core.touchConfiguration();
+ success = true;
+ logger.log(Level.INFO, String.format("Inserted Sone “%s” at %s.", sone.getName(), finalUri));
+ } catch (SoneException se1) {
+ eventBus.post(new SoneInsertAbortedEvent(sone, se1));
+ logger.log(Level.WARNING, String.format("Could not insert Sone “%s”!", sone.getName()), se1);
+ } finally {
+ insertInformation.close();
+ sone.setStatus(SoneStatus.idle);
+ }