3946c049327fc889e65d704c6b8bcf41e2755d24
[Sone.git] / src / main / java / net / pterodactylus / sone / 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.sone.freenet.wot;
19
20 import java.util.Collection;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27
28 import net.pterodactylus.sone.freenet.plugin.PluginException;
29 import net.pterodactylus.util.logging.Logging;
30 import net.pterodactylus.util.service.AbstractService;
31
32 import com.google.common.collect.Sets;
33 import com.google.common.eventbus.EventBus;
34 import com.google.inject.Inject;
35
36 /**
37  * The identity manager takes care of loading and storing identities, their
38  * contexts, and properties. It does so in a way that does not expose errors via
39  * exceptions but it only logs them and tries to return sensible defaults.
40  * <p>
41  * It is also responsible for polling identities from the Web of Trust plugin
42  * and sending events to the {@link EventBus} when {@link Identity}s and
43  * {@link OwnIdentity}s are discovered or disappearing.
44  *
45  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46  */
47 public class IdentityManagerImpl extends AbstractService implements IdentityManager {
48
49         /** The logger. */
50         private static final Logger logger = Logging.getLogger(IdentityManagerImpl.class);
51
52         /** The event bus. */
53         private final EventBus eventBus;
54
55         private final IdentityLoader identityLoader;
56
57         /** The Web of Trust connector. */
58         private final WebOfTrustConnector webOfTrustConnector;
59
60         /** The currently known own identities. */
61         private final Set<OwnIdentity> currentOwnIdentities = Sets.newHashSet();
62
63         /**
64          * Creates a new identity manager.
65          *
66          * @param eventBus
67          *            The event bus
68          * @param webOfTrustConnector
69          *            The Web of Trust connector
70          */
71         @Inject
72         public IdentityManagerImpl(EventBus eventBus, WebOfTrustConnector webOfTrustConnector, IdentityLoader identityLoader) {
73                 super("Sone Identity Manager", false);
74                 this.eventBus = eventBus;
75                 this.webOfTrustConnector = webOfTrustConnector;
76                 this.identityLoader = identityLoader;
77         }
78
79         //
80         // ACCESSORS
81         //
82
83         /**
84          * Returns whether the Web of Trust plugin could be reached during the last
85          * try.
86          *
87          * @return {@code true} if the Web of Trust plugin is connected,
88          *         {@code false} otherwise
89          */
90         @Override
91         public boolean isConnected() {
92                 try {
93                         webOfTrustConnector.ping();
94                         return true;
95                 } catch (PluginException pe1) {
96                         /* not connected, ignore. */
97                         return false;
98                 }
99         }
100
101         /**
102          * Returns all own identities.
103          *
104          * @return All own identities
105          */
106         @Override
107         public Set<OwnIdentity> getAllOwnIdentities() {
108                 synchronized (currentOwnIdentities) {
109                         return new HashSet<OwnIdentity>(currentOwnIdentities);
110                 }
111         }
112
113         //
114         // SERVICE METHODS
115         //
116
117         /**
118          * {@inheritDoc}
119          */
120         @Override
121         protected void serviceRun() {
122                 Map<OwnIdentity, Collection<Identity>> oldIdentities = new HashMap<OwnIdentity, Collection<Identity>>();
123
124                 while (!shouldStop()) {
125                         try {
126                                 Map<OwnIdentity, Collection<Identity>> currentIdentities = identityLoader.loadIdentities();
127
128                                 IdentityChangeEventSender identityChangeEventSender = new IdentityChangeEventSender(eventBus, oldIdentities);
129                                 identityChangeEventSender.detectChanges(currentIdentities);
130
131                                 oldIdentities = currentIdentities;
132
133                                 synchronized (currentOwnIdentities) {
134                                         currentOwnIdentities.clear();
135                                         currentOwnIdentities.addAll(currentIdentities.keySet());
136                                 }
137                         } catch (WebOfTrustException wote1) {
138                                 logger.log(Level.WARNING, "WoT has disappeared!", wote1);
139                         }
140
141                         /* wait a minute before checking again. */
142                         sleep(60 * 1000);
143                 }
144         }
145
146 }