Add Freenet plugin and WoT subprojects
[fwot.git] / wot / src / main / java / net / pterodactylus / freenet / wot / IdentityManagerImpl.java
1 /*
2  * Sone - IdentityManager.java - Copyright © 2010–2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.freenet.wot;
19
20 import static java.util.logging.Logger.getLogger;
21
22 import java.util.Collection;
23 import java.util.HashSet;
24 import java.util.Map;
25 import java.util.Set;
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;
30
31 import javax.inject.Inject;
32 import javax.inject.Singleton;
33
34 import net.pterodactylus.freenet.plugin.PluginException;
35
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;
40
41 /**
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.
45  * <p>
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.
49  *
50  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
51  */
52 @Singleton
53 public class IdentityManagerImpl extends AbstractScheduledService implements IdentityManager {
54
55         private static final Logger logger = getLogger("FWoT.Identities");
56
57         private final EventBus eventBus;
58         private final IdentityLoader identityLoader;
59         private final WebOfTrustConnector webOfTrustConnector;
60
61         private final Set<OwnIdentity> currentOwnIdentities = Sets.newHashSet();
62         private final AtomicReference<Map<OwnIdentity, Collection<Identity>>> oldIdentities =
63                         new AtomicReference<>(Maps.newHashMap());
64
65         @Inject
66         public IdentityManagerImpl(EventBus eventBus, WebOfTrustConnector webOfTrustConnector,
67                         IdentityLoader identityLoader) {
68                 this.eventBus = eventBus;
69                 this.webOfTrustConnector = webOfTrustConnector;
70                 this.identityLoader = identityLoader;
71         }
72
73         @Override
74         protected String serviceName() {
75                 return "Freenet-WoT Identity Manager";
76         }
77
78         @Override
79         public boolean isConnected() {
80                 try {
81                         webOfTrustConnector.ping();
82                         return true;
83                 } catch (PluginException pe1) {
84                         /* not connected, ignore. */
85                         return false;
86                 }
87         }
88
89         @Override
90         public Set<OwnIdentity> getAllOwnIdentities() {
91                 synchronized (currentOwnIdentities) {
92                         return new HashSet<>(currentOwnIdentities);
93                 }
94         }
95
96         @Override
97         protected Scheduler scheduler() {
98                 return Scheduler.newFixedDelaySchedule(60, 60, TimeUnit.SECONDS);
99         }
100
101         @Override
102         protected void runOneIteration() {
103                 try {
104                         Map<OwnIdentity, Collection<Identity>> currentIdentities = identityLoader.loadIdentities();
105
106                         IdentityChangeEventSender identityChangeEventSender =
107                                         new IdentityChangeEventSender(eventBus, oldIdentities.get());
108                         identityChangeEventSender.detectChanges(currentIdentities);
109
110                         synchronized (currentOwnIdentities) {
111                                 currentOwnIdentities.clear();
112                                 currentOwnIdentities.addAll(currentIdentities.keySet());
113                         }
114                         oldIdentities.set(currentIdentities);
115                 } catch (PluginException pe1) {
116                         logger.log(Level.WARNING, "WoT has disappeared!", pe1);
117                 }
118         }
119
120 }