2 * FreenetSone - SoneInserter.java - Copyright © 2010 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.logging.Level;
25 import java.util.logging.Logger;
27 import net.pterodactylus.sone.data.Sone;
28 import net.pterodactylus.sone.freenet.StringBucket;
29 import net.pterodactylus.util.io.Closer;
30 import net.pterodactylus.util.logging.Logging;
31 import net.pterodactylus.util.service.AbstractService;
32 import net.pterodactylus.util.template.DefaultTemplateFactory;
33 import net.pterodactylus.util.template.ReflectionAccessor;
34 import net.pterodactylus.util.template.Template;
35 import net.pterodactylus.util.template.TemplateException;
36 import freenet.client.async.ManifestElement;
37 import freenet.keys.FreenetURI;
40 * A Sone inserter is responsible for inserting a Sone if it has changed.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class SoneInserter extends AbstractService {
47 private static final Logger logger = Logging.getLogger(SoneInserter.class);
49 /** The template factory used to create the templates. */
50 private static final DefaultTemplateFactory templateFactory = new DefaultTemplateFactory();
53 templateFactory.addAccessor(Object.class, new ReflectionAccessor());
56 /** The UTF-8 charset. */
57 private static final Charset utf8Charset = Charset.forName("UTF-8");
59 /** The Freenet interface. */
60 private final FreenetInterface freenetInterface;
62 /** The Sone to insert. */
63 private final Sone sone;
66 * Creates a new Sone inserter.
68 * @param freenetInterface
69 * The freenet interface
73 public SoneInserter(FreenetInterface freenetInterface, Sone sone) {
74 super("Sone Inserter for “" + sone.getName() + "”");
75 this.freenetInterface = freenetInterface;
87 protected void serviceRun() {
88 long modificationCounter = 0;
89 boolean restartNow = true;
90 while (!shouldStop()) {
92 logger.log(Level.FINEST, "Waiting 60 seconds before checking Sone “" + sone.getName() + "”.");
96 InsertInformation insertInformation = null;
98 modificationCounter = sone.getModificationCounter();
99 if (modificationCounter > 0) {
100 insertInformation = new InsertInformation(sone.getRequestUri(), sone.getInsertUri());
103 if (insertInformation != null) {
104 logger.log(Level.INFO, "Inserting Sone “%s”…", new Object[] { sone.getName() });
106 boolean success = false;
108 FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri().setKeyType("USK").setDocName("Sone-" + sone.getName()).setSuggestedEdition(0), insertInformation.generateManifestEntries(), "index.html");
110 logger.log(Level.INFO, "Inserted Sone “%s” at %s.", new Object[] { sone.getName(), finalUri });
111 } catch (SoneException se1) {
112 logger.log(Level.WARNING, "Could not insert Sone “" + sone.getName() + "”!", se1);
116 * reset modification counter if Sone has not been modified
117 * while it was inserted.
120 synchronized (sone) {
121 if (sone.getModificationCounter() == modificationCounter) {
122 logger.log(Level.FINE, "Sone “%s” was not modified further, resetting counter…", new Object[] { sone });
123 sone.setModificationCounter(0);
125 logger.log(Level.FINE, "Sone “%s” was modified since the insert started, starting another insert…", new Object[] { sone });
135 * Container for information that are required to insert a Sone. This
136 * container merely exists to copy all relevant data without holding a lock
137 * on the {@link Sone} object for too long.
139 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
141 private class InsertInformation {
143 /** The request URI of the Sone. */
144 private final FreenetURI requestUri;
146 /** The insert URI of the Sone. */
147 private final FreenetURI insertUri;
150 * Creates a new insert information container.
153 * The request URI of the Sone
155 * The insert URI of the Sone
157 public InsertInformation(FreenetURI requestUri, FreenetURI insertUri) {
158 this.requestUri = requestUri;
159 this.insertUri = insertUri;
167 * Returns the request URI of the Sone.
169 * @return The request URI of the Sone
171 @SuppressWarnings("unused")
172 public FreenetURI getRequestUri() {
177 * Returns the insert URI of the Sone.
179 * @return The insert URI of the Sone
181 public FreenetURI getInsertUri() {
190 * Generates all manifest entries required to insert this Sone.
192 * @return The manifest entries for the Sone insert
194 public HashMap<String, Object> generateManifestEntries() {
195 HashMap<String, Object> manifestEntries = new HashMap<String, Object>();
197 /* first, create an index.html. */
198 manifestEntries.put("index.html", createManifestElement("index.html", "text/html; charset=utf-8", "/templates/insert/index.html"));
200 /* now, store the sone. */
201 manifestEntries.put("sone.xml", createManifestElement("sone.xml", "text/xml; charset=utf-8", "/templates/insert/sone.xml"));
203 return manifestEntries;
211 * Creates a new manifest element.
214 * The name of the file
216 * The content type of the file
217 * @param templateName
218 * The name of the template to render
219 * @return The manifest element
221 @SuppressWarnings("synthetic-access")
222 private ManifestElement createManifestElement(String name, String contentType, String templateName) {
223 InputStreamReader templateInputStreamReader;
224 Template template = templateFactory.createTemplate(templateInputStreamReader = new InputStreamReader(getClass().getResourceAsStream(templateName), utf8Charset));
227 } catch (TemplateException te1) {
228 logger.log(Level.SEVERE, "Could not parse template “" + templateName + "”!", te1);
231 Closer.close(templateInputStreamReader);
233 template.set("currentSone", sone);
234 StringWriter writer = new StringWriter();
235 StringBucket bucket = null;
237 template.render(writer);
238 bucket = new StringBucket(writer.toString(), utf8Charset);
239 return new ManifestElement(name, bucket, contentType, bucket.size());
240 } catch (TemplateException te1) {
241 logger.log(Level.SEVERE, "Could not render template “" + templateName + "”!", te1);
244 Closer.close(writer);
245 if (bucket != null) {