2 * Sone - IdentityLoader.java - Copyright © 2013–2019 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 static java.util.concurrent.TimeUnit.*;
21 import static net.pterodactylus.sone.freenet.wot.Context.*;
24 import java.util.logging.*;
26 import net.pterodactylus.sone.freenet.plugin.*;
28 import com.google.common.base.Optional;
29 import com.google.common.base.*;
30 import com.google.inject.*;
33 * Loads {@link OwnIdentity}s and the {@link Identity}s they trust.
35 public class IdentityLoader {
37 private final Logger logger = Logger.getLogger(IdentityLoader.class.getName());
38 private final WebOfTrustConnector webOfTrustConnector;
39 private final Optional<Context> context;
41 public IdentityLoader(WebOfTrustConnector webOfTrustConnector) {
42 this(webOfTrustConnector, Optional.<Context>absent());
46 public IdentityLoader(WebOfTrustConnector webOfTrustConnector, Optional<Context> context) {
47 this.webOfTrustConnector = webOfTrustConnector;
48 this.context = context;
51 public Map<OwnIdentity, Collection<Identity>> loadIdentities() throws WebOfTrustException {
52 Stopwatch stopwatch = Stopwatch.createStarted();
53 Collection<OwnIdentity> currentOwnIdentities = webOfTrustConnector.loadAllOwnIdentities();
54 logger.fine("Loaded " + currentOwnIdentities.size() + " own identities in " + (stopwatch.elapsed(MILLISECONDS) / 1000.0) + "s.");
55 return loadTrustedIdentitiesForOwnIdentities(currentOwnIdentities);
58 private Map<OwnIdentity, Collection<Identity>> loadTrustedIdentitiesForOwnIdentities(Collection<OwnIdentity> ownIdentities) throws PluginException {
59 Map<OwnIdentity, Collection<Identity>> currentIdentities = new HashMap<>();
61 for (OwnIdentity ownIdentity : ownIdentities) {
62 if (identityDoesNotHaveTheCorrectContext(ownIdentity)) {
63 currentIdentities.put(ownIdentity, Collections.<Identity>emptySet());
67 Stopwatch stopwatch = Stopwatch.createStarted();
68 Set<Identity> trustedIdentities = webOfTrustConnector.loadTrustedIdentities(ownIdentity, context.transform(extractContext));
69 logger.fine("Loaded " + trustedIdentities.size() + " identities for " + ownIdentity.getNickname() + " in " + (stopwatch.elapsed(MILLISECONDS) / 1000.0) + "s.");
70 currentIdentities.put(ownIdentity, trustedIdentities);
73 return currentIdentities;
76 private boolean identityDoesNotHaveTheCorrectContext(OwnIdentity ownIdentity) {
77 return context.isPresent() && !ownIdentity.hasContext(context.transform(extractContext).get());