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 {
59 /** The default Sones. */
60 private static final Set<String> defaultSones = new HashSet<String>();
64 defaultSones.add("USK@eRHt0ceFsHjRZ11j6dd68RSdIvfd8f9YjJLZ9lnhEyo,iJWjIWh6TkMZm1NY8qBranKTIuwsCPkVPG6T6c6ft-I,AQACAAE/Sone/3");
66 defaultSones.add("USK@RuW~uAO35Ipne896-1OmaVJNPuYE4ZIB5oZ5ziaU57A,7rV3uiyztXBDt03DCoRiNwiGjgFCJuznM9Okc1opURU,AQACAAE/Sone/24");
70 * Enumeration for the possible states of a {@link Sone}.
72 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
74 public enum SoneStatus {
76 /** The Sone is unknown, i.e. not yet downloaded. */
79 /** The Sone is idle, i.e. not being downloaded or inserted. */
82 /** The Sone is currently being inserted. */
85 /** The Sone is currently being downloaded. */
90 private static final Logger logger = Logging.getLogger(Core.class);
93 private final Options options = new Options();
95 /** The configuration. */
96 private Configuration configuration;
98 /** Interface to freenet. */
99 private FreenetInterface freenetInterface;
101 /** The Sone downloader. */
102 private SoneDownloader soneDownloader;
104 /** The Sone blacklist. */
105 private final Set<Sone> blacklistedSones = Collections.synchronizedSet(new HashSet<Sone>());
107 /** The local Sones. */
108 private final Set<Sone> localSones = Collections.synchronizedSet(new HashSet<Sone>());
110 /** Sone inserters. */
111 private final Map<Sone, SoneInserter> soneInserters = Collections.synchronizedMap(new HashMap<Sone, SoneInserter>());
113 /** The Sones’ statuses. */
114 private final Map<Sone, SoneStatus> soneStatuses = Collections.synchronizedMap(new HashMap<Sone, SoneStatus>());
116 /* various caches follow here. */
118 /** Cache for all known Sones. */
119 private final Map<String, Sone> soneCache = Collections.synchronizedMap(new HashMap<String, Sone>());
121 /** Cache for all known posts. */
122 private final Map<String, Post> postCache = Collections.synchronizedMap(new HashMap<String, Post>());
124 /** Cache for all known replies. */
125 private final Map<String, Reply> replyCache = Collections.synchronizedMap(new HashMap<String, Reply>());
128 * Creates a new core.
131 super("Sone Core", false);
139 * Returns the options of the Sone plugin.
141 * @return The options of the Sone plugin
143 public Options getOptions() {
148 * Sets the configuration of the core.
150 * @param configuration
151 * The configuration of the core
152 * @return This core (for method chaining)
154 public Core configuration(Configuration configuration) {
155 this.configuration = configuration;
160 * Sets the Freenet interface to use.
162 * @param freenetInterface
163 * The Freenet interface to use
164 * @return This core (for method chaining)
166 public Core freenetInterface(FreenetInterface freenetInterface) {
167 this.freenetInterface = freenetInterface;
168 soneDownloader = new SoneDownloader(this, freenetInterface);
169 soneDownloader.start();
174 * Returns the local Sones.
176 * @return The local Sones
178 public Set<Sone> getSones() {
179 return Filters.filteredSet(localSones, new Filter<Sone>() {
185 @SuppressWarnings("synthetic-access")
186 public boolean filterObject(Sone sone) {
187 return !blacklistedSones.contains(sone);
193 * Returns the Sone with the given ID, or an empty Sone that has been
194 * initialized with the given ID.
200 public Sone getSone(String soneId) {
201 if (!soneCache.containsKey(soneId)) {
202 Sone sone = new Sone(soneId);
203 soneCache.put(soneId, sone);
204 setSoneStatus(sone, SoneStatus.unknown);
206 return soneCache.get(soneId);
210 * Returns all known sones.
212 * @return All known sones
214 public Collection<Sone> getKnownSones() {
215 return Filters.filteredCollection(soneCache.values(), new Filter<Sone>() {
221 @SuppressWarnings("synthetic-access")
222 public boolean filterObject(Sone sone) {
223 return !blacklistedSones.contains(sone);
229 * Gets all known Sones that are not local Sones.
231 * @return All remote Sones
233 public Collection<Sone> getRemoteSones() {
234 return Filters.filteredCollection(getKnownSones(), new Filter<Sone>() {
237 @SuppressWarnings("synthetic-access")
238 public boolean filterObject(Sone sone) {
239 return !blacklistedSones.contains(sone) && !localSones.contains(sone);
245 * Returns all blacklisted Sones.
247 * @return All blacklisted Sones
249 public Collection<Sone> getBlacklistedSones() {
250 return Collections.unmodifiableCollection(blacklistedSones);
254 * Checks whether the given Sone is blacklisted.
258 * @return {@code true} if this Sone is blacklisted, {@code false} otherwise
260 public boolean isBlacklistedSone(Sone sone) {
261 return blacklistedSones.contains(sone);
265 * Returns the status of the given Sone.
268 * The Sone to get the status for
269 * @return The status of the Sone
271 public SoneStatus getSoneStatus(Sone sone) {
272 return soneStatuses.get(sone);
276 * Sets the status of the Sone.
279 * The Sone to set the status for
281 * The status of the Sone
283 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
284 soneStatuses.put(sone, soneStatus);
288 * Creates a new post and adds it to the given Sone.
291 * The sone that creates the post
293 * The text of the post
294 * @return The created post
296 public Post createPost(Sone sone, String text) {
297 return createPost(sone, System.currentTimeMillis(), text);
301 * Creates a new post and adds it to the given Sone.
304 * The Sone that creates the post
306 * The time of the post
308 * The text of the post
309 * @return The created post
311 public Post createPost(Sone sone, long time, String text) {
312 Post post = getPost(UUID.randomUUID().toString()).setSone(sone).setTime(time).setText(text);
321 * The Sone that posts the reply
323 * The post the reply refers to
325 * The text of the reply
326 * @return The created reply
328 public Reply createReply(Sone sone, Post post, String text) {
329 return createReply(sone, post, System.currentTimeMillis(), text);
336 * The Sone that posts the reply
338 * The post the reply refers to
340 * The time of the post
342 * The text of the reply
343 * @return The created reply
345 public Reply createReply(Sone sone, Post post, long time, String text) {
346 Reply reply = getReply(UUID.randomUUID().toString()).setSone(sone).setPost(post).setTime(time).setText(text);
347 sone.addReply(reply);
356 * Adds a Sone to watch for updates. The Sone needs to be completely
360 * The Sone to watch for updates
362 public void addSone(Sone sone) {
363 soneCache.put(sone.getId(), sone);
364 if (!localSones.contains(sone)) {
365 soneDownloader.addSone(sone);
370 * Adds the given Sone.
375 public void addLocalSone(Sone sone) {
376 if (localSones.add(sone)) {
377 setSoneStatus(sone, SoneStatus.idle);
378 SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
379 soneInserter.start();
380 soneInserters.put(sone, soneInserter);
385 * Blackslists the given Sone.
388 * The Sone to blacklist
390 public void blacklistSone(Sone sone) {
391 if (blacklistedSones.add(sone)) {
392 soneDownloader.removeSone(sone);
393 if (localSones.remove(sone)) {
394 SoneInserter soneInserter = soneInserters.remove(sone);
401 * Unblacklists the given Sone.
404 * The Sone to unblacklist
406 public void unblacklistSone(Sone sone) {
407 if (blacklistedSones.remove(sone)) {
408 if (sone.getInsertUri() != null) {
417 * Creates a new Sone at a random location.
420 * The name of the Sone
421 * @return The created Sone
422 * @throws SoneException
423 * if a Sone error occurs
425 public Sone createSone(String name) throws SoneException {
426 return createSone(name, "Sone", null, null);
430 * Creates a new Sone at the given location. If one of {@code requestUri} or
431 * {@code insertUrI} is {@code null}, the Sone is created at a random
435 * The name of the Sone
436 * @param documentName
437 * The document name in the SSK
439 * The request URI of the Sone, or {@link NullPointerException}
440 * to create a Sone at a random location
442 * The insert URI of the Sone, or {@code null} to create a Sone
443 * at a random location
444 * @return The created Sone
445 * @throws SoneException
446 * if a Sone error occurs
448 public Sone createSone(String name, String documentName, String requestUri, String insertUri) throws SoneException {
449 if ((name == null) || (name.trim().length() == 0)) {
450 throw new SoneException(Type.INVALID_SONE_NAME);
452 String finalRequestUri;
453 String finalInsertUri;
454 if ((requestUri == null) || (insertUri == null)) {
455 String[] keyPair = freenetInterface.generateKeyPair();
456 finalRequestUri = keyPair[0];
457 finalInsertUri = keyPair[1];
459 finalRequestUri = requestUri;
460 finalInsertUri = insertUri;
464 logger.log(Level.FINEST, "Creating new Sone “%s” at %s (%s)…", new Object[] { name, finalRequestUri, finalInsertUri });
465 sone = getSone(UUID.randomUUID().toString()).setName(name).setRequestUri(new FreenetURI(finalRequestUri).setKeyType("USK").setDocName(documentName)).setInsertUri(new FreenetURI(finalInsertUri).setKeyType("USK").setDocName(documentName));
466 sone.setProfile(new Profile());
467 /* set modification counter to 1 so it is inserted immediately. */
468 sone.setModificationCounter(1);
470 } catch (MalformedURLException mue1) {
471 throw new SoneException(Type.INVALID_URI);
477 * Loads the Sone from the given request URI. The fetching of the data is
478 * performed in a new thread so this method returns immediately.
481 * The request URI to load the Sone from
483 public void loadSone(final String requestUri) {
484 loadSone(requestUri, null);
488 * Loads the Sone from the given request URI. The fetching of the data is
489 * performed in a new thread so this method returns immediately. If
490 * {@code insertUri} is not {@code null} the loaded Sone is converted into a
491 * local Sone and available using as any other local Sone.
494 * The request URI to load the Sone from
496 * The insert URI of the Sone
498 public void loadSone(final String requestUri, final String insertUri) {
499 new Thread(new Runnable() {
502 @SuppressWarnings("synthetic-access")
505 FreenetURI realRequestUri = new FreenetURI(requestUri).setMetaString(new String[] { "sone.xml" });
506 FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
507 if (fetchResult == null) {
510 Sone parsedSone = soneDownloader.parseSone(null, fetchResult, realRequestUri);
511 if (parsedSone != null) {
512 if (insertUri != null) {
513 parsedSone.setInsertUri(new FreenetURI(insertUri));
514 addLocalSone(parsedSone);
518 setSoneStatus(parsedSone, SoneStatus.idle);
520 } catch (MalformedURLException mue1) {
521 logger.log(Level.INFO, "Could not create URI from “" + requestUri + "”.", mue1);
524 }, "Sone Downloader").start();
528 * Loads a Sone from an input stream.
530 * @param soneInputStream
531 * The input stream to load the Sone from
532 * @return The parsed Sone, or {@code null} if the Sone could not be parsed
534 public Sone loadSone(InputStream soneInputStream) {
535 Sone parsedSone = soneDownloader.parseSone(soneInputStream);
536 if (parsedSone == null) {
539 if (parsedSone.getInsertUri() != null) {
540 addLocalSone(parsedSone);
548 * Loads and updates the given Sone.
553 public void loadSone(final Sone sone) {
554 new Thread(new Runnable() {
557 @SuppressWarnings("synthetic-access")
559 FreenetURI realRequestUri = sone.getRequestUri().setMetaString(new String[] { "sone.xml" });
560 setSoneStatus(sone, SoneStatus.downloading);
562 FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
563 if (fetchResult == null) {
564 /* TODO - mark Sone as bad. */
567 Sone parsedSone = soneDownloader.parseSone(sone, fetchResult, realRequestUri);
568 if (parsedSone != null) {
572 setSoneStatus(sone, (sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
575 }, "Sone Downloader").start();
579 * Deletes the given Sone from this plugin instance.
584 public void deleteSone(Sone sone) {
585 SoneInserter soneInserter = soneInserters.remove(sone);
587 localSones.remove(sone);
588 soneStatuses.remove(sone);
589 soneCache.remove(sone.getId());
593 * Returns the post with the given ID. If no post exists yet with the given
594 * ID, a new post is returned.
600 public Post getPost(String postId) {
601 if (!postCache.containsKey(postId)) {
602 postCache.put(postId, new Post(postId));
604 return postCache.get(postId);
608 * Returns the reply with the given ID. If no reply exists yet with the
609 * given ID, a new reply is returned.
612 * The ID of the reply
615 public Reply getReply(String replyId) {
616 if (!replyCache.containsKey(replyId)) {
617 replyCache.put(replyId, new Reply(replyId));
619 return replyCache.get(replyId);
623 * Gets all replies to the given post, sorted by date, oldest first.
626 * The post the replies refer to
627 * @return The sorted list of replies for the post
629 public List<Reply> getReplies(Post post) {
630 List<Reply> replies = new ArrayList<Reply>();
631 for (Reply reply : replyCache.values()) {
632 if (reply.getPost().equals(post)) {
636 Collections.sort(replies, new Comparator<Reply>() {
642 public int compare(Reply leftReply, Reply rightReply) {
643 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime()));
650 * Gets all Sones that like the given post.
653 * The post to check for
654 * @return All Sones that like the post
656 public Collection<Sone> getLikes(final Post post) {
657 return Filters.filteredCollection(getKnownSones(), new Filter<Sone>() {
660 public boolean filterObject(Sone sone) {
661 return sone.isLikedPostId(post.getId());
667 * Gets all Sones that like the given reply.
670 * The reply to check for
671 * @return All Sones that like the reply
673 public Collection<Sone> getLikes(final Reply reply) {
674 return Filters.filteredCollection(getKnownSones(), new Filter<Sone>() {
677 public boolean filterObject(Sone sone) {
678 return sone.isLikedReplyId(reply.getId());
684 * Deletes the given reply. It is removed from its Sone and from the reply
688 * The reply to remove
690 public void deleteReply(Reply reply) {
691 reply.getSone().removeReply(reply);
692 replyCache.remove(reply.getId());
703 protected void serviceStart() {
711 protected void serviceStop() {
712 soneDownloader.stop();
713 /* stop all Sone inserters. */
714 for (SoneInserter soneInserter : soneInserters.values()) {
725 * Adds some default Sones.
727 private void addDefaultSones() {
728 for (String soneUri : defaultSones) {
734 * Loads the configuration.
736 @SuppressWarnings("unchecked")
737 private void loadConfiguration() {
738 logger.entering(Core.class.getName(), "loadConfiguration()");
740 boolean firstStart = configuration.getBooleanValue("FirstStart").getValue(true);
742 logger.log(Level.INFO, "First start of Sone, adding a couple of default Sones…");
745 configuration.getBooleanValue("FirstStart").setValue(false);
746 } catch (ConfigurationException ce1) {
747 logger.log(Level.WARNING, "Could not clear “first start” flag!");
751 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
754 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
755 SoneInserter.setInsertionDelay(newValue);
760 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
761 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
767 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
768 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
769 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
770 options.getBooleanOption("ClearOnNextRestart").set(null);
771 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
772 if (clearConfiguration) {
773 /* stop loading the configuration. */
778 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
780 /* parse local Sones. */
781 logger.log(Level.INFO, "Loading Sones…");
784 String sonePrefix = "Sone/Sone." + soneId++;
785 String id = configuration.getStringValue(sonePrefix + "/ID").getValue(null);
789 String name = configuration.getStringValue(sonePrefix + "/Name").getValue(null);
790 long time = configuration.getLongValue(sonePrefix + "/Time").getValue((long) 0);
791 String insertUri = configuration.getStringValue(sonePrefix + "/InsertURI").getValue(null);
792 String requestUri = configuration.getStringValue(sonePrefix + "/RequestURI").getValue(null);
793 long modificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0);
794 String firstName = configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null);
795 String middleName = configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null);
796 String lastName = configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null);
797 Integer birthDay = configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null);
798 Integer birthMonth = configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null);
799 Integer birthYear = configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null);
801 Profile profile = new Profile();
802 profile.setFirstName(firstName);
803 profile.setMiddleName(middleName);
804 profile.setLastName(lastName);
805 profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear);
806 Sone sone = getSone(id).setName(name).setTime(time).setRequestUri(new FreenetURI(requestUri)).setInsertUri(new FreenetURI(insertUri));
807 sone.setProfile(profile);
810 String postPrefix = sonePrefix + "/Post." + postId++;
811 id = configuration.getStringValue(postPrefix + "/ID").getValue(null);
815 time = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
816 String text = configuration.getStringValue(postPrefix + "/Text").getValue(null);
817 Post post = getPost(id).setSone(sone).setTime(time).setText(text);
820 int replyCounter = 0;
822 String replyPrefix = sonePrefix + "/Reply." + replyCounter++;
823 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
824 if (replyId == null) {
827 Post replyPost = getPost(configuration.getStringValue(replyPrefix + "/Post").getValue(null));
828 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue(null);
829 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
830 Reply reply = getReply(replyId).setSone(sone).setPost(replyPost).setTime(replyTime).setText(replyText);
831 sone.addReply(reply);
835 int friendCounter = 0;
837 String friendPrefix = sonePrefix + "/Friend." + friendCounter++;
838 String friendId = configuration.getStringValue(friendPrefix + "/ID").getValue(null);
839 if (friendId == null) {
842 Sone friendSone = getSone(friendId);
843 String friendKey = configuration.getStringValue(friendPrefix + "/Key").getValue(null);
844 String friendName = configuration.getStringValue(friendPrefix + "/Name").getValue(null);
845 friendSone.setRequestUri(new FreenetURI(friendKey)).setName(friendName);
846 sone.addFriend(friendSone);
849 /* load blocked Sone IDs. */
850 int blockedSoneCounter = 0;
852 String blockedSonePrefix = sonePrefix + "/BlockedSone." + blockedSoneCounter++;
853 String blockedSoneId = configuration.getStringValue(blockedSonePrefix + "/ID").getValue(null);
854 if (blockedSoneId == null) {
857 sone.addBlockedSoneId(blockedSoneId);
860 /* load liked post IDs. */
861 int likedPostIdCounter = 0;
863 String likedPostIdPrefix = sonePrefix + "/LikedPostId." + likedPostIdCounter++;
864 String likedPostId = configuration.getStringValue(likedPostIdPrefix + "/ID").getValue(null);
865 if (likedPostId == null) {
868 sone.addLikedPostId(likedPostId);
871 /* load liked reply IDs. */
872 int likedReplyIdCounter = 0;
874 String likedReplyIdPrefix = sonePrefix + "/LikedReplyId." + likedReplyIdCounter++;
875 String likedReplyId = configuration.getStringValue(likedReplyIdPrefix + "/ID").getValue(null);
876 if (likedReplyId == null) {
879 sone.addLikedReplyId(likedReplyId);
882 sone.setModificationCounter(modificationCounter);
884 } catch (MalformedURLException mue1) {
885 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
888 logger.log(Level.INFO, "Loaded %d Sones.", getSones().size());
890 /* load all known Sones. */
891 int knownSonesCounter = 0;
893 String knownSonePrefix = "KnownSone." + knownSonesCounter++;
894 String knownSoneId = configuration.getStringValue(knownSonePrefix + "/ID").getValue(null);
895 if (knownSoneId == null) {
898 String knownSoneName = configuration.getStringValue(knownSonePrefix + "/Name").getValue(null);
899 String knownSoneKey = configuration.getStringValue(knownSonePrefix + "/Key").getValue(null);
901 getSone(knownSoneId).setName(knownSoneName).setRequestUri(new FreenetURI(knownSoneKey));
902 } catch (MalformedURLException mue1) {
903 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + knownSoneKey + "”)!", mue1);
907 /* load all blacklisted Sones. */
908 int blacklistedSonesCounter = 0;
910 String blacklistedSonePrefix = "BlacklistedSone." + blacklistedSonesCounter++;
911 String blacklistedSoneId = configuration.getStringValue(blacklistedSonePrefix + "/ID").getValue(null);
912 if (blacklistedSoneId == null) {
915 String blacklistedSoneName = configuration.getStringValue(blacklistedSonePrefix + "/Name").getValue(null);
916 String blacklistedSoneKey = configuration.getStringValue(blacklistedSonePrefix + "/Key").getValue(null);
917 String blacklistedSoneInsertKey = configuration.getStringValue(blacklistedSonePrefix + "/InsertKey").getValue(null);
919 blacklistSone(getSone(blacklistedSoneId).setName(blacklistedSoneName).setRequestUri(new FreenetURI(blacklistedSoneKey)).setInsertUri((blacklistedSoneInsertKey != null) ? new FreenetURI(blacklistedSoneInsertKey) : null));
920 } catch (MalformedURLException mue1) {
921 logger.log(Level.WARNING, "Could not create blacklisted Sone from requestUri (“" + blacklistedSoneKey + "”)!", mue1);
925 /* load all remote Sones. */
926 for (Sone remoteSone : getRemoteSones()) {
927 loadSone(remoteSone);
930 logger.exiting(Core.class.getName(), "loadConfiguration()");
934 * Saves the configuraiton.
936 private void saveConfiguration() {
937 Set<Sone> sones = getSones();
938 logger.log(Level.INFO, "Storing %d Sones…", sones.size());
941 /* store the options first. */
942 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
943 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
944 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
946 /* store all Sones. */
948 for (Sone sone : localSones) {
949 String sonePrefix = "Sone/Sone." + soneId++;
950 configuration.getStringValue(sonePrefix + "/ID").setValue(sone.getId());
951 configuration.getStringValue(sonePrefix + "/Name").setValue(sone.getName());
952 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
953 configuration.getStringValue(sonePrefix + "/RequestURI").setValue(sone.getRequestUri().toString());
954 configuration.getStringValue(sonePrefix + "/InsertURI").setValue(sone.getInsertUri().toString());
955 configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter());
956 Profile profile = sone.getProfile();
957 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
958 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
959 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
960 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
961 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
962 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
964 for (Post post : sone.getPosts()) {
965 String postPrefix = sonePrefix + "/Post." + postId++;
966 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
967 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
968 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
970 /* write null ID as terminator. */
971 configuration.getStringValue(sonePrefix + "/Post." + postId + "/ID").setValue(null);
974 for (Reply reply : sone.getReplies()) {
975 String replyPrefix = sonePrefix + "/Reply." + replyId++;
976 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
977 configuration.getStringValue(replyPrefix + "/Post").setValue(reply.getPost().getId());
978 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
979 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
981 /* write null ID as terminator. */
982 configuration.getStringValue(sonePrefix + "/Reply." + replyId + "/ID").setValue(null);
985 for (Sone friend : sone.getFriends()) {
986 String friendPrefix = sonePrefix + "/Friend." + friendId++;
987 configuration.getStringValue(friendPrefix + "/ID").setValue(friend.getId());
988 configuration.getStringValue(friendPrefix + "/Key").setValue(friend.getRequestUri().toString());
989 configuration.getStringValue(friendPrefix + "/Name").setValue(friend.getName());
991 /* write null ID as terminator. */
992 configuration.getStringValue(sonePrefix + "/Friend." + friendId + "/ID").setValue(null);
994 /* write all blocked Sones. */
995 int blockedSoneCounter = 0;
996 for (String blockedSoneId : sone.getBlockedSoneIds()) {
997 String blockedSonePrefix = sonePrefix + "/BlockedSone." + blockedSoneCounter++;
998 configuration.getStringValue(blockedSonePrefix + "/ID").setValue(blockedSoneId);
1000 configuration.getStringValue(sonePrefix + "/BlockedSone." + blockedSoneCounter + "/ID").setValue(null);
1002 /* write all liked posts. */
1003 int likedPostIdCounter = 0;
1004 for (String soneLikedPostId : sone.getLikedPostIds()) {
1005 String likedPostIdPrefix = sonePrefix + "/LikedPostId." + likedPostIdCounter++;
1006 configuration.getStringValue(likedPostIdPrefix + "/ID").setValue(soneLikedPostId);
1008 configuration.getStringValue(sonePrefix + "/LikedPostId." + likedPostIdCounter + "/ID").setValue(null);
1010 /* write all liked replies. */
1011 int likedReplyIdCounter = 0;
1012 for (String soneLikedReplyId : sone.getLikedReplyIds()) {
1013 String likedReplyIdPrefix = sonePrefix + "/LikedReplyId." + likedReplyIdCounter++;
1014 configuration.getStringValue(likedReplyIdPrefix + "/ID").setValue(soneLikedReplyId);
1016 configuration.getStringValue(sonePrefix + "/LikedReplyId." + likedReplyIdCounter + "/ID").setValue(null);
1019 /* write null ID as terminator. */
1020 configuration.getStringValue("Sone/Sone." + soneId + "/ID").setValue(null);
1022 /* write all known Sones. */
1023 int knownSonesCounter = 0;
1024 for (Sone knownSone : getRemoteSones()) {
1025 String knownSonePrefix = "KnownSone." + knownSonesCounter++;
1026 configuration.getStringValue(knownSonePrefix + "/ID").setValue(knownSone.getId());
1027 configuration.getStringValue(knownSonePrefix + "/Name").setValue(knownSone.getName());
1028 configuration.getStringValue(knownSonePrefix + "/Key").setValue(knownSone.getRequestUri().toString());
1029 /* TODO - store all known stuff? */
1031 configuration.getStringValue("KnownSone." + knownSonesCounter + "/ID").setValue(null);
1033 /* write all blacklisted Sones. */
1034 int blacklistedSonesCounter = 0;
1035 for (Sone blacklistedSone : getBlacklistedSones()) {
1036 String blacklistedSonePrefix = "BlacklistedSone." + blacklistedSonesCounter++;
1037 configuration.getStringValue(blacklistedSonePrefix + "/ID").setValue(blacklistedSone.getId());
1038 configuration.getStringValue(blacklistedSonePrefix + "/Name").setValue(blacklistedSone.getName());
1039 configuration.getStringValue(blacklistedSonePrefix + "/Key").setValue(blacklistedSone.getRequestUri().toString());
1040 configuration.getStringValue(blacklistedSonePrefix + "/InsertKey").setValue((blacklistedSone.getInsertUri() != null) ? blacklistedSone.getInsertUri().toString() : null);
1041 /* TODO - store all known stuff? */
1043 configuration.getStringValue("BlacklistedSone." + blacklistedSonesCounter + "/ID").setValue(null);
1045 } catch (ConfigurationException ce1) {
1046 logger.log(Level.WARNING, "Could not store configuration!", ce1);