X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Ffreenet%2Fwot%2FIdentityManager.java;h=3b85e0d6cabd4623f51b0bb9dee2314a3c0de9af;hb=dfdb5045fcdb13c23782375f3990f0546ff71e6b;hp=29334836da39d940765bbe8b70c158c0b1ffa9d7;hpb=133def7f9b2c97c03aed4edb6af831460a801394;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java index 2933483..3b85e0d 100644 --- a/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java +++ b/src/main/java/net/pterodactylus/sone/freenet/wot/IdentityManager.java @@ -17,21 +17,15 @@ package net.pterodactylus.sone.freenet.wot; +import static com.google.common.base.Optional.fromNullable; import static com.google.common.collect.HashMultimap.create; -import java.util.Collection; import java.util.HashSet; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.sone.freenet.plugin.PluginException; -import net.pterodactylus.sone.freenet.wot.IdentityChangeDetector.IdentityProcessor; -import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent; -import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent; -import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent; -import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent; -import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.service.AbstractService; @@ -54,24 +48,17 @@ import com.google.inject.name.Named; */ public class IdentityManager extends AbstractService { - /** Object used for synchronization. */ - @SuppressWarnings("hiding") - private final Object syncObject = new Object() { - /* inner class for better lock names. */ - }; - /** The logger. */ private static final Logger logger = Logging.getLogger(IdentityManager.class); /** The event bus. */ private final EventBus eventBus; + private final IdentityLoader identityLoader; + /** The Web of Trust connector. */ private final WebOfTrustConnector webOfTrustConnector; - /** The context to filter for. */ - private final String context; - /** The currently known own identities. */ /* synchronize access on syncObject. */ private final Set currentOwnIdentities = Sets.newHashSet(); @@ -91,7 +78,7 @@ public class IdentityManager extends AbstractService { super("Sone Identity Manager", false); this.eventBus = eventBus; this.webOfTrustConnector = webOfTrustConnector; - this.context = context; + this.identityLoader = new IdentityLoader(webOfTrustConnector, fromNullable(context)); } // @@ -121,7 +108,7 @@ public class IdentityManager extends AbstractService { * @return All own identities */ public Set getAllOwnIdentities() { - synchronized (syncObject) { + synchronized (currentOwnIdentities) { return new HashSet(currentOwnIdentities); } } @@ -136,15 +123,16 @@ public class IdentityManager extends AbstractService { while (!shouldStop()) { try { - Collection currentOwnIdentities = webOfTrustConnector.loadAllOwnIdentities(); - Multimap currentIdentities = loadTrustedIdentitiesForOwnIdentities(currentOwnIdentities); + Multimap currentIdentities = identityLoader.loadIdentities(); + + IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, oldIdentities); + identityChangeEventSender.detectChanges(currentIdentities); - detectChangesInIdentities(currentOwnIdentities, currentIdentities, oldIdentities); oldIdentities = currentIdentities; - synchronized (syncObject) { - this.currentOwnIdentities.clear(); - this.currentOwnIdentities.addAll(currentOwnIdentities); + synchronized (currentOwnIdentities) { + currentOwnIdentities.clear(); + currentOwnIdentities.addAll(currentIdentities.keySet()); } } catch (WebOfTrustException wote1) { logger.log(Level.WARNING, "WoT has disappeared!", wote1); @@ -155,105 +143,4 @@ public class IdentityManager extends AbstractService { } } - private void detectChangesInIdentities(Collection currentOwnIdentities, Multimap newIdentities, Multimap oldIdentities) { - IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(getAllOwnIdentities()); - identityChangeDetector.onNewIdentity(addNewOwnIdentityAndItsTrustedIdentities(newIdentities)); - identityChangeDetector.onRemovedIdentity(removeOwnIdentityAndItsTrustedIdentities(oldIdentities)); - identityChangeDetector.onUnchangedIdentity(detectChangesInTrustedIdentities(newIdentities, oldIdentities)); - identityChangeDetector.detectChanges(currentOwnIdentities); - } - - private IdentityProcessor detectChangesInTrustedIdentities(Multimap newIdentities, Multimap oldIdentities) { - return new DefaultIdentityProcessor(oldIdentities, newIdentities); - } - - private IdentityProcessor removeOwnIdentityAndItsTrustedIdentities(final Multimap oldIdentities) { - return new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - eventBus.post(new OwnIdentityRemovedEvent((OwnIdentity) identity)); - for (Identity removedIdentity : oldIdentities.get((OwnIdentity) identity)) { - eventBus.post(new IdentityRemovedEvent((OwnIdentity) identity, removedIdentity)); - } - } - }; - } - - private IdentityProcessor addNewOwnIdentityAndItsTrustedIdentities(final Multimap newIdentities) { - return new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - eventBus.post(new OwnIdentityAddedEvent((OwnIdentity) identity)); - for (Identity newIdentity : newIdentities.get((OwnIdentity) identity)) { - eventBus.post(new IdentityAddedEvent((OwnIdentity) identity, newIdentity)); - } - } - }; - } - - private Multimap loadTrustedIdentitiesForOwnIdentities(Collection ownIdentities) throws PluginException { - Multimap currentIdentities = create(); - - for (OwnIdentity ownIdentity : ownIdentities) { - if ((context != null) && !ownIdentity.hasContext(context)) { - continue; - } - - logger.finer(String.format("Getting trusted identities for %s...", ownIdentity.getId())); - Set trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context); - logger.finest(String.format("Got %d trusted identities.", trustedIdentities.size())); - currentIdentities.putAll(ownIdentity, trustedIdentities); - } - - return currentIdentities; - } - - private class DefaultIdentityProcessor implements IdentityProcessor { - - private final Multimap oldIdentities; - private final Multimap newIdentities; - - public DefaultIdentityProcessor(Multimap oldIdentities, Multimap newIdentities) { - this.oldIdentities = oldIdentities; - this.newIdentities = newIdentities; - } - - @Override - public void processIdentity(Identity ownIdentity) { - IdentityChangeDetector identityChangeDetector = new IdentityChangeDetector(oldIdentities.get((OwnIdentity) ownIdentity)); - identityChangeDetector.onNewIdentity(notifyForAddedIdentities((OwnIdentity) ownIdentity)); - identityChangeDetector.onRemovedIdentity(notifyForRemovedIdentities((OwnIdentity) ownIdentity)); - identityChangeDetector.onChangedIdentity(notifyForChangedIdentities((OwnIdentity) ownIdentity)); - identityChangeDetector.detectChanges(newIdentities.get((OwnIdentity) ownIdentity)); - } - - private IdentityProcessor notifyForChangedIdentities(final OwnIdentity ownIdentity) { - return new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - eventBus.post(new IdentityUpdatedEvent(ownIdentity, identity)); - } - }; - } - - private IdentityProcessor notifyForRemovedIdentities(final OwnIdentity ownIdentity) { - return new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - eventBus.post(new IdentityRemovedEvent(ownIdentity, identity)); - } - }; - } - - private IdentityProcessor notifyForAddedIdentities(final OwnIdentity ownIdentity) { - return new IdentityProcessor() { - @Override - public void processIdentity(Identity identity) { - eventBus.post(new IdentityAddedEvent(ownIdentity, identity)); - } - }; - } - - } - }