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.freenet.wot;
20 import static java.util.logging.Logger.getLogger;
22 import java.util.Collection;
23 import java.util.HashSet;
26 import java.util.concurrent.TimeUnit;
27 import java.util.concurrent.atomic.AtomicReference;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
31 import javax.inject.Inject;
32 import javax.inject.Singleton;
34 import net.pterodactylus.freenet.plugin.PluginException;
36 import com.google.common.collect.Maps;
37 import com.google.common.collect.Sets;
38 import com.google.common.eventbus.EventBus;
39 import com.google.common.util.concurrent.AbstractScheduledService;
42 * The identity manager takes care of loading and storing identities, their
43 * contexts, and properties. It does so in a way that does not expose errors via
44 * exceptions but it only logs them and tries to return sensible defaults.
46 * It is also responsible for polling identities from the Web of Trust plugin
47 * and sending events to the {@link EventBus} when {@link Identity}s and
48 * {@link OwnIdentity}s are discovered or disappearing.
50 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53 public class IdentityManagerImpl extends AbstractScheduledService implements IdentityManager {
55 private static final Logger logger = getLogger("FWoT.Identities");
57 private final EventBus eventBus;
58 private final IdentityLoader identityLoader;
59 private final WebOfTrustConnector webOfTrustConnector;
61 private final Set<OwnIdentity> currentOwnIdentities = Sets.newHashSet();
62 private final AtomicReference<Map<OwnIdentity, Collection<Identity>>> oldIdentities =
63 new AtomicReference<>(Maps.newHashMap());
66 public IdentityManagerImpl(EventBus eventBus, WebOfTrustConnector webOfTrustConnector,
67 IdentityLoader identityLoader) {
68 this.eventBus = eventBus;
69 this.webOfTrustConnector = webOfTrustConnector;
70 this.identityLoader = identityLoader;
74 protected String serviceName() {
75 return "Freenet-WoT Identity Manager";
79 public boolean isConnected() {
81 webOfTrustConnector.ping();
83 } catch (PluginException pe1) {
84 /* not connected, ignore. */
90 public Set<OwnIdentity> getAllOwnIdentities() {
91 synchronized (currentOwnIdentities) {
92 return new HashSet<>(currentOwnIdentities);
97 protected Scheduler scheduler() {
98 return Scheduler.newFixedDelaySchedule(60, 60, TimeUnit.SECONDS);
102 protected void runOneIteration() {
104 Map<OwnIdentity, Collection<Identity>> currentIdentities = identityLoader.loadIdentities();
106 IdentityChangeEventSender identityChangeEventSender =
107 new IdentityChangeEventSender(eventBus, oldIdentities.get());
108 identityChangeEventSender.detectChanges(currentIdentities);
110 synchronized (currentOwnIdentities) {
111 currentOwnIdentities.clear();
112 currentOwnIdentities.addAll(currentIdentities.keySet());
114 oldIdentities.set(currentIdentities);
115 } catch (PluginException pe1) {
116 logger.log(Level.WARNING, "WoT has disappeared!", pe1);