Don’t forget to fire events.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
1 /*
2  * Sone - Core.java - Copyright © 2010 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.net.MalformedURLException;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
30
31 import net.pterodactylus.sone.core.Options.DefaultOption;
32 import net.pterodactylus.sone.core.Options.Option;
33 import net.pterodactylus.sone.core.Options.OptionWatcher;
34 import net.pterodactylus.sone.data.Client;
35 import net.pterodactylus.sone.data.Post;
36 import net.pterodactylus.sone.data.Profile;
37 import net.pterodactylus.sone.data.Reply;
38 import net.pterodactylus.sone.data.Sone;
39 import net.pterodactylus.sone.freenet.wot.Identity;
40 import net.pterodactylus.sone.freenet.wot.IdentityListener;
41 import net.pterodactylus.sone.freenet.wot.IdentityManager;
42 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
43 import net.pterodactylus.sone.main.SonePlugin;
44 import net.pterodactylus.util.config.Configuration;
45 import net.pterodactylus.util.config.ConfigurationException;
46 import net.pterodactylus.util.logging.Logging;
47 import net.pterodactylus.util.number.Numbers;
48 import freenet.keys.FreenetURI;
49
50 /**
51  * The Sone core.
52  *
53  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
54  */
55 public class Core implements IdentityListener {
56
57         /**
58          * Enumeration for the possible states of a {@link Sone}.
59          *
60          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
61          */
62         public enum SoneStatus {
63
64                 /** The Sone is unknown, i.e. not yet downloaded. */
65                 unknown,
66
67                 /** The Sone is idle, i.e. not being downloaded or inserted. */
68                 idle,
69
70                 /** The Sone is currently being inserted. */
71                 inserting,
72
73                 /** The Sone is currently being downloaded. */
74                 downloading,
75         }
76
77         /** The logger. */
78         private static final Logger logger = Logging.getLogger(Core.class);
79
80         /** The options. */
81         private final Options options = new Options();
82
83         /** The core listener manager. */
84         private final CoreListenerManager coreListenerManager = new CoreListenerManager(this);
85
86         /** The configuration. */
87         private final Configuration configuration;
88
89         /** The identity manager. */
90         private final IdentityManager identityManager;
91
92         /** Interface to freenet. */
93         private final FreenetInterface freenetInterface;
94
95         /** The Sone downloader. */
96         private final SoneDownloader soneDownloader;
97
98         /** Whether the core has been stopped. */
99         private volatile boolean stopped;
100
101         /** The Sones’ statuses. */
102         /* synchronize access on itself. */
103         private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
104
105         /** Locked local Sones. */
106         /* synchronize on itself. */
107         private final Set<Sone> lockedSones = new HashSet<Sone>();
108
109         /** Sone inserters. */
110         /* synchronize access on this on localSones. */
111         private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
112
113         /** All local Sones. */
114         /* synchronize access on this on itself. */
115         private Map<String, Sone> localSones = new HashMap<String, Sone>();
116
117         /** All remote Sones. */
118         /* synchronize access on this on itself. */
119         private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
120
121         /** All new Sones. */
122         private Set<String> newSones = new HashSet<String>();
123
124         /** All known Sones. */
125         /* synchronize access on {@link #newSones}. */
126         private Set<String> knownSones = new HashSet<String>();
127
128         /** All posts. */
129         private Map<String, Post> posts = new HashMap<String, Post>();
130
131         /** All new posts. */
132         private Set<String> newPosts = new HashSet<String>();
133
134         /** All known posts. */
135         /* synchronize access on {@link #newPosts}. */
136         private Set<String> knownPosts = new HashSet<String>();
137
138         /** All replies. */
139         private Map<String, Reply> replies = new HashMap<String, Reply>();
140
141         /** All new replies. */
142         private Set<String> newReplies = new HashSet<String>();
143
144         /** All known replies. */
145         private Set<String> knownReplies = new HashSet<String>();
146
147         /**
148          * Creates a new core.
149          *
150          * @param configuration
151          *            The configuration of the core
152          * @param freenetInterface
153          *            The freenet interface
154          * @param identityManager
155          *            The identity manager
156          */
157         public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
158                 this.configuration = configuration;
159                 this.freenetInterface = freenetInterface;
160                 this.identityManager = identityManager;
161                 this.soneDownloader = new SoneDownloader(this, freenetInterface);
162         }
163
164         //
165         // LISTENER MANAGEMENT
166         //
167
168         /**
169          * Adds a new core listener.
170          *
171          * @param coreListener
172          *            The listener to add
173          */
174         public void addCoreListener(CoreListener coreListener) {
175                 coreListenerManager.addListener(coreListener);
176         }
177
178         /**
179          * Removes a core listener.
180          *
181          * @param coreListener
182          *            The listener to remove
183          */
184         public void removeCoreListener(CoreListener coreListener) {
185                 coreListenerManager.removeListener(coreListener);
186         }
187
188         //
189         // ACCESSORS
190         //
191
192         /**
193          * Returns the options used by the core.
194          *
195          * @return The options of the core
196          */
197         public Options getOptions() {
198                 return options;
199         }
200
201         /**
202          * Returns whether the “Sone rescue mode” is currently activated.
203          *
204          * @return {@code true} if the “Sone rescue mode” is currently activated,
205          *         {@code false} if it is not
206          */
207         public boolean isSoneRescueMode() {
208                 return options.getBooleanOption("SoneRescueMode").get();
209         }
210
211         /**
212          * Returns the identity manager used by the core.
213          *
214          * @return The identity manager
215          */
216         public IdentityManager getIdentityManager() {
217                 return identityManager;
218         }
219
220         /**
221          * Returns the status of the given Sone.
222          *
223          * @param sone
224          *            The Sone to get the status for
225          * @return The status of the Sone
226          */
227         public SoneStatus getSoneStatus(Sone sone) {
228                 synchronized (soneStatuses) {
229                         return soneStatuses.get(sone);
230                 }
231         }
232
233         /**
234          * Sets the status of the given Sone.
235          *
236          * @param sone
237          *            The Sone to set the status of
238          * @param soneStatus
239          *            The status to set
240          */
241         public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
242                 synchronized (soneStatuses) {
243                         soneStatuses.put(sone, soneStatus);
244                 }
245         }
246
247         /**
248          * Returns whether the given Sone is currently locked.
249          *
250          * @param sone
251          *            The sone to check
252          * @return {@code true} if the Sone is locked, {@code false} if it is not
253          */
254         public boolean isLocked(Sone sone) {
255                 synchronized (lockedSones) {
256                         return lockedSones.contains(sone);
257                 }
258         }
259
260         /**
261          * Returns all Sones, remote and local.
262          *
263          * @return All Sones
264          */
265         public Set<Sone> getSones() {
266                 Set<Sone> allSones = new HashSet<Sone>();
267                 allSones.addAll(getLocalSones());
268                 allSones.addAll(getRemoteSones());
269                 return allSones;
270         }
271
272         /**
273          * Returns the Sone with the given ID, regardless whether it’s local or
274          * remote.
275          *
276          * @param id
277          *            The ID of the Sone to get
278          * @return The Sone with the given ID, or {@code null} if there is no such
279          *         Sone
280          */
281         public Sone getSone(String id) {
282                 return getSone(id, true);
283         }
284
285         /**
286          * Returns the Sone with the given ID, regardless whether it’s local or
287          * remote.
288          *
289          * @param id
290          *            The ID of the Sone to get
291          * @param create
292          *            {@code true} to create a new Sone if none exists,
293          *            {@code false} to return {@code null} if a Sone with the given
294          *            ID does not exist
295          * @return The Sone with the given ID, or {@code null} if there is no such
296          *         Sone
297          */
298         public Sone getSone(String id, boolean create) {
299                 if (isLocalSone(id)) {
300                         return getLocalSone(id);
301                 }
302                 return getRemoteSone(id, create);
303         }
304
305         /**
306          * Checks whether the core knows a Sone with the given ID.
307          *
308          * @param id
309          *            The ID of the Sone
310          * @return {@code true} if there is a Sone with the given ID, {@code false}
311          *         otherwise
312          */
313         public boolean hasSone(String id) {
314                 return isLocalSone(id) || isRemoteSone(id);
315         }
316
317         /**
318          * Returns whether the given Sone is a local Sone.
319          *
320          * @param sone
321          *            The Sone to check for its locality
322          * @return {@code true} if the given Sone is local, {@code false} otherwise
323          */
324         public boolean isLocalSone(Sone sone) {
325                 synchronized (localSones) {
326                         return localSones.containsKey(sone.getId());
327                 }
328         }
329
330         /**
331          * Returns whether the given ID is the ID of a local Sone.
332          *
333          * @param id
334          *            The Sone ID to check for its locality
335          * @return {@code true} if the given ID is a local Sone, {@code false}
336          *         otherwise
337          */
338         public boolean isLocalSone(String id) {
339                 synchronized (localSones) {
340                         return localSones.containsKey(id);
341                 }
342         }
343
344         /**
345          * Returns all local Sones.
346          *
347          * @return All local Sones
348          */
349         public Set<Sone> getLocalSones() {
350                 synchronized (localSones) {
351                         return new HashSet<Sone>(localSones.values());
352                 }
353         }
354
355         /**
356          * Returns the local Sone with the given ID.
357          *
358          * @param id
359          *            The ID of the Sone to get
360          * @return The Sone with the given ID
361          */
362         public Sone getLocalSone(String id) {
363                 return getLocalSone(id, true);
364         }
365
366         /**
367          * Returns the local Sone with the given ID, optionally creating a new Sone.
368          *
369          * @param id
370          *            The ID of the Sone
371          * @param create
372          *            {@code true} to create a new Sone if none exists,
373          *            {@code false} to return null if none exists
374          * @return The Sone with the given ID, or {@code null}
375          */
376         public Sone getLocalSone(String id, boolean create) {
377                 synchronized (localSones) {
378                         Sone sone = localSones.get(id);
379                         if ((sone == null) && create) {
380                                 sone = new Sone(id);
381                                 localSones.put(id, sone);
382                         }
383                         return sone;
384                 }
385         }
386
387         /**
388          * Returns all remote Sones.
389          *
390          * @return All remote Sones
391          */
392         public Set<Sone> getRemoteSones() {
393                 synchronized (remoteSones) {
394                         return new HashSet<Sone>(remoteSones.values());
395                 }
396         }
397
398         /**
399          * Returns the remote Sone with the given ID.
400          *
401          * @param id
402          *            The ID of the remote Sone to get
403          * @return The Sone with the given ID
404          */
405         public Sone getRemoteSone(String id) {
406                 return getRemoteSone(id, true);
407         }
408
409         /**
410          * Returns the remote Sone with the given ID.
411          *
412          * @param id
413          *            The ID of the remote Sone to get
414          * @param create
415          *            {@code true} to always create a Sone, {@code false} to return
416          *            {@code null} if no Sone with the given ID exists
417          * @return The Sone with the given ID
418          */
419         public Sone getRemoteSone(String id, boolean create) {
420                 synchronized (remoteSones) {
421                         Sone sone = remoteSones.get(id);
422                         if ((sone == null) && create) {
423                                 sone = new Sone(id);
424                                 remoteSones.put(id, sone);
425                         }
426                         return sone;
427                 }
428         }
429
430         /**
431          * Returns whether the given Sone is a remote Sone.
432          *
433          * @param sone
434          *            The Sone to check
435          * @return {@code true} if the given Sone is a remote Sone, {@code false}
436          *         otherwise
437          */
438         public boolean isRemoteSone(Sone sone) {
439                 synchronized (remoteSones) {
440                         return remoteSones.containsKey(sone.getId());
441                 }
442         }
443
444         /**
445          * Returns whether the Sone with the given ID is a remote Sone.
446          *
447          * @param id
448          *            The ID of the Sone to check
449          * @return {@code true} if the Sone with the given ID is a remote Sone,
450          *         {@code false} otherwise
451          */
452         public boolean isRemoteSone(String id) {
453                 synchronized (remoteSones) {
454                         return remoteSones.containsKey(id);
455                 }
456         }
457
458         /**
459          * Returns whether the given Sone is a new Sone. After this check, the Sone
460          * is marked as known, i.e. a second call with the same parameters will
461          * always yield {@code false}.
462          *
463          * @param sone
464          *            The sone to check for
465          * @return {@code true} if the given Sone is new, false otherwise
466          */
467         public boolean isNewSone(Sone sone) {
468                 synchronized (newSones) {
469                         boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
470                         knownSones.add(sone.getId());
471                         if (isNew) {
472                                 coreListenerManager.fireMarkSoneKnown(sone);
473                         }
474                         return isNew;
475                 }
476         }
477
478         /**
479          * Returns whether the given Sone has been modified.
480          *
481          * @param sone
482          *            The Sone to check for modifications
483          * @return {@code true} if a modification has been detected in the Sone,
484          *         {@code false} otherwise
485          */
486         public boolean isModifiedSone(Sone sone) {
487                 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
488         }
489
490         /**
491          * Returns the post with the given ID.
492          *
493          * @param postId
494          *            The ID of the post to get
495          * @return The post, or {@code null} if there is no such post
496          */
497         public Post getPost(String postId) {
498                 synchronized (posts) {
499                         Post post = posts.get(postId);
500                         if (post == null) {
501                                 post = new Post(postId);
502                                 posts.put(postId, post);
503                         }
504                         return post;
505                 }
506         }
507
508         /**
509          * Returns whether the given post ID is new. After this method returns it is
510          * marked a known post ID.
511          *
512          * @param postId
513          *            The post ID
514          * @return {@code true} if the post is considered to be new, {@code false}
515          *         otherwise
516          */
517         public boolean isNewPost(String postId) {
518                 synchronized (newPosts) {
519                         boolean isNew = !knownPosts.contains(postId) && newPosts.remove(postId);
520                         knownPosts.add(postId);
521                         if (isNew) {
522                                 coreListenerManager.fireMarkPostKnown(getPost(postId));
523                         }
524                         return isNew;
525                 }
526         }
527
528         /**
529          * Returns the reply with the given ID.
530          *
531          * @param replyId
532          *            The ID of the reply to get
533          * @return The reply, or {@code null} if there is no such reply
534          */
535         public Reply getReply(String replyId) {
536                 synchronized (replies) {
537                         Reply reply = replies.get(replyId);
538                         if (reply == null) {
539                                 reply = new Reply(replyId);
540                                 replies.put(replyId, reply);
541                         }
542                         return reply;
543                 }
544         }
545
546         /**
547          * Returns all replies for the given post, order ascending by time.
548          *
549          * @param post
550          *            The post to get all replies for
551          * @return All replies for the given post
552          */
553         public List<Reply> getReplies(Post post) {
554                 Set<Sone> sones = getSones();
555                 List<Reply> replies = new ArrayList<Reply>();
556                 for (Sone sone : sones) {
557                         for (Reply reply : sone.getReplies()) {
558                                 if (reply.getPost().equals(post)) {
559                                         replies.add(reply);
560                                 }
561                         }
562                 }
563                 Collections.sort(replies, Reply.TIME_COMPARATOR);
564                 return replies;
565         }
566
567         /**
568          * Returns whether the reply with the given ID is new.
569          *
570          * @param replyId
571          *            The ID of the reply to check
572          * @return {@code true} if the reply is considered to be new, {@code false}
573          *         otherwise
574          */
575         public boolean isNewReply(String replyId) {
576                 synchronized (newReplies) {
577                         boolean isNew = !knownReplies.contains(replyId) && newReplies.remove(replyId);
578                         knownReplies.add(replyId);
579                         if (isNew) {
580                                 coreListenerManager.fireMarkReplyKnown(getReply(replyId));
581                         }
582                         return isNew;
583                 }
584         }
585
586         /**
587          * Returns all Sones that have liked the given post.
588          *
589          * @param post
590          *            The post to get the liking Sones for
591          * @return The Sones that like the given post
592          */
593         public Set<Sone> getLikes(Post post) {
594                 Set<Sone> sones = new HashSet<Sone>();
595                 for (Sone sone : getSones()) {
596                         if (sone.getLikedPostIds().contains(post.getId())) {
597                                 sones.add(sone);
598                         }
599                 }
600                 return sones;
601         }
602
603         /**
604          * Returns all Sones that have liked the given reply.
605          *
606          * @param reply
607          *            The reply to get the liking Sones for
608          * @return The Sones that like the given reply
609          */
610         public Set<Sone> getLikes(Reply reply) {
611                 Set<Sone> sones = new HashSet<Sone>();
612                 for (Sone sone : getSones()) {
613                         if (sone.getLikedReplyIds().contains(reply.getId())) {
614                                 sones.add(sone);
615                         }
616                 }
617                 return sones;
618         }
619
620         //
621         // ACTIONS
622         //
623
624         /**
625          * Locks the given Sone. A locked Sone will not be inserted by
626          * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
627          * again.
628          *
629          * @param sone
630          *            The sone to lock
631          */
632         public void lockSone(Sone sone) {
633                 synchronized (lockedSones) {
634                         lockedSones.add(sone);
635                 }
636         }
637
638         /**
639          * Unlocks the given Sone.
640          *
641          * @see #lockSone(Sone)
642          * @param sone
643          *            The sone to unlock
644          */
645         public void unlockSone(Sone sone) {
646                 synchronized (lockedSones) {
647                         lockedSones.remove(sone);
648                 }
649         }
650
651         /**
652          * Adds a local Sone from the given ID which has to be the ID of an own
653          * identity.
654          *
655          * @param id
656          *            The ID of an own identity to add a Sone for
657          * @return The added (or already existing) Sone
658          */
659         public Sone addLocalSone(String id) {
660                 synchronized (localSones) {
661                         if (localSones.containsKey(id)) {
662                                 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
663                                 return localSones.get(id);
664                         }
665                         OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
666                         if (ownIdentity == null) {
667                                 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
668                                 return null;
669                         }
670                         return addLocalSone(ownIdentity);
671                 }
672         }
673
674         /**
675          * Adds a local Sone from the given own identity.
676          *
677          * @param ownIdentity
678          *            The own identity to create a Sone from
679          * @return The added (or already existing) Sone
680          */
681         public Sone addLocalSone(OwnIdentity ownIdentity) {
682                 if (ownIdentity == null) {
683                         logger.log(Level.WARNING, "Given OwnIdentity is null!");
684                         return null;
685                 }
686                 synchronized (localSones) {
687                         final Sone sone;
688                         try {
689                                 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
690                         } catch (MalformedURLException mue1) {
691                                 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
692                                 return null;
693                         }
694                         sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
695                         sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
696                         /* TODO - load posts ’n stuff */
697                         localSones.put(ownIdentity.getId(), sone);
698                         final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
699                         soneInserters.put(sone, soneInserter);
700                         setSoneStatus(sone, SoneStatus.idle);
701                         loadSone(sone);
702                         if (!isSoneRescueMode()) {
703                                 soneInserter.start();
704                         }
705                         new Thread(new Runnable() {
706
707                                 @Override
708                                 @SuppressWarnings("synthetic-access")
709                                 public void run() {
710                                         if (!isSoneRescueMode()) {
711                                                 soneDownloader.fetchSone(sone);
712                                                 return;
713                                         }
714                                         logger.log(Level.INFO, "Trying to restore Sone from Freenet…");
715                                         coreListenerManager.fireRescuingSone(sone);
716                                         lockSone(sone);
717                                         long edition = sone.getLatestEdition();
718                                         while (!stopped && (edition >= 0) && isSoneRescueMode()) {
719                                                 logger.log(Level.FINE, "Downloading edition " + edition + "…");
720                                                 soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition));
721                                                 --edition;
722                                         }
723                                         logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…");
724                                         coreListenerManager.fireRescuedSone(sone);
725                                         soneInserter.start();
726                                 }
727
728                         }, "Sone Downloader").start();
729                         return sone;
730                 }
731         }
732
733         /**
734          * Creates a new Sone for the given own identity.
735          *
736          * @param ownIdentity
737          *            The own identity to create a Sone for
738          * @return The created Sone
739          */
740         public Sone createSone(OwnIdentity ownIdentity) {
741                 identityManager.addContext(ownIdentity, "Sone");
742                 Sone sone = addLocalSone(ownIdentity);
743                 return sone;
744         }
745
746         /**
747          * Adds the Sone of the given identity.
748          *
749          * @param identity
750          *            The identity whose Sone to add
751          * @return The added or already existing Sone
752          */
753         public Sone addRemoteSone(Identity identity) {
754                 if (identity == null) {
755                         logger.log(Level.WARNING, "Given Identity is null!");
756                         return null;
757                 }
758                 synchronized (remoteSones) {
759                         final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
760                         boolean newSone = sone.getRequestUri() == null;
761                         sone.setRequestUri(getSoneUri(identity.getRequestUri()));
762                         sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
763                         if (newSone) {
764                                 synchronized (newSones) {
765                                         newSone = !knownSones.contains(sone.getId());
766                                         if (newSone) {
767                                                 newSones.add(sone.getId());
768                                         }
769                                 }
770                                 if (newSone) {
771                                         coreListenerManager.fireNewSoneFound(sone);
772                                 }
773                         }
774                         remoteSones.put(identity.getId(), sone);
775                         soneDownloader.addSone(sone);
776                         setSoneStatus(sone, SoneStatus.unknown);
777                         new Thread(new Runnable() {
778
779                                 @Override
780                                 @SuppressWarnings("synthetic-access")
781                                 public void run() {
782                                         soneDownloader.fetchSone(sone);
783                                 }
784
785                         }, "Sone Downloader").start();
786                         return sone;
787                 }
788         }
789
790         /**
791          * Updates the stores Sone with the given Sone.
792          *
793          * @param sone
794          *            The updated Sone
795          */
796         public void updateSone(Sone sone) {
797                 if (hasSone(sone.getId())) {
798                         boolean soneRescueMode = isLocalSone(sone) && isSoneRescueMode();
799                         Sone storedSone = getSone(sone.getId());
800                         if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
801                                 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
802                                 return;
803                         }
804                         synchronized (posts) {
805                                 if (!soneRescueMode) {
806                                         for (Post post : storedSone.getPosts()) {
807                                                 posts.remove(post.getId());
808                                         }
809                                 }
810                                 synchronized (newPosts) {
811                                         for (Post post : sone.getPosts()) {
812                                                 if (!storedSone.getPosts().contains(post) && !knownPosts.contains(post.getId())) {
813                                                         post.setSone(getSone(post.getSone().getId()));
814                                                         newPosts.add(post.getId());
815                                                         coreListenerManager.fireNewPostFound(post);
816                                                 }
817                                                 posts.put(post.getId(), post);
818                                         }
819                                 }
820                         }
821                         synchronized (replies) {
822                                 if (!soneRescueMode) {
823                                         for (Reply reply : storedSone.getReplies()) {
824                                                 replies.remove(reply.getId());
825                                         }
826                                 }
827                                 synchronized (newReplies) {
828                                         for (Reply reply : sone.getReplies()) {
829                                                 if (!storedSone.getReplies().contains(reply) && !knownReplies.contains(reply.getId())) {
830                                                         newReplies.add(reply.getId());
831                                                         coreListenerManager.fireNewReplyFound(reply);
832                                                 }
833                                                 replies.put(reply.getId(), reply);
834                                         }
835                                 }
836                         }
837                         synchronized (storedSone) {
838                                 storedSone.setTime(sone.getTime());
839                                 storedSone.setClient(sone.getClient());
840                                 storedSone.setProfile(sone.getProfile());
841                                 if (soneRescueMode) {
842                                         for (Post post : sone.getPosts()) {
843                                                 storedSone.addPost(post);
844                                         }
845                                         for (Reply reply : sone.getReplies()) {
846                                                 storedSone.addReply(reply);
847                                         }
848                                         for (String likedPostId : sone.getLikedPostIds()) {
849                                                 storedSone.addLikedPostId(likedPostId);
850                                         }
851                                         for (String likedReplyId : sone.getLikedReplyIds()) {
852                                                 storedSone.addLikedReplyId(likedReplyId);
853                                         }
854                                 } else {
855                                         storedSone.setPosts(sone.getPosts());
856                                         storedSone.setReplies(sone.getReplies());
857                                         storedSone.setLikePostIds(sone.getLikedPostIds());
858                                         storedSone.setLikeReplyIds(sone.getLikedReplyIds());
859                                 }
860                                 storedSone.setLatestEdition(sone.getRequestUri().getEdition());
861                         }
862                 }
863         }
864
865         /**
866          * Deletes the given Sone. This will remove the Sone from the
867          * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
868          * and remove the context from its identity.
869          *
870          * @param sone
871          *            The Sone to delete
872          */
873         public void deleteSone(Sone sone) {
874                 if (!(sone.getIdentity() instanceof OwnIdentity)) {
875                         logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
876                         return;
877                 }
878                 synchronized (localSones) {
879                         if (!localSones.containsKey(sone.getId())) {
880                                 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
881                                 return;
882                         }
883                         localSones.remove(sone.getId());
884                         soneInserters.remove(sone).stop();
885                 }
886                 identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
887                 identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
888                 try {
889                         configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
890                 } catch (ConfigurationException ce1) {
891                         logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
892                 }
893         }
894
895         /**
896          * Loads and updates the given Sone from the configuration. If any error is
897          * encountered, loading is aborted and the given Sone is not changed.
898          *
899          * @param sone
900          *            The Sone to load and update
901          */
902         public void loadSone(Sone sone) {
903                 if (!isLocalSone(sone)) {
904                         logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
905                         return;
906                 }
907
908                 /* load Sone. */
909                 String sonePrefix = "Sone/" + sone.getId();
910                 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
911                 if (soneTime == null) {
912                         logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
913                         return;
914                 }
915                 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
916
917                 /* load profile. */
918                 Profile profile = new Profile();
919                 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
920                 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
921                 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
922                 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
923                 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
924                 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
925
926                 /* load posts. */
927                 Set<Post> posts = new HashSet<Post>();
928                 while (true) {
929                         String postPrefix = sonePrefix + "/Posts/" + posts.size();
930                         String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
931                         if (postId == null) {
932                                 break;
933                         }
934                         long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
935                         String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
936                         if ((postTime == 0) || (postText == null)) {
937                                 logger.log(Level.WARNING, "Invalid post found, aborting load!");
938                                 return;
939                         }
940                         posts.add(getPost(postId).setSone(sone).setTime(postTime).setText(postText));
941                 }
942
943                 /* load replies. */
944                 Set<Reply> replies = new HashSet<Reply>();
945                 while (true) {
946                         String replyPrefix = sonePrefix + "/Replies/" + replies.size();
947                         String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
948                         if (replyId == null) {
949                                 break;
950                         }
951                         String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
952                         long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
953                         String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
954                         if ((postId == null) || (replyTime == 0) || (replyText == null)) {
955                                 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
956                                 return;
957                         }
958                         replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
959                 }
960
961                 /* load post likes. */
962                 Set<String> likedPostIds = new HashSet<String>();
963                 while (true) {
964                         String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
965                         if (likedPostId == null) {
966                                 break;
967                         }
968                         likedPostIds.add(likedPostId);
969                 }
970
971                 /* load reply likes. */
972                 Set<String> likedReplyIds = new HashSet<String>();
973                 while (true) {
974                         String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
975                         if (likedReplyId == null) {
976                                 break;
977                         }
978                         likedReplyIds.add(likedReplyId);
979                 }
980
981                 /* load friends. */
982                 Set<String> friends = new HashSet<String>();
983                 while (true) {
984                         String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
985                         if (friendId == null) {
986                                 break;
987                         }
988                         friends.add(friendId);
989                 }
990
991                 /* if we’re still here, Sone was loaded successfully. */
992                 synchronized (sone) {
993                         sone.setTime(soneTime);
994                         sone.setProfile(profile);
995                         sone.setPosts(posts);
996                         sone.setReplies(replies);
997                         sone.setLikePostIds(likedPostIds);
998                         sone.setLikeReplyIds(likedReplyIds);
999                         sone.setFriends(friends);
1000                         soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
1001                 }
1002                 synchronized (newSones) {
1003                         for (String friend : friends) {
1004                                 knownSones.add(friend);
1005                         }
1006                 }
1007                 synchronized (newPosts) {
1008                         for (Post post : posts) {
1009                                 knownPosts.add(post.getId());
1010                         }
1011                 }
1012                 synchronized (newReplies) {
1013                         for (Reply reply : replies) {
1014                                 knownReplies.add(reply.getId());
1015                         }
1016                 }
1017         }
1018
1019         /**
1020          * Saves the given Sone. This will persist all local settings for the given
1021          * Sone, such as the friends list and similar, private options.
1022          *
1023          * @param sone
1024          *            The Sone to save
1025          */
1026         public void saveSone(Sone sone) {
1027                 if (!isLocalSone(sone)) {
1028                         logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
1029                         return;
1030                 }
1031                 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1032                         logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
1033                         return;
1034                 }
1035
1036                 logger.log(Level.INFO, "Saving Sone: %s", sone);
1037                 identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1038                 try {
1039                         /* save Sone into configuration. */
1040                         String sonePrefix = "Sone/" + sone.getId();
1041                         configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1042                         configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1043
1044                         /* save profile. */
1045                         Profile profile = sone.getProfile();
1046                         configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1047                         configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1048                         configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1049                         configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1050                         configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1051                         configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1052
1053                         /* save posts. */
1054                         int postCounter = 0;
1055                         for (Post post : sone.getPosts()) {
1056                                 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1057                                 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1058                                 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1059                                 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1060                         }
1061                         configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1062
1063                         /* save replies. */
1064                         int replyCounter = 0;
1065                         for (Reply reply : sone.getReplies()) {
1066                                 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1067                                 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1068                                 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
1069                                 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1070                                 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1071                         }
1072                         configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1073
1074                         /* save post likes. */
1075                         int postLikeCounter = 0;
1076                         for (String postId : sone.getLikedPostIds()) {
1077                                 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1078                         }
1079                         configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1080
1081                         /* save reply likes. */
1082                         int replyLikeCounter = 0;
1083                         for (String replyId : sone.getLikedReplyIds()) {
1084                                 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1085                         }
1086                         configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1087
1088                         /* save friends. */
1089                         int friendCounter = 0;
1090                         for (String friendId : sone.getFriends()) {
1091                                 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1092                         }
1093                         configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1094
1095                         logger.log(Level.INFO, "Sone %s saved.", sone);
1096                 } catch (ConfigurationException ce1) {
1097                         logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1098                 }
1099         }
1100
1101         /**
1102          * Creates a new post.
1103          *
1104          * @param sone
1105          *            The Sone that creates the post
1106          * @param text
1107          *            The text of the post
1108          */
1109         public void createPost(Sone sone, String text) {
1110                 createPost(sone, System.currentTimeMillis(), text);
1111         }
1112
1113         /**
1114          * Creates a new post.
1115          *
1116          * @param sone
1117          *            The Sone that creates the post
1118          * @param time
1119          *            The time of the post
1120          * @param text
1121          *            The text of the post
1122          */
1123         public void createPost(Sone sone, long time, String text) {
1124                 if (!isLocalSone(sone)) {
1125                         logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1126                         return;
1127                 }
1128                 Post post = new Post(sone, time, text);
1129                 synchronized (posts) {
1130                         posts.put(post.getId(), post);
1131                 }
1132                 synchronized (newPosts) {
1133                         knownPosts.add(post.getId());
1134                 }
1135                 sone.addPost(post);
1136                 saveSone(sone);
1137         }
1138
1139         /**
1140          * Deletes the given post.
1141          *
1142          * @param post
1143          *            The post to delete
1144          */
1145         public void deletePost(Post post) {
1146                 if (!isLocalSone(post.getSone())) {
1147                         logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1148                         return;
1149                 }
1150                 post.getSone().removePost(post);
1151                 synchronized (posts) {
1152                         posts.remove(post.getId());
1153                 }
1154                 saveSone(post.getSone());
1155         }
1156
1157         /**
1158          * Creates a new reply.
1159          *
1160          * @param sone
1161          *            The Sone that creates the reply
1162          * @param post
1163          *            The post that this reply refers to
1164          * @param text
1165          *            The text of the reply
1166          * @return The created reply
1167          */
1168         public Reply createReply(Sone sone, Post post, String text) {
1169                 return createReply(sone, post, System.currentTimeMillis(), text);
1170         }
1171
1172         /**
1173          * Creates a new reply.
1174          *
1175          * @param sone
1176          *            The Sone that creates the reply
1177          * @param post
1178          *            The post that this reply refers to
1179          * @param time
1180          *            The time of the reply
1181          * @param text
1182          *            The text of the reply
1183          * @return The created reply
1184          */
1185         public Reply createReply(Sone sone, Post post, long time, String text) {
1186                 if (!isLocalSone(sone)) {
1187                         logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1188                         return null;
1189                 }
1190                 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1191                 synchronized (replies) {
1192                         replies.put(reply.getId(), reply);
1193                 }
1194                 synchronized (newReplies) {
1195                         knownReplies.add(reply.getId());
1196                 }
1197                 sone.addReply(reply);
1198                 saveSone(sone);
1199                 return reply;
1200         }
1201
1202         /**
1203          * Deletes the given reply.
1204          *
1205          * @param reply
1206          *            The reply to delete
1207          */
1208         public void deleteReply(Reply reply) {
1209                 Sone sone = reply.getSone();
1210                 if (!isLocalSone(sone)) {
1211                         logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1212                         return;
1213                 }
1214                 synchronized (replies) {
1215                         replies.remove(reply.getId());
1216                 }
1217                 sone.removeReply(reply);
1218                 saveSone(sone);
1219         }
1220
1221         /**
1222          * Starts the core.
1223          */
1224         public void start() {
1225                 loadConfiguration();
1226         }
1227
1228         /**
1229          * Stops the core.
1230          */
1231         public void stop() {
1232                 synchronized (localSones) {
1233                         for (SoneInserter soneInserter : soneInserters.values()) {
1234                                 soneInserter.stop();
1235                         }
1236                 }
1237                 saveConfiguration();
1238                 stopped = true;
1239         }
1240
1241         //
1242         // PRIVATE METHODS
1243         //
1244
1245         /**
1246          * Loads the configuration.
1247          */
1248         @SuppressWarnings("unchecked")
1249         private void loadConfiguration() {
1250                 /* create options. */
1251                 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1252
1253                         @Override
1254                         public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1255                                 SoneInserter.setInsertionDelay(newValue);
1256                         }
1257
1258                 }));
1259                 options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
1260                 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1261                 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1262
1263                 /* read options from configuration. */
1264                 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1265                 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1266                 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1267                 options.getBooleanOption("ClearOnNextRestart").set(null);
1268                 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1269                 if (clearConfiguration) {
1270                         /* stop loading the configuration. */
1271                         return;
1272                 }
1273
1274                 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1275                 options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
1276
1277                 /* load known Sones. */
1278                 int soneCounter = 0;
1279                 while (true) {
1280                         String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1281                         if (knownSoneId == null) {
1282                                 break;
1283                         }
1284                         synchronized (newSones) {
1285                                 knownSones.add(knownSoneId);
1286                         }
1287                 }
1288
1289                 /* load known posts. */
1290                 int postCounter = 0;
1291                 while (true) {
1292                         String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1293                         if (knownPostId == null) {
1294                                 break;
1295                         }
1296                         synchronized (newPosts) {
1297                                 knownPosts.add(knownPostId);
1298                         }
1299                 }
1300
1301                 /* load known replies. */
1302                 int replyCounter = 0;
1303                 while (true) {
1304                         String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1305                         if (knownReplyId == null) {
1306                                 break;
1307                         }
1308                         synchronized (newReplies) {
1309                                 knownReplies.add(knownReplyId);
1310                         }
1311                 }
1312
1313         }
1314
1315         /**
1316          * Saves the current options.
1317          */
1318         private void saveConfiguration() {
1319                 /* store the options first. */
1320                 try {
1321                         configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1322                         configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
1323                         configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1324                         configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1325
1326                         /* save known Sones. */
1327                         int soneCounter = 0;
1328                         synchronized (newSones) {
1329                                 for (String knownSoneId : knownSones) {
1330                                         configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1331                                 }
1332                                 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1333                         }
1334
1335                         /* save known posts. */
1336                         int postCounter = 0;
1337                         synchronized (newPosts) {
1338                                 for (String knownPostId : knownPosts) {
1339                                         configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1340                                 }
1341                                 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1342                         }
1343
1344                         /* save known replies. */
1345                         int replyCounter = 0;
1346                         synchronized (newReplies) {
1347                                 for (String knownReplyId : knownReplies) {
1348                                         configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1349                                 }
1350                                 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1351                         }
1352
1353                 } catch (ConfigurationException ce1) {
1354                         logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1355                 }
1356         }
1357
1358         /**
1359          * Generate a Sone URI from the given URI and latest edition.
1360          *
1361          * @param uriString
1362          *            The URI to derive the Sone URI from
1363          * @return The derived URI
1364          */
1365         private FreenetURI getSoneUri(String uriString) {
1366                 try {
1367                         FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1368                         return uri;
1369                 } catch (MalformedURLException mue1) {
1370                         logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1371                         return null;
1372                 }
1373         }
1374
1375         //
1376         // INTERFACE IdentityListener
1377         //
1378
1379         /**
1380          * {@inheritDoc}
1381          */
1382         @Override
1383         public void ownIdentityAdded(OwnIdentity ownIdentity) {
1384                 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1385                 if (ownIdentity.hasContext("Sone")) {
1386                         addLocalSone(ownIdentity);
1387                 }
1388         }
1389
1390         /**
1391          * {@inheritDoc}
1392          */
1393         @Override
1394         public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1395                 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1396         }
1397
1398         /**
1399          * {@inheritDoc}
1400          */
1401         @Override
1402         public void identityAdded(Identity identity) {
1403                 logger.log(Level.FINEST, "Adding Identity: " + identity);
1404                 addRemoteSone(identity);
1405         }
1406
1407         /**
1408          * {@inheritDoc}
1409          */
1410         @Override
1411         public void identityUpdated(final Identity identity) {
1412                 new Thread(new Runnable() {
1413
1414                         @Override
1415                         @SuppressWarnings("synthetic-access")
1416                         public void run() {
1417                                 Sone sone = getRemoteSone(identity.getId());
1418                                 soneDownloader.fetchSone(sone);
1419                         }
1420                 }).start();
1421         }
1422
1423         /**
1424          * {@inheritDoc}
1425          */
1426         @Override
1427         public void identityRemoved(Identity identity) {
1428                 /* TODO */
1429         }
1430
1431 }