2 * Sone - IdentityManager.java - Copyright © 2010 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;
23 import java.util.Map.Entry;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
28 import net.pterodactylus.util.logging.Logging;
29 import net.pterodactylus.util.service.AbstractService;
32 * The identity manager takes care of loading and storing identities, their
33 * contexts, and properties. It does so in a way that does not expose errors via
34 * exceptions but it only logs them and tries to return sensible defaults.
36 * It is also responsible for polling identities from the Web of Trust plugin
37 * and notifying registered {@link IdentityListener}s when {@link Identity}s and
38 * {@link OwnIdentity}s are discovered or disappearing.
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class IdentityManager extends AbstractService {
44 /** Object used for synchronization. */
45 private final Object syncObject = new Object() {
46 /* inner class for better lock names. */
50 private static final Logger logger = Logging.getLogger(IdentityManager.class);
52 /** The event manager. */
53 private final IdentityListenerManager identityListenerManager = new IdentityListenerManager();
55 /** The Web of Trust connector. */
56 private final WebOfTrustConnector webOfTrustConnector;
58 /** The context to filter for. */
59 private volatile String context;
61 /** The currently known own identities. */
62 /* synchronize access on syncObject. */
63 private Map<String, OwnIdentity> currentOwnIdentities = new HashMap<String, OwnIdentity>();
66 * Creates a new identity manager.
68 * @param webOfTrustConnector
69 * The Web of Trust connector
71 public IdentityManager(WebOfTrustConnector webOfTrustConnector) {
72 super("Sone Identity Manager", false);
73 this.webOfTrustConnector = webOfTrustConnector;
77 // LISTENER MANAGEMENT
81 * Adds a listener for identity events.
83 * @param identityListener
86 public void addIdentityListener(IdentityListener identityListener) {
87 identityListenerManager.addListener(identityListener);
91 * Removes a listener for identity events.
93 * @param identityListener
94 * The listener to remove
96 public void removeIdentityListener(IdentityListener identityListener) {
97 identityListenerManager.removeListener(identityListener);
105 * Sets the context to filter own identities and trusted identities for.
108 * The context to filter for, or {@code null} to not filter
110 public void setContext(String context) {
111 this.context = context;
115 * Returns whether the Web of Trust plugin could be reached during the last
118 * @return {@code true} if the Web of Trust plugin is connected,
119 * {@code false} otherwise
121 public boolean isConnected() {
123 webOfTrustConnector.ping();
125 } catch (PluginException pe1) {
126 /* not connected, ignore. */
132 * Returns the own identity with the given ID.
135 * The ID of the own identity
136 * @return The own identity, or {@code null} if there is no such identity
138 public OwnIdentity getOwnIdentity(String id) {
139 Set<OwnIdentity> allOwnIdentities = getAllOwnIdentities();
140 for (OwnIdentity ownIdentity : allOwnIdentities) {
141 if (ownIdentity.getId().equals(id)) {
149 * Returns all own identities.
151 * @return All own identities
153 public Set<OwnIdentity> getAllOwnIdentities() {
155 Set<OwnIdentity> ownIdentities = webOfTrustConnector.loadAllOwnIdentities();
156 Map<String, OwnIdentity> newOwnIdentities = new HashMap<String, OwnIdentity>();
157 for (OwnIdentity ownIdentity : ownIdentities) {
158 newOwnIdentities.put(ownIdentity.getId(), ownIdentity);
160 checkOwnIdentities(newOwnIdentities);
161 return ownIdentities;
162 } catch (PluginException pe1) {
163 logger.log(Level.WARNING, "Could not load all own identities!", pe1);
164 return Collections.emptySet();
173 * Adds a context to the given own identity.
180 public void addContext(OwnIdentity ownIdentity, String context) {
181 if (ownIdentity.hasContext(context)) {
185 webOfTrustConnector.addContext(ownIdentity, context);
186 ownIdentity.addContext(context);
187 } catch (PluginException pe1) {
188 logger.log(Level.WARNING, "Could not add context " + context + " to OwnIdentity " + ownIdentity + ".", pe1);
193 * Removes a context from the given own identity.
198 * The context to remove
200 public void removeContext(OwnIdentity ownIdentity, String context) {
201 if (!ownIdentity.hasContext(context)) {
205 webOfTrustConnector.removeContext(ownIdentity, context);
206 ownIdentity.removeContext(context);
207 } catch (PluginException pe1) {
208 logger.log(Level.WARNING, "Could not remove context " + context + " from OwnIdentity " + ownIdentity + ".", pe1);
213 * Sets the property with the given name to the given value.
218 * The name of the property
220 * The value of the property
222 public void setProperty(OwnIdentity ownIdentity, String name, String value) {
224 webOfTrustConnector.setProperty(ownIdentity, name, value);
225 ownIdentity.setProperty(name, value);
226 } catch (PluginException pe1) {
227 logger.log(Level.WARNING, "Could not set property “" + name + "” to “" + value + "” for OwnIdentity: " + ownIdentity, pe1);
232 * Removes the property with the given name.
237 * The name of the property to remove
239 public void removeProperty(OwnIdentity ownIdentity, String name) {
241 webOfTrustConnector.removeProperty(ownIdentity, name);
242 ownIdentity.removeProperty(name);
243 } catch (PluginException pe1) {
244 logger.log(Level.WARNING, "Could not remove property “" + name + "” from OwnIdentity: " + ownIdentity, pe1);
256 protected void serviceRun() {
257 Map<String, Identity> oldIdentities = Collections.emptyMap();
258 while (!shouldStop()) {
259 Map<String, Identity> currentIdentities = new HashMap<String, Identity>();
260 Map<String, OwnIdentity> currentOwnIdentities = new HashMap<String, OwnIdentity>();
262 /* get all identities with the wanted context from WoT. */
263 Set<OwnIdentity> ownIdentities;
265 ownIdentities = webOfTrustConnector.loadAllOwnIdentities();
267 /* check for changes. */
268 for (OwnIdentity ownIdentity : ownIdentities) {
269 currentOwnIdentities.put(ownIdentity.getId(), ownIdentity);
271 checkOwnIdentities(currentOwnIdentities);
273 /* now filter for context and get all identities. */
274 currentOwnIdentities.clear();
275 for (OwnIdentity ownIdentity : ownIdentities) {
276 if ((context != null) && !ownIdentity.hasContext(context)) {
279 currentOwnIdentities.put(ownIdentity.getId(), ownIdentity);
280 for (Identity identity : webOfTrustConnector.loadTrustedIdentities(ownIdentity, context)) {
281 currentIdentities.put(identity.getId(), identity);
285 /* find removed identities. */
286 for (Identity oldIdentity : oldIdentities.values()) {
287 if (!currentIdentities.containsKey(oldIdentity.getId())) {
288 identityListenerManager.fireIdentityRemoved(oldIdentity);
292 /* find new identities. */
293 for (Identity currentIdentity : currentIdentities.values()) {
294 if (!oldIdentities.containsKey(currentIdentity.getId())) {
295 identityListenerManager.fireIdentityAdded(currentIdentity);
299 /* check for changes in the contexts. */
300 for (Identity oldIdentity : oldIdentities.values()) {
301 if (!currentIdentities.containsKey(oldIdentity.getId())) {
304 Identity newIdentity = currentIdentities.get(oldIdentity.getId());
305 Set<String> oldContexts = oldIdentity.getContexts();
306 Set<String> newContexts = newIdentity.getContexts();
307 if (oldContexts.size() != newContexts.size()) {
308 identityListenerManager.fireIdentityUpdated(newIdentity);
311 for (String oldContext : oldContexts) {
312 if (!newContexts.contains(oldContext)) {
313 identityListenerManager.fireIdentityUpdated(newIdentity);
319 /* check for changes in the properties. */
320 for (Identity oldIdentity : oldIdentities.values()) {
321 if (!currentIdentities.containsKey(oldIdentity.getId())) {
324 Identity newIdentity = currentIdentities.get(oldIdentity.getId());
325 Map<String, String> oldProperties = oldIdentity.getProperties();
326 Map<String, String> newProperties = newIdentity.getProperties();
327 if (oldProperties.size() != newProperties.size()) {
328 identityListenerManager.fireIdentityUpdated(newIdentity);
331 for (Entry<String, String> oldProperty : oldProperties.entrySet()) {
332 if (!newProperties.containsKey(oldProperty.getKey()) || !newProperties.get(oldProperty.getKey()).equals(oldProperty.getValue())) {
333 identityListenerManager.fireIdentityUpdated(newIdentity);
339 /* remember the current set of identities. */
340 oldIdentities = currentIdentities;
342 } catch (PluginException pe1) {
343 logger.log(Level.WARNING, "WoT has disappeared!", pe1);
346 /* wait a minute before checking again. */
356 * Checks the given new list of own identities for added or removed own
357 * identities, as compared to {@link #currentOwnIdentities}.
359 * @param newOwnIdentities
360 * The new own identities
362 private void checkOwnIdentities(Map<String, OwnIdentity> newOwnIdentities) {
363 synchronized (syncObject) {
365 /* find removed own identities: */
366 for (OwnIdentity oldOwnIdentity : currentOwnIdentities.values()) {
367 if (!newOwnIdentities.containsKey(oldOwnIdentity.getId())) {
368 identityListenerManager.fireOwnIdentityRemoved(oldOwnIdentity);
372 /* find added own identities. */
373 for (OwnIdentity currentOwnIdentity : newOwnIdentities.values()) {
374 if (!currentOwnIdentities.containsKey(currentOwnIdentity.getId())) {
375 identityListenerManager.fireOwnIdentityAdded(currentOwnIdentity);
379 currentOwnIdentities.clear();
380 currentOwnIdentities.putAll(newOwnIdentities);