Store identity trust locally, make it updatable from outside.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / DefaultIdentity.java
index dcafb18..f200022 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - DefaultIdentity.java - Copyright © 2010 David Roden
+ * Sone - DefaultIdentity.java - Copyright © 2010–2012 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -22,14 +22,9 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.logging.Logger;
 
-import net.pterodactylus.sone.freenet.plugin.PluginException;
-import net.pterodactylus.util.cache.CacheException;
-import net.pterodactylus.util.cache.CacheItem;
-import net.pterodactylus.util.cache.DefaultCacheItem;
-import net.pterodactylus.util.cache.MemoryCache;
-import net.pterodactylus.util.cache.ValueRetriever;
-import net.pterodactylus.util.cache.WritableCache;
+import net.pterodactylus.util.logging.Logging;
 
 /**
  * A Web of Trust identity.
@@ -38,6 +33,9 @@ import net.pterodactylus.util.cache.WritableCache;
  */
 public class DefaultIdentity implements Identity {
 
+       /** The logger. */
+       private static final Logger logger = Logging.getLogger(DefaultIdentity.class);
+
        /** The web of trust connector. */
        private final WebOfTrustConnector webOfTrustConnector;
 
@@ -57,19 +55,8 @@ public class DefaultIdentity implements Identity {
        private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
 
        /** Cached trust. */
-       private final WritableCache<OwnIdentity, Trust> trustCache = new MemoryCache<OwnIdentity, Trust>(new ValueRetriever<OwnIdentity, Trust>() {
-
-               @Override
-               @SuppressWarnings("synthetic-access")
-               public CacheItem<Trust> retrieve(OwnIdentity ownIdentity) throws CacheException {
-                       try {
-                               return new DefaultCacheItem<Trust>(webOfTrustConnector.getTrust(ownIdentity, DefaultIdentity.this));
-                       } catch (PluginException pe1) {
-                               throw new CacheException("Could not retrieve trust for OwnIdentity: " + ownIdentity, pe1);
-                       }
-               }
-
-       });
+       /* synchronize on itself. */
+       private final Map<OwnIdentity, Trust> trustCache = new HashMap<OwnIdentity, Trust>();
 
        /**
         * Creates a new identity.
@@ -240,11 +227,9 @@ public class DefaultIdentity implements Identity {
         * {@inheritDoc}
         */
        @Override
-       public Trust getTrust(OwnIdentity ownIdentity) throws WebOfTrustException {
-               try {
+       public Trust getTrust(OwnIdentity ownIdentity) {
+               synchronized (trustCache) {
                        return trustCache.get(ownIdentity);
-               } catch (CacheException ce1) {
-                       throw new WebOfTrustException("Could not get trust for OwnIdentity: " + ownIdentity, ce1);
                }
        }
 
@@ -256,8 +241,10 @@ public class DefaultIdentity implements Identity {
         * @param trust
         *            The trust received for this identity
         */
-       void setTrustPrivate(OwnIdentity ownIdentity, Trust trust) {
-               trustCache.put(ownIdentity, trust);
+       public void setTrust(OwnIdentity ownIdentity, Trust trust) {
+               synchronized (trustCache) {
+                       trustCache.put(ownIdentity, trust);
+               }
        }
 
        //