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.logging.Level;
26 import java.util.logging.Logger;
28 import net.pterodactylus.sone.data.Sone;
29 import net.pterodactylus.util.config.Configuration;
30 import net.pterodactylus.util.config.ConfigurationException;
31 import net.pterodactylus.util.logging.Logging;
32 import net.pterodactylus.util.service.AbstractService;
33 import net.pterodactylus.util.text.StringEscaper;
34 import net.pterodactylus.util.text.TextException;
35 import freenet.keys.FreenetURI;
40 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
42 public class Core extends AbstractService {
45 private static final Logger logger = Logging.getLogger(Core.class);
47 /** The configuration. */
48 private Configuration configuration;
50 /** Interface to freenet. */
51 private FreenetInterface freenetInterface;
53 /** The local Sones. */
54 private final Set<Sone> localSones = new HashSet<Sone>();
68 * Sets the configuration of the core.
70 * @param configuration
71 * The configuration of the core
72 * @return This core (for method chaining)
74 public Core configuration(Configuration configuration) {
75 this.configuration = configuration;
80 * Sets the Freenet interface to use.
82 * @param freenetInterface
83 * The Freenet interface to use
84 * @return This core (for method chaining)
86 public Core freenetInterface(FreenetInterface freenetInterface) {
87 this.freenetInterface = freenetInterface;
92 * Returns the local Sones.
94 * @return The local Sones
96 public Set<Sone> localSones() {
112 protected void serviceStart() {
120 protected void serviceStop() {
129 * Loads the configuration.
131 private void loadConfiguration() {
132 logger.entering(Core.class.getName(), "loadConfiguration()");
134 /* get names of all local Sones. */
135 String allSoneNamesString = configuration.getStringValue("Sone/Names").getValue(null);
136 if (allSoneNamesString == null) {
137 allSoneNamesString = "";
139 List<String> allSoneNames;
141 allSoneNames = StringEscaper.parseLine(allSoneNamesString);
142 } catch (TextException te1) {
143 logger.log(Level.WARNING, "Could not parse Sone names: “" + allSoneNamesString + "”", te1);
144 allSoneNames = Collections.emptyList();
147 /* parse local Sones. */
148 logger.log(Level.INFO, "Loading %d Sones…", allSoneNames.size());
149 for (String soneName : allSoneNames) {
150 String insertUri = configuration.getStringValue("Sone/Name." + soneName + "/InsertURI").getValue(null);
151 String requestUri = configuration.getStringValue("Sone/Name." + soneName + "/RequestURI").getValue(null);
153 localSones.add(new Sone(soneName, new FreenetURI(requestUri), new FreenetURI(insertUri)));
154 } catch (MalformedURLException mue1) {
155 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
159 logger.exiting(Core.class.getName(), "loadConfiguration()");
163 * Saves the configuraiton.
165 private void saveConfiguration() {
167 /* get the names of all Sones. */
168 Set<String> soneNames = new HashSet<String>();
169 for (Sone sone : localSones) {
170 soneNames.add(sone.getName());
172 String soneNamesString = StringEscaper.escapeWords(soneNames);
174 logger.log(Level.INFO, "Storing %d Sones…", soneNames.size());
176 /* store names of all Sones. */
177 configuration.getStringValue("Sone/Names").setValue(soneNamesString);
179 /* store all Sones. */
180 for (Sone sone : localSones) {
181 configuration.getStringValue("Sone/Name." + sone.getName() + "/RequestURI").setValue(sone.getRequestUri().toString());
182 configuration.getStringValue("Sone/Name." + sone.getName() + "/InsertURI").setValue(sone.getInsertUri().toString());
184 } catch (ConfigurationException ce1) {
185 logger.log(Level.WARNING, "Could not store configuration!", ce1);