Implement Sone inserting.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 13 Oct 2010 19:28:35 +0000 (21:28 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 13 Oct 2010 19:28:35 +0000 (21:28 +0200)
src/main/java/net/pterodactylus/sone/core/SoneInserter.java
src/main/resources/templates/insert/index.html [new file with mode: 0644]

index a8f9951..ee98cb2 100644 (file)
 
 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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       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<String, Object> generateManifestEntries() {
+                       HashMap<String, Object> manifestEntries = new HashMap<String, Object>();
+
+                       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 (file)
index 0000000..73e35a8
--- /dev/null
@@ -0,0 +1,12 @@
+<html>
+       <head>
+               <title>Sone “<% currentSone.name|html>” - Sone</title>
+       </head>
+       <body>
+               <h1>Sone “<% currentSone.name|html>”</h1>
+               <p>
+                       This page should not be viewed directly; it is merely a reminder
+                       that you should install the <i>Sone</i> plugin.
+               </p>
+       </body>
+</html>