X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneInserter.java;h=44f8bbcd51ca6c13f825b371f4f3ab9b9f7bd48c;hb=f46d58c2917d46095a31f6281cf8b7288a527cba;hp=05fa4b4d605503d01d63506e57bdef5fd9b01e75;hpb=ee05a67b3f1e796b6c4bdcd709ef0983103c455f;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 05fa4b4..44f8bbc 100644
--- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java
+++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java
@@ -1,5 +1,5 @@
/*
- * FreenetSone - SoneInserter.java - Copyright © 2010 David Roden
+ * Sone - SoneInserter.java - Copyright © 2010 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -33,6 +33,8 @@ import net.pterodactylus.sone.data.Reply;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.freenet.StringBucket;
import net.pterodactylus.sone.main.SonePlugin;
+import net.pterodactylus.util.collection.ListBuilder;
+import net.pterodactylus.util.collection.ReverseComparator;
import net.pterodactylus.util.io.Closer;
import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.service.AbstractService;
@@ -81,6 +83,9 @@ public class SoneInserter extends AbstractService {
/** The Sone to insert. */
private final Sone sone;
+ /** The insert listener manager. */
+ private SoneInsertListenerManager soneInsertListenerManager;
+
/** Whether a modification has been detected. */
private volatile boolean modified = false;
@@ -102,6 +107,31 @@ public class SoneInserter extends AbstractService {
this.core = core;
this.freenetInterface = freenetInterface;
this.sone = sone;
+ this.soneInsertListenerManager = new SoneInsertListenerManager(sone);
+ }
+
+ //
+ // LISTENER MANAGEMENT
+ //
+
+ /**
+ * Adds a listener for Sone insert events.
+ *
+ * @param soneInsertListener
+ * The Sone insert listener
+ */
+ public void addSoneInsertListener(SoneInsertListener soneInsertListener) {
+ soneInsertListenerManager.addListener(soneInsertListener);
+ }
+
+ /**
+ * Removes a listener for Sone insert events.
+ *
+ * @param soneInsertListener
+ * The Sone insert listener
+ */
+ public void removeSoneInsertListener(SoneInsertListener soneInsertListener) {
+ soneInsertListenerManager.removeListener(soneInsertListener);
}
//
@@ -160,7 +190,7 @@ public class SoneInserter extends AbstractService {
protected void serviceRun() {
long lastModificationTime = 0;
String lastFingerprint = "";
- while (!shouldStop()) {
+ while (!shouldStop()) { try {
/* check every seconds. */
sleep(1000);
@@ -204,7 +234,9 @@ public class SoneInserter extends AbstractService {
core.setSoneStatus(sone, SoneStatus.inserting);
long insertTime = System.currentTimeMillis();
insertInformation.setTime(insertTime);
+ soneInsertListenerManager.fireInsertStarted();
FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri(), insertInformation.generateManifestEntries(), "index.html");
+ soneInsertListenerManager.fireInsertFinished(System.currentTimeMillis() - insertTime);
/* at this point we might already be stopped. */
if (shouldStop()) {
/* if so, bail out, donât change anything. */
@@ -212,10 +244,11 @@ public class SoneInserter extends AbstractService {
}
sone.setTime(insertTime);
sone.setLatestEdition(finalUri.getEdition());
- core.saveSone(sone);
+ core.touchConfiguration();
success = true;
logger.log(Level.INFO, "Inserted Sone â%sâ at %s.", new Object[] { sone.getName(), finalUri });
} catch (SoneException se1) {
+ soneInsertListenerManager.fireInsertAborted(se1);
logger.log(Level.WARNING, "Could not insert Sone â" + sone.getName() + "â!", se1);
} finally {
core.setSoneStatus(sone, SoneStatus.idle);
@@ -229,14 +262,15 @@ public class SoneInserter extends AbstractService {
synchronized (sone) {
if (lastInsertFingerprint.equals(sone.getFingerprint())) {
logger.log(Level.FINE, "Sone â%sâ was not modified further, resetting counterâ¦", new Object[] { sone });
- core.saveSone(sone);
lastModificationTime = 0;
modified = false;
}
}
}
}
- }
+ } catch (Throwable t1) {
+ logger.log(Level.SEVERE, "SoneInserter threw an Exception!", t1);
+ }}
}
/**
@@ -246,7 +280,7 @@ public class SoneInserter extends AbstractService {
*
* @author David âBombeâ Roden
*/
- private static class InsertInformation {
+ private class InsertInformation {
/** All properties of the Sone, copied for thread safety. */
private final Map soneProperties = new HashMap();
@@ -264,10 +298,11 @@ public class SoneInserter extends AbstractService {
soneProperties.put("requestUri", sone.getRequestUri());
soneProperties.put("insertUri", sone.getInsertUri());
soneProperties.put("profile", sone.getProfile());
- soneProperties.put("posts", new ArrayList(sone.getPosts()));
- soneProperties.put("replies", new HashSet(sone.getReplies()));
+ soneProperties.put("posts", new ListBuilder(new ArrayList(sone.getPosts())).sort(Post.TIME_COMPARATOR).get());
+ soneProperties.put("replies", new ListBuilder(new ArrayList(sone.getReplies())).sort(new ReverseComparator(Reply.TIME_COMPARATOR)).get());
soneProperties.put("likedPostIds", new HashSet(sone.getLikedPostIds()));
soneProperties.put("likedReplyIds", new HashSet(sone.getLikedReplyIds()));
+ soneProperties.put("albums", Sone.flattenAlbums(sone.getAlbums()));
}
//
@@ -344,7 +379,9 @@ public class SoneInserter extends AbstractService {
}
TemplateContext templateContext = templateContextFactory.createTemplateContext();
+ templateContext.set("core", core);
templateContext.set("currentSone", soneProperties);
+ templateContext.set("currentEdition", core.getUpdateChecker().getLatestEdition());
templateContext.set("version", SonePlugin.VERSION);
StringWriter writer = new StringWriter();
StringBucket bucket = null;