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.service.AbstractService;
32 import net.pterodactylus.wotns.freenet.plugin.PluginException;
35 * The identity manager takes care of loading and storing identities, their
36 * contexts, and properties. It does so in a way that does not expose errors via
37 * exceptions but it only logs them and tries to return sensible defaults.
39 * It is also responsible for polling identities from the Web of Trust plugin
40 * and notifying registered {@link IdentityListener}s when {@link Identity}s and
41 * {@link OwnIdentity}s are discovered or disappearing.
43 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
45 public class IdentityManager extends AbstractService {
47 /** Object used for synchronization. */
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 volatile String context;
64 /** The currently known own identities. */
65 /* synchronize access on syncObject. */
66 private Map<String, OwnIdentity> currentOwnIdentities = new HashMap<String, OwnIdentity>();
68 /** The currently trusted identities. */
69 private Map<OwnIdentity, Set<Identity>> currentTrustedIdentities = new HashMap<OwnIdentity, Set<Identity>>();
72 * Creates a new identity manager.
74 * @param webOfTrustConnector
75 * The Web of Trust connector
77 public IdentityManager(WebOfTrustConnector webOfTrustConnector) {
78 super("Sone Identity Manager", false);
79 this.webOfTrustConnector = webOfTrustConnector;
83 // LISTENER MANAGEMENT
87 * Adds a listener for identity events.
89 * @param identityListener
92 public void addIdentityListener(IdentityListener identityListener) {
93 identityListenerManager.addListener(identityListener);
97 * Removes a listener for identity events.
99 * @param identityListener
100 * The listener to remove
102 public void removeIdentityListener(IdentityListener identityListener) {
103 identityListenerManager.removeListener(identityListener);
111 * Sets the context to filter own identities and trusted identities for.
114 * The context to filter for, or {@code null} to not filter
116 public void setContext(String context) {
117 this.context = context;
121 * Returns whether the Web of Trust plugin could be reached during the last
124 * @return {@code true} if the Web of Trust plugin is connected, {@code
127 public boolean isConnected() {
129 webOfTrustConnector.ping();
131 } catch (PluginException pe1) {
132 /* not connected, ignore. */
138 * Returns the own identity with the given ID.
141 * The ID of the own identity
142 * @return The own identity, or {@code null} if there is no such identity
144 public OwnIdentity getOwnIdentity(String id) {
145 Set<OwnIdentity> allOwnIdentities = getAllOwnIdentities();
146 for (OwnIdentity ownIdentity : allOwnIdentities) {
147 if (ownIdentity.getId().equals(id)) {
155 * Returns all own identities.
157 * @return All own identities
159 public Set<OwnIdentity> getAllOwnIdentities() {
161 Set<OwnIdentity> ownIdentities = webOfTrustConnector.loadAllOwnIdentities();
162 Map<String, OwnIdentity> newOwnIdentities = new HashMap<String, OwnIdentity>();
163 for (OwnIdentity ownIdentity : ownIdentities) {
164 newOwnIdentities.put(ownIdentity.getId(), ownIdentity);
166 checkOwnIdentities(newOwnIdentities);
167 return ownIdentities;
168 } catch (WebOfTrustException wote1) {
169 logger.log(Level.WARNING, "Could not load all own identities!", wote1);
170 return Collections.emptySet();
174 public Set<Identity> getTrustedIdentities(OwnIdentity ownIdentity) {
175 SetBuilder<Identity> identities = new SetBuilder<Identity>();
176 if ((context == null) || ownIdentity.getContexts().contains(context)) {
177 identities.add(ownIdentity);
179 synchronized (syncObject) {
180 identities.addAll(currentTrustedIdentities.get(ownIdentity));
182 return identities.get();
193 protected void serviceRun() {
194 Map<OwnIdentity, Map<String, Identity>> oldIdentities = Collections.emptyMap();
195 while (!shouldStop()) {
196 Map<OwnIdentity, Map<String, Identity>> currentIdentities = new HashMap<OwnIdentity, Map<String, Identity>>();
197 @SuppressWarnings("hiding")
198 Map<String, OwnIdentity> currentOwnIdentities = new HashMap<String, OwnIdentity>();
200 Set<OwnIdentity> ownIdentities = null;
201 boolean identitiesLoaded = false;
203 /* get all identities with the wanted context from WoT. */
204 ownIdentities = webOfTrustConnector.loadAllOwnIdentities();
206 /* load trusted identities. */
207 for (OwnIdentity ownIdentity : ownIdentities) {
208 if ((context != null) && !ownIdentity.hasContext(context)) {
211 currentOwnIdentities.put(ownIdentity.getId(), ownIdentity);
213 Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context);
214 Map<String, Identity> identities = new HashMap<String, Identity>();
215 currentIdentities.put(ownIdentity, identities);
216 for (Identity identity : trustedIdentities) {
217 identities.put(identity.getId(), identity);
220 identitiesLoaded = true;
221 } catch (WebOfTrustException wote1) {
222 logger.log(Level.WARNING, "WoT has disappeared!", wote1);
225 if (identitiesLoaded) {
227 /* check for changes. */
228 checkOwnIdentities(currentOwnIdentities);
230 /* now check for changes in remote identities. */
231 for (OwnIdentity ownIdentity : currentOwnIdentities.values()) {
233 /* find new identities. */
234 for (Identity currentIdentity : currentIdentities.get(ownIdentity).values()) {
235 if (!oldIdentities.containsKey(ownIdentity) || !oldIdentities.get(ownIdentity).containsKey(currentIdentity.getId())) {
236 identityListenerManager.fireIdentityAdded(ownIdentity, currentIdentity);
240 /* find removed identities. */
241 if (oldIdentities.containsKey(ownIdentity)) {
242 for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
243 if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
244 identityListenerManager.fireIdentityRemoved(ownIdentity, oldIdentity);
248 /* check for changes in the contexts. */
249 for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
250 if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
253 Identity newIdentity = currentIdentities.get(ownIdentity).get(oldIdentity.getId());
254 Set<String> oldContexts = oldIdentity.getContexts();
255 Set<String> newContexts = newIdentity.getContexts();
256 if (oldContexts.size() != newContexts.size()) {
257 identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
260 for (String oldContext : oldContexts) {
261 if (!newContexts.contains(oldContext)) {
262 identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
268 /* check for changes in the properties. */
269 for (Identity oldIdentity : oldIdentities.get(ownIdentity).values()) {
270 if (!currentIdentities.get(ownIdentity).containsKey(oldIdentity.getId())) {
273 Identity newIdentity = currentIdentities.get(ownIdentity).get(oldIdentity.getId());
274 Map<String, String> oldProperties = oldIdentity.getProperties();
275 Map<String, String> newProperties = newIdentity.getProperties();
276 if (oldProperties.size() != newProperties.size()) {
277 identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
280 for (Entry<String, String> oldProperty : oldProperties.entrySet()) {
281 if (!newProperties.containsKey(oldProperty.getKey()) || !newProperties.get(oldProperty.getKey()).equals(oldProperty.getValue())) {
282 identityListenerManager.fireIdentityUpdated(ownIdentity, newIdentity);
290 /* remember the current set of identities. */
291 oldIdentities = currentIdentities;
292 synchronized (syncObject) {
293 currentTrustedIdentities.clear();
294 for (Entry<OwnIdentity, Map<String, Identity>> entry : currentIdentities.entrySet()) {
295 Set<Identity> identities = new HashSet<Identity>();
296 currentTrustedIdentities.put(entry.getKey(), identities);
297 for (Identity identity : entry.getValue().values()) {
298 identities.add(identity);
304 /* wait a minute before checking again. */
314 * Checks the given new list of own identities for added or removed own
315 * identities, as compared to {@link #currentOwnIdentities}.
317 * @param newOwnIdentities
318 * The new own identities
320 private void checkOwnIdentities(Map<String, OwnIdentity> newOwnIdentities) {
321 synchronized (syncObject) {
323 /* find removed own identities: */
324 for (OwnIdentity oldOwnIdentity : currentOwnIdentities.values()) {
325 if (!newOwnIdentities.containsKey(oldOwnIdentity.getId())) {
326 identityListenerManager.fireOwnIdentityRemoved(oldOwnIdentity);
330 /* find added own identities. */
331 for (OwnIdentity currentOwnIdentity : newOwnIdentities.values()) {
332 if (!currentOwnIdentities.containsKey(currentOwnIdentity.getId())) {
333 identityListenerManager.fireOwnIdentityAdded(currentOwnIdentity);
337 currentOwnIdentities.clear();
338 currentOwnIdentities.putAll(newOwnIdentities);