From d30a8ff8477a24f8a1f7c90924ba6d89c0c6242e Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 13 Oct 2010 21:28:35 +0200 Subject: [PATCH] Implement Sone inserting. --- .../net/pterodactylus/sone/core/SoneInserter.java | 136 ++++++++++++++++++++- src/main/resources/templates/insert/index.html | 12 ++ 2 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 src/main/resources/templates/insert/index.html diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index a8f9951..ee98cb2 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -17,12 +17,22 @@ package net.pterodactylus.sone.core; +import java.io.InputStreamReader; +import java.io.StringWriter; +import java.nio.charset.Charset; +import java.util.HashMap; import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.freenet.StringBucket; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.service.AbstractService; +import net.pterodactylus.util.template.DefaultTemplateFactory; +import net.pterodactylus.util.template.ReflectionAccessor; +import net.pterodactylus.util.template.Template; +import freenet.client.async.ManifestElement; +import freenet.keys.FreenetURI; /** * A Sone inserter is responsible for inserting a Sone if it has changed. @@ -34,6 +44,16 @@ public class SoneInserter extends AbstractService { /** The logger. */ private static final Logger logger = Logging.getLogger(SoneInserter.class); + /** The template factory used to create the templates. */ + private static final DefaultTemplateFactory templateFactory = new DefaultTemplateFactory(); + + static { + templateFactory.addAccessor(Object.class, new ReflectionAccessor()); + } + + /** The Freenet interface. */ + private final FreenetInterface freenetInterface; + /** The Sone to insert. */ private final Sone sone; @@ -47,6 +67,7 @@ public class SoneInserter extends AbstractService { */ public SoneInserter(FreenetInterface freenetInterface, Sone sone) { super("Sone Inserter for “" + sone.getName() + "”"); + this.freenetInterface = freenetInterface; this.sone = sone; } @@ -59,10 +80,37 @@ public class SoneInserter extends AbstractService { */ @Override protected void serviceRun() { + long modificationCounter = 0; while (!shouldStop()) { + InsertInformation insertInformation = null; synchronized (sone) { - if (sone.getModificationCounter() > 0) { - sone.setModificationCounter(0); + modificationCounter = sone.getModificationCounter(); + if (modificationCounter > 0) { + insertInformation = new InsertInformation(sone.getRequestUri(), sone.getInsertUri()); + } + } + if (insertInformation != null) { + logger.log(Level.INFO, "Inserting Sone “%s”…", new Object[] { sone.getName() }); + + boolean success = false; + try { + FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri().setKeyType("USK").setDocName("Sone-" + sone.getName()).setSuggestedEdition(0), insertInformation.generateManifestEntries(), "index.html"); + 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); + } + + /* + * reset modification counter if Sone has not been modified + * while it was inserted. + */ + if (success) { + synchronized (sone) { + if (sone.getModificationCounter() == modificationCounter) { + sone.setModificationCounter(0); + } + } } } logger.log(Level.FINEST, "Waiting 60 seconds before checking Sone “" + sone.getName() + "” again."); @@ -70,4 +118,88 @@ public class SoneInserter extends AbstractService { } } + /** + * Container for information that are required to insert a Sone. This + * container merely exists to copy all relevant data without holding a lock + * on the {@link Sone} object for too long. + * + * @author David ‘Bombe’ Roden + */ + private class InsertInformation { + + /** The request URI of the Sone. */ + private final FreenetURI requestUri; + + /** The insert URI of the Sone. */ + private final FreenetURI insertUri; + + /** + * Creates a new insert information container. + * + * @param requestUri + * The request URI of the Sone + * @param insertUri + * The insert URI of the Sone + */ + public InsertInformation(FreenetURI requestUri, FreenetURI insertUri) { + this.requestUri = requestUri; + this.insertUri = insertUri; + } + + // + // ACCESSORS + // + + /** + * Returns the request URI of the Sone. + * + * @return The request URI of the Sone + */ + @SuppressWarnings("unused") + public FreenetURI getRequestUri() { + return requestUri; + } + + /** + * Returns the insert URI of the Sone. + * + * @return The insert URI of the Sone + */ + public FreenetURI getInsertUri() { + return insertUri; + } + + // + // ACTIONS + // + + /** + * Generates all manifest entries required to insert this Sone. + * + * @return The manifest entries for the Sone insert + */ + @SuppressWarnings("synthetic-access") + public HashMap generateManifestEntries() { + HashMap manifestEntries = new HashMap(); + + Charset utf8Charset = Charset.forName("UTF-8"); + + /* first, create an index.html. */ + Template indexTemplate = templateFactory.createTemplate(new InputStreamReader(getClass().getResourceAsStream("/templates/insert/index.html"), utf8Charset)); + indexTemplate.set("currentSone", sone); + StringWriter indexWriter = new StringWriter(); + indexTemplate.render(indexWriter); + StringBucket indexBucket = new StringBucket(indexWriter.toString(), utf8Charset); + ManifestElement indexManifestElement = new ManifestElement("index.html", indexBucket, "text/html; charset=utf-8", indexBucket.size()); + manifestEntries.put("index.html", indexManifestElement); + + return manifestEntries; + } + + // + // PRIVATE METHODS + // + + } + } diff --git a/src/main/resources/templates/insert/index.html b/src/main/resources/templates/insert/index.html new file mode 100644 index 0000000..73e35a8 --- /dev/null +++ b/src/main/resources/templates/insert/index.html @@ -0,0 +1,12 @@ + + + Sone “<% currentSone.name|html>” - Sone + + +

Sone “<% currentSone.name|html>”

+

+ This page should not be viewed directly; it is merely a reminder + that you should install the Sone plugin. +

+ + -- 2.7.4