Fix ALL the logging!
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / DefaultIdentity.java
index d262d57..6a16cc5 100644 (file)
@@ -22,14 +22,18 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 import net.pterodactylus.sone.freenet.plugin.PluginException;
-import net.pterodactylus.util.cache.Cache;
 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.collection.TimedMap;
+import net.pterodactylus.util.logging.Logging;
 
 /**
  * A Web of Trust identity.
@@ -38,6 +42,9 @@ import net.pterodactylus.util.cache.ValueRetriever;
  */
 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,7 +64,8 @@ public class DefaultIdentity implements Identity {
        private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
 
        /** Cached trust. */
-       private final Cache<OwnIdentity, Trust> trustCache = new MemoryCache<OwnIdentity, Trust>(new ValueRetriever<OwnIdentity, Trust>() {
+       /* synchronize on itself. */
+       private final WritableCache<OwnIdentity, Trust> trustCache = new MemoryCache<OwnIdentity, Trust>(new ValueRetriever<OwnIdentity, Trust>() {
 
                @Override
                @SuppressWarnings("synthetic-access")
@@ -69,7 +77,7 @@ public class DefaultIdentity implements Identity {
                        }
                }
 
-       });
+       }, new TimedMap<OwnIdentity, CacheItem<Trust>>(60 * 60 * 1000));
 
        /**
         * Creates a new identity.
@@ -240,11 +248,28 @@ public class DefaultIdentity implements Identity {
         * {@inheritDoc}
         */
        @Override
-       public Trust getTrust(OwnIdentity ownIdentity) throws WebOfTrustException {
+       public Trust getTrust(OwnIdentity ownIdentity) {
                try {
-                       return trustCache.get(ownIdentity);
+                       synchronized (trustCache) {
+                               return trustCache.get(ownIdentity);
+                       }
                } catch (CacheException ce1) {
-                       throw new WebOfTrustException("Could not get trust for OwnIdentity: " + ownIdentity, ce1);
+                       logger.log(Level.WARNING, String.format("Could not get trust for OwnIdentity: %s", ownIdentity), ce1);
+                       return null;
+               }
+       }
+
+       /**
+        * Sets the trust received for this identity by the given own identity.
+        *
+        * @param ownIdentity
+        *            The own identity that gives the trust
+        * @param trust
+        *            The trust received for this identity
+        */
+       void setTrustPrivate(OwnIdentity ownIdentity, Trust trust) {
+               synchronized (trustCache) {
+                       trustCache.put(ownIdentity, trust);
                }
        }