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.data.Sone;
30 import net.pterodactylus.util.config.Configuration;
31 import net.pterodactylus.util.config.ConfigurationException;
32 import net.pterodactylus.util.logging.Logging;
33 import net.pterodactylus.util.service.AbstractService;
34 import net.pterodactylus.util.text.StringEscaper;
35 import net.pterodactylus.util.text.TextException;
36 import freenet.keys.FreenetURI;
41 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
43 public class Core extends AbstractService {
46 private static final Logger logger = Logging.getLogger(Core.class);
48 /** The configuration. */
49 private Configuration configuration;
51 /** Interface to freenet. */
52 private FreenetInterface freenetInterface;
54 /** The local Sones. */
55 private final Set<Sone> localSones = new HashSet<Sone>();
69 * Sets the configuration of the core.
71 * @param configuration
72 * The configuration of the core
73 * @return This core (for method chaining)
75 public Core configuration(Configuration configuration) {
76 this.configuration = configuration;
81 * Sets the Freenet interface to use.
83 * @param freenetInterface
84 * The Freenet interface to use
85 * @return This core (for method chaining)
87 public Core freenetInterface(FreenetInterface freenetInterface) {
88 this.freenetInterface = freenetInterface;
93 * Returns the local Sones.
95 * @return The local Sones
97 public Set<Sone> localSones() {
113 protected void serviceStart() {
121 protected void serviceStop() {
130 * Loads the configuration.
132 private void loadConfiguration() {
133 logger.entering(Core.class.getName(), "loadConfiguration()");
135 /* get names of all local Sones. */
136 String allSoneNamesString = configuration.getStringValue("Sone/Names").getValue(null);
137 if (allSoneNamesString == null) {
138 allSoneNamesString = "";
140 List<String> allSoneNames;
142 allSoneNames = StringEscaper.parseLine(allSoneNamesString);
143 } catch (TextException te1) {
144 logger.log(Level.WARNING, "Could not parse Sone names: “" + allSoneNamesString + "”", te1);
145 allSoneNames = Collections.emptyList();
148 /* parse local Sones. */
149 logger.log(Level.INFO, "Loading %d Sones…", allSoneNames.size());
150 for (String soneName : allSoneNames) {
151 String id = configuration.getStringValue("Sone/Name." + soneName + "/ID").getValue(null);
152 String insertUri = configuration.getStringValue("Sone/Name." + soneName + "/InsertURI").getValue(null);
153 String requestUri = configuration.getStringValue("Sone/Name." + soneName + "/RequestURI").getValue(null);
155 localSones.add(new Sone(UUID.fromString(id), soneName, new FreenetURI(requestUri), new FreenetURI(insertUri)));
156 } catch (MalformedURLException mue1) {
157 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
161 logger.exiting(Core.class.getName(), "loadConfiguration()");
165 * Saves the configuraiton.
167 private void saveConfiguration() {
169 /* get the names of all Sones. */
170 Set<String> soneNames = new HashSet<String>();
171 for (Sone sone : localSones) {
172 soneNames.add(sone.getName());
174 String soneNamesString = StringEscaper.escapeWords(soneNames);
176 logger.log(Level.INFO, "Storing %d Sones…", soneNames.size());
178 /* store names of all Sones. */
179 configuration.getStringValue("Sone/Names").setValue(soneNamesString);
181 /* store all Sones. */
182 for (Sone sone : localSones) {
183 configuration.getStringValue("Sone/Name." + sone.getName() + "/ID").setValue(sone.getId());
184 configuration.getStringValue("Sone/Name." + sone.getName() + "/RequestURI").setValue(sone.getRequestUri().toString());
185 configuration.getStringValue("Sone/Name." + sone.getName() + "/InsertURI").setValue(sone.getInsertUri().toString());
187 } catch (ConfigurationException ce1) {
188 logger.log(Level.WARNING, "Could not store configuration!", ce1);