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;
24 import java.util.List;
27 import java.util.UUID;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
31 import net.pterodactylus.sone.core.SoneException.Type;
32 import net.pterodactylus.sone.data.Profile;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.util.config.Configuration;
35 import net.pterodactylus.util.config.ConfigurationException;
36 import net.pterodactylus.util.logging.Logging;
37 import net.pterodactylus.util.service.AbstractService;
38 import net.pterodactylus.util.text.StringEscaper;
39 import net.pterodactylus.util.text.TextException;
40 import freenet.keys.FreenetURI;
45 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
47 public class Core extends AbstractService {
50 private static final Logger logger = Logging.getLogger(Core.class);
52 /** The configuration. */
53 private Configuration configuration;
55 /** Interface to freenet. */
56 private FreenetInterface freenetInterface;
58 /** The local Sones. */
59 private final Set<Sone> localSones = new HashSet<Sone>();
61 /** Sone inserters. */
62 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
76 * Sets the configuration of the core.
78 * @param configuration
79 * The configuration of the core
80 * @return This core (for method chaining)
82 public Core configuration(Configuration configuration) {
83 this.configuration = configuration;
88 * Sets the Freenet interface to use.
90 * @param freenetInterface
91 * The Freenet interface to use
92 * @return This core (for method chaining)
94 public Core freenetInterface(FreenetInterface freenetInterface) {
95 this.freenetInterface = freenetInterface;
100 * Returns the local Sones.
102 * @return The local Sones
104 public Set<Sone> getSones() {
105 return Collections.unmodifiableSet(localSones);
113 * Adds the given Sone.
118 public void addSone(Sone sone) {
119 if (localSones.add(sone)) {
120 SoneInserter soneInserter = new SoneInserter(freenetInterface, sone);
121 soneInserter.start();
122 soneInserters.put(sone, soneInserter);
127 * Creates a new Sone at a random location.
130 * The name of the Sone
131 * @return The created Sone
132 * @throws SoneException
133 * if a Sone error occurs
135 public Sone createSone(String name) throws SoneException {
136 return createSone(name, null, null);
140 * Creates a new Sone at the given location. If one of {@code requestUri} or
141 * {@code insertUrI} is {@code null}, the Sone is created at a random
145 * The name of the Sone
147 * The request URI of the Sone, or {@link NullPointerException}
148 * to create a Sone at a random location
150 * The insert URI of the Sone, or {@code null} to create a Sone
151 * at a random location
152 * @return The created Sone
153 * @throws SoneException
154 * if a Sone error occurs
156 public Sone createSone(String name, String requestUri, String insertUri) throws SoneException {
157 if ((name == null) || (name.trim().length() == 0)) {
158 throw new SoneException(Type.INVALID_SONE_NAME);
160 String finalRequestUri;
161 String finalInsertUri;
162 if ((requestUri == null) || (insertUri == null)) {
163 String[] keyPair = freenetInterface.generateKeyPair();
164 finalRequestUri = keyPair[0];
165 finalInsertUri = keyPair[1];
167 finalRequestUri = requestUri;
168 finalInsertUri = insertUri;
172 logger.log(Level.FINEST, "Creating new Sone “%s” at %s (%s)…", new Object[] { name, finalRequestUri, finalInsertUri });
173 sone = new Sone(UUID.randomUUID(), name, new FreenetURI(finalRequestUri), new FreenetURI(finalInsertUri));
174 sone.setProfile(new Profile());
175 /* set modification counter to 1 so it is inserted immediately. */
176 sone.setModificationCounter(1);
178 } catch (MalformedURLException mue1) {
179 throw new SoneException(Type.INVALID_URI);
181 localSones.add(sone);
186 * Deletes the given Sone from this plugin instance.
191 public void deleteSone(Sone sone) {
192 SoneInserter soneInserter = soneInserters.remove(sone);
194 localSones.remove(sone);
205 protected void serviceStart() {
213 protected void serviceStop() {
214 /* stop all Sone inserters. */
215 for (SoneInserter soneInserter : soneInserters.values()) {
226 * Loads the configuration.
228 private void loadConfiguration() {
229 logger.entering(Core.class.getName(), "loadConfiguration()");
231 /* get names of all local Sones. */
232 String allSoneNamesString = configuration.getStringValue("Sone/Names").getValue(null);
233 if (allSoneNamesString == null) {
234 allSoneNamesString = "";
236 List<String> allSoneNames;
238 allSoneNames = StringEscaper.parseLine(allSoneNamesString);
239 } catch (TextException te1) {
240 logger.log(Level.WARNING, "Could not parse Sone names: “" + allSoneNamesString + "”", te1);
241 allSoneNames = Collections.emptyList();
244 /* parse local Sones. */
245 logger.log(Level.INFO, "Loading %d Sones…", allSoneNames.size());
246 for (String soneName : allSoneNames) {
247 String id = configuration.getStringValue("Sone/Name." + soneName + "/ID").getValue(null);
248 String insertUri = configuration.getStringValue("Sone/Name." + soneName + "/InsertURI").getValue(null);
249 String requestUri = configuration.getStringValue("Sone/Name." + soneName + "/RequestURI").getValue(null);
250 long modificationCounter = configuration.getLongValue("Sone/Name." + soneName + "/ModificationCounter").getValue((long) 0);
251 String firstName = configuration.getStringValue("Sone/Name." + soneName + "/Profile/FirstName").getValue(null);
252 String middleName = configuration.getStringValue("Sone/Name." + soneName + "/Profile/MiddleName").getValue(null);
253 String lastName = configuration.getStringValue("Sone/Name." + soneName + "/Profile/LastName").getValue(null);
255 Profile profile = new Profile();
256 profile.setFirstName(firstName);
257 profile.setMiddleName(middleName);
258 profile.setLastName(lastName);
259 Sone sone = new Sone(UUID.fromString(id), soneName, new FreenetURI(requestUri), new FreenetURI(insertUri));
260 sone.setProfile(profile);
261 sone.setModificationCounter(modificationCounter);
263 } catch (MalformedURLException mue1) {
264 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
268 logger.exiting(Core.class.getName(), "loadConfiguration()");
272 * Saves the configuraiton.
274 private void saveConfiguration() {
276 /* get the names of all Sones. */
277 Set<String> soneNames = new HashSet<String>();
278 for (Sone sone : localSones) {
279 soneNames.add(sone.getName());
281 String soneNamesString = StringEscaper.escapeWords(soneNames);
283 logger.log(Level.INFO, "Storing %d Sones…", soneNames.size());
285 /* store names of all Sones. */
286 configuration.getStringValue("Sone/Names").setValue(soneNamesString);
288 /* store all Sones. */
289 for (Sone sone : localSones) {
290 configuration.getStringValue("Sone/Name." + sone.getName() + "/ID").setValue(sone.getId());
291 configuration.getStringValue("Sone/Name." + sone.getName() + "/RequestURI").setValue(sone.getRequestUri().toString());
292 configuration.getStringValue("Sone/Name." + sone.getName() + "/InsertURI").setValue(sone.getInsertUri().toString());
293 configuration.getLongValue("Sone/Name." + sone.getName() + "/ModificationCounter").setValue(sone.getModificationCounter());
294 Profile profile = sone.getProfile();
295 configuration.getStringValue("Sone/Name." + sone.getName() + "/Profile/FirstName").setValue(profile.getFirstName());
296 configuration.getStringValue("Sone/Name." + sone.getName() + "/Profile/MiddleName").setValue(profile.getMiddleName());
297 configuration.getStringValue("Sone/Name." + sone.getName() + "/Profile/LastName").setValue(profile.getLastName());
299 } catch (ConfigurationException ce1) {
300 logger.log(Level.WARNING, "Could not store configuration!", ce1);