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.wotns.freenet.wot;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.HashSet;
25 import java.util.Map.Entry;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
29 import net.pterodactylus.util.collection.SetBuilder;
30 import net.pterodactylus.util.logging.Logging;
31 import net.pterodactylus.util.object.Default;
32 import net.pterodactylus.util.service.AbstractService;
33 import net.pterodactylus.wotns.freenet.plugin.PluginException;
36 * The identity manager takes care of loading and storing identities, their
37 * contexts, and properties. It does so in a way that does not expose errors via
38 * exceptions but it only logs them and tries to return sensible defaults.
40 * It is also responsible for polling identities from the Web of Trust plugin
41 * and notifying registered {@link IdentityListener}s when {@link Identity}s and
42 * {@link OwnIdentity}s are discovered or disappearing.
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public class IdentityManager extends AbstractService {
48 /** Object used for synchronization. */
49 private final Object syncObject = new Object() {
50 /* inner class for better lock names. */
54 private static final Logger logger = Logging.getLogger(IdentityManager.class);
56 /** The event manager. */
57 private final IdentityListenerManager identityListenerManager = new IdentityListenerManager();
59 /** The Web of Trust connector. */
60 private final WebOfTrustConnector webOfTrustConnector;
62 /** The context to filter for. */
63 private volatile String context;
65 /** The currently known own identities. */
66 /* synchronize access on syncObject. */
67 private Map<String, OwnIdentity> currentOwnIdentities = new HashMap<String, OwnIdentity>();
69 /** The currently trusted identities. */
70 private Map<OwnIdentity, Set<Identity>> currentTrustedIdentities = new HashMap<OwnIdentity, Set<Identity>>();
73 * Creates a new identity manager.
75 * @param webOfTrustConnector
76 * The Web of Trust connector
78 public IdentityManager(WebOfTrustConnector webOfTrustConnector) {
79 super("Sone Identity Manager", false);
80 this.webOfTrustConnector = webOfTrustConnector;
84 // LISTENER MANAGEMENT
88 * Adds a listener for identity events.
90 * @param identityListener
93 public void addIdentityListener(IdentityListener identityListener) {
94 identityListenerManager.addListener(identityListener);
98 * Removes a listener for identity events.
100 * @param identityListener
101 * The listener to remove
103 public void removeIdentityListener(IdentityListener identityListener) {
104 identityListenerManager.removeListener(identityListener);
112 * Sets the context to filter own identities and trusted identities for.
115 * The context to filter for, or {@code null} to not filter
117 public void setContext(String context) {
118 this.context = context;
122 * Returns whether the Web of Trust plugin could be reached during the last
125 * @return {@code true} if the Web of Trust plugin is connected, {@code
128 public boolean isConnected() {
130 webOfTrustConnector.ping();
132 } catch (PluginException pe1) {
133 /* not connected, ignore. */
139 * Returns the own identity with the given ID.
142 * The ID of the own identity
143 * @return The own identity, or {@code null} if there is no such identity
145 public OwnIdentity getOwnIdentity(String id) {
146 Set<OwnIdentity> allOwnIdentities = getAllOwnIdentities();
147 for (OwnIdentity ownIdentity : allOwnIdentities) {
148 if (ownIdentity.getId().equals(id)) {
156 * Returns all own identities.
158 * @return All own identities
160 public Set<OwnIdentity> getAllOwnIdentities() {
162 Set<OwnIdentity> ownIdentities = webOfTrustConnector.loadAllOwnIdentities();
163 Map<String, OwnIdentity> newOwnIdentities = new HashMap<String, OwnIdentity>();
164 for (OwnIdentity ownIdentity : ownIdentities) {
165 newOwnIdentities.put(ownIdentity.getId(), ownIdentity);
167 checkOwnIdentities(newOwnIdentities);
168 return ownIdentities;
169 } catch (WebOfTrustException wote1) {
170 logger.log(Level.WARNING, "Could not load all own identities!", wote1);
171 return Collections.emptySet();
175 public Set<Identity> getTrustedIdentities(OwnIdentity ownIdentity) {
176 SetBuilder<Identity> identities = new SetBuilder<Identity>();
177 if ((context == null) || ownIdentity.getContexts().contains(context)) {
178 identities.add(ownIdentity);
180 synchronized (syncObject) {
181 identities.addAll(Default.forNull(currentTrustedIdentities.get(ownIdentity), Collections.<Identity> emptySet()));
183 return identities.get();
194 protected void serviceRun() {
195 Map<OwnIdentity, Map<String, Identity>> oldIdentities = Collections.emptyMap();
196 while (!shouldStop()) {
197 Map<OwnIdentity, Map<String, Identity>> currentIdentities = new HashMap<OwnIdentity, Map<String, Identity>>();
198 @SuppressWarnings("hiding")
199 Map<String, OwnIdentity> currentOwnIdentities = new HashMap<String, OwnIdentity>();
201 Set<OwnIdentity> ownIdentities = null;
202 boolean identitiesLoaded = false;
204 /* get all identities with the wanted context from WoT. */
205 ownIdentities = webOfTrustConnector.loadAllOwnIdentities();
207 /* load trusted identities. */
208 for (OwnIdentity ownIdentity : ownIdentities) {
209 if ((context != null) && !ownIdentity.hasContext(context)) {
212 currentOwnIdentities.put(ownIdentity.getId(), ownIdentity);
214 Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context);
215 Map<String, Identity> identities = new HashMap<String, Identity>();
216 currentIdentities.put(ownIdentity, identities);
217 for (Identity identity : trustedIdentities) {
218 identities.put(identity.getId(), identity);
221 /* add own identities, too, as long as the WoT doesn’t do that. */
222 for (OwnIdentity additionalOwnIdentity : ownIdentities) {
223 if (additionalOwnIdentity == ownIdentity) {
226 if ((context != null) && !additionalOwnIdentity.hasContext(context)) {
229 identities.put(additionalOwnIdentity.getId(), additionalOwnIdentity);
232 identitiesLoaded = true;
233 } catch (WebOfTrustException wote1) {
234 logger.log(Level.WARNING, "WoT has disappeared!", wote1);
237 if (identitiesLoaded) {
239 /* check for changes. */
240 checkOwnIdentities(currentOwnIdentities);
242 /* now check for changes in remote identities. */
243 for (OwnIdentity ownIdentity : currentOwnIdentities.values()) {
245 /* find new identities. */
246 for (Identity currentIdentity : currentIdentities.get(ownIdentity).values()) {
247 if (!oldIdentities.containsKey(ownIdentity) || !oldIdentities.get(ownIdentity).containsKey(currentIdentity.getId())) {
248 identityListenerManager.fireIdentityAdded(ownIdentity, currentIdentity);
252 /* find removed identities. */
253 if (oldIdentities.containsKey(ownIdentity)) {
254 for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
255 if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
256 identityListenerManager.fireIdentityRemoved(ownIdentity, oldIdentity);
260 /* check for changes in the contexts. */
261 for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
262 if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
265 Identity newIdentity = currentIdentities.get(ownIdentity).get(oldIdentity.getId());
266 Set<String> oldContexts = oldIdentity.getContexts();
267 Set<String> newContexts = newIdentity.getContexts();
268 if (oldContexts.size() != newContexts.size()) {
269 identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
272 for (String oldContext : oldContexts) {
273 if (!newContexts.contains(oldContext)) {
274 identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
280 /* check for changes in the properties. */
281 for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
282 if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
285 Identity newIdentity = currentIdentities.get(ownIdentity).get(oldIdentity.getId());
286 Map<String, String> oldProperties = oldIdentity.getProperties();
287 Map<String, String> newProperties = newIdentity.getProperties();
288 if (oldProperties.size() != newProperties.size()) {
289 identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
292 for (Entry<String, String> oldProperty : oldProperties.entrySet()) {
293 if (!newProperties.containsKey(oldProperty.getKey()) || !newProperties.get(oldProperty.getKey()).equals(oldProperty.getValue())) {
294 identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
302 /* remember the current set of identities. */
303 oldIdentities = currentIdentities;
304 synchronized (syncObject) {
305 currentTrustedIdentities.clear();
306 for (Entry<OwnIdentity, Map<String, Identity>> entry : currentIdentities.entrySet()) {
307 Set<Identity> identities = new HashSet<Identity>();
308 currentTrustedIdentities.put(entry.getKey(), identities);
309 for (Identity identity : entry.getValue().values()) {
310 identities.add(identity);
316 /* wait a minute before checking again. */
326 * Checks the given new list of own identities for added or removed own
327 * identities, as compared to {@link #currentOwnIdentities}.
329 * @param newOwnIdentities
330 * The new own identities
332 private void checkOwnIdentities(Map<String, OwnIdentity> newOwnIdentities) {
333 synchronized (syncObject) {
335 /* find removed own identities: */
336 for (OwnIdentity oldOwnIdentity : currentOwnIdentities.values()) {
337 if (!newOwnIdentities.containsKey(oldOwnIdentity.getId())) {
338 identityListenerManager.fireOwnIdentityRemoved(oldOwnIdentity);
342 /* find added own identities. */
343 for (OwnIdentity currentOwnIdentity : newOwnIdentities.values()) {
344 if (!currentOwnIdentities.containsKey(currentOwnIdentity.getId())) {
345 identityListenerManager.fireOwnIdentityAdded(currentOwnIdentity);
349 currentOwnIdentities.clear();
350 currentOwnIdentities.putAll(newOwnIdentities);