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 java.util.Collection;
21 import java.util.HashMap;
22 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.Sets;
33 import com.google.common.eventbus.EventBus;
34 import com.google.inject.Inject;
35 import com.google.inject.Singleton;
38 * The identity manager takes care of loading and storing identities, their
39 * contexts, and properties. It does so in a way that does not expose errors via
40 * exceptions but it only logs them and tries to return sensible defaults.
42 * It is also responsible for polling identities from the Web of Trust plugin
43 * and sending events to the {@link EventBus} when {@link Identity}s and
44 * {@link OwnIdentity}s are discovered or disappearing.
46 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49 public class IdentityManagerImpl extends AbstractService implements IdentityManager {
52 private static final Logger logger = Logging.getLogger(IdentityManagerImpl.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 private final Set<OwnIdentity> currentOwnIdentities = Sets.newHashSet();
66 * Creates a new identity manager.
70 * @param webOfTrustConnector
71 * The Web of Trust connector
74 public IdentityManagerImpl(EventBus eventBus, WebOfTrustConnector webOfTrustConnector, IdentityLoader identityLoader) {
75 super("Sone Identity Manager", false);
76 this.eventBus = eventBus;
77 this.webOfTrustConnector = webOfTrustConnector;
78 this.identityLoader = identityLoader;
86 * Returns whether the Web of Trust plugin could be reached during the last
89 * @return {@code true} if the Web of Trust plugin is connected,
90 * {@code false} otherwise
93 public boolean isConnected() {
95 webOfTrustConnector.ping();
97 } catch (PluginException pe1) {
98 /* not connected, ignore. */
104 * Returns all own identities.
106 * @return All own identities
109 public Set<OwnIdentity> getAllOwnIdentities() {
110 synchronized (currentOwnIdentities) {
111 return new HashSet<OwnIdentity>(currentOwnIdentities);
123 protected void serviceRun() {
124 Map<OwnIdentity, Collection<Identity>> oldIdentities = new HashMap<OwnIdentity, Collection<Identity>>();
126 while (!shouldStop()) {
128 Map<OwnIdentity, Collection<Identity>> currentIdentities = identityLoader.loadIdentities();
130 IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, oldIdentities);
131 identityChangeEventSender.detectChanges(currentIdentities);
133 oldIdentities = currentIdentities;
135 synchronized (currentOwnIdentities) {
136 currentOwnIdentities.clear();
137 currentOwnIdentities.addAll(currentIdentities.keySet());
139 } catch (WebOfTrustException wote1) {
140 logger.log(Level.WARNING, "WoT has disappeared!", wote1);
143 /* wait a minute before checking again. */