Allow Sone, Post, and Reply creation only through the Core and its caches.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index d906055..459431c 100644 (file)
@@ -54,12 +54,26 @@ public class Core extends AbstractService {
        /** Interface to freenet. */
        private FreenetInterface freenetInterface;
 
+       /** The Sone downloader. */
+       private SoneDownloader soneDownloader;
+
        /** The local Sones. */
        private final Set<Sone> localSones = new HashSet<Sone>();
 
        /** Sone inserters. */
        private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
 
+       /* various caches follow here. */
+
+       /** Cache for all known Sones. */
+       private final Map<String, Sone> soneCache = new HashMap<String, Sone>();
+
+       /** Cache for all known posts. */
+       private final Map<String, Post> postCache = new HashMap<String, Post>();
+
+       /** Cache for all known replies. */
+       private final Map<String, Reply> replyCache = new HashMap<String, Reply>();
+
        /**
         * Creates a new core.
         */
@@ -92,6 +106,8 @@ public class Core extends AbstractService {
         */
        public Core freenetInterface(FreenetInterface freenetInterface) {
                this.freenetInterface = freenetInterface;
+               soneDownloader = new SoneDownloader(this, freenetInterface);
+               soneDownloader.start();
                return this;
        }
 
@@ -104,6 +120,50 @@ public class Core extends AbstractService {
                return Collections.unmodifiableSet(localSones);
        }
 
+       /**
+        * Returns the Sone with the given ID, or an empty Sone that has been
+        * initialized with the given ID.
+        *
+        * @param soneId
+        *            The ID of the Sone
+        * @return The Sone
+        */
+       public Sone getSone(String soneId) {
+               if (!soneCache.containsKey(soneId)) {
+                       Sone sone = new Sone(soneId);
+                       soneCache.put(soneId, sone);
+               }
+               return soneCache.get(soneId);
+       }
+
+       /**
+        * Creates a new post.
+        *
+        * @param sone
+        *            The sone that creates the post
+        * @param text
+        *            The text of the post
+        * @return The created post
+        */
+       public Post createPost(Sone sone, String text) {
+               return createPost(sone, System.currentTimeMillis(), text);
+       }
+
+       /**
+        * Creates a new post.
+        *
+        * @param sone
+        *            The Sone that creates the post
+        * @param time
+        *            The time of the post
+        * @param text
+        *            The text of the post
+        * @return The created post
+        */
+       public Post createPost(Sone sone, long time, String text) {
+               return getPost(UUID.randomUUID().toString()).setSone(sone).setTime(time).setText(text);
+       }
+
        //
        // ACTIONS
        //
@@ -114,15 +174,30 @@ public class Core extends AbstractService {
         * @param sone
         *            The Sone to add
         */
-       public void addSone(Sone sone) {
+       public void addLocalSone(Sone sone) {
                if (localSones.add(sone)) {
+                       soneCache.put(sone.getId(), sone);
                        SoneInserter soneInserter = new SoneInserter(freenetInterface, sone);
                        soneInserter.start();
+                       soneDownloader.addSone(sone);
                        soneInserters.put(sone, soneInserter);
                }
        }
 
        /**
+        * Adds a remote Sone so that it is watched for updates.
+        *
+        * @param sone
+        *            The sone to watch
+        */
+       public void addSone(Sone sone) {
+               if (!soneCache.containsKey(sone.getId())) {
+                       soneCache.put(sone.getId(), sone);
+               }
+               soneDownloader.addSone(sone);
+       }
+
+       /**
         * Creates a new Sone at a random location.
         *
         * @param name
@@ -169,15 +244,14 @@ public class Core extends AbstractService {
                Sone sone;
                try {
                        logger.log(Level.FINEST, "Creating new Sone “%s” at %s (%s)…", new Object[] { name, finalRequestUri, finalInsertUri });
-                       sone = new Sone(UUID.randomUUID(), name, new FreenetURI(finalRequestUri), new FreenetURI(finalInsertUri));
+                       sone = new Sone(UUID.randomUUID().toString()).setName(name).setRequestUri(new FreenetURI(finalRequestUri).setKeyType("USK").setDocName("Sone-" + name)).setInsertUri(new FreenetURI(finalInsertUri).setKeyType("USK").setDocName("Sone-" + name));
                        sone.setProfile(new Profile());
                        /* set modification counter to 1 so it is inserted immediately. */
                        sone.setModificationCounter(1);
-                       addSone(sone);
+                       addLocalSone(sone);
                } catch (MalformedURLException mue1) {
                        throw new SoneException(Type.INVALID_URI);
                }
-               localSones.add(sone);
                return sone;
        }
 
@@ -193,6 +267,36 @@ public class Core extends AbstractService {
                localSones.remove(sone);
        }
 
+       /**
+        * Returns the post with the given ID. If no post exists yet with the given
+        * ID, a new post is returned.
+        *
+        * @param postId
+        *            The ID of the post
+        * @return The post
+        */
+       public Post getPost(String postId) {
+               if (!postCache.containsKey(postId)) {
+                       postCache.put(postId, new Post(postId));
+               }
+               return postCache.get(postId);
+       }
+
+       /**
+        * Returns the reply with the given ID. If no reply exists yet with the
+        * given ID, a new reply is returned.
+        *
+        * @param replyId
+        *            The ID of the reply
+        * @return The reply
+        */
+       public Reply getReply(String replyId) {
+               if (!replyCache.containsKey(replyId)) {
+                       replyCache.put(replyId, new Reply(replyId));
+               }
+               return replyCache.get(replyId);
+       }
+
        //
        // SERVICE METHODS
        //
@@ -210,6 +314,7 @@ public class Core extends AbstractService {
         */
        @Override
        protected void serviceStop() {
+               soneDownloader.stop();
                /* stop all Sone inserters. */
                for (SoneInserter soneInserter : soneInserters.values()) {
                        soneInserter.stop();
@@ -248,7 +353,8 @@ public class Core extends AbstractService {
                                profile.setFirstName(firstName);
                                profile.setMiddleName(middleName);
                                profile.setLastName(lastName);
-                               Sone sone = new Sone(UUID.fromString(id), name, new FreenetURI(requestUri), new FreenetURI(insertUri));
+                               Sone sone = new Sone(id).setName(name).setRequestUri(new FreenetURI(requestUri)).setInsertUri(new FreenetURI(insertUri));
+                               soneCache.put(id, sone);
                                sone.setProfile(profile);
                                int postId = 0;
                                do {
@@ -259,11 +365,45 @@ public class Core extends AbstractService {
                                        }
                                        long time = configuration.getLongValue(postPrefix + "/Time").getValue(null);
                                        String text = configuration.getStringValue(postPrefix + "/Text").getValue(null);
-                                       Post post = new Post(UUID.fromString(id), sone, time, text);
+                                       Post post = getPost(id).setSone(sone).setTime(time).setText(text);
                                        sone.addPost(post);
                                } while (true);
+                               int replyCounter = 0;
+                               do {
+                                       String replyPrefix = sonePrefix + "/Reply." + replyCounter++;
+                                       String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
+                                       if (replyId == null) {
+                                               break;
+                                       }
+                                       Sone replySone = getSone(configuration.getStringValue(replyPrefix + "/Sone/ID").getValue(null));
+                                       String replySoneKey = configuration.getStringValue(replyPrefix + "/Sone/Key").getValue(null);
+                                       String replySoneName = configuration.getStringValue(replyPrefix + "/Sone/Name").getValue(null);
+                                       replySone.setRequestUri(new FreenetURI(replySoneKey)).setName(replySoneName);
+                                       Post replyPost = postCache.get(configuration.getStringValue(replyPrefix + "/Post").getValue(null));
+                                       long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue(null);
+                                       String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
+                                       Reply reply = getReply(replyId).setSone(replySone).setPost(replyPost).setTime(replyTime).setText(replyText);
+                                       replyCache.put(replyId, reply);
+                               } while (true);
+
+                               /* load friends. */
+                               int friendCounter = 0;
+                               while (true) {
+                                       String friendPrefix = sonePrefix + "/Friend." + friendCounter++;
+                                       String friendId = configuration.getStringValue(friendPrefix + "/ID").getValue(null);
+                                       if (friendId == null) {
+                                               break;
+                                       }
+                                       Sone friendSone = getSone(friendId);
+                                       String friendKey = configuration.getStringValue(friendPrefix + "/Key").getValue(null);
+                                       String friendName = configuration.getStringValue(friendPrefix + "/Name").getValue(null);
+                                       friendSone.setRequestUri(new FreenetURI(friendKey)).setName(friendName);
+                                       addSone(friendSone);
+                                       sone.addFriend(sone);
+                               }
+
                                sone.setModificationCounter(modificationCounter);
-                               addSone(sone);
+                               addLocalSone(sone);
                        } catch (MalformedURLException mue1) {
                                logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
                        }
@@ -300,16 +440,37 @@ public class Core extends AbstractService {
                                        configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
                                        configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
                                }
+                               /* write null ID as terminator. */
+                               configuration.getStringValue(sonePrefix + "/Post." + postId + "/ID").setValue(null);
+
                                int replyId = 0;
                                for (Reply reply : sone.getReplies()) {
                                        String replyPrefix = sonePrefix + "/Reply." + replyId++;
                                        configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
-                                       configuration.getStringValue(replyPrefix + "/Sone").setValue(reply.getSone().getId());
+                                       configuration.getStringValue(replyPrefix + "/Sone/ID").setValue(reply.getSone().getId());
+                                       configuration.getStringValue(replyPrefix + "/Sone/Key").setValue(reply.getSone().getRequestUri().toString());
+                                       configuration.getStringValue(replyPrefix + "/Sone/Name").setValue(reply.getSone().getName());
                                        configuration.getStringValue(replyPrefix + "/Post").setValue(reply.getPost().getId());
                                        configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
                                        configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
                                }
+                               /* write null ID as terminator. */
+                               configuration.getStringValue(sonePrefix + "/Reply." + replyId + "/ID").setValue(null);
+
+                               int friendId = 0;
+                               for (Sone friend : sone.getFriends()) {
+                                       String friendPrefix = sonePrefix + "/Friend." + friendId++;
+                                       configuration.getStringValue(friendPrefix + "/ID").setValue(friend.getId());
+                                       configuration.getStringValue(friendPrefix + "/Key").setValue(friend.getRequestUri().toString());
+                                       configuration.getStringValue(friendPrefix + "/Name").setValue(friend.getName());
+                               }
+                               /* write null ID as terminator. */
+                               configuration.getStringValue(sonePrefix + "/Friend." + friendId + "/ID").setValue(null);
+
                        }
+                       /* write null ID as terminator. */
+                       configuration.getStringValue("Sone/Sone." + soneId + "/ID").setValue(null);
+
                } catch (ConfigurationException ce1) {
                        logger.log(Level.WARNING, "Could not store configuration!", ce1);
                }