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 static com.google.common.base.Optional.fromNullable;
21 import static com.google.common.collect.HashMultimap.create;
23 import java.util.HashSet;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
28 import net.pterodactylus.sone.freenet.plugin.PluginException;
29 import net.pterodactylus.util.logging.Logging;
30 import net.pterodactylus.util.service.AbstractService;
32 import com.google.common.collect.Multimap;
33 import com.google.common.collect.Sets;
34 import com.google.common.eventbus.EventBus;
35 import com.google.inject.Inject;
36 import com.google.inject.name.Named;
39 * The identity manager takes care of loading and storing identities, their
40 * contexts, and properties. It does so in a way that does not expose errors via
41 * exceptions but it only logs them and tries to return sensible defaults.
43 * It is also responsible for polling identities from the Web of Trust plugin
44 * and sending events to the {@link EventBus} when {@link Identity}s and {@link
45 * OwnIdentity}s are discovered or disappearing.
47 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49 public class IdentityManager extends AbstractService {
52 private static final Logger logger = Logging.getLogger(IdentityManager.class);
55 private final EventBus eventBus;
57 private final IdentityLoader identityLoader;
59 /** The Web of Trust connector. */
60 private final WebOfTrustConnector webOfTrustConnector;
62 /** The currently known own identities. */
63 /* synchronize access on syncObject. */
64 private final Set<OwnIdentity> currentOwnIdentities = Sets.newHashSet();
67 * Creates a new identity manager.
71 * @param webOfTrustConnector
72 * The Web of Trust connector
74 * The context to focus on (may be {@code null} to ignore contexts)
77 public IdentityManager(EventBus eventBus, WebOfTrustConnector webOfTrustConnector, @Named("WebOfTrustContext") String context) {
78 super("Sone Identity Manager", false);
79 this.eventBus = eventBus;
80 this.webOfTrustConnector = webOfTrustConnector;
81 this.identityLoader = new IdentityLoader(webOfTrustConnector, fromNullable(context));
89 * Returns whether the Web of Trust plugin could be reached during the last
92 * @return {@code true} if the Web of Trust plugin is connected, {@code false}
95 public boolean isConnected() {
97 webOfTrustConnector.ping();
99 } catch (PluginException pe1) {
100 /* not connected, ignore. */
106 * Returns all own identities.
108 * @return All own identities
110 public Set<OwnIdentity> getAllOwnIdentities() {
111 synchronized (currentOwnIdentities) {
112 return new HashSet<OwnIdentity>(currentOwnIdentities);
121 protected void serviceRun() {
122 Multimap<OwnIdentity, Identity> oldIdentities = create();
124 while (!shouldStop()) {
126 Multimap<OwnIdentity, Identity> currentIdentities = identityLoader.loadIdentities();
128 IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, oldIdentities);
129 identityChangeEventSender.detectChanges(currentIdentities);
131 oldIdentities = currentIdentities;
133 synchronized (currentOwnIdentities) {
134 currentOwnIdentities.clear();
135 currentOwnIdentities.addAll(currentIdentities.keySet());
137 } catch (WebOfTrustException wote1) {
138 logger.log(Level.WARNING, "WoT has disappeared!", wote1);
141 /* wait a minute before checking again. */