7a0ec9c673c219b6f1ceb5166eecad7e9c8c1466
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
1 /*
2  * FreenetSone - 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.Collection;
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import java.util.UUID;
31 import java.util.logging.Level;
32 import java.util.logging.Logger;
33
34 import net.pterodactylus.sone.core.SoneException.Type;
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.util.config.Configuration;
40 import net.pterodactylus.util.config.ConfigurationException;
41 import net.pterodactylus.util.filter.Filter;
42 import net.pterodactylus.util.filter.Filters;
43 import net.pterodactylus.util.logging.Logging;
44 import net.pterodactylus.util.service.AbstractService;
45 import freenet.client.FetchResult;
46 import freenet.keys.FreenetURI;
47
48 /**
49  * The Sone core.
50  *
51  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
52  */
53 public class Core extends AbstractService {
54
55         /**
56          * Enumeration for the possible states of a {@link Sone}.
57          *
58          * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
59          */
60         public enum SoneStatus {
61
62                 /** The Sone is idle, i.e. not being downloaded or inserted. */
63                 idle,
64
65                 /** The Sone is currently being inserted. */
66                 inserting,
67
68                 /** The Sone is currently being downloaded. */
69                 downloading,
70         }
71
72         /** The logger. */
73         private static final Logger logger = Logging.getLogger(Core.class);
74
75         /** The configuration. */
76         private Configuration configuration;
77
78         /** Interface to freenet. */
79         private FreenetInterface freenetInterface;
80
81         /** The Sone downloader. */
82         private SoneDownloader soneDownloader;
83
84         /** The local Sones. */
85         private final Set<Sone> localSones = new HashSet<Sone>();
86
87         /** Sone inserters. */
88         private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
89
90         /** The Sones’ statuses. */
91         private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
92
93         /* various caches follow here. */
94
95         /** Cache for all known Sones. */
96         private final Map<String, Sone> soneCache = new HashMap<String, Sone>();
97
98         /** Cache for all known posts. */
99         private final Map<String, Post> postCache = new HashMap<String, Post>();
100
101         /** Cache for all known replies. */
102         private final Map<String, Reply> replyCache = new HashMap<String, Reply>();
103
104         /**
105          * Creates a new core.
106          */
107         public Core() {
108                 super("Sone Core");
109         }
110
111         //
112         // ACCESSORS
113         //
114
115         /**
116          * Sets the configuration of the core.
117          *
118          * @param configuration
119          *            The configuration of the core
120          * @return This core (for method chaining)
121          */
122         public Core configuration(Configuration configuration) {
123                 this.configuration = configuration;
124                 return this;
125         }
126
127         /**
128          * Sets the Freenet interface to use.
129          *
130          * @param freenetInterface
131          *            The Freenet interface to use
132          * @return This core (for method chaining)
133          */
134         public Core freenetInterface(FreenetInterface freenetInterface) {
135                 this.freenetInterface = freenetInterface;
136                 soneDownloader = new SoneDownloader(this, freenetInterface);
137                 soneDownloader.start();
138                 return this;
139         }
140
141         /**
142          * Returns the local Sones.
143          *
144          * @return The local Sones
145          */
146         public Set<Sone> getSones() {
147                 return Collections.unmodifiableSet(localSones);
148         }
149
150         /**
151          * Returns the Sone with the given ID, or an empty Sone that has been
152          * initialized with the given ID.
153          *
154          * @param soneId
155          *            The ID of the Sone
156          * @return The Sone
157          */
158         public Sone getSone(String soneId) {
159                 if (!soneCache.containsKey(soneId)) {
160                         Sone sone = new Sone(soneId);
161                         soneCache.put(soneId, sone);
162                         setSoneStatus(sone, SoneStatus.idle);
163                 }
164                 return soneCache.get(soneId);
165         }
166
167         /**
168          * Returns all known sones.
169          *
170          * @return All known sones
171          */
172         public Collection<Sone> getKnownSones() {
173                 return soneCache.values();
174         }
175
176         /**
177          * Gets all known Sones that are not local Sones.
178          *
179          * @return All remote Sones
180          */
181         public Collection<Sone> getRemoteSones() {
182                 return Filters.filteredCollection(getKnownSones(), new Filter<Sone>() {
183
184                         @Override
185                         @SuppressWarnings("synthetic-access")
186                         public boolean filterObject(Sone object) {
187                                 return !localSones.contains(object);
188                         }
189                 });
190         }
191
192         /**
193          * Returns the status of the given Sone.
194          *
195          * @param sone
196          *            The Sone to get the status for
197          * @return The status of the Sone
198          */
199         public SoneStatus getSoneStatus(Sone sone) {
200                 return soneStatuses.get(sone);
201         }
202
203         /**
204          * Sets the status of the Sone.
205          *
206          * @param sone
207          *            The Sone to set the status for
208          * @param soneStatus
209          *            The status of the Sone
210          */
211         public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
212                 soneStatuses.put(sone, soneStatus);
213         }
214
215         /**
216          * Creates a new post and adds it to the given Sone.
217          *
218          * @param sone
219          *            The sone that creates the post
220          * @param text
221          *            The text of the post
222          * @return The created post
223          */
224         public Post createPost(Sone sone, String text) {
225                 return createPost(sone, System.currentTimeMillis(), text);
226         }
227
228         /**
229          * Creates a new post and adds it to the given Sone.
230          *
231          * @param sone
232          *            The Sone that creates the post
233          * @param time
234          *            The time of the post
235          * @param text
236          *            The text of the post
237          * @return The created post
238          */
239         public Post createPost(Sone sone, long time, String text) {
240                 Post post = getPost(UUID.randomUUID().toString()).setSone(sone).setTime(time).setText(text);
241                 sone.addPost(post);
242                 return post;
243         }
244
245         /**
246          * Creates a reply.
247          *
248          * @param sone
249          *            The Sone that posts the reply
250          * @param post
251          *            The post the reply refers to
252          * @param text
253          *            The text of the reply
254          * @return The created reply
255          */
256         public Reply createReply(Sone sone, Post post, String text) {
257                 return createReply(sone, post, System.currentTimeMillis(), text);
258         }
259
260         /**
261          * Creates a reply.
262          *
263          * @param sone
264          *            The Sone that posts the reply
265          * @param post
266          *            The post the reply refers to
267          * @param time
268          *            The time of the post
269          * @param text
270          *            The text of the reply
271          * @return The created reply
272          */
273         public Reply createReply(Sone sone, Post post, long time, String text) {
274                 Reply reply = getReply(UUID.randomUUID().toString()).setSone(sone).setPost(post).setTime(time).setText(text);
275                 sone.addReply(reply);
276                 return reply;
277         }
278
279         //
280         // ACTIONS
281         //
282
283         /**
284          * Adds a Sone to watch for updates. The Sone needs to be completely
285          * initialized.
286          *
287          * @param sone
288          *            The Sone to watch for updates
289          */
290         public void addSone(Sone sone) {
291                 soneCache.put(sone.getId(), sone);
292                 if (!localSones.contains(sone)) {
293                         soneDownloader.addSone(sone);
294                 }
295         }
296
297         /**
298          * Adds the given Sone.
299          *
300          * @param sone
301          *            The Sone to add
302          */
303         public void addLocalSone(Sone sone) {
304                 if (localSones.add(sone)) {
305                         SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
306                         soneInserter.start();
307                         soneInserters.put(sone, soneInserter);
308                 }
309         }
310
311         /**
312          * Creates a new Sone at a random location.
313          *
314          * @param name
315          *            The name of the Sone
316          * @return The created Sone
317          * @throws SoneException
318          *             if a Sone error occurs
319          */
320         public Sone createSone(String name) throws SoneException {
321                 return createSone(name, "Sone-" + name, null, null);
322         }
323
324         /**
325          * Creates a new Sone at the given location. If one of {@code requestUri} or
326          * {@code insertUrI} is {@code null}, the Sone is created at a random
327          * location.
328          *
329          * @param name
330          *            The name of the Sone
331          * @param documentName
332          *            The document name in the SSK
333          * @param requestUri
334          *            The request URI of the Sone, or {@link NullPointerException}
335          *            to create a Sone at a random location
336          * @param insertUri
337          *            The insert URI of the Sone, or {@code null} to create a Sone
338          *            at a random location
339          * @return The created Sone
340          * @throws SoneException
341          *             if a Sone error occurs
342          */
343         public Sone createSone(String name, String documentName, String requestUri, String insertUri) throws SoneException {
344                 if ((name == null) || (name.trim().length() == 0)) {
345                         throw new SoneException(Type.INVALID_SONE_NAME);
346                 }
347                 String finalRequestUri;
348                 String finalInsertUri;
349                 if ((requestUri == null) || (insertUri == null)) {
350                         String[] keyPair = freenetInterface.generateKeyPair();
351                         finalRequestUri = keyPair[0];
352                         finalInsertUri = keyPair[1];
353                 } else {
354                         finalRequestUri = requestUri;
355                         finalInsertUri = insertUri;
356                 }
357                 Sone sone;
358                 try {
359                         logger.log(Level.FINEST, "Creating new Sone “%s” at %s (%s)…", new Object[] { name, finalRequestUri, finalInsertUri });
360                         sone = getSone(UUID.randomUUID().toString()).setName(name).setRequestUri(new FreenetURI(finalRequestUri).setKeyType("USK").setDocName(documentName)).setInsertUri(new FreenetURI(finalInsertUri).setKeyType("USK").setDocName(documentName));
361                         sone.setProfile(new Profile());
362                         /* set modification counter to 1 so it is inserted immediately. */
363                         sone.setModificationCounter(1);
364                         addLocalSone(sone);
365                 } catch (MalformedURLException mue1) {
366                         throw new SoneException(Type.INVALID_URI);
367                 }
368                 return sone;
369         }
370
371         /**
372          * Loads the Sone from the given request URI. The fetching of the data is
373          * performed in a new thread so this method returns immediately.
374          *
375          * @param requestUri
376          *            The request URI to load the Sone from
377          */
378         public void loadSone(final String requestUri) {
379                 loadSone(requestUri, null);
380         }
381
382         /**
383          * Loads the Sone from the given request URI. The fetching of the data is
384          * performed in a new thread so this method returns immediately. If
385          * {@code insertUri} is not {@code null} the loaded Sone is converted into a
386          * local Sone and available using as any other local Sone.
387          *
388          * @param requestUri
389          *            The request URI to load the Sone from
390          * @param insertUri
391          *            The insert URI of the Sone
392          */
393         public void loadSone(final String requestUri, final String insertUri) {
394                 new Thread(new Runnable() {
395
396                         @Override
397                         @SuppressWarnings("synthetic-access")
398                         public void run() {
399                                 try {
400                                         FreenetURI realRequestUri = new FreenetURI(requestUri).setMetaString(new String[] { "sone.xml" });
401                                         FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
402                                         Sone parsedSone = soneDownloader.parseSone(null, fetchResult, realRequestUri);
403                                         if (parsedSone != null) {
404                                                 if (insertUri != null) {
405                                                         parsedSone.setInsertUri(new FreenetURI(insertUri));
406                                                         addLocalSone(parsedSone);
407                                                 } else {
408                                                         addSone(parsedSone);
409                                                 }
410                                         }
411                                 } catch (MalformedURLException mue1) {
412                                         logger.log(Level.INFO, "Could not create URI from “" + requestUri + "”.", mue1);
413                                 }
414                         }
415                 }, "Sone Downloader").start();
416         }
417
418         /**
419          * Loads and updates the given Sone.
420          *
421          * @param sone
422          *            The Sone to load
423          */
424         public void loadSone(final Sone sone) {
425                 new Thread(new Runnable() {
426
427                         @Override
428                         @SuppressWarnings("synthetic-access")
429                         public void run() {
430                                 FreenetURI realRequestUri = sone.getRequestUri().setMetaString(new String[] { "sone.xml" });
431                                 FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
432                                 Sone parsedSone = soneDownloader.parseSone(sone, fetchResult, realRequestUri);
433                                 if (parsedSone != null) {
434                                         addSone(parsedSone);
435                                 }
436                         }
437                 }, "Sone Downloader").start();
438         }
439
440         /**
441          * Deletes the given Sone from this plugin instance.
442          *
443          * @param sone
444          *            The sone to delete
445          */
446         public void deleteSone(Sone sone) {
447                 SoneInserter soneInserter = soneInserters.remove(sone);
448                 soneInserter.stop();
449                 localSones.remove(sone);
450         }
451
452         /**
453          * Returns the post with the given ID. If no post exists yet with the given
454          * ID, a new post is returned.
455          *
456          * @param postId
457          *            The ID of the post
458          * @return The post
459          */
460         public Post getPost(String postId) {
461                 if (!postCache.containsKey(postId)) {
462                         postCache.put(postId, new Post(postId));
463                 }
464                 return postCache.get(postId);
465         }
466
467         /**
468          * Returns the reply with the given ID. If no reply exists yet with the
469          * given ID, a new reply is returned.
470          *
471          * @param replyId
472          *            The ID of the reply
473          * @return The reply
474          */
475         public Reply getReply(String replyId) {
476                 if (!replyCache.containsKey(replyId)) {
477                         replyCache.put(replyId, new Reply(replyId));
478                 }
479                 return replyCache.get(replyId);
480         }
481
482         /**
483          * Gets all replies to the given post, sorted by date, oldest first.
484          *
485          * @param post
486          *            The post the replies refer to
487          * @return The sorted list of replies for the post
488          */
489         public List<Reply> getReplies(Post post) {
490                 List<Reply> replies = new ArrayList<Reply>();
491                 for (Reply reply : replyCache.values()) {
492                         if (reply.getPost().equals(post)) {
493                                 replies.add(reply);
494                         }
495                 }
496                 Collections.sort(replies, new Comparator<Reply>() {
497
498                         /**
499                          * {@inheritDoc}
500                          */
501                         @Override
502                         public int compare(Reply leftReply, Reply rightReply) {
503                                 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime()));
504                         }
505                 });
506                 return replies;
507         }
508
509         //
510         // SERVICE METHODS
511         //
512
513         /**
514          * {@inheritDoc}
515          */
516         @Override
517         protected void serviceStart() {
518                 loadConfiguration();
519         }
520
521         /**
522          * {@inheritDoc}
523          */
524         @Override
525         protected void serviceStop() {
526                 soneDownloader.stop();
527                 /* stop all Sone inserters. */
528                 for (SoneInserter soneInserter : soneInserters.values()) {
529                         soneInserter.stop();
530                 }
531                 saveConfiguration();
532         }
533
534         //
535         // PRIVATE METHODS
536         //
537
538         /**
539          * Loads the configuration.
540          */
541         private void loadConfiguration() {
542                 logger.entering(Core.class.getName(), "loadConfiguration()");
543
544                 /* parse local Sones. */
545                 logger.log(Level.INFO, "Loading Sones…");
546                 int soneId = 0;
547                 do {
548                         String sonePrefix = "Sone/Sone." + soneId++;
549                         String id = configuration.getStringValue(sonePrefix + "/ID").getValue(null);
550                         if (id == null) {
551                                 break;
552                         }
553                         String name = configuration.getStringValue(sonePrefix + "/Name").getValue(null);
554                         long time = configuration.getLongValue(sonePrefix + "/Time").getValue((long) 0);
555                         String insertUri = configuration.getStringValue(sonePrefix + "/InsertURI").getValue(null);
556                         String requestUri = configuration.getStringValue(sonePrefix + "/RequestURI").getValue(null);
557                         long modificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0);
558                         String firstName = configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null);
559                         String middleName = configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null);
560                         String lastName = configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null);
561                         try {
562                                 Profile profile = new Profile();
563                                 profile.setFirstName(firstName);
564                                 profile.setMiddleName(middleName);
565                                 profile.setLastName(lastName);
566                                 Sone sone = getSone(id).setName(name).setTime(time).setRequestUri(new FreenetURI(requestUri)).setInsertUri(new FreenetURI(insertUri));
567                                 sone.setProfile(profile);
568                                 int postId = 0;
569                                 do {
570                                         String postPrefix = sonePrefix + "/Post." + postId++;
571                                         id = configuration.getStringValue(postPrefix + "/ID").getValue(null);
572                                         if (id == null) {
573                                                 break;
574                                         }
575                                         time = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
576                                         String text = configuration.getStringValue(postPrefix + "/Text").getValue(null);
577                                         Post post = getPost(id).setSone(sone).setTime(time).setText(text);
578                                         sone.addPost(post);
579                                 } while (true);
580                                 int replyCounter = 0;
581                                 do {
582                                         String replyPrefix = sonePrefix + "/Reply." + replyCounter++;
583                                         String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
584                                         if (replyId == null) {
585                                                 break;
586                                         }
587                                         Post replyPost = getPost(configuration.getStringValue(replyPrefix + "/Post").getValue(null));
588                                         long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue(null);
589                                         String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
590                                         Reply reply = getReply(replyId).setSone(sone).setPost(replyPost).setTime(replyTime).setText(replyText);
591                                         sone.addReply(reply);
592                                 } while (true);
593
594                                 /* load friends. */
595                                 int friendCounter = 0;
596                                 while (true) {
597                                         String friendPrefix = sonePrefix + "/Friend." + friendCounter++;
598                                         String friendId = configuration.getStringValue(friendPrefix + "/ID").getValue(null);
599                                         if (friendId == null) {
600                                                 break;
601                                         }
602                                         Sone friendSone = getSone(friendId);
603                                         String friendKey = configuration.getStringValue(friendPrefix + "/Key").getValue(null);
604                                         String friendName = configuration.getStringValue(friendPrefix + "/Name").getValue(null);
605                                         friendSone.setRequestUri(new FreenetURI(friendKey)).setName(friendName);
606                                         sone.addFriend(friendSone);
607                                 }
608
609                                 /* load blocked Sone IDs. */
610                                 int blockedSoneCounter = 0;
611                                 while (true) {
612                                         String blockedSonePrefix = sonePrefix + "/BlockedSone." + blockedSoneCounter++;
613                                         String blockedSoneId = configuration.getStringValue(blockedSonePrefix + "/ID").getValue(null);
614                                         if (blockedSoneId == null) {
615                                                 break;
616                                         }
617                                         sone.addBlockedSoneId(blockedSoneId);
618                                 }
619
620                                 sone.setModificationCounter(modificationCounter);
621                                 addLocalSone(sone);
622                         } catch (MalformedURLException mue1) {
623                                 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
624                         }
625                 } while (true);
626                 logger.log(Level.INFO, "Loaded %d Sones.", getSones().size());
627
628                 /* load all known Sones. */
629                 int knownSonesCounter = 0;
630                 while (true) {
631                         String knownSonePrefix = "KnownSone." + knownSonesCounter++;
632                         String knownSoneId = configuration.getStringValue(knownSonePrefix + "/ID").getValue(null);
633                         if (knownSoneId == null) {
634                                 break;
635                         }
636                         String knownSoneName = configuration.getStringValue(knownSonePrefix + "/Name").getValue(null);
637                         String knownSoneKey = configuration.getStringValue(knownSonePrefix + "/Key").getValue(null);
638                         try {
639                                 getSone(knownSoneId).setName(knownSoneName).setRequestUri(new FreenetURI(knownSoneKey));
640                         } catch (MalformedURLException mue1) {
641                                 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + knownSoneKey + "”)!", mue1);
642                         }
643                 }
644
645                 /* load all remote Sones. */
646                 for (Sone remoteSone : getRemoteSones()) {
647                         loadSone(remoteSone);
648                 }
649
650                 logger.exiting(Core.class.getName(), "loadConfiguration()");
651         }
652
653         /**
654          * Saves the configuraiton.
655          */
656         private void saveConfiguration() {
657                 Set<Sone> sones = getSones();
658                 logger.log(Level.INFO, "Storing %d Sones…", sones.size());
659                 try {
660                         /* store all Sones. */
661                         int soneId = 0;
662                         for (Sone sone : localSones) {
663                                 String sonePrefix = "Sone/Sone." + soneId++;
664                                 configuration.getStringValue(sonePrefix + "/ID").setValue(sone.getId());
665                                 configuration.getStringValue(sonePrefix + "/Name").setValue(sone.getName());
666                                 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
667                                 configuration.getStringValue(sonePrefix + "/RequestURI").setValue(sone.getRequestUri().toString());
668                                 configuration.getStringValue(sonePrefix + "/InsertURI").setValue(sone.getInsertUri().toString());
669                                 configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter());
670                                 Profile profile = sone.getProfile();
671                                 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
672                                 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
673                                 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
674                                 int postId = 0;
675                                 for (Post post : sone.getPosts()) {
676                                         String postPrefix = sonePrefix + "/Post." + postId++;
677                                         configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
678                                         configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
679                                         configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
680                                 }
681                                 /* write null ID as terminator. */
682                                 configuration.getStringValue(sonePrefix + "/Post." + postId + "/ID").setValue(null);
683
684                                 int replyId = 0;
685                                 for (Reply reply : sone.getReplies()) {
686                                         String replyPrefix = sonePrefix + "/Reply." + replyId++;
687                                         configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
688                                         configuration.getStringValue(replyPrefix + "/Post").setValue(reply.getPost().getId());
689                                         configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
690                                         configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
691                                 }
692                                 /* write null ID as terminator. */
693                                 configuration.getStringValue(sonePrefix + "/Reply." + replyId + "/ID").setValue(null);
694
695                                 int friendId = 0;
696                                 for (Sone friend : sone.getFriends()) {
697                                         String friendPrefix = sonePrefix + "/Friend." + friendId++;
698                                         configuration.getStringValue(friendPrefix + "/ID").setValue(friend.getId());
699                                         configuration.getStringValue(friendPrefix + "/Key").setValue(friend.getRequestUri().toString());
700                                         configuration.getStringValue(friendPrefix + "/Name").setValue(friend.getName());
701                                 }
702                                 /* write null ID as terminator. */
703                                 configuration.getStringValue(sonePrefix + "/Friend." + friendId + "/ID").setValue(null);
704
705                                 /* write all blocked Sones. */
706                                 int blockedSoneCounter = 0;
707                                 for (String blockedSoneId : sone.getBlockedSoneIds()) {
708                                         String blockedSonePrefix = sonePrefix + "/BlockedSone." + blockedSoneCounter++;
709                                         configuration.getStringValue(blockedSonePrefix + "/ID").setValue(blockedSoneId);
710                                 }
711                                 configuration.getStringValue(sonePrefix + "/BlockedSone." + blockedSoneCounter + "/ID").setValue(null);
712
713                         }
714                         /* write null ID as terminator. */
715                         configuration.getStringValue("Sone/Sone." + soneId + "/ID").setValue(null);
716
717                         /* write all known Sones. */
718                         int knownSonesCounter = 0;
719                         for (Sone knownSone : getRemoteSones()) {
720                                 String knownSonePrefix = "KnownSone." + knownSonesCounter++;
721                                 configuration.getStringValue(knownSonePrefix + "/ID").setValue(knownSone.getId());
722                                 configuration.getStringValue(knownSonePrefix + "/Name").setValue(knownSone.getName());
723                                 configuration.getStringValue(knownSonePrefix + "/Key").setValue(knownSone.getRequestUri().toString());
724                                 /* TODO - store all known stuff? */
725                         }
726                         configuration.getStringValue("KnownSone." + knownSonesCounter + "/ID").setValue(null);
727
728                 } catch (ConfigurationException ce1) {
729                         logger.log(Level.WARNING, "Could not store configuration!", ce1);
730                 }
731         }
732
733 }