Implement removal of contexts.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 10 Sep 2012 11:08:57 +0000 (13:08 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 10 Sep 2012 11:32:31 +0000 (13:32 +0200)
src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java

index 59abc8e..ded6da9 100644 (file)
@@ -183,6 +183,26 @@ public class WebOfTrustUpdater extends AbstractService {
                return false;
        }
 
+       /**
+        * Removes the given context from the given own identity.
+        *
+        * @param ownIdentity
+        *            The own identity to remove the context from
+        * @param context
+        *            The context to remove
+        */
+       public void removeContext(OwnIdentity ownIdentity, String context) {
+               RemoveContextJob removeContextJob = new RemoveContextJob(ownIdentity, context);
+               if (!updateJobs.contains(removeContextJob)) {
+                       logger.log(Level.FINER, "Adding Context Job: " + removeContextJob);
+                       try {
+                               updateJobs.put(removeContextJob);
+                       } catch (InterruptedException ie1) {
+                               /* the queue is unbounded so it should never block. */
+                       }
+               }
+       }
+
        //
        // SERVICE METHODS
        //
@@ -553,4 +573,41 @@ public class WebOfTrustUpdater extends AbstractService {
 
        }
 
+       /**
+        * Job that removes a context from an {@link OwnIdentity}.
+        *
+        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+        */
+       private class RemoveContextJob extends WebOfTrustContextUpdateJob {
+
+               /**
+                * Creates a new remove-context job.
+                *
+                * @param ownIdentity
+                *            The own identity whose contexts to manage
+                * @param context
+                *            The context to remove
+                */
+               public RemoveContextJob(OwnIdentity ownIdentity, String context) {
+                       super(ownIdentity, context);
+               }
+
+               /**
+                * {@inheritDoc}
+                */
+               @Override
+               @SuppressWarnings("synthetic-access")
+               public void run() {
+                       try {
+                               webOfTrustConnector.removeContext(ownIdentity, context);
+                               ownIdentity.removeContext(context);
+                               finish(true);
+                       } catch (PluginException pe1) {
+                               logger.log(Level.WARNING, String.format("Could not remove Context “%2$s” to Own Identity %1$s!", ownIdentity, context), pe1);
+                               finish(false);
+                       }
+               }
+
+       }
+
 }