2 * FreenetSone - Core.java - Copyright © 2010 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.core;
20 import java.io.InputStream;
21 import java.net.MalformedURLException;
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Comparator;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.List;
31 import java.util.UUID;
32 import java.util.logging.Level;
33 import java.util.logging.Logger;
35 import net.pterodactylus.sone.core.Options.DefaultOption;
36 import net.pterodactylus.sone.core.Options.Option;
37 import net.pterodactylus.sone.core.Options.OptionWatcher;
38 import net.pterodactylus.sone.core.SoneException.Type;
39 import net.pterodactylus.sone.data.Post;
40 import net.pterodactylus.sone.data.Profile;
41 import net.pterodactylus.sone.data.Reply;
42 import net.pterodactylus.sone.data.Sone;
43 import net.pterodactylus.util.config.Configuration;
44 import net.pterodactylus.util.config.ConfigurationException;
45 import net.pterodactylus.util.filter.Filter;
46 import net.pterodactylus.util.filter.Filters;
47 import net.pterodactylus.util.logging.Logging;
48 import net.pterodactylus.util.service.AbstractService;
49 import freenet.client.FetchResult;
50 import freenet.keys.FreenetURI;
55 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
57 public class Core extends AbstractService {
60 * Enumeration for the possible states of a {@link Sone}.
62 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
64 public enum SoneStatus {
66 /** The Sone is unknown, i.e. not yet downloaded. */
69 /** The Sone is idle, i.e. not being downloaded or inserted. */
72 /** The Sone is currently being inserted. */
75 /** The Sone is currently being downloaded. */
80 private static final Logger logger = Logging.getLogger(Core.class);
83 private final Options options = new Options();
85 /** The configuration. */
86 private Configuration configuration;
88 /** Interface to freenet. */
89 private FreenetInterface freenetInterface;
91 /** The Sone downloader. */
92 private SoneDownloader soneDownloader;
94 /** The local Sones. */
95 private final Set<Sone> localSones = new HashSet<Sone>();
97 /** Sone inserters. */
98 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
100 /** The Sones’ statuses. */
101 private final Map<Sone, SoneStatus> soneStatuses = Collections.synchronizedMap(new HashMap<Sone, SoneStatus>());
103 /* various caches follow here. */
105 /** Cache for all known Sones. */
106 private final Map<String, Sone> soneCache = new HashMap<String, Sone>();
108 /** Cache for all known posts. */
109 private final Map<String, Post> postCache = new HashMap<String, Post>();
111 /** Cache for all known replies. */
112 private final Map<String, Reply> replyCache = new HashMap<String, Reply>();
115 * Creates a new core.
118 super("Sone Core", false);
126 * Returns the options of the Sone plugin.
128 * @return The options of the Sone plugin
130 public Options getOptions() {
135 * Sets the configuration of the core.
137 * @param configuration
138 * The configuration of the core
139 * @return This core (for method chaining)
141 public Core configuration(Configuration configuration) {
142 this.configuration = configuration;
147 * Sets the Freenet interface to use.
149 * @param freenetInterface
150 * The Freenet interface to use
151 * @return This core (for method chaining)
153 public Core freenetInterface(FreenetInterface freenetInterface) {
154 this.freenetInterface = freenetInterface;
155 soneDownloader = new SoneDownloader(this, freenetInterface);
156 soneDownloader.start();
161 * Returns the local Sones.
163 * @return The local Sones
165 public Set<Sone> getSones() {
166 return Collections.unmodifiableSet(localSones);
170 * Returns the Sone with the given ID, or an empty Sone that has been
171 * initialized with the given ID.
177 public Sone getSone(String soneId) {
178 if (!soneCache.containsKey(soneId)) {
179 Sone sone = new Sone(soneId);
180 soneCache.put(soneId, sone);
181 setSoneStatus(sone, SoneStatus.unknown);
183 return soneCache.get(soneId);
187 * Returns all known sones.
189 * @return All known sones
191 public Collection<Sone> getKnownSones() {
192 return soneCache.values();
196 * Gets all known Sones that are not local Sones.
198 * @return All remote Sones
200 public Collection<Sone> getRemoteSones() {
201 return Filters.filteredCollection(getKnownSones(), new Filter<Sone>() {
204 @SuppressWarnings("synthetic-access")
205 public boolean filterObject(Sone object) {
206 return !localSones.contains(object);
212 * Returns the status of the given Sone.
215 * The Sone to get the status for
216 * @return The status of the Sone
218 public SoneStatus getSoneStatus(Sone sone) {
219 return soneStatuses.get(sone);
223 * Sets the status of the Sone.
226 * The Sone to set the status for
228 * The status of the Sone
230 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
231 soneStatuses.put(sone, soneStatus);
235 * Creates a new post and adds it to the given Sone.
238 * The sone that creates the post
240 * The text of the post
241 * @return The created post
243 public Post createPost(Sone sone, String text) {
244 return createPost(sone, System.currentTimeMillis(), text);
248 * Creates a new post and adds it to the given Sone.
251 * The Sone that creates the post
253 * The time of the post
255 * The text of the post
256 * @return The created post
258 public Post createPost(Sone sone, long time, String text) {
259 Post post = getPost(UUID.randomUUID().toString()).setSone(sone).setTime(time).setText(text);
268 * The Sone that posts the reply
270 * The post the reply refers to
272 * The text of the reply
273 * @return The created reply
275 public Reply createReply(Sone sone, Post post, String text) {
276 return createReply(sone, post, System.currentTimeMillis(), text);
283 * The Sone that posts the reply
285 * The post the reply refers to
287 * The time of the post
289 * The text of the reply
290 * @return The created reply
292 public Reply createReply(Sone sone, Post post, long time, String text) {
293 Reply reply = getReply(UUID.randomUUID().toString()).setSone(sone).setPost(post).setTime(time).setText(text);
294 sone.addReply(reply);
303 * Adds a Sone to watch for updates. The Sone needs to be completely
307 * The Sone to watch for updates
309 public void addSone(Sone sone) {
310 soneCache.put(sone.getId(), sone);
311 if (!localSones.contains(sone)) {
312 soneDownloader.addSone(sone);
317 * Adds the given Sone.
322 public void addLocalSone(Sone sone) {
323 if (localSones.add(sone)) {
324 setSoneStatus(sone, SoneStatus.idle);
325 SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
326 soneInserter.start();
327 soneInserters.put(sone, soneInserter);
332 * Creates a new Sone at a random location.
335 * The name of the Sone
336 * @return The created Sone
337 * @throws SoneException
338 * if a Sone error occurs
340 public Sone createSone(String name) throws SoneException {
341 return createSone(name, "Sone", null, null);
345 * Creates a new Sone at the given location. If one of {@code requestUri} or
346 * {@code insertUrI} is {@code null}, the Sone is created at a random
350 * The name of the Sone
351 * @param documentName
352 * The document name in the SSK
354 * The request URI of the Sone, or {@link NullPointerException}
355 * to create a Sone at a random location
357 * The insert URI of the Sone, or {@code null} to create a Sone
358 * at a random location
359 * @return The created Sone
360 * @throws SoneException
361 * if a Sone error occurs
363 public Sone createSone(String name, String documentName, String requestUri, String insertUri) throws SoneException {
364 if ((name == null) || (name.trim().length() == 0)) {
365 throw new SoneException(Type.INVALID_SONE_NAME);
367 String finalRequestUri;
368 String finalInsertUri;
369 if ((requestUri == null) || (insertUri == null)) {
370 String[] keyPair = freenetInterface.generateKeyPair();
371 finalRequestUri = keyPair[0];
372 finalInsertUri = keyPair[1];
374 finalRequestUri = requestUri;
375 finalInsertUri = insertUri;
379 logger.log(Level.FINEST, "Creating new Sone “%s” at %s (%s)…", new Object[] { name, finalRequestUri, finalInsertUri });
380 sone = getSone(UUID.randomUUID().toString()).setName(name).setRequestUri(new FreenetURI(finalRequestUri).setKeyType("USK").setDocName(documentName)).setInsertUri(new FreenetURI(finalInsertUri).setKeyType("USK").setDocName(documentName));
381 sone.setProfile(new Profile());
382 /* set modification counter to 1 so it is inserted immediately. */
383 sone.setModificationCounter(1);
385 } catch (MalformedURLException mue1) {
386 throw new SoneException(Type.INVALID_URI);
392 * Loads the Sone from the given request URI. The fetching of the data is
393 * performed in a new thread so this method returns immediately.
396 * The request URI to load the Sone from
398 public void loadSone(final String requestUri) {
399 loadSone(requestUri, null);
403 * Loads the Sone from the given request URI. The fetching of the data is
404 * performed in a new thread so this method returns immediately. If
405 * {@code insertUri} is not {@code null} the loaded Sone is converted into a
406 * local Sone and available using as any other local Sone.
409 * The request URI to load the Sone from
411 * The insert URI of the Sone
413 public void loadSone(final String requestUri, final String insertUri) {
414 new Thread(new Runnable() {
417 @SuppressWarnings("synthetic-access")
420 FreenetURI realRequestUri = new FreenetURI(requestUri).setMetaString(new String[] { "sone.xml" });
421 FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
422 if (fetchResult == null) {
425 Sone parsedSone = soneDownloader.parseSone(null, fetchResult, realRequestUri);
426 if (parsedSone != null) {
427 if (insertUri != null) {
428 parsedSone.setInsertUri(new FreenetURI(insertUri));
429 addLocalSone(parsedSone);
434 } catch (MalformedURLException mue1) {
435 logger.log(Level.INFO, "Could not create URI from “" + requestUri + "”.", mue1);
438 }, "Sone Downloader").start();
442 * Loads a Sone from an input stream.
444 * @param soneInputStream
445 * The input stream to load the Sone from
446 * @return The parsed Sone, or {@code null} if the Sone could not be parsed
448 public Sone loadSone(InputStream soneInputStream) {
449 Sone parsedSone = soneDownloader.parseSone(soneInputStream);
450 if (parsedSone == null) {
453 if (parsedSone.getInsertUri() != null) {
454 addLocalSone(parsedSone);
462 * Loads and updates the given Sone.
467 public void loadSone(final Sone sone) {
468 new Thread(new Runnable() {
471 @SuppressWarnings("synthetic-access")
473 FreenetURI realRequestUri = sone.getRequestUri().setMetaString(new String[] { "sone.xml" });
474 setSoneStatus(sone, SoneStatus.downloading);
476 FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
477 if (fetchResult == null) {
478 /* TODO - mark Sone as bad. */
481 Sone parsedSone = soneDownloader.parseSone(sone, fetchResult, realRequestUri);
482 if (parsedSone != null) {
486 setSoneStatus(sone, (sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
489 }, "Sone Downloader").start();
493 * Deletes the given Sone from this plugin instance.
498 public void deleteSone(Sone sone) {
499 SoneInserter soneInserter = soneInserters.remove(sone);
501 localSones.remove(sone);
505 * Returns the post with the given ID. If no post exists yet with the given
506 * ID, a new post is returned.
512 public Post getPost(String postId) {
513 if (!postCache.containsKey(postId)) {
514 postCache.put(postId, new Post(postId));
516 return postCache.get(postId);
520 * Returns the reply with the given ID. If no reply exists yet with the
521 * given ID, a new reply is returned.
524 * The ID of the reply
527 public Reply getReply(String replyId) {
528 if (!replyCache.containsKey(replyId)) {
529 replyCache.put(replyId, new Reply(replyId));
531 return replyCache.get(replyId);
535 * Gets all replies to the given post, sorted by date, oldest first.
538 * The post the replies refer to
539 * @return The sorted list of replies for the post
541 public List<Reply> getReplies(Post post) {
542 List<Reply> replies = new ArrayList<Reply>();
543 for (Reply reply : replyCache.values()) {
544 if (reply.getPost().equals(post)) {
548 Collections.sort(replies, new Comparator<Reply>() {
554 public int compare(Reply leftReply, Reply rightReply) {
555 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime()));
562 * Gets all Sones that like the given post.
565 * The post to check for
566 * @return All Sones that like the post
568 public Set<Sone> getLikes(final Post post) {
569 return Filters.filteredSet(getSones(), new Filter<Sone>() {
572 public boolean filterObject(Sone sone) {
573 return sone.isLikedPostId(post.getId());
579 * Gets all Sones that like the given reply.
582 * The reply to check for
583 * @return All Sones that like the reply
585 public Set<Sone> getLikes(final Reply reply) {
586 return Filters.filteredSet(getSones(), new Filter<Sone>() {
589 public boolean filterObject(Sone sone) {
590 return sone.isLikedReplyId(reply.getId());
596 * Deletes the given reply. It is removed from its Sone and from the reply
600 * The reply to remove
602 public void deleteReply(Reply reply) {
603 reply.getSone().removeReply(reply);
604 replyCache.remove(reply.getId());
615 protected void serviceStart() {
623 protected void serviceStop() {
624 soneDownloader.stop();
625 /* stop all Sone inserters. */
626 for (SoneInserter soneInserter : soneInserters.values()) {
637 * Loads the configuration.
639 @SuppressWarnings("unchecked")
640 private void loadConfiguration() {
641 logger.entering(Core.class.getName(), "loadConfiguration()");
643 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
646 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
647 SoneInserter.setInsertionDelay(newValue);
652 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false)).set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
653 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false)).set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
655 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
656 options.getBooleanOption("ClearOnNextRestart").set(null);
657 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
658 if (clearConfiguration) {
659 /* stop loading the configuration. */
663 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
665 /* parse local Sones. */
666 logger.log(Level.INFO, "Loading Sones…");
669 String sonePrefix = "Sone/Sone." + soneId++;
670 String id = configuration.getStringValue(sonePrefix + "/ID").getValue(null);
674 String name = configuration.getStringValue(sonePrefix + "/Name").getValue(null);
675 long time = configuration.getLongValue(sonePrefix + "/Time").getValue((long) 0);
676 String insertUri = configuration.getStringValue(sonePrefix + "/InsertURI").getValue(null);
677 String requestUri = configuration.getStringValue(sonePrefix + "/RequestURI").getValue(null);
678 long modificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0);
679 String firstName = configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null);
680 String middleName = configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null);
681 String lastName = configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null);
682 Integer birthDay = configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null);
683 Integer birthMonth = configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null);
684 Integer birthYear = configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null);
686 Profile profile = new Profile();
687 profile.setFirstName(firstName);
688 profile.setMiddleName(middleName);
689 profile.setLastName(lastName);
690 profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear);
691 Sone sone = getSone(id).setName(name).setTime(time).setRequestUri(new FreenetURI(requestUri)).setInsertUri(new FreenetURI(insertUri));
692 sone.setProfile(profile);
695 String postPrefix = sonePrefix + "/Post." + postId++;
696 id = configuration.getStringValue(postPrefix + "/ID").getValue(null);
700 time = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
701 String text = configuration.getStringValue(postPrefix + "/Text").getValue(null);
702 Post post = getPost(id).setSone(sone).setTime(time).setText(text);
705 int replyCounter = 0;
707 String replyPrefix = sonePrefix + "/Reply." + replyCounter++;
708 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
709 if (replyId == null) {
712 Post replyPost = getPost(configuration.getStringValue(replyPrefix + "/Post").getValue(null));
713 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue(null);
714 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
715 Reply reply = getReply(replyId).setSone(sone).setPost(replyPost).setTime(replyTime).setText(replyText);
716 sone.addReply(reply);
720 int friendCounter = 0;
722 String friendPrefix = sonePrefix + "/Friend." + friendCounter++;
723 String friendId = configuration.getStringValue(friendPrefix + "/ID").getValue(null);
724 if (friendId == null) {
727 Sone friendSone = getSone(friendId);
728 String friendKey = configuration.getStringValue(friendPrefix + "/Key").getValue(null);
729 String friendName = configuration.getStringValue(friendPrefix + "/Name").getValue(null);
730 friendSone.setRequestUri(new FreenetURI(friendKey)).setName(friendName);
731 sone.addFriend(friendSone);
734 /* load blocked Sone IDs. */
735 int blockedSoneCounter = 0;
737 String blockedSonePrefix = sonePrefix + "/BlockedSone." + blockedSoneCounter++;
738 String blockedSoneId = configuration.getStringValue(blockedSonePrefix + "/ID").getValue(null);
739 if (blockedSoneId == null) {
742 sone.addBlockedSoneId(blockedSoneId);
745 /* load liked post IDs. */
746 int likedPostIdCounter = 0;
748 String likedPostIdPrefix = sonePrefix + "/LikedPostId." + likedPostIdCounter++;
749 String likedPostId = configuration.getStringValue(likedPostIdPrefix + "/ID").getValue(null);
750 if (likedPostId == null) {
753 sone.addLikedPostId(likedPostId);
756 /* load liked reply IDs. */
757 int likedReplyIdCounter = 0;
759 String likedReplyIdPrefix = sonePrefix + "/LikedReplyId." + likedReplyIdCounter++;
760 String likedReplyId = configuration.getStringValue(likedReplyIdPrefix + "/ID").getValue(null);
761 if (likedReplyId == null) {
764 sone.addLikedReplyId(likedReplyId);
767 sone.setModificationCounter(modificationCounter);
769 } catch (MalformedURLException mue1) {
770 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
773 logger.log(Level.INFO, "Loaded %d Sones.", getSones().size());
775 /* load all known Sones. */
776 int knownSonesCounter = 0;
778 String knownSonePrefix = "KnownSone." + knownSonesCounter++;
779 String knownSoneId = configuration.getStringValue(knownSonePrefix + "/ID").getValue(null);
780 if (knownSoneId == null) {
783 String knownSoneName = configuration.getStringValue(knownSonePrefix + "/Name").getValue(null);
784 String knownSoneKey = configuration.getStringValue(knownSonePrefix + "/Key").getValue(null);
786 getSone(knownSoneId).setName(knownSoneName).setRequestUri(new FreenetURI(knownSoneKey));
787 } catch (MalformedURLException mue1) {
788 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + knownSoneKey + "”)!", mue1);
792 /* load all remote Sones. */
793 for (Sone remoteSone : getRemoteSones()) {
794 loadSone(remoteSone);
797 logger.exiting(Core.class.getName(), "loadConfiguration()");
801 * Saves the configuraiton.
803 private void saveConfiguration() {
804 Set<Sone> sones = getSones();
805 logger.log(Level.INFO, "Storing %d Sones…", sones.size());
808 /* store the options first. */
809 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
810 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
811 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
813 /* store all Sones. */
815 for (Sone sone : localSones) {
816 String sonePrefix = "Sone/Sone." + soneId++;
817 configuration.getStringValue(sonePrefix + "/ID").setValue(sone.getId());
818 configuration.getStringValue(sonePrefix + "/Name").setValue(sone.getName());
819 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
820 configuration.getStringValue(sonePrefix + "/RequestURI").setValue(sone.getRequestUri().toString());
821 configuration.getStringValue(sonePrefix + "/InsertURI").setValue(sone.getInsertUri().toString());
822 configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter());
823 Profile profile = sone.getProfile();
824 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
825 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
826 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
827 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
828 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
829 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
831 for (Post post : sone.getPosts()) {
832 String postPrefix = sonePrefix + "/Post." + postId++;
833 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
834 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
835 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
837 /* write null ID as terminator. */
838 configuration.getStringValue(sonePrefix + "/Post." + postId + "/ID").setValue(null);
841 for (Reply reply : sone.getReplies()) {
842 String replyPrefix = sonePrefix + "/Reply." + replyId++;
843 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
844 configuration.getStringValue(replyPrefix + "/Post").setValue(reply.getPost().getId());
845 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
846 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
848 /* write null ID as terminator. */
849 configuration.getStringValue(sonePrefix + "/Reply." + replyId + "/ID").setValue(null);
852 for (Sone friend : sone.getFriends()) {
853 String friendPrefix = sonePrefix + "/Friend." + friendId++;
854 configuration.getStringValue(friendPrefix + "/ID").setValue(friend.getId());
855 configuration.getStringValue(friendPrefix + "/Key").setValue(friend.getRequestUri().toString());
856 configuration.getStringValue(friendPrefix + "/Name").setValue(friend.getName());
858 /* write null ID as terminator. */
859 configuration.getStringValue(sonePrefix + "/Friend." + friendId + "/ID").setValue(null);
861 /* write all blocked Sones. */
862 int blockedSoneCounter = 0;
863 for (String blockedSoneId : sone.getBlockedSoneIds()) {
864 String blockedSonePrefix = sonePrefix + "/BlockedSone." + blockedSoneCounter++;
865 configuration.getStringValue(blockedSonePrefix + "/ID").setValue(blockedSoneId);
867 configuration.getStringValue(sonePrefix + "/BlockedSone." + blockedSoneCounter + "/ID").setValue(null);
869 /* write all liked posts. */
870 int likedPostIdCounter = 0;
871 for (String soneLikedPostId : sone.getLikedPostIds()) {
872 String likedPostIdPrefix = sonePrefix + "/LikedPostId." + likedPostIdCounter++;
873 configuration.getStringValue(likedPostIdPrefix + "/ID").setValue(soneLikedPostId);
875 configuration.getStringValue(sonePrefix + "/LikedPostId." + likedPostIdCounter + "/ID").setValue(null);
877 /* write all liked replies. */
878 int likedReplyIdCounter = 0;
879 for (String soneLikedReplyId : sone.getLikedReplyIds()) {
880 String likedReplyIdPrefix = sonePrefix + "/LikedReplyId." + likedReplyIdCounter++;
881 configuration.getStringValue(likedReplyIdPrefix + "/ID").setValue(soneLikedReplyId);
883 configuration.getStringValue(sonePrefix + "/LikedReplyId." + likedReplyIdCounter + "/ID").setValue(null);
886 /* write null ID as terminator. */
887 configuration.getStringValue("Sone/Sone." + soneId + "/ID").setValue(null);
889 /* write all known Sones. */
890 int knownSonesCounter = 0;
891 for (Sone knownSone : getRemoteSones()) {
892 String knownSonePrefix = "KnownSone." + knownSonesCounter++;
893 configuration.getStringValue(knownSonePrefix + "/ID").setValue(knownSone.getId());
894 configuration.getStringValue(knownSonePrefix + "/Name").setValue(knownSone.getName());
895 configuration.getStringValue(knownSonePrefix + "/Key").setValue(knownSone.getRequestUri().toString());
896 /* TODO - store all known stuff? */
898 configuration.getStringValue("KnownSone." + knownSonesCounter + "/ID").setValue(null);
900 } catch (ConfigurationException ce1) {
901 logger.log(Level.WARNING, "Could not store configuration!", ce1);