2 * FreenetSone - Core.java - Copyright © 2010 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.core;
20 import java.net.MalformedURLException;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.HashSet;
26 import java.util.UUID;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
30 import net.pterodactylus.sone.core.SoneException.Type;
31 import net.pterodactylus.sone.data.Post;
32 import net.pterodactylus.sone.data.Profile;
33 import net.pterodactylus.sone.data.Reply;
34 import net.pterodactylus.sone.data.Sone;
35 import net.pterodactylus.util.config.Configuration;
36 import net.pterodactylus.util.config.ConfigurationException;
37 import net.pterodactylus.util.logging.Logging;
38 import net.pterodactylus.util.service.AbstractService;
39 import freenet.keys.FreenetURI;
44 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
46 public class Core extends AbstractService {
49 private static final Logger logger = Logging.getLogger(Core.class);
51 /** The configuration. */
52 private Configuration configuration;
54 /** Interface to freenet. */
55 private FreenetInterface freenetInterface;
57 /** The Sone downloader. */
58 private SoneDownloader soneDownloader;
60 /** The local Sones. */
61 private final Set<Sone> localSones = new HashSet<Sone>();
63 /** Sone inserters. */
64 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
66 /* various caches follow here. */
68 /** Cache for all known Sones. */
69 private final Map<String, Sone> soneCache = new HashMap<String, Sone>();
71 /** Cache for all known posts. */
72 private final Map<String, Post> postCache = new HashMap<String, Post>();
74 /** Cache for all known replies. */
75 private final Map<String, Reply> replyCache = new HashMap<String, Reply>();
89 * Sets the configuration of the core.
91 * @param configuration
92 * The configuration of the core
93 * @return This core (for method chaining)
95 public Core configuration(Configuration configuration) {
96 this.configuration = configuration;
101 * Sets the Freenet interface to use.
103 * @param freenetInterface
104 * The Freenet interface to use
105 * @return This core (for method chaining)
107 public Core freenetInterface(FreenetInterface freenetInterface) {
108 this.freenetInterface = freenetInterface;
109 soneDownloader = new SoneDownloader(freenetInterface);
110 soneDownloader.start();
115 * Returns the local Sones.
117 * @return The local Sones
119 public Set<Sone> getSones() {
120 return Collections.unmodifiableSet(localSones);
124 * Returns the Sone with the given ID, or an empty Sone that has been
125 * initialized with the given ID.
131 public Sone getSone(String soneId) {
132 if (!soneCache.containsKey(soneId)) {
133 Sone sone = new Sone(soneId);
134 soneCache.put(soneId, sone);
136 return soneCache.get(soneId);
144 * Adds the given Sone.
149 public void addLocalSone(Sone sone) {
150 if (localSones.add(sone)) {
151 soneCache.put(sone.getId(), sone);
152 SoneInserter soneInserter = new SoneInserter(freenetInterface, sone);
153 soneInserter.start();
154 soneDownloader.addSone(sone);
155 soneInserters.put(sone, soneInserter);
160 * Adds a remote Sone so that it is watched for updates.
165 public void addSone(Sone sone) {
166 if (!soneCache.containsKey(sone.getId())) {
167 soneCache.put(sone.getId(), sone);
169 soneDownloader.addSone(sone);
173 * Creates a new Sone at a random location.
176 * The name of the Sone
177 * @return The created Sone
178 * @throws SoneException
179 * if a Sone error occurs
181 public Sone createSone(String name) throws SoneException {
182 return createSone(name, null, null);
186 * Creates a new Sone at the given location. If one of {@code requestUri} or
187 * {@code insertUrI} is {@code null}, the Sone is created at a random
191 * The name of the Sone
193 * The request URI of the Sone, or {@link NullPointerException}
194 * to create a Sone at a random location
196 * The insert URI of the Sone, or {@code null} to create a Sone
197 * at a random location
198 * @return The created Sone
199 * @throws SoneException
200 * if a Sone error occurs
202 public Sone createSone(String name, String requestUri, String insertUri) throws SoneException {
203 if ((name == null) || (name.trim().length() == 0)) {
204 throw new SoneException(Type.INVALID_SONE_NAME);
206 String finalRequestUri;
207 String finalInsertUri;
208 if ((requestUri == null) || (insertUri == null)) {
209 String[] keyPair = freenetInterface.generateKeyPair();
210 finalRequestUri = keyPair[0];
211 finalInsertUri = keyPair[1];
213 finalRequestUri = requestUri;
214 finalInsertUri = insertUri;
218 logger.log(Level.FINEST, "Creating new Sone “%s” at %s (%s)…", new Object[] { name, finalRequestUri, finalInsertUri });
219 sone = new Sone(UUID.randomUUID().toString()).setName(name).setRequestUri(new FreenetURI(finalRequestUri).setKeyType("USK").setDocName("Sone-" + name)).setInsertUri(new FreenetURI(finalInsertUri).setKeyType("USK").setDocName("Sone-" + name));
220 sone.setProfile(new Profile());
221 /* set modification counter to 1 so it is inserted immediately. */
222 sone.setModificationCounter(1);
224 } catch (MalformedURLException mue1) {
225 throw new SoneException(Type.INVALID_URI);
231 * Deletes the given Sone from this plugin instance.
236 public void deleteSone(Sone sone) {
237 SoneInserter soneInserter = soneInserters.remove(sone);
239 localSones.remove(sone);
250 protected void serviceStart() {
258 protected void serviceStop() {
259 soneDownloader.stop();
260 /* stop all Sone inserters. */
261 for (SoneInserter soneInserter : soneInserters.values()) {
272 * Loads the configuration.
274 private void loadConfiguration() {
275 logger.entering(Core.class.getName(), "loadConfiguration()");
277 /* parse local Sones. */
278 logger.log(Level.INFO, "Loading Sones…");
281 String sonePrefix = "Sone/Sone." + soneId++;
282 String id = configuration.getStringValue(sonePrefix + "/ID").getValue(null);
286 String name = configuration.getStringValue(sonePrefix + "/Name").getValue(null);
287 String insertUri = configuration.getStringValue(sonePrefix + "/InsertURI").getValue(null);
288 String requestUri = configuration.getStringValue(sonePrefix + "/RequestURI").getValue(null);
289 long modificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0);
290 String firstName = configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null);
291 String middleName = configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null);
292 String lastName = configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null);
294 Profile profile = new Profile();
295 profile.setFirstName(firstName);
296 profile.setMiddleName(middleName);
297 profile.setLastName(lastName);
298 Sone sone = new Sone(id).setName(name).setRequestUri(new FreenetURI(requestUri)).setInsertUri(new FreenetURI(insertUri));
299 soneCache.put(id, sone);
300 sone.setProfile(profile);
303 String postPrefix = sonePrefix + "/Post." + postId++;
304 id = configuration.getStringValue(postPrefix + "/ID").getValue(null);
308 long time = configuration.getLongValue(postPrefix + "/Time").getValue(null);
309 String text = configuration.getStringValue(postPrefix + "/Text").getValue(null);
310 Post post = new Post(id, sone, time, text);
311 postCache.put(id, post);
314 int replyCounter = 0;
316 String replyPrefix = sonePrefix + "/Reply." + replyCounter++;
317 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
318 if (replyId == null) {
321 Sone replySone = getSone(configuration.getStringValue(replyPrefix + "/Sone/ID").getValue(null));
322 String replySoneKey = configuration.getStringValue(replyPrefix + "/Sone/Key").getValue(null);
323 String replySoneName = configuration.getStringValue(replyPrefix + "/Sone/Name").getValue(null);
324 replySone.setRequestUri(new FreenetURI(replySoneKey)).setName(replySoneName);
325 Post replyPost = postCache.get(configuration.getStringValue(replyPrefix + "/Post").getValue(null));
326 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue(null);
327 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
328 Reply reply = new Reply(replyId, replySone, replyPost, replyTime, replyText);
329 replyCache.put(replyId, reply);
333 int friendCounter = 0;
335 String friendPrefix = sonePrefix + "/Friend." + friendCounter++;
336 String friendId = configuration.getStringValue(friendPrefix + "/ID").getValue(null);
337 if (friendId == null) {
340 Sone friendSone = getSone(friendId);
341 String friendKey = configuration.getStringValue(friendPrefix + "/Key").getValue(null);
342 String friendName = configuration.getStringValue(friendPrefix + "/Name").getValue(null);
343 friendSone.setRequestUri(new FreenetURI(friendKey)).setName(friendName);
345 sone.addFriend(sone);
348 sone.setModificationCounter(modificationCounter);
350 } catch (MalformedURLException mue1) {
351 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
354 logger.log(Level.INFO, "Loaded %d Sones.", getSones().size());
356 logger.exiting(Core.class.getName(), "loadConfiguration()");
360 * Saves the configuraiton.
362 private void saveConfiguration() {
363 Set<Sone> sones = getSones();
364 logger.log(Level.INFO, "Storing %d Sones…", sones.size());
366 /* store all Sones. */
368 for (Sone sone : localSones) {
369 String sonePrefix = "Sone/Sone." + soneId++;
370 configuration.getStringValue(sonePrefix + "/ID").setValue(sone.getId());
371 configuration.getStringValue(sonePrefix + "/Name").setValue(sone.getName());
372 configuration.getStringValue(sonePrefix + "/RequestURI").setValue(sone.getRequestUri().toString());
373 configuration.getStringValue(sonePrefix + "/InsertURI").setValue(sone.getInsertUri().toString());
374 configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter());
375 Profile profile = sone.getProfile();
376 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
377 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
378 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
380 for (Post post : sone.getPosts()) {
381 String postPrefix = sonePrefix + "/Post." + postId++;
382 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
383 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
384 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
386 /* write null ID as terminator. */
387 configuration.getStringValue(sonePrefix + "/Post." + postId + "/ID").setValue(null);
390 for (Reply reply : sone.getReplies()) {
391 String replyPrefix = sonePrefix + "/Reply." + replyId++;
392 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
393 configuration.getStringValue(replyPrefix + "/Sone/ID").setValue(reply.getSone().getId());
394 configuration.getStringValue(replyPrefix + "/Sone/Key").setValue(reply.getSone().getRequestUri().toString());
395 configuration.getStringValue(replyPrefix + "/Sone/Name").setValue(reply.getSone().getName());
396 configuration.getStringValue(replyPrefix + "/Post").setValue(reply.getPost().getId());
397 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
398 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
400 /* write null ID as terminator. */
401 configuration.getStringValue(sonePrefix + "/Reply." + replyId + "/ID").setValue(null);
404 for (Sone friend : sone.getFriends()) {
405 String friendPrefix = sonePrefix + "/Friend." + friendId++;
406 configuration.getStringValue(friendPrefix + "/ID").setValue(friend.getId());
407 configuration.getStringValue(friendPrefix + "/Key").setValue(friend.getRequestUri().toString());
408 configuration.getStringValue(friendPrefix + "/Name").setValue(friend.getName());
410 /* write null ID as terminator. */
411 configuration.getStringValue(sonePrefix + "/Friend." + friendId + "/ID").setValue(null);
414 /* write null ID as terminator. */
415 configuration.getStringValue("Sone/Sone." + soneId + "/ID").setValue(null);
417 } catch (ConfigurationException ce1) {
418 logger.log(Level.WARNING, "Could not store configuration!", ce1);