2 * Sone - IdentityManager.java - Copyright © 2010–2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.freenet.wot;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.HashSet;
24 import java.util.Map.Entry;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
29 import net.pterodactylus.sone.freenet.plugin.PluginException;
30 import net.pterodactylus.sone.freenet.wot.event.IdentityAddedEvent;
31 import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent;
32 import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent;
33 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent;
34 import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent;
35 import net.pterodactylus.util.logging.Logging;
36 import net.pterodactylus.util.service.AbstractService;
38 import com.google.common.eventbus.EventBus;
39 import com.google.inject.Inject;
40 import com.google.inject.name.Named;
43 * The identity manager takes care of loading and storing identities, their
44 * contexts, and properties. It does so in a way that does not expose errors via
45 * exceptions but it only logs them and tries to return sensible defaults.
47 * It is also responsible for polling identities from the Web of Trust plugin
48 * and sending events to the {@link EventBus} when {@link Identity}s and
49 * {@link OwnIdentity}s are discovered or disappearing.
51 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53 public class IdentityManager extends AbstractService {
55 /** Object used for synchronization. */
56 @SuppressWarnings("hiding")
57 private final Object syncObject = new Object() {
58 /* inner class for better lock names. */
62 private static final Logger logger = Logging.getLogger(IdentityManager.class);
65 private final EventBus eventBus;
67 /** The Web of Trust connector. */
68 private final WebOfTrustConnector webOfTrustConnector;
70 /** The context to filter for. */
71 private final String context;
73 /** The currently known own identities. */
74 /* synchronize access on syncObject. */
75 private final Map<String, OwnIdentity> currentOwnIdentities = new HashMap<String, OwnIdentity>();
77 /** The last time all identities were loaded. */
78 private volatile long identitiesLastLoaded;
81 * Creates a new identity manager.
85 * @param webOfTrustConnector
86 * The Web of Trust connector
88 * The context to focus on (may be {@code null} to ignore
92 public IdentityManager(EventBus eventBus, WebOfTrustConnector webOfTrustConnector, @Named("WebOfTrustContext") String context) {
93 super("Sone Identity Manager", false);
94 this.eventBus = eventBus;
95 this.webOfTrustConnector = webOfTrustConnector;
96 this.context = context;
104 * Returns the last time all identities were loaded.
106 * @return The last time all identities were loaded (in milliseconds since
109 public long getIdentitiesLastLoaded() {
110 return identitiesLastLoaded;
114 * Returns whether the Web of Trust plugin could be reached during the last
117 * @return {@code true} if the Web of Trust plugin is connected,
118 * {@code false} otherwise
120 public boolean isConnected() {
122 webOfTrustConnector.ping();
124 } catch (PluginException pe1) {
125 /* not connected, ignore. */
131 * Returns the own identity with the given ID.
134 * The ID of the own identity
135 * @return The own identity, or {@code null} if there is no such identity
137 public OwnIdentity getOwnIdentity(String id) {
138 Set<OwnIdentity> allOwnIdentities = getAllOwnIdentities();
139 for (OwnIdentity ownIdentity : allOwnIdentities) {
140 if (ownIdentity.getId().equals(id)) {
141 return new DefaultOwnIdentity(ownIdentity);
148 * Returns all own identities.
150 * @return All own identities
152 public Set<OwnIdentity> getAllOwnIdentities() {
153 return new HashSet<OwnIdentity>(currentOwnIdentities.values());
161 protected void serviceRun() {
162 Map<OwnIdentity, Map<String, Identity>> oldIdentities = Collections.emptyMap();
163 while (!shouldStop()) {
164 Map<OwnIdentity, Map<String, Identity>> currentIdentities = new HashMap<OwnIdentity, Map<String, Identity>>();
165 Map<String, OwnIdentity> currentOwnIdentities = new HashMap<String, OwnIdentity>();
167 Set<OwnIdentity> ownIdentities = null;
168 boolean identitiesLoaded = false;
170 /* get all identities with the wanted context from WoT. */
171 logger.finer("Getting all Own Identities from WoT...");
172 ownIdentities = webOfTrustConnector.loadAllOwnIdentities();
173 logger.finest(String.format("Loaded %d Own Identities.", ownIdentities.size()));
175 /* load trusted identities. */
176 for (OwnIdentity ownIdentity : ownIdentities) {
177 currentOwnIdentities.put(ownIdentity.getId(), ownIdentity);
178 Map<String, Identity> identities = new HashMap<String, Identity>();
179 currentIdentities.put(ownIdentity, identities);
182 * if the context doesn’t match, skip getting trusted
185 if ((context != null) && !ownIdentity.hasContext(context)) {
189 /* load trusted identities. */
190 logger.finer(String.format("Getting trusted identities for %s...", ownIdentity.getId()));
191 Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context);
192 logger.finest(String.format("Got %d trusted identities.", trustedIdentities.size()));
193 for (Identity identity : trustedIdentities) {
194 identities.put(identity.getId(), identity);
197 identitiesLoaded = true;
198 identitiesLastLoaded = System.currentTimeMillis();
199 } catch (WebOfTrustException wote1) {
200 logger.log(Level.WARNING, "WoT has disappeared!", wote1);
203 if (identitiesLoaded) {
205 /* check for changes. */
206 checkOwnIdentities(currentOwnIdentities);
208 /* now check for changes in remote identities. */
209 for (OwnIdentity ownIdentity : currentOwnIdentities.values()) {
211 /* find new identities. */
212 for (Identity currentIdentity : currentIdentities.get(ownIdentity).values()) {
213 if (!oldIdentities.containsKey(ownIdentity) || !oldIdentities.get(ownIdentity).containsKey(currentIdentity.getId())) {
214 logger.finest(String.format("Identity added for %s: %s", ownIdentity.getId(), currentIdentity));
215 eventBus.post(new IdentityAddedEvent(ownIdentity, currentIdentity));
219 /* find removed identities. */
220 if (oldIdentities.containsKey(ownIdentity)) {
221 for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
222 if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
223 logger.finest(String.format("Identity removed for %s: %s", ownIdentity.getId(), oldIdentity));
224 eventBus.post(new IdentityRemovedEvent(ownIdentity, oldIdentity));
228 /* check for changes in the contexts. */
229 for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
230 if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
233 Identity newIdentity = currentIdentities.get(ownIdentity).get(oldIdentity.getId());
234 Set<String> oldContexts = oldIdentity.getContexts();
235 Set<String> newContexts = newIdentity.getContexts();
236 if (oldContexts.size() != newContexts.size()) {
237 logger.finest(String.format("Contexts changed for %s: was: %s, is now: %s", ownIdentity.getId(), oldContexts, newContexts));
238 eventBus.post(new IdentityUpdatedEvent(ownIdentity, newIdentity));
241 for (String oldContext : oldContexts) {
242 if (!newContexts.contains(oldContext)) {
243 logger.finest(String.format("Context was removed for %s: %s", ownIdentity.getId(), oldContext));
244 eventBus.post(new IdentityUpdatedEvent(ownIdentity, newIdentity));
250 /* check for changes in the properties. */
251 for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
252 if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
255 Identity newIdentity = currentIdentities.get(ownIdentity).get(oldIdentity.getId());
256 Map<String, String> oldProperties = oldIdentity.getProperties();
257 Map<String, String> newProperties = newIdentity.getProperties();
258 if (oldProperties.size() != newProperties.size()) {
259 logger.finest(String.format("Properties changed for %s: was: %s, is now: %s", ownIdentity.getId(), oldProperties, newProperties));
260 eventBus.post(new IdentityUpdatedEvent(ownIdentity, newIdentity));
263 for (Entry<String, String> oldProperty : oldProperties.entrySet()) {
264 if (!newProperties.containsKey(oldProperty.getKey()) || !newProperties.get(oldProperty.getKey()).equals(oldProperty.getValue())) {
265 logger.finest(String.format("Property was removed for %s: %s", ownIdentity.getId(), oldProperty));
266 eventBus.post(new IdentityUpdatedEvent(ownIdentity, newIdentity));
274 /* remember the current set of identities. */
275 oldIdentities = currentIdentities;
278 /* wait a minute before checking again. */
288 * Checks the given new list of own identities for added or removed own
289 * identities, as compared to {@link #currentOwnIdentities}.
291 * @param newOwnIdentities
292 * The new own identities
294 private void checkOwnIdentities(Map<String, OwnIdentity> newOwnIdentities) {
295 synchronized (syncObject) {
297 /* find removed own identities: */
298 for (OwnIdentity oldOwnIdentity : currentOwnIdentities.values()) {
299 OwnIdentity newOwnIdentity = newOwnIdentities.get(oldOwnIdentity.getId());
300 if ((newOwnIdentity == null) || ((context != null) && oldOwnIdentity.hasContext(context) && !newOwnIdentity.hasContext(context))) {
301 logger.finest(String.format("Own Identity removed: %s", oldOwnIdentity));
302 eventBus.post(new OwnIdentityRemovedEvent(new DefaultOwnIdentity(oldOwnIdentity)));
306 /* find added own identities. */
307 for (OwnIdentity currentOwnIdentity : newOwnIdentities.values()) {
308 OwnIdentity oldOwnIdentity = currentOwnIdentities.get(currentOwnIdentity.getId());
309 if (((oldOwnIdentity == null) && ((context == null) || currentOwnIdentity.hasContext(context))) || ((oldOwnIdentity != null) && (context != null) && (!oldOwnIdentity.hasContext(context) && currentOwnIdentity.hasContext(context)))) {
310 logger.finest(String.format("Own Identity added: %s", currentOwnIdentity));
311 eventBus.post(new OwnIdentityAddedEvent(new DefaultOwnIdentity(currentOwnIdentity)));
315 currentOwnIdentities.clear();
316 currentOwnIdentities.putAll(newOwnIdentities);