2 * Sone - SoneInserter.java - Copyright © 2010–2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.core;
20 import java.io.InputStreamReader;
21 import java.io.StringWriter;
22 import java.nio.charset.Charset;
23 import java.util.HashMap;
24 import java.util.HashSet;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
29 import net.pterodactylus.sone.core.event.SoneInsertAbortedEvent;
30 import net.pterodactylus.sone.core.event.SoneInsertedEvent;
31 import net.pterodactylus.sone.core.event.SoneInsertingEvent;
32 import net.pterodactylus.sone.data.Post;
33 import net.pterodactylus.sone.data.Reply;
34 import net.pterodactylus.sone.data.Sone;
35 import net.pterodactylus.sone.data.Sone.SoneStatus;
36 import net.pterodactylus.sone.freenet.StringBucket;
37 import net.pterodactylus.sone.main.SonePlugin;
38 import net.pterodactylus.util.io.Closer;
39 import net.pterodactylus.util.logging.Logging;
40 import net.pterodactylus.util.service.AbstractService;
41 import net.pterodactylus.util.template.HtmlFilter;
42 import net.pterodactylus.util.template.ReflectionAccessor;
43 import net.pterodactylus.util.template.Template;
44 import net.pterodactylus.util.template.TemplateContext;
45 import net.pterodactylus.util.template.TemplateContextFactory;
46 import net.pterodactylus.util.template.TemplateException;
47 import net.pterodactylus.util.template.TemplateParser;
48 import net.pterodactylus.util.template.XmlFilter;
50 import com.google.common.collect.Ordering;
51 import com.google.common.eventbus.EventBus;
53 import freenet.client.async.ManifestElement;
54 import freenet.keys.FreenetURI;
57 * A Sone inserter is responsible for inserting a Sone if it has changed.
59 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
61 public class SoneInserter extends AbstractService {
64 private static final Logger logger = Logging.getLogger(SoneInserter.class);
66 /** The insertion delay (in seconds). */
67 private static volatile int insertionDelay = 60;
69 /** The template factory used to create the templates. */
70 private static final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
73 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
74 templateContextFactory.addFilter("xml", new XmlFilter());
75 templateContextFactory.addFilter("html", new HtmlFilter());
78 /** The UTF-8 charset. */
79 private static final Charset utf8Charset = Charset.forName("UTF-8");
82 private final Core core;
85 private final EventBus eventBus;
87 /** The Freenet interface. */
88 private final FreenetInterface freenetInterface;
90 /** The Sone to insert. */
91 private final Sone sone;
93 /** Whether a modification has been detected. */
94 private volatile boolean modified = false;
96 /** The fingerprint of the last insert. */
97 private volatile String lastInsertFingerprint;
100 * Creates a new Sone inserter.
106 * @param freenetInterface
107 * The freenet interface
111 public SoneInserter(Core core, EventBus eventBus, FreenetInterface freenetInterface, Sone sone) {
112 super("Sone Inserter for “" + sone.getName() + "”", false);
114 this.eventBus = eventBus;
115 this.freenetInterface = freenetInterface;
124 * Changes the insertion delay, i.e. the time the Sone inserter waits after
125 * it has noticed a Sone modification before it starts the insert.
127 * @param insertionDelay
128 * The insertion delay (in seconds)
130 public static void setInsertionDelay(int insertionDelay) {
131 SoneInserter.insertionDelay = insertionDelay;
135 * Returns the fingerprint of the last insert.
137 * @return The fingerprint of the last insert
139 public String getLastInsertFingerprint() {
140 return lastInsertFingerprint;
144 * Sets the fingerprint of the last insert.
146 * @param lastInsertFingerprint
147 * The fingerprint of the last insert
149 public void setLastInsertFingerprint(String lastInsertFingerprint) {
150 this.lastInsertFingerprint = lastInsertFingerprint;
154 * Returns whether the Sone inserter has detected a modification of the
157 * @return {@code true} if the Sone has been modified, {@code false}
160 public boolean isModified() {
172 protected void serviceRun() {
173 long lastModificationTime = 0;
174 String lastInsertedFingerprint = lastInsertFingerprint;
175 String lastFingerprint = "";
176 while (!shouldStop()) {
178 /* check every seconds. */
181 /* don’t insert locked Sones. */
182 if (core.isLocked(sone)) {
183 /* trigger redetection when the Sone is unlocked. */
184 synchronized (sone) {
185 modified = !sone.getFingerprint().equals(lastInsertedFingerprint);
187 lastFingerprint = "";
188 lastModificationTime = 0;
192 InsertInformation insertInformation = null;
193 synchronized (sone) {
194 String fingerprint = sone.getFingerprint();
195 if (!fingerprint.equals(lastFingerprint)) {
196 if (fingerprint.equals(lastInsertedFingerprint)) {
198 lastModificationTime = 0;
199 logger.log(Level.FINE, String.format("Sone %s has been reverted to last insert state.", sone));
201 lastModificationTime = System.currentTimeMillis();
203 logger.log(Level.FINE, String.format("Sone %s has been modified, waiting %d seconds before inserting.", sone.getName(), insertionDelay));
205 lastFingerprint = fingerprint;
207 if (modified && (lastModificationTime > 0) && ((System.currentTimeMillis() - lastModificationTime) > (insertionDelay * 1000))) {
208 lastInsertedFingerprint = fingerprint;
209 insertInformation = new InsertInformation(sone);
213 if (insertInformation != null) {
214 logger.log(Level.INFO, String.format("Inserting Sone “%s”…", sone.getName()));
216 boolean success = false;
218 sone.setStatus(SoneStatus.inserting);
219 long insertTime = System.currentTimeMillis();
220 insertInformation.setTime(insertTime);
221 eventBus.post(new SoneInsertingEvent(sone));
222 FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri(), insertInformation.generateManifestEntries(), "index.html");
223 eventBus.post(new SoneInsertedEvent(sone, System.currentTimeMillis() - insertTime));
224 /* at this point we might already be stopped. */
226 /* if so, bail out, don’t change anything. */
229 sone.setTime(insertTime);
230 sone.setLatestEdition(finalUri.getEdition());
231 core.touchConfiguration();
233 logger.log(Level.INFO, String.format("Inserted Sone “%s” at %s.", sone.getName(), finalUri));
234 } catch (SoneException se1) {
235 eventBus.post(new SoneInsertAbortedEvent(sone, se1));
236 logger.log(Level.WARNING, String.format("Could not insert Sone “%s”!", sone.getName()), se1);
238 sone.setStatus(SoneStatus.idle);
242 * reset modification counter if Sone has not been modified
243 * while it was inserted.
246 synchronized (sone) {
247 if (lastInsertedFingerprint.equals(sone.getFingerprint())) {
248 logger.log(Level.FINE, String.format("Sone “%s” was not modified further, resetting counter…", sone));
249 lastModificationTime = 0;
250 lastInsertFingerprint = lastInsertedFingerprint;
251 core.touchConfiguration();
257 } catch (Throwable t1) {
258 logger.log(Level.SEVERE, "SoneInserter threw an Exception!", t1);
264 * Container for information that are required to insert a Sone. This
265 * container merely exists to copy all relevant data without holding a lock
266 * on the {@link Sone} object for too long.
268 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
270 private class InsertInformation {
272 /** All properties of the Sone, copied for thread safety. */
273 private final Map<String, Object> soneProperties = new HashMap<String, Object>();
276 * Creates a new insert information container.
281 public InsertInformation(Sone sone) {
282 soneProperties.put("id", sone.getId());
283 soneProperties.put("name", sone.getName());
284 soneProperties.put("time", sone.getTime());
285 soneProperties.put("requestUri", sone.getRequestUri());
286 soneProperties.put("insertUri", sone.getInsertUri());
287 soneProperties.put("profile", sone.getProfile());
288 soneProperties.put("posts", Ordering.from(Post.TIME_COMPARATOR).sortedCopy(sone.getPosts()));
289 soneProperties.put("replies", Ordering.from(Reply.TIME_COMPARATOR).reverse().sortedCopy(sone.getReplies()));
290 soneProperties.put("likedPostIds", new HashSet<String>(sone.getLikedPostIds()));
291 soneProperties.put("likedReplyIds", new HashSet<String>(sone.getLikedReplyIds()));
292 soneProperties.put("albums", sone.getAllAlbums());
300 * Returns the insert URI of the Sone.
302 * @return The insert URI of the Sone
304 public FreenetURI getInsertUri() {
305 return (FreenetURI) soneProperties.get("insertUri");
309 * Sets the time of the Sone at the time of the insert.
312 * The time of the Sone
314 public void setTime(long time) {
315 soneProperties.put("time", time);
323 * Generates all manifest entries required to insert this Sone.
325 * @return The manifest entries for the Sone insert
327 public HashMap<String, Object> generateManifestEntries() {
328 HashMap<String, Object> manifestEntries = new HashMap<String, Object>();
330 /* first, create an index.html. */
331 manifestEntries.put("index.html", createManifestElement("index.html", "text/html; charset=utf-8", "/templates/insert/index.html"));
333 /* now, store the sone. */
334 manifestEntries.put("sone.xml", createManifestElement("sone.xml", "text/xml; charset=utf-8", "/templates/insert/sone.xml"));
336 return manifestEntries;
344 * Creates a new manifest element.
347 * The name of the file
349 * The content type of the file
350 * @param templateName
351 * The name of the template to render
352 * @return The manifest element
354 @SuppressWarnings("synthetic-access")
355 private ManifestElement createManifestElement(String name, String contentType, String templateName) {
356 InputStreamReader templateInputStreamReader = null;
359 templateInputStreamReader = new InputStreamReader(getClass().getResourceAsStream(templateName), utf8Charset);
360 template = TemplateParser.parse(templateInputStreamReader);
361 } catch (TemplateException te1) {
362 logger.log(Level.SEVERE, String.format("Could not parse template “%s”!", templateName), te1);
365 Closer.close(templateInputStreamReader);
368 TemplateContext templateContext = templateContextFactory.createTemplateContext();
369 templateContext.set("core", core);
370 templateContext.set("currentSone", soneProperties);
371 templateContext.set("currentEdition", core.getUpdateChecker().getLatestEdition());
372 templateContext.set("version", SonePlugin.VERSION);
373 StringWriter writer = new StringWriter();
374 StringBucket bucket = null;
376 template.render(templateContext, writer);
377 bucket = new StringBucket(writer.toString(), utf8Charset);
378 return new ManifestElement(name, bucket, contentType, bucket.size());
379 } catch (TemplateException te1) {
380 logger.log(Level.SEVERE, String.format("Could not render template “%s”!", templateName), te1);
383 Closer.close(writer);
384 if (bucket != null) {