Always at least try to load the contexts before using the empty set.
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / Identity.java
index c3053ae..0e13e9e 100644 (file)
@@ -30,6 +30,9 @@ import java.util.Set;
  */
 public class Identity {
 
+       /** The Web of Trust connector. */
+       protected final WebOfTrustConnector webOfTrustConnector;
+
        /** The ID of the identity. */
        private final String id;
 
@@ -42,12 +45,17 @@ public class Identity {
        /** The contexts of the identity. */
        protected final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
 
+       /** Whether the contexts have already been loaded. */
+       private volatile boolean contextsLoaded = false;
+
        /** The properties of the identity. */
        private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
 
        /**
         * Creates a new identity.
         *
+        * @param webOfTrustConnector
+        *            The Web of Trust connector
         * @param id
         *            The ID of the identity
         * @param nickname
@@ -55,7 +63,8 @@ public class Identity {
         * @param requestUri
         *            The request URI of the identity
         */
-       public Identity(String id, String nickname, String requestUri) {
+       public Identity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) {
+               this.webOfTrustConnector = webOfTrustConnector;
                this.id = id;
                this.nickname = nickname;
                this.requestUri = requestUri;
@@ -93,11 +102,37 @@ public class Identity {
        }
 
        /**
-        * Returns the contexts of the identity.
+        * Returns the contexts of the identity. If the contexts have not been
+        * loaded yet, they will be loaded. If loading the contexts fails, an empty
+        * set is returned.
         *
         * @return The contexts of the identity
         */
        public Set<String> getContexts() {
+               try {
+                       return getContexts(false);
+               } catch (PluginException pe1) {
+                       return Collections.emptySet();
+               }
+       }
+
+       /**
+        * Returns the contexts of the identity.
+        *
+        * @param forceReload
+        *            {@code true} to force a reload of the contexts
+        * @return The contexts of the identity
+        * @throws PluginException
+        *             if an error occured communicating with the Web of Trust
+        *             plugin
+        */
+       public Set<String> getContexts(boolean forceReload) throws PluginException {
+               if (!contextsLoaded || forceReload) {
+                       Set<String> contexts = webOfTrustConnector.loadIdentityContexts(this);
+                       contextsLoaded = true;
+                       this.contexts.clear();
+                       this.contexts.addAll(contexts);
+               }
                return Collections.unmodifiableSet(contexts);
        }
 
@@ -110,7 +145,7 @@ public class Identity {
         *         {@code false} otherwise
         */
        public boolean hasContext(String context) {
-               return contexts.contains(context);
+               return getContexts().contains(context);
        }
 
        /**