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.HashSet;
23 import java.util.List;
25 import java.util.UUID;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
29 import net.pterodactylus.sone.core.SoneException.Type;
30 import net.pterodactylus.sone.data.Sone;
31 import net.pterodactylus.util.config.Configuration;
32 import net.pterodactylus.util.config.ConfigurationException;
33 import net.pterodactylus.util.logging.Logging;
34 import net.pterodactylus.util.service.AbstractService;
35 import net.pterodactylus.util.text.StringEscaper;
36 import net.pterodactylus.util.text.TextException;
37 import freenet.keys.FreenetURI;
42 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
44 public class Core extends AbstractService {
47 private static final Logger logger = Logging.getLogger(Core.class);
49 /** The configuration. */
50 private Configuration configuration;
52 /** Interface to freenet. */
53 private FreenetInterface freenetInterface;
55 /** The local Sones. */
56 private final Set<Sone> localSones = new HashSet<Sone>();
70 * Sets the configuration of the core.
72 * @param configuration
73 * The configuration of the core
74 * @return This core (for method chaining)
76 public Core configuration(Configuration configuration) {
77 this.configuration = configuration;
82 * Sets the Freenet interface to use.
84 * @param freenetInterface
85 * The Freenet interface to use
86 * @return This core (for method chaining)
88 public Core freenetInterface(FreenetInterface freenetInterface) {
89 this.freenetInterface = freenetInterface;
94 * Returns the local Sones.
96 * @return The local Sones
98 public Set<Sone> localSones() {
107 * Creates a new Sone at a random location.
110 * The name of the Sone
111 * @return The created Sone
112 * @throws SoneException
113 * if a Sone error occurs
115 public Sone createSone(String name) throws SoneException {
116 return createSone(name, null, null);
120 * Creates a new Sone at the given location. If one of {@code requestUri} or
121 * {@code insertUrI} is {@code null}, the Sone is created at a random
125 * The name of the Sone
127 * The request URI of the Sone, or {@link NullPointerException}
128 * to create a Sone at a random location
130 * The insert URI of the Sone, or {@code null} to create a Sone
131 * at a random location
132 * @return The created Sone
133 * @throws SoneException
134 * if a Sone error occurs
136 public Sone createSone(String name, String requestUri, String insertUri) throws SoneException {
137 if ((name == null) || (name.trim().length() == 0)) {
138 throw new SoneException(Type.INVALID_SONE_NAME);
140 if ((requestUri == null) || (insertUri == null)) {
141 String[] keyPair = freenetInterface.generateKeyPair();
142 requestUri = keyPair[0];
143 insertUri = keyPair[1];
147 sone = new Sone(UUID.randomUUID(), name, new FreenetURI(requestUri), new FreenetURI(insertUri));
148 } catch (MalformedURLException mue1) {
149 throw new SoneException(Type.INVALID_URI);
151 localSones.add(sone);
163 protected void serviceStart() {
171 protected void serviceStop() {
180 * Loads the configuration.
182 private void loadConfiguration() {
183 logger.entering(Core.class.getName(), "loadConfiguration()");
185 /* get names of all local Sones. */
186 String allSoneNamesString = configuration.getStringValue("Sone/Names").getValue(null);
187 if (allSoneNamesString == null) {
188 allSoneNamesString = "";
190 List<String> allSoneNames;
192 allSoneNames = StringEscaper.parseLine(allSoneNamesString);
193 } catch (TextException te1) {
194 logger.log(Level.WARNING, "Could not parse Sone names: “" + allSoneNamesString + "”", te1);
195 allSoneNames = Collections.emptyList();
198 /* parse local Sones. */
199 logger.log(Level.INFO, "Loading %d Sones…", allSoneNames.size());
200 for (String soneName : allSoneNames) {
201 String id = configuration.getStringValue("Sone/Name." + soneName + "/ID").getValue(null);
202 String insertUri = configuration.getStringValue("Sone/Name." + soneName + "/InsertURI").getValue(null);
203 String requestUri = configuration.getStringValue("Sone/Name." + soneName + "/RequestURI").getValue(null);
205 localSones.add(new Sone(UUID.fromString(id), soneName, new FreenetURI(requestUri), new FreenetURI(insertUri)));
206 } catch (MalformedURLException mue1) {
207 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
211 logger.exiting(Core.class.getName(), "loadConfiguration()");
215 * Saves the configuraiton.
217 private void saveConfiguration() {
219 /* get the names of all Sones. */
220 Set<String> soneNames = new HashSet<String>();
221 for (Sone sone : localSones) {
222 soneNames.add(sone.getName());
224 String soneNamesString = StringEscaper.escapeWords(soneNames);
226 logger.log(Level.INFO, "Storing %d Sones…", soneNames.size());
228 /* store names of all Sones. */
229 configuration.getStringValue("Sone/Names").setValue(soneNamesString);
231 /* store all Sones. */
232 for (Sone sone : localSones) {
233 configuration.getStringValue("Sone/Name." + sone.getName() + "/ID").setValue(sone.getId());
234 configuration.getStringValue("Sone/Name." + sone.getName() + "/RequestURI").setValue(sone.getRequestUri().toString());
235 configuration.getStringValue("Sone/Name." + sone.getName() + "/InsertURI").setValue(sone.getInsertUri().toString());
237 } catch (ConfigurationException ce1) {
238 logger.log(Level.WARNING, "Could not store configuration!", ce1);