Merge branch 'next' into edit-wot-trust
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 30 Dec 2010 20:39:40 +0000 (21:39 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 30 Dec 2010 20:39:40 +0000 (21:39 +0100)
1  2 
src/main/java/net/pterodactylus/sone/core/Core.java

@@@ -40,8 -40,6 +40,8 @@@ import net.pterodactylus.sone.freenet.w
  import net.pterodactylus.sone.freenet.wot.IdentityListener;
  import net.pterodactylus.sone.freenet.wot.IdentityManager;
  import net.pterodactylus.sone.freenet.wot.OwnIdentity;
 +import net.pterodactylus.sone.freenet.wot.Trust;
 +import net.pterodactylus.sone.freenet.wot.WebOfTrustException;
  import net.pterodactylus.sone.main.SonePlugin;
  import net.pterodactylus.util.config.Configuration;
  import net.pterodactylus.util.config.ConfigurationException;
@@@ -88,6 -86,9 +88,9 @@@ public class Core implements IdentityLi
        /** The configuration. */
        private Configuration configuration;
  
+       /** Whether we’re currently saving the configuration. */
+       private boolean storingConfiguration = false;
        /** The identity manager. */
        private final IdentityManager identityManager;
  
        /** All known replies. */
        private Set<String> knownReplies = new HashSet<String>();
  
 +      /** Trusted identities, sorted by own identities. */
 +      private Map<OwnIdentity, Set<Identity>> trustedIdentities = Collections.synchronizedMap(new HashMap<OwnIdentity, Set<Identity>>());
 +
        /**
         * Creates a new core.
         *
        }
  
        /**
 +       * Returns whether the target Sone is trusted by the origin Sone.
 +       *
 +       * @param origin
 +       *            The origin Sone
 +       * @param target
 +       *            The target Sone
 +       * @return {@code true} if the target Sone is trusted by the origin Sone
 +       */
 +      public boolean isSoneTrusted(Sone origin, Sone target) {
 +              return trustedIdentities.containsKey(origin) && trustedIdentities.get(origin.getIdentity()).contains(target);
 +      }
 +
 +      /**
         * Returns the post with the given ID.
         *
         * @param postId
         * @return The created Sone
         */
        public Sone createSone(OwnIdentity ownIdentity) {
 -              identityManager.addContext(ownIdentity, "Sone");
 +              try {
 +                      ownIdentity.addContext("Sone");
 +              } catch (WebOfTrustException wote1) {
 +                      logger.log(Level.SEVERE, "Could not add “Sone” context to own identity: " + ownIdentity, wote1);
 +                      return null;
 +              }
                Sone sone = addLocalSone(ownIdentity);
                return sone;
        }
        }
  
        /**
 +       * Retrieves the trust relationship from the origin to the target.
 +       *
 +       * @param origin
 +       *            The origin of the trust tree
 +       * @param target
 +       *            The target of the trust
 +       * @return The trust relationship
 +       */
 +      public Trust getTrust(Sone origin, Sone target) {
 +              if (!isLocalSone(origin)) {
 +                      logger.log(Level.WARNING, "Tried to get trust from remote Sone: %s", origin);
 +                      return null;
 +              }
 +              try {
 +                      return target.getIdentity().getTrust((OwnIdentity) origin.getIdentity());
 +              } catch (WebOfTrustException wote1) {
 +                      logger.log(Level.WARNING, "Could not get trust for Sone: " + target, wote1);
 +                      return null;
 +              }
 +      }
 +
 +      /**
         * Updates the stores Sone with the given Sone.
         *
         * @param sone
                        localSones.remove(sone.getId());
                        soneInserters.remove(sone).stop();
                }
 -              identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
 -              identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
 +              try {
 +                      ((OwnIdentity) sone.getIdentity()).removeContext("Sone");
 +                      ((OwnIdentity) sone.getIdentity()).removeProperty("Sone.LatestEdition");
 +              } catch (WebOfTrustException wote1) {
 +                      logger.log(Level.WARNING, "Could not remove context and properties from Sone: " + sone, wote1);
 +              }
                try {
                        configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
                } catch (ConfigurationException ce1) {
                }
  
                logger.log(Level.INFO, "Saving Sone: %s", sone);
 -              identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
                try {
 +                      ((OwnIdentity) sone.getIdentity()).setProperty("Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
 +
                        /* save Sone into configuration. */
                        String sonePrefix = "Sone/" + sone.getId();
                        configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
                        logger.log(Level.INFO, "Sone %s saved.", sone);
                } catch (ConfigurationException ce1) {
                        logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
 +              } catch (WebOfTrustException wote1) {
 +                      logger.log(Level.WARNING, "Could not set WoT property for Sone: " + sone, wote1);
                }
        }
  
        /**
         * Saves the current options.
         */
-       public synchronized void saveConfiguration() {
+       public void saveConfiguration() {
+               synchronized (configuration) {
+                       if (storingConfiguration) {
+                               logger.log(Level.FINE, "Already storing configuration…");
+                               return;
+                       }
+                       storingConfiguration = true;
+               }
                /* store the options first. */
                try {
                        configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
  
                } catch (ConfigurationException ce1) {
                        logger.log(Level.SEVERE, "Could not store configuration!", ce1);
+               } finally {
+                       synchronized (configuration) {
+                               storingConfiguration = false;
+                       }
                }
        }
  
        public void ownIdentityAdded(OwnIdentity ownIdentity) {
                logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
                if (ownIdentity.hasContext("Sone")) {
 +                      trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet<Identity>()));
                        addLocalSone(ownIdentity);
                }
        }
        @Override
        public void ownIdentityRemoved(OwnIdentity ownIdentity) {
                logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
 +              trustedIdentities.remove(ownIdentity);
        }
  
        /**
         * {@inheritDoc}
         */
        @Override
 -      public void identityAdded(Identity identity) {
 +      public void identityAdded(OwnIdentity ownIdentity, Identity identity) {
                logger.log(Level.FINEST, "Adding Identity: " + identity);
 +              trustedIdentities.get(ownIdentity).add(identity);
                addRemoteSone(identity);
        }
  
         * {@inheritDoc}
         */
        @Override
 -      public void identityUpdated(final Identity identity) {
 +      public void identityUpdated(OwnIdentity ownIdentity, final Identity identity) {
                new Thread(new Runnable() {
  
                        @Override
         * {@inheritDoc}
         */
        @Override
 -      public void identityRemoved(Identity identity) {
 -              /* TODO */
 +      public void identityRemoved(OwnIdentity ownIdentity, Identity identity) {
 +              trustedIdentities.get(ownIdentity).remove(identity);
        }
  
  }