2 * Sone - IdentityManager.java - Copyright © 2010–2012 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.util.logging.Logging;
31 import net.pterodactylus.util.service.AbstractService;
34 * The identity manager takes care of loading and storing identities, their
35 * contexts, and properties. It does so in a way that does not expose errors via
36 * exceptions but it only logs them and tries to return sensible defaults.
38 * It is also responsible for polling identities from the Web of Trust plugin
39 * and notifying registered {@link IdentityListener}s when {@link Identity}s and
40 * {@link OwnIdentity}s are discovered or disappearing.
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class IdentityManager extends AbstractService {
46 /** Object used for synchronization. */
47 @SuppressWarnings("hiding")
48 private final Object syncObject = new Object() {
49 /* inner class for better lock names. */
53 private static final Logger logger = Logging.getLogger(IdentityManager.class);
55 /** The event manager. */
56 private final IdentityListenerManager identityListenerManager = new IdentityListenerManager();
58 /** The Web of Trust connector. */
59 private final WebOfTrustConnector webOfTrustConnector;
61 /** The context to filter for. */
62 private final String context;
64 /** The currently known own identities. */
65 /* synchronize access on syncObject. */
66 private final Map<String, OwnIdentity> currentOwnIdentities = new HashMap<String, OwnIdentity>();
68 /** The last time all identities were loaded. */
69 private volatile long identitiesLastLoaded;
72 * Creates a new identity manager.
74 * @param webOfTrustConnector
75 * The Web of Trust connector
77 * The context to focus on (may be {@code null} to ignore
80 public IdentityManager(WebOfTrustConnector webOfTrustConnector, String context) {
81 super("Sone Identity Manager", false);
82 this.webOfTrustConnector = webOfTrustConnector;
83 this.context = context;
87 // LISTENER MANAGEMENT
91 * Adds a listener for identity events.
93 * @param identityListener
96 public void addIdentityListener(IdentityListener identityListener) {
97 identityListenerManager.addListener(identityListener);
101 * Removes a listener for identity events.
103 * @param identityListener
104 * The listener to remove
106 public void removeIdentityListener(IdentityListener identityListener) {
107 identityListenerManager.removeListener(identityListener);
115 * Returns the last time all identities were loaded.
117 * @return The last time all identities were loaded (in milliseconds since
120 public long getIdentitiesLastLoaded() {
121 return identitiesLastLoaded;
125 * Returns whether the Web of Trust plugin could be reached during the last
128 * @return {@code true} if the Web of Trust plugin is connected,
129 * {@code false} otherwise
131 public boolean isConnected() {
133 webOfTrustConnector.ping();
135 } catch (PluginException pe1) {
136 /* not connected, ignore. */
142 * Returns the own identity with the given ID.
145 * The ID of the own identity
146 * @return The own identity, or {@code null} if there is no such identity
148 public OwnIdentity getOwnIdentity(String id) {
149 Set<OwnIdentity> allOwnIdentities = getAllOwnIdentities();
150 for (OwnIdentity ownIdentity : allOwnIdentities) {
151 if (ownIdentity.getId().equals(id)) {
152 return new DefaultOwnIdentity(ownIdentity);
159 * Returns all own identities.
161 * @return All own identities
163 public Set<OwnIdentity> getAllOwnIdentities() {
164 return new HashSet<OwnIdentity>(currentOwnIdentities.values());
175 protected void serviceRun() {
176 Map<OwnIdentity, Map<String, Identity>> oldIdentities = Collections.emptyMap();
177 while (!shouldStop()) {
178 Map<OwnIdentity, Map<String, Identity>> currentIdentities = new HashMap<OwnIdentity, Map<String, Identity>>();
179 Map<String, OwnIdentity> currentOwnIdentities = new HashMap<String, OwnIdentity>();
181 Set<OwnIdentity> ownIdentities = null;
182 boolean identitiesLoaded = false;
184 /* get all identities with the wanted context from WoT. */
185 ownIdentities = webOfTrustConnector.loadAllOwnIdentities();
187 /* load trusted identities. */
188 for (OwnIdentity ownIdentity : ownIdentities) {
189 currentOwnIdentities.put(ownIdentity.getId(), ownIdentity);
190 Map<String, Identity> identities = new HashMap<String, Identity>();
191 currentIdentities.put(ownIdentity, identities);
193 /* if the context doesn’t match, skip getting trusted identities. */
194 if ((context != null) && !ownIdentity.hasContext(context)) {
198 /* load trusted identities. */
199 Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context);
200 for (Identity identity : trustedIdentities) {
201 identities.put(identity.getId(), identity);
204 identitiesLoaded = true;
205 identitiesLastLoaded = System.currentTimeMillis();
206 } catch (WebOfTrustException wote1) {
207 logger.log(Level.WARNING, "WoT has disappeared!", wote1);
210 if (identitiesLoaded) {
212 /* check for changes. */
213 checkOwnIdentities(currentOwnIdentities);
215 /* now check for changes in remote identities. */
216 for (OwnIdentity ownIdentity : currentOwnIdentities.values()) {
218 /* find new identities. */
219 for (Identity currentIdentity : currentIdentities.get(ownIdentity).values()) {
220 if (!oldIdentities.containsKey(ownIdentity) || !oldIdentities.get(ownIdentity).containsKey(currentIdentity.getId())) {
221 identityListenerManager.fireIdentityAdded(ownIdentity, currentIdentity);
225 /* find removed identities. */
226 if (oldIdentities.containsKey(ownIdentity)) {
227 for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
228 if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
229 identityListenerManager.fireIdentityRemoved(ownIdentity, oldIdentity);
233 /* check for changes in the contexts. */
234 for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
235 if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
238 Identity newIdentity = currentIdentities.get(ownIdentity).get(oldIdentity.getId());
239 Set<String> oldContexts = oldIdentity.getContexts();
240 Set<String> newContexts = newIdentity.getContexts();
241 if (oldContexts.size() != newContexts.size()) {
242 identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
245 for (String oldContext : oldContexts) {
246 if (!newContexts.contains(oldContext)) {
247 identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
253 /* check for changes in the properties. */
254 for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
255 if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
258 Identity newIdentity = currentIdentities.get(ownIdentity).get(oldIdentity.getId());
259 Map<String, String> oldProperties = oldIdentity.getProperties();
260 Map<String, String> newProperties = newIdentity.getProperties();
261 if (oldProperties.size() != newProperties.size()) {
262 identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
265 for (Entry<String, String> oldProperty : oldProperties.entrySet()) {
266 if (!newProperties.containsKey(oldProperty.getKey()) || !newProperties.get(oldProperty.getKey()).equals(oldProperty.getValue())) {
267 identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
275 /* remember the current set of identities. */
276 oldIdentities = currentIdentities;
279 /* wait a minute before checking again. */
289 * Checks the given new list of own identities for added or removed own
290 * identities, as compared to {@link #currentOwnIdentities}.
292 * @param newOwnIdentities
293 * The new own identities
295 private void checkOwnIdentities(Map<String, OwnIdentity> newOwnIdentities) {
296 synchronized (syncObject) {
298 /* find removed own identities: */
299 for (OwnIdentity oldOwnIdentity : currentOwnIdentities.values()) {
300 OwnIdentity newOwnIdentity = newOwnIdentities.get(oldOwnIdentity.getId());
301 if ((newOwnIdentity == null) || ((context != null) && oldOwnIdentity.hasContext(context) && !newOwnIdentity.hasContext(context))) {
302 identityListenerManager.fireOwnIdentityRemoved(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 identityListenerManager.fireOwnIdentityAdded(new DefaultOwnIdentity(currentOwnIdentity));
314 currentOwnIdentities.clear();
315 currentOwnIdentities.putAll(newOwnIdentities);