Don’t extend FreenetPage, we don’t need the isLinkExcepted() method in the JSON pages
[Sone.git] / src / main / java / net / pterodactylus / sone / freenet / wot / IdentityManagerImpl.java
1 /*
2  * Sone - IdentityManagerImpl.java - Copyright © 2010–2016 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.sone.freenet.wot;
19
20 import static java.util.logging.Logger.getLogger;
21
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
29
30 import net.pterodactylus.sone.freenet.plugin.PluginException;
31 import net.pterodactylus.util.service.AbstractService;
32
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.Singleton;
37
38 /**
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.
42  * <p>
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
45  * {@link OwnIdentity}s are discovered or disappearing.
46  *
47  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
48  */
49 @Singleton
50 public class IdentityManagerImpl extends AbstractService implements IdentityManager {
51
52         /** The logger. */
53         private static final Logger logger = getLogger(IdentityManagerImpl.class.getName());
54
55         /** The event bus. */
56         private final EventBus eventBus;
57
58         private final IdentityLoader identityLoader;
59
60         /** The Web of Trust connector. */
61         private final WebOfTrustConnector webOfTrustConnector;
62
63         /** The currently known own identities. */
64         private final Set<OwnIdentity> currentOwnIdentities = Sets.newHashSet();
65
66         /**
67          * Creates a new identity manager.
68          *
69          * @param eventBus
70          *            The event bus
71          * @param webOfTrustConnector
72          *            The Web of Trust connector
73          */
74         @Inject
75         public IdentityManagerImpl(EventBus eventBus, WebOfTrustConnector webOfTrustConnector, IdentityLoader identityLoader) {
76                 super("Sone Identity Manager", false);
77                 this.eventBus = eventBus;
78                 this.webOfTrustConnector = webOfTrustConnector;
79                 this.identityLoader = identityLoader;
80         }
81
82         //
83         // ACCESSORS
84         //
85
86         /**
87          * Returns whether the Web of Trust plugin could be reached during the last
88          * try.
89          *
90          * @return {@code true} if the Web of Trust plugin is connected,
91          *         {@code false} otherwise
92          */
93         @Override
94         public boolean isConnected() {
95                 try {
96                         webOfTrustConnector.ping();
97                         return true;
98                 } catch (PluginException pe1) {
99                         /* not connected, ignore. */
100                         return false;
101                 }
102         }
103
104         /**
105          * Returns all own identities.
106          *
107          * @return All own identities
108          */
109         @Override
110         public Set<OwnIdentity> getAllOwnIdentities() {
111                 synchronized (currentOwnIdentities) {
112                         return new HashSet<OwnIdentity>(currentOwnIdentities);
113                 }
114         }
115
116         //
117         // SERVICE METHODS
118         //
119
120         /**
121          * {@inheritDoc}
122          */
123         @Override
124         protected void serviceRun() {
125                 Map<OwnIdentity, Collection<Identity>> oldIdentities = new HashMap<OwnIdentity, Collection<Identity>>();
126
127                 while (!shouldStop()) {
128                         try {
129                                 Map<OwnIdentity, Collection<Identity>> currentIdentities = identityLoader.loadIdentities();
130
131                                 IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, oldIdentities);
132                                 identityChangeEventSender.detectChanges(currentIdentities);
133
134                                 oldIdentities = currentIdentities;
135
136                                 synchronized (currentOwnIdentities) {
137                                         currentOwnIdentities.clear();
138                                         currentOwnIdentities.addAll(currentIdentities.keySet());
139                                 }
140                         } catch (WebOfTrustException wote1) {
141                                 logger.log(Level.WARNING, "WoT has disappeared!", wote1);
142                         }
143
144                         /* wait a minute before checking again. */
145                         sleep(60 * 1000);
146                 }
147         }
148
149 }