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.Sone;
33 import net.pterodactylus.util.config.Configuration;
34 import net.pterodactylus.util.config.ConfigurationException;
35 import net.pterodactylus.util.logging.Logging;
36 import net.pterodactylus.util.service.AbstractService;
37 import net.pterodactylus.util.text.StringEscaper;
38 import net.pterodactylus.util.text.TextException;
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 local Sones. */
58 private final Set<Sone> localSones = new HashSet<Sone>();
60 /** Sone inserters. */
61 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
75 * Sets the configuration of the core.
77 * @param configuration
78 * The configuration of the core
79 * @return This core (for method chaining)
81 public Core configuration(Configuration configuration) {
82 this.configuration = configuration;
87 * Sets the Freenet interface to use.
89 * @param freenetInterface
90 * The Freenet interface to use
91 * @return This core (for method chaining)
93 public Core freenetInterface(FreenetInterface freenetInterface) {
94 this.freenetInterface = freenetInterface;
99 * Returns the local Sones.
101 * @return The local Sones
103 public Set<Sone> getSones() {
104 return Collections.unmodifiableSet(localSones);
112 * Adds the given Sone.
117 public void addSone(Sone sone) {
118 if (localSones.add(sone)) {
119 SoneInserter soneInserter = new SoneInserter(freenetInterface, sone);
120 soneInserter.start();
121 soneInserters.put(sone, soneInserter);
126 * Creates a new Sone at a random location.
129 * The name of the Sone
130 * @return The created Sone
131 * @throws SoneException
132 * if a Sone error occurs
134 public Sone createSone(String name) throws SoneException {
135 return createSone(name, null, null);
139 * Creates a new Sone at the given location. If one of {@code requestUri} or
140 * {@code insertUrI} is {@code null}, the Sone is created at a random
144 * The name of the Sone
146 * The request URI of the Sone, or {@link NullPointerException}
147 * to create a Sone at a random location
149 * The insert URI of the Sone, or {@code null} to create a Sone
150 * at a random location
151 * @return The created Sone
152 * @throws SoneException
153 * if a Sone error occurs
155 public Sone createSone(String name, String requestUri, String insertUri) throws SoneException {
156 if ((name == null) || (name.trim().length() == 0)) {
157 throw new SoneException(Type.INVALID_SONE_NAME);
159 String finalRequestUri;
160 String finalInsertUri;
161 if ((requestUri == null) || (insertUri == null)) {
162 String[] keyPair = freenetInterface.generateKeyPair();
163 finalRequestUri = keyPair[0];
164 finalInsertUri = keyPair[1];
166 finalRequestUri = requestUri;
167 finalInsertUri = insertUri;
171 logger.log(Level.FINEST, "Creating new Sone “%s” at %s (%s)…", new Object[] { name, finalRequestUri, finalInsertUri });
172 sone = new Sone(UUID.randomUUID(), name, new FreenetURI(finalRequestUri), new FreenetURI(finalInsertUri));
173 /* set modification counter to 1 so it is inserted immediately. */
174 sone.setModificationCounter(1);
176 } catch (MalformedURLException mue1) {
177 throw new SoneException(Type.INVALID_URI);
179 localSones.add(sone);
184 * Deletes the given Sone from this plugin instance.
189 public void deleteSone(Sone sone) {
190 SoneInserter soneInserter = soneInserters.remove(sone);
192 localSones.remove(sone);
203 protected void serviceStart() {
211 protected void serviceStop() {
212 /* stop all Sone inserters. */
213 for (SoneInserter soneInserter : soneInserters.values()) {
224 * Loads the configuration.
226 private void loadConfiguration() {
227 logger.entering(Core.class.getName(), "loadConfiguration()");
229 /* get names of all local Sones. */
230 String allSoneNamesString = configuration.getStringValue("Sone/Names").getValue(null);
231 if (allSoneNamesString == null) {
232 allSoneNamesString = "";
234 List<String> allSoneNames;
236 allSoneNames = StringEscaper.parseLine(allSoneNamesString);
237 } catch (TextException te1) {
238 logger.log(Level.WARNING, "Could not parse Sone names: “" + allSoneNamesString + "”", te1);
239 allSoneNames = Collections.emptyList();
242 /* parse local Sones. */
243 logger.log(Level.INFO, "Loading %d Sones…", allSoneNames.size());
244 for (String soneName : allSoneNames) {
245 String id = configuration.getStringValue("Sone/Name." + soneName + "/ID").getValue(null);
246 String insertUri = configuration.getStringValue("Sone/Name." + soneName + "/InsertURI").getValue(null);
247 String requestUri = configuration.getStringValue("Sone/Name." + soneName + "/RequestURI").getValue(null);
248 long modificationCounter = configuration.getLongValue("Sone/Name." + soneName + "/ModificationCounter").getValue((long) 0);
250 Sone sone = new Sone(UUID.fromString(id), soneName, new FreenetURI(requestUri), new FreenetURI(insertUri));
251 sone.setModificationCounter(modificationCounter);
253 } catch (MalformedURLException mue1) {
254 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
258 logger.exiting(Core.class.getName(), "loadConfiguration()");
262 * Saves the configuraiton.
264 private void saveConfiguration() {
266 /* get the names of all Sones. */
267 Set<String> soneNames = new HashSet<String>();
268 for (Sone sone : localSones) {
269 soneNames.add(sone.getName());
271 String soneNamesString = StringEscaper.escapeWords(soneNames);
273 logger.log(Level.INFO, "Storing %d Sones…", soneNames.size());
275 /* store names of all Sones. */
276 configuration.getStringValue("Sone/Names").setValue(soneNamesString);
278 /* store all Sones. */
279 for (Sone sone : localSones) {
280 configuration.getStringValue("Sone/Name." + sone.getName() + "/ID").setValue(sone.getId());
281 configuration.getStringValue("Sone/Name." + sone.getName() + "/RequestURI").setValue(sone.getRequestUri().toString());
282 configuration.getStringValue("Sone/Name." + sone.getName() + "/InsertURI").setValue(sone.getInsertUri().toString());
283 configuration.getLongValue("Sone/Name." + sone.getName() + "/ModificationCounter").setValue(sone.getModificationCounter());
285 } catch (ConfigurationException ce1) {
286 logger.log(Level.WARNING, "Could not store configuration!", ce1);