Add post store interface.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / SoneInserter.java
1 /*
2  * Sone - SoneInserter.java - Copyright © 2010–2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
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;
25 import java.util.Map;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28
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;
49
50 import com.google.common.collect.Ordering;
51 import com.google.common.eventbus.EventBus;
52
53 import freenet.client.async.ManifestElement;
54 import freenet.keys.FreenetURI;
55
56 /**
57  * A Sone inserter is responsible for inserting a Sone if it has changed.
58  *
59  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
60  */
61 public class SoneInserter extends AbstractService {
62
63         /** The logger. */
64         private static final Logger logger = Logging.getLogger(SoneInserter.class);
65
66         /** The insertion delay (in seconds). */
67         private static volatile int insertionDelay = 60;
68
69         /** The template factory used to create the templates. */
70         private static final TemplateContextFactory templateContextFactory = new TemplateContextFactory();
71
72         static {
73                 templateContextFactory.addAccessor(Object.class, new ReflectionAccessor());
74                 templateContextFactory.addFilter("xml", new XmlFilter());
75                 templateContextFactory.addFilter("html", new HtmlFilter());
76         }
77
78         /** The UTF-8 charset. */
79         private static final Charset utf8Charset = Charset.forName("UTF-8");
80
81         /** The core. */
82         private final Core core;
83
84         /** The event bus. */
85         private final EventBus eventBus;
86
87         /** The Freenet interface. */
88         private final FreenetInterface freenetInterface;
89
90         /** The Sone to insert. */
91         private final Sone sone;
92
93         /** Whether a modification has been detected. */
94         private volatile boolean modified = false;
95
96         /** The fingerprint of the last insert. */
97         private volatile String lastInsertFingerprint;
98
99         /**
100          * Creates a new Sone inserter.
101          *
102          * @param core
103          *            The core
104          * @param eventBus
105          *            The event bus
106          * @param freenetInterface
107          *            The freenet interface
108          * @param sone
109          *            The Sone to insert
110          */
111         public SoneInserter(Core core, EventBus eventBus, FreenetInterface freenetInterface, Sone sone) {
112                 super("Sone Inserter for “" + sone.getName() + "”", false);
113                 this.core = core;
114                 this.eventBus = eventBus;
115                 this.freenetInterface = freenetInterface;
116                 this.sone = sone;
117         }
118
119         //
120         // ACCESSORS
121         //
122
123         /**
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.
126          *
127          * @param insertionDelay
128          *            The insertion delay (in seconds)
129          */
130         public static void setInsertionDelay(int insertionDelay) {
131                 SoneInserter.insertionDelay = insertionDelay;
132         }
133
134         /**
135          * Returns the fingerprint of the last insert.
136          *
137          * @return The fingerprint of the last insert
138          */
139         public String getLastInsertFingerprint() {
140                 return lastInsertFingerprint;
141         }
142
143         /**
144          * Sets the fingerprint of the last insert.
145          *
146          * @param lastInsertFingerprint
147          *            The fingerprint of the last insert
148          */
149         public void setLastInsertFingerprint(String lastInsertFingerprint) {
150                 this.lastInsertFingerprint = lastInsertFingerprint;
151         }
152
153         /**
154          * Returns whether the Sone inserter has detected a modification of the
155          * Sone.
156          *
157          * @return {@code true} if the Sone has been modified, {@code false}
158          *         otherwise
159          */
160         public boolean isModified() {
161                 return modified;
162         }
163
164         //
165         // SERVICE METHODS
166         //
167
168         /**
169          * {@inheritDoc}
170          */
171         @Override
172         protected void serviceRun() {
173                 long lastModificationTime = 0;
174                 String lastInsertedFingerprint = lastInsertFingerprint;
175                 String lastFingerprint = "";
176                 while (!shouldStop()) {
177                         try {
178                                 /* check every seconds. */
179                                 sleep(1000);
180
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);
186                                         }
187                                         lastFingerprint = "";
188                                         lastModificationTime = 0;
189                                         continue;
190                                 }
191
192                                 InsertInformation insertInformation = null;
193                                 synchronized (sone) {
194                                         String fingerprint = sone.getFingerprint();
195                                         if (!fingerprint.equals(lastFingerprint)) {
196                                                 if (fingerprint.equals(lastInsertedFingerprint)) {
197                                                         modified = false;
198                                                         lastModificationTime = 0;
199                                                         logger.log(Level.FINE, String.format("Sone %s has been reverted to last insert state.", sone));
200                                                 } else {
201                                                         lastModificationTime = System.currentTimeMillis();
202                                                         modified = true;
203                                                         logger.log(Level.FINE, String.format("Sone %s has been modified, waiting %d seconds before inserting.", sone.getName(), insertionDelay));
204                                                 }
205                                                 lastFingerprint = fingerprint;
206                                         }
207                                         if (modified && (lastModificationTime > 0) && ((System.currentTimeMillis() - lastModificationTime) > (insertionDelay * 1000))) {
208                                                 lastInsertedFingerprint = fingerprint;
209                                                 insertInformation = new InsertInformation(sone);
210                                         }
211                                 }
212
213                                 if (insertInformation != null) {
214                                         logger.log(Level.INFO, String.format("Inserting Sone “%s”…", sone.getName()));
215
216                                         boolean success = false;
217                                         try {
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. */
225                                                 if (shouldStop()) {
226                                                         /* if so, bail out, don’t change anything. */
227                                                         break;
228                                                 }
229                                                 sone.setTime(insertTime);
230                                                 sone.setLatestEdition(finalUri.getEdition());
231                                                 core.touchConfiguration();
232                                                 success = true;
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);
237                                         } finally {
238                                                 sone.setStatus(SoneStatus.idle);
239                                         }
240
241                                         /*
242                                          * reset modification counter if Sone has not been modified
243                                          * while it was inserted.
244                                          */
245                                         if (success) {
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();
252                                                                 modified = false;
253                                                         }
254                                                 }
255                                         }
256                                 }
257                         } catch (Throwable t1) {
258                                 logger.log(Level.SEVERE, "SoneInserter threw an Exception!", t1);
259                         }
260                 }
261         }
262
263         /**
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.
267          *
268          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
269          */
270         private class InsertInformation {
271
272                 /** All properties of the Sone, copied for thread safety. */
273                 private final Map<String, Object> soneProperties = new HashMap<String, Object>();
274
275                 /**
276                  * Creates a new insert information container.
277                  *
278                  * @param sone
279                  *            The sone to insert
280                  */
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());
293                 }
294
295                 //
296                 // ACCESSORS
297                 //
298
299                 /**
300                  * Returns the insert URI of the Sone.
301                  *
302                  * @return The insert URI of the Sone
303                  */
304                 public FreenetURI getInsertUri() {
305                         return (FreenetURI) soneProperties.get("insertUri");
306                 }
307
308                 /**
309                  * Sets the time of the Sone at the time of the insert.
310                  *
311                  * @param time
312                  *            The time of the Sone
313                  */
314                 public void setTime(long time) {
315                         soneProperties.put("time", time);
316                 }
317
318                 //
319                 // ACTIONS
320                 //
321
322                 /**
323                  * Generates all manifest entries required to insert this Sone.
324                  *
325                  * @return The manifest entries for the Sone insert
326                  */
327                 public HashMap<String, Object> generateManifestEntries() {
328                         HashMap<String, Object> manifestEntries = new HashMap<String, Object>();
329
330                         /* first, create an index.html. */
331                         manifestEntries.put("index.html", createManifestElement("index.html", "text/html; charset=utf-8", "/templates/insert/index.html"));
332
333                         /* now, store the sone. */
334                         manifestEntries.put("sone.xml", createManifestElement("sone.xml", "text/xml; charset=utf-8", "/templates/insert/sone.xml"));
335
336                         return manifestEntries;
337                 }
338
339                 //
340                 // PRIVATE METHODS
341                 //
342
343                 /**
344                  * Creates a new manifest element.
345                  *
346                  * @param name
347                  *            The name of the file
348                  * @param contentType
349                  *            The content type of the file
350                  * @param templateName
351                  *            The name of the template to render
352                  * @return The manifest element
353                  */
354                 @SuppressWarnings("synthetic-access")
355                 private ManifestElement createManifestElement(String name, String contentType, String templateName) {
356                         InputStreamReader templateInputStreamReader = null;
357                         Template template;
358                         try {
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);
363                                 return null;
364                         } finally {
365                                 Closer.close(templateInputStreamReader);
366                         }
367
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;
375                         try {
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);
381                                 return null;
382                         } finally {
383                                 Closer.close(writer);
384                                 if (bucket != null) {
385                                         bucket.free();
386                                 }
387                         }
388                 }
389
390         }
391
392 }