2 * Sone - DefaultIdentity.java - Copyright © 2010–2012 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 java.util.Collections;
21 import java.util.HashMap;
22 import java.util.HashSet;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
28 import net.pterodactylus.sone.freenet.plugin.PluginException;
29 import net.pterodactylus.util.cache.CacheException;
30 import net.pterodactylus.util.cache.CacheItem;
31 import net.pterodactylus.util.cache.DefaultCacheItem;
32 import net.pterodactylus.util.cache.MemoryCache;
33 import net.pterodactylus.util.cache.ValueRetriever;
34 import net.pterodactylus.util.cache.WritableCache;
35 import net.pterodactylus.util.collection.TimedMap;
36 import net.pterodactylus.util.logging.Logging;
39 * A Web of Trust identity.
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class DefaultIdentity implements Identity {
46 private static final Logger logger = Logging.getLogger(DefaultIdentity.class);
48 /** The web of trust connector. */
49 private final WebOfTrustConnector webOfTrustConnector;
51 /** The ID of the identity. */
52 private final String id;
54 /** The nickname of the identity. */
55 private final String nickname;
57 /** The request URI of the identity. */
58 private final String requestUri;
60 /** The contexts of the identity. */
61 private final Set<String> contexts = Collections.synchronizedSet(new HashSet<String>());
63 /** The properties of the identity. */
64 private final Map<String, String> properties = Collections.synchronizedMap(new HashMap<String, String>());
67 /* synchronize on itself. */
68 private final WritableCache<OwnIdentity, Trust> trustCache = new MemoryCache<OwnIdentity, Trust>(new ValueRetriever<OwnIdentity, Trust>() {
71 @SuppressWarnings("synthetic-access")
72 public CacheItem<Trust> retrieve(OwnIdentity ownIdentity) throws CacheException {
74 return new DefaultCacheItem<Trust>(webOfTrustConnector.getTrust(ownIdentity, DefaultIdentity.this));
75 } catch (PluginException pe1) {
76 throw new CacheException("Could not retrieve trust for OwnIdentity: " + ownIdentity, pe1);
80 }, new TimedMap<OwnIdentity, CacheItem<Trust>>(60 * 60 * 1000));
83 * Creates a new identity.
85 * @param webOfTrustConnector
86 * The web of trust connector
88 * The ID of the identity
90 * The nickname of the identity
92 * The request URI of the identity
94 public DefaultIdentity(WebOfTrustConnector webOfTrustConnector, String id, String nickname, String requestUri) {
95 this.webOfTrustConnector = webOfTrustConnector;
97 this.nickname = nickname;
98 this.requestUri = requestUri;
109 public String getId() {
117 public String getNickname() {
125 public String getRequestUri() {
133 public Set<String> getContexts() {
134 return Collections.unmodifiableSet(contexts);
138 * Sets the contexts of this identity.
140 * This method is only called by the {@link IdentityManager}.
143 * The contexts to set
145 void setContextsPrivate(Set<String> contexts) {
146 this.contexts.clear();
147 this.contexts.addAll(contexts);
154 public boolean hasContext(String context) {
155 return contexts.contains(context);
159 * Adds the given context to this identity.
161 * This method is only called by the {@link IdentityManager}.
166 void addContextPrivate(String context) {
167 contexts.add(context);
171 * Removes the given context from this identity.
173 * This method is only called by the {@link IdentityManager}.
176 * The context to remove
178 public void removeContextPrivate(String context) {
179 contexts.remove(context);
186 public Map<String, String> getProperties() {
187 synchronized (properties) {
188 return Collections.unmodifiableMap(properties);
193 * Sets all properties of this identity.
195 * This method is only called by the {@link IdentityManager}.
198 * The new properties of this identity
200 void setPropertiesPrivate(Map<String, String> properties) {
201 synchronized (this.properties) {
202 this.properties.clear();
203 this.properties.putAll(properties);
208 * Sets the property with the given name to the given value.
210 * This method is only called by the {@link IdentityManager}.
213 * The name of the property
215 * The value of the property
217 void setPropertyPrivate(String name, String value) {
218 synchronized (properties) {
219 properties.put(name, value);
227 public String getProperty(String name) {
228 synchronized (properties) {
229 return properties.get(name);
234 * Removes the property with the given name.
236 * This method is only called by the {@link IdentityManager}.
239 * The name of the property to remove
241 void removePropertyPrivate(String name) {
242 synchronized (properties) {
243 properties.remove(name);
251 public Trust getTrust(OwnIdentity ownIdentity) {
253 synchronized (trustCache) {
254 return trustCache.get(ownIdentity);
256 } catch (CacheException ce1) {
257 logger.log(Level.WARNING, "Could not get trust for OwnIdentity: " + ownIdentity, ce1);
263 * Sets the trust received for this identity by the given own identity.
266 * The own identity that gives the trust
268 * The trust received for this identity
270 void setTrustPrivate(OwnIdentity ownIdentity, Trust trust) {
271 synchronized (trustCache) {
272 trustCache.put(ownIdentity, trust);
284 public int hashCode() {
285 return id.hashCode();
292 public boolean equals(Object object) {
293 if (!(object instanceof DefaultIdentity)) {
296 DefaultIdentity identity = (DefaultIdentity) object;
297 return identity.id.equals(id);
304 public String toString() {
305 return getClass().getSimpleName() + "[id=" + id + ",nickname=" + nickname + ",contexts=" + contexts + ",properties=" + properties + "]";