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 Sone blacklist. */
95 private final Set<Sone> blacklistedSones = new HashSet<Sone>();
97 /** The local Sones. */
98 private final Set<Sone> localSones = new HashSet<Sone>();
100 /** Sone inserters. */
101 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
103 /** The Sones’ statuses. */
104 private final Map<Sone, SoneStatus> soneStatuses = Collections.synchronizedMap(new HashMap<Sone, SoneStatus>());
106 /* various caches follow here. */
108 /** Cache for all known Sones. */
109 private final Map<String, Sone> soneCache = new HashMap<String, Sone>();
111 /** Cache for all known posts. */
112 private final Map<String, Post> postCache = new HashMap<String, Post>();
114 /** Cache for all known replies. */
115 private final Map<String, Reply> replyCache = new HashMap<String, Reply>();
118 * Creates a new core.
121 super("Sone Core", false);
129 * Returns the options of the Sone plugin.
131 * @return The options of the Sone plugin
133 public Options getOptions() {
138 * Sets the configuration of the core.
140 * @param configuration
141 * The configuration of the core
142 * @return This core (for method chaining)
144 public Core configuration(Configuration configuration) {
145 this.configuration = configuration;
150 * Sets the Freenet interface to use.
152 * @param freenetInterface
153 * The Freenet interface to use
154 * @return This core (for method chaining)
156 public Core freenetInterface(FreenetInterface freenetInterface) {
157 this.freenetInterface = freenetInterface;
158 soneDownloader = new SoneDownloader(this, freenetInterface);
159 soneDownloader.start();
164 * Returns the local Sones.
166 * @return The local Sones
168 public Set<Sone> getSones() {
169 return Filters.filteredSet(localSones, new Filter<Sone>() {
175 @SuppressWarnings("synthetic-access")
176 public boolean filterObject(Sone sone) {
177 return !blacklistedSones.contains(sone);
183 * Returns the Sone with the given ID, or an empty Sone that has been
184 * initialized with the given ID.
190 public Sone getSone(String soneId) {
191 if (!soneCache.containsKey(soneId)) {
192 Sone sone = new Sone(soneId);
193 soneCache.put(soneId, sone);
194 setSoneStatus(sone, SoneStatus.unknown);
196 return soneCache.get(soneId);
200 * Returns all known sones.
202 * @return All known sones
204 public Collection<Sone> getKnownSones() {
205 return Filters.filteredCollection(soneCache.values(), new Filter<Sone>() {
211 @SuppressWarnings("synthetic-access")
212 public boolean filterObject(Sone sone) {
213 return !blacklistedSones.contains(sone);
219 * Gets all known Sones that are not local Sones.
221 * @return All remote Sones
223 public Collection<Sone> getRemoteSones() {
224 return Filters.filteredCollection(getKnownSones(), new Filter<Sone>() {
227 @SuppressWarnings("synthetic-access")
228 public boolean filterObject(Sone sone) {
229 return !blacklistedSones.contains(sone) && !localSones.contains(sone);
235 * Returns all blacklisted Sones.
237 * @return All blacklisted Sones
239 public Collection<Sone> getBlacklistedSones() {
240 return Collections.unmodifiableCollection(blacklistedSones);
244 * Checks whether the given Sone is blacklisted.
248 * @return {@code true} if this Sone is blacklisted, {@code false} otherwise
250 public boolean isBlacklistedSone(Sone sone) {
251 return blacklistedSones.contains(sone);
255 * Returns the status of the given Sone.
258 * The Sone to get the status for
259 * @return The status of the Sone
261 public SoneStatus getSoneStatus(Sone sone) {
262 return soneStatuses.get(sone);
266 * Sets the status of the Sone.
269 * The Sone to set the status for
271 * The status of the Sone
273 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
274 soneStatuses.put(sone, soneStatus);
278 * Creates a new post and adds it to the given Sone.
281 * The sone that creates the post
283 * The text of the post
284 * @return The created post
286 public Post createPost(Sone sone, String text) {
287 return createPost(sone, System.currentTimeMillis(), text);
291 * Creates a new post and adds it to the given Sone.
294 * The Sone that creates the post
296 * The time of the post
298 * The text of the post
299 * @return The created post
301 public Post createPost(Sone sone, long time, String text) {
302 Post post = getPost(UUID.randomUUID().toString()).setSone(sone).setTime(time).setText(text);
311 * The Sone that posts the reply
313 * The post the reply refers to
315 * The text of the reply
316 * @return The created reply
318 public Reply createReply(Sone sone, Post post, String text) {
319 return createReply(sone, post, System.currentTimeMillis(), text);
326 * The Sone that posts the reply
328 * The post the reply refers to
330 * The time of the post
332 * The text of the reply
333 * @return The created reply
335 public Reply createReply(Sone sone, Post post, long time, String text) {
336 Reply reply = getReply(UUID.randomUUID().toString()).setSone(sone).setPost(post).setTime(time).setText(text);
337 sone.addReply(reply);
346 * Adds a Sone to watch for updates. The Sone needs to be completely
350 * The Sone to watch for updates
352 public void addSone(Sone sone) {
353 soneCache.put(sone.getId(), sone);
354 if (!localSones.contains(sone)) {
355 soneDownloader.addSone(sone);
360 * Adds the given Sone.
365 public void addLocalSone(Sone sone) {
366 if (localSones.add(sone)) {
367 setSoneStatus(sone, SoneStatus.idle);
368 SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
369 soneInserter.start();
370 soneInserters.put(sone, soneInserter);
375 * Blackslists the given Sone.
378 * The Sone to blacklist
380 public void blacklistSone(Sone sone) {
381 if (blacklistedSones.add(sone)) {
382 soneDownloader.removeSone(sone);
383 if (localSones.remove(sone)) {
384 SoneInserter soneInserter = soneInserters.remove(sone);
391 * Unblacklists the given Sone.
394 * The Sone to unblacklist
396 public void unblacklistSone(Sone sone) {
397 if (blacklistedSones.remove(sone)) {
398 if (sone.getInsertUri() != null) {
407 * Creates a new Sone at a random location.
410 * The name of the Sone
411 * @return The created Sone
412 * @throws SoneException
413 * if a Sone error occurs
415 public Sone createSone(String name) throws SoneException {
416 return createSone(name, "Sone", null, null);
420 * Creates a new Sone at the given location. If one of {@code requestUri} or
421 * {@code insertUrI} is {@code null}, the Sone is created at a random
425 * The name of the Sone
426 * @param documentName
427 * The document name in the SSK
429 * The request URI of the Sone, or {@link NullPointerException}
430 * to create a Sone at a random location
432 * The insert URI of the Sone, or {@code null} to create a Sone
433 * at a random location
434 * @return The created Sone
435 * @throws SoneException
436 * if a Sone error occurs
438 public Sone createSone(String name, String documentName, String requestUri, String insertUri) throws SoneException {
439 if ((name == null) || (name.trim().length() == 0)) {
440 throw new SoneException(Type.INVALID_SONE_NAME);
442 String finalRequestUri;
443 String finalInsertUri;
444 if ((requestUri == null) || (insertUri == null)) {
445 String[] keyPair = freenetInterface.generateKeyPair();
446 finalRequestUri = keyPair[0];
447 finalInsertUri = keyPair[1];
449 finalRequestUri = requestUri;
450 finalInsertUri = insertUri;
454 logger.log(Level.FINEST, "Creating new Sone “%s” at %s (%s)…", new Object[] { name, finalRequestUri, finalInsertUri });
455 sone = getSone(UUID.randomUUID().toString()).setName(name).setRequestUri(new FreenetURI(finalRequestUri).setKeyType("USK").setDocName(documentName)).setInsertUri(new FreenetURI(finalInsertUri).setKeyType("USK").setDocName(documentName));
456 sone.setProfile(new Profile());
457 /* set modification counter to 1 so it is inserted immediately. */
458 sone.setModificationCounter(1);
460 } catch (MalformedURLException mue1) {
461 throw new SoneException(Type.INVALID_URI);
467 * Loads the Sone from the given request URI. The fetching of the data is
468 * performed in a new thread so this method returns immediately.
471 * The request URI to load the Sone from
473 public void loadSone(final String requestUri) {
474 loadSone(requestUri, null);
478 * Loads the Sone from the given request URI. The fetching of the data is
479 * performed in a new thread so this method returns immediately. If
480 * {@code insertUri} is not {@code null} the loaded Sone is converted into a
481 * local Sone and available using as any other local Sone.
484 * The request URI to load the Sone from
486 * The insert URI of the Sone
488 public void loadSone(final String requestUri, final String insertUri) {
489 new Thread(new Runnable() {
492 @SuppressWarnings("synthetic-access")
495 FreenetURI realRequestUri = new FreenetURI(requestUri).setMetaString(new String[] { "sone.xml" });
496 FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
497 if (fetchResult == null) {
500 Sone parsedSone = soneDownloader.parseSone(null, fetchResult, realRequestUri);
501 if (parsedSone != null) {
502 if (insertUri != null) {
503 parsedSone.setInsertUri(new FreenetURI(insertUri));
504 addLocalSone(parsedSone);
508 setSoneStatus(parsedSone, SoneStatus.idle);
510 } catch (MalformedURLException mue1) {
511 logger.log(Level.INFO, "Could not create URI from “" + requestUri + "”.", mue1);
514 }, "Sone Downloader").start();
518 * Loads a Sone from an input stream.
520 * @param soneInputStream
521 * The input stream to load the Sone from
522 * @return The parsed Sone, or {@code null} if the Sone could not be parsed
524 public Sone loadSone(InputStream soneInputStream) {
525 Sone parsedSone = soneDownloader.parseSone(soneInputStream);
526 if (parsedSone == null) {
529 if (parsedSone.getInsertUri() != null) {
530 addLocalSone(parsedSone);
538 * Loads and updates the given Sone.
543 public void loadSone(final Sone sone) {
544 new Thread(new Runnable() {
547 @SuppressWarnings("synthetic-access")
549 FreenetURI realRequestUri = sone.getRequestUri().setMetaString(new String[] { "sone.xml" });
550 setSoneStatus(sone, SoneStatus.downloading);
552 FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
553 if (fetchResult == null) {
554 /* TODO - mark Sone as bad. */
557 Sone parsedSone = soneDownloader.parseSone(sone, fetchResult, realRequestUri);
558 if (parsedSone != null) {
562 setSoneStatus(sone, (sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
565 }, "Sone Downloader").start();
569 * Deletes the given Sone from this plugin instance.
574 public void deleteSone(Sone sone) {
575 SoneInserter soneInserter = soneInserters.remove(sone);
577 localSones.remove(sone);
578 soneStatuses.remove(sone);
579 soneCache.remove(sone.getId());
583 * Returns the post with the given ID. If no post exists yet with the given
584 * ID, a new post is returned.
590 public Post getPost(String postId) {
591 if (!postCache.containsKey(postId)) {
592 postCache.put(postId, new Post(postId));
594 return postCache.get(postId);
598 * Returns the reply with the given ID. If no reply exists yet with the
599 * given ID, a new reply is returned.
602 * The ID of the reply
605 public Reply getReply(String replyId) {
606 if (!replyCache.containsKey(replyId)) {
607 replyCache.put(replyId, new Reply(replyId));
609 return replyCache.get(replyId);
613 * Gets all replies to the given post, sorted by date, oldest first.
616 * The post the replies refer to
617 * @return The sorted list of replies for the post
619 public List<Reply> getReplies(Post post) {
620 List<Reply> replies = new ArrayList<Reply>();
621 for (Reply reply : replyCache.values()) {
622 if (reply.getPost().equals(post)) {
626 Collections.sort(replies, new Comparator<Reply>() {
632 public int compare(Reply leftReply, Reply rightReply) {
633 return (int) Math.max(Integer.MIN_VALUE, Math.min(Integer.MAX_VALUE, leftReply.getTime() - rightReply.getTime()));
640 * Gets all Sones that like the given post.
643 * The post to check for
644 * @return All Sones that like the post
646 public Set<Sone> getLikes(final Post post) {
647 return Filters.filteredSet(getSones(), new Filter<Sone>() {
650 public boolean filterObject(Sone sone) {
651 return sone.isLikedPostId(post.getId());
657 * Gets all Sones that like the given reply.
660 * The reply to check for
661 * @return All Sones that like the reply
663 public Set<Sone> getLikes(final Reply reply) {
664 return Filters.filteredSet(getSones(), new Filter<Sone>() {
667 public boolean filterObject(Sone sone) {
668 return sone.isLikedReplyId(reply.getId());
674 * Deletes the given reply. It is removed from its Sone and from the reply
678 * The reply to remove
680 public void deleteReply(Reply reply) {
681 reply.getSone().removeReply(reply);
682 replyCache.remove(reply.getId());
693 protected void serviceStart() {
701 protected void serviceStop() {
702 soneDownloader.stop();
703 /* stop all Sone inserters. */
704 for (SoneInserter soneInserter : soneInserters.values()) {
715 * Adds some default Sones.
717 private void addDefaultSones() {
719 loadSone("USK@eRHt0ceFsHjRZ11j6dd68RSdIvfd8f9YjJLZ9lnhEyo,iJWjIWh6TkMZm1NY8qBranKTIuwsCPkVPG6T6c6ft-I,AQACAAE/Sone/0");
721 loadSone("USK@RuW~uAO35Ipne896-1OmaVJNPuYE4ZIB5oZ5ziaU57A,7rV3uiyztXBDt03DCoRiNwiGjgFCJuznM9Okc1opURU,AQACAAE/Sone/15");
725 * Loads the configuration.
727 @SuppressWarnings("unchecked")
728 private void loadConfiguration() {
729 logger.entering(Core.class.getName(), "loadConfiguration()");
731 boolean firstStart = configuration.getBooleanValue("FirstStart").getValue(true);
733 logger.log(Level.INFO, "First start of Sone, adding a couple of default Sones…");
736 configuration.getBooleanValue("FirstStart").setValue(false);
737 } catch (ConfigurationException ce1) {
738 logger.log(Level.WARNING, "Could not clear “first start” flag!");
742 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
745 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
746 SoneInserter.setInsertionDelay(newValue);
751 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
752 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
758 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
759 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
760 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
761 options.getBooleanOption("ClearOnNextRestart").set(null);
762 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
763 if (clearConfiguration) {
764 /* stop loading the configuration. */
769 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
771 /* parse local Sones. */
772 logger.log(Level.INFO, "Loading Sones…");
775 String sonePrefix = "Sone/Sone." + soneId++;
776 String id = configuration.getStringValue(sonePrefix + "/ID").getValue(null);
780 String name = configuration.getStringValue(sonePrefix + "/Name").getValue(null);
781 long time = configuration.getLongValue(sonePrefix + "/Time").getValue((long) 0);
782 String insertUri = configuration.getStringValue(sonePrefix + "/InsertURI").getValue(null);
783 String requestUri = configuration.getStringValue(sonePrefix + "/RequestURI").getValue(null);
784 long modificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0);
785 String firstName = configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null);
786 String middleName = configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null);
787 String lastName = configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null);
788 Integer birthDay = configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null);
789 Integer birthMonth = configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null);
790 Integer birthYear = configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null);
792 Profile profile = new Profile();
793 profile.setFirstName(firstName);
794 profile.setMiddleName(middleName);
795 profile.setLastName(lastName);
796 profile.setBirthDay(birthDay).setBirthMonth(birthMonth).setBirthYear(birthYear);
797 Sone sone = getSone(id).setName(name).setTime(time).setRequestUri(new FreenetURI(requestUri)).setInsertUri(new FreenetURI(insertUri));
798 sone.setProfile(profile);
801 String postPrefix = sonePrefix + "/Post." + postId++;
802 id = configuration.getStringValue(postPrefix + "/ID").getValue(null);
806 time = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
807 String text = configuration.getStringValue(postPrefix + "/Text").getValue(null);
808 Post post = getPost(id).setSone(sone).setTime(time).setText(text);
811 int replyCounter = 0;
813 String replyPrefix = sonePrefix + "/Reply." + replyCounter++;
814 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
815 if (replyId == null) {
818 Post replyPost = getPost(configuration.getStringValue(replyPrefix + "/Post").getValue(null));
819 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue(null);
820 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
821 Reply reply = getReply(replyId).setSone(sone).setPost(replyPost).setTime(replyTime).setText(replyText);
822 sone.addReply(reply);
826 int friendCounter = 0;
828 String friendPrefix = sonePrefix + "/Friend." + friendCounter++;
829 String friendId = configuration.getStringValue(friendPrefix + "/ID").getValue(null);
830 if (friendId == null) {
833 Sone friendSone = getSone(friendId);
834 String friendKey = configuration.getStringValue(friendPrefix + "/Key").getValue(null);
835 String friendName = configuration.getStringValue(friendPrefix + "/Name").getValue(null);
836 friendSone.setRequestUri(new FreenetURI(friendKey)).setName(friendName);
837 sone.addFriend(friendSone);
840 /* load blocked Sone IDs. */
841 int blockedSoneCounter = 0;
843 String blockedSonePrefix = sonePrefix + "/BlockedSone." + blockedSoneCounter++;
844 String blockedSoneId = configuration.getStringValue(blockedSonePrefix + "/ID").getValue(null);
845 if (blockedSoneId == null) {
848 sone.addBlockedSoneId(blockedSoneId);
851 /* load liked post IDs. */
852 int likedPostIdCounter = 0;
854 String likedPostIdPrefix = sonePrefix + "/LikedPostId." + likedPostIdCounter++;
855 String likedPostId = configuration.getStringValue(likedPostIdPrefix + "/ID").getValue(null);
856 if (likedPostId == null) {
859 sone.addLikedPostId(likedPostId);
862 /* load liked reply IDs. */
863 int likedReplyIdCounter = 0;
865 String likedReplyIdPrefix = sonePrefix + "/LikedReplyId." + likedReplyIdCounter++;
866 String likedReplyId = configuration.getStringValue(likedReplyIdPrefix + "/ID").getValue(null);
867 if (likedReplyId == null) {
870 sone.addLikedReplyId(likedReplyId);
873 sone.setModificationCounter(modificationCounter);
875 } catch (MalformedURLException mue1) {
876 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
879 logger.log(Level.INFO, "Loaded %d Sones.", getSones().size());
881 /* load all known Sones. */
882 int knownSonesCounter = 0;
884 String knownSonePrefix = "KnownSone." + knownSonesCounter++;
885 String knownSoneId = configuration.getStringValue(knownSonePrefix + "/ID").getValue(null);
886 if (knownSoneId == null) {
889 String knownSoneName = configuration.getStringValue(knownSonePrefix + "/Name").getValue(null);
890 String knownSoneKey = configuration.getStringValue(knownSonePrefix + "/Key").getValue(null);
892 getSone(knownSoneId).setName(knownSoneName).setRequestUri(new FreenetURI(knownSoneKey));
893 } catch (MalformedURLException mue1) {
894 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + knownSoneKey + "”)!", mue1);
898 /* load all blacklisted Sones. */
899 int blacklistedSonesCounter = 0;
901 String blacklistedSonePrefix = "BlacklistedSone." + blacklistedSonesCounter++;
902 String blacklistedSoneId = configuration.getStringValue(blacklistedSonePrefix + "/ID").getValue(null);
903 if (blacklistedSoneId == null) {
906 String blacklistedSoneName = configuration.getStringValue(blacklistedSonePrefix + "/Name").getValue(null);
907 String blacklistedSoneKey = configuration.getStringValue(blacklistedSonePrefix + "/Key").getValue(null);
908 String blacklistedSoneInsertKey = configuration.getStringValue(blacklistedSonePrefix + "/InsertKey").getValue(null);
910 blacklistSone(getSone(blacklistedSoneId).setName(blacklistedSoneName).setRequestUri(new FreenetURI(blacklistedSoneKey)).setInsertUri((blacklistedSoneInsertKey != null) ? new FreenetURI(blacklistedSoneInsertKey) : null));
911 } catch (MalformedURLException mue1) {
912 logger.log(Level.WARNING, "Could not create blacklisted Sone from requestUri (“" + blacklistedSoneKey + "”)!", mue1);
916 /* load all remote Sones. */
917 for (Sone remoteSone : getRemoteSones()) {
918 loadSone(remoteSone);
921 logger.exiting(Core.class.getName(), "loadConfiguration()");
925 * Saves the configuraiton.
927 private void saveConfiguration() {
928 Set<Sone> sones = getSones();
929 logger.log(Level.INFO, "Storing %d Sones…", sones.size());
932 /* store the options first. */
933 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
934 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
935 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
937 /* store all Sones. */
939 for (Sone sone : localSones) {
940 String sonePrefix = "Sone/Sone." + soneId++;
941 configuration.getStringValue(sonePrefix + "/ID").setValue(sone.getId());
942 configuration.getStringValue(sonePrefix + "/Name").setValue(sone.getName());
943 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
944 configuration.getStringValue(sonePrefix + "/RequestURI").setValue(sone.getRequestUri().toString());
945 configuration.getStringValue(sonePrefix + "/InsertURI").setValue(sone.getInsertUri().toString());
946 configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter());
947 Profile profile = sone.getProfile();
948 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
949 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
950 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
951 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
952 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
953 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
955 for (Post post : sone.getPosts()) {
956 String postPrefix = sonePrefix + "/Post." + postId++;
957 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
958 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
959 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
961 /* write null ID as terminator. */
962 configuration.getStringValue(sonePrefix + "/Post." + postId + "/ID").setValue(null);
965 for (Reply reply : sone.getReplies()) {
966 String replyPrefix = sonePrefix + "/Reply." + replyId++;
967 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
968 configuration.getStringValue(replyPrefix + "/Post").setValue(reply.getPost().getId());
969 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
970 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
972 /* write null ID as terminator. */
973 configuration.getStringValue(sonePrefix + "/Reply." + replyId + "/ID").setValue(null);
976 for (Sone friend : sone.getFriends()) {
977 String friendPrefix = sonePrefix + "/Friend." + friendId++;
978 configuration.getStringValue(friendPrefix + "/ID").setValue(friend.getId());
979 configuration.getStringValue(friendPrefix + "/Key").setValue(friend.getRequestUri().toString());
980 configuration.getStringValue(friendPrefix + "/Name").setValue(friend.getName());
982 /* write null ID as terminator. */
983 configuration.getStringValue(sonePrefix + "/Friend." + friendId + "/ID").setValue(null);
985 /* write all blocked Sones. */
986 int blockedSoneCounter = 0;
987 for (String blockedSoneId : sone.getBlockedSoneIds()) {
988 String blockedSonePrefix = sonePrefix + "/BlockedSone." + blockedSoneCounter++;
989 configuration.getStringValue(blockedSonePrefix + "/ID").setValue(blockedSoneId);
991 configuration.getStringValue(sonePrefix + "/BlockedSone." + blockedSoneCounter + "/ID").setValue(null);
993 /* write all liked posts. */
994 int likedPostIdCounter = 0;
995 for (String soneLikedPostId : sone.getLikedPostIds()) {
996 String likedPostIdPrefix = sonePrefix + "/LikedPostId." + likedPostIdCounter++;
997 configuration.getStringValue(likedPostIdPrefix + "/ID").setValue(soneLikedPostId);
999 configuration.getStringValue(sonePrefix + "/LikedPostId." + likedPostIdCounter + "/ID").setValue(null);
1001 /* write all liked replies. */
1002 int likedReplyIdCounter = 0;
1003 for (String soneLikedReplyId : sone.getLikedReplyIds()) {
1004 String likedReplyIdPrefix = sonePrefix + "/LikedReplyId." + likedReplyIdCounter++;
1005 configuration.getStringValue(likedReplyIdPrefix + "/ID").setValue(soneLikedReplyId);
1007 configuration.getStringValue(sonePrefix + "/LikedReplyId." + likedReplyIdCounter + "/ID").setValue(null);
1010 /* write null ID as terminator. */
1011 configuration.getStringValue("Sone/Sone." + soneId + "/ID").setValue(null);
1013 /* write all known Sones. */
1014 int knownSonesCounter = 0;
1015 for (Sone knownSone : getRemoteSones()) {
1016 String knownSonePrefix = "KnownSone." + knownSonesCounter++;
1017 configuration.getStringValue(knownSonePrefix + "/ID").setValue(knownSone.getId());
1018 configuration.getStringValue(knownSonePrefix + "/Name").setValue(knownSone.getName());
1019 configuration.getStringValue(knownSonePrefix + "/Key").setValue(knownSone.getRequestUri().toString());
1020 /* TODO - store all known stuff? */
1022 configuration.getStringValue("KnownSone." + knownSonesCounter + "/ID").setValue(null);
1024 /* write all blacklisted Sones. */
1025 int blacklistedSonesCounter = 0;
1026 for (Sone blacklistedSone : getBlacklistedSones()) {
1027 String blacklistedSonePrefix = "BlacklistedSone." + blacklistedSonesCounter++;
1028 configuration.getStringValue(blacklistedSonePrefix + "/ID").setValue(blacklistedSone.getId());
1029 configuration.getStringValue(blacklistedSonePrefix + "/Name").setValue(blacklistedSone.getName());
1030 configuration.getStringValue(blacklistedSonePrefix + "/Key").setValue(blacklistedSone.getRequestUri().toString());
1031 configuration.getStringValue(blacklistedSonePrefix + "/InsertKey").setValue((blacklistedSone.getInsertUri() != null) ? blacklistedSone.getInsertUri().toString() : null);
1032 /* TODO - store all known stuff? */
1034 configuration.getStringValue("BlacklistedSone." + blacklistedSonesCounter + "/ID").setValue(null);
1036 } catch (ConfigurationException ce1) {
1037 logger.log(Level.WARNING, "Could not store configuration!", ce1);