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.logging.Logging;
31 import net.pterodactylus.util.service.AbstractService;
32 import net.pterodactylus.util.text.StringEscaper;
33 import net.pterodactylus.util.text.TextException;
34 import freenet.keys.FreenetURI;
39 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
41 public class Core extends AbstractService {
44 private static final Logger logger = Logging.getLogger(Core.class);
46 /** The configuration. */
47 private Configuration configuration;
49 /** Interface to freenet. */
50 private FreenetInterface freenetInterface;
52 /** The local Sones. */
53 private final Set<Sone> localSones = new HashSet<Sone>();
67 * Sets the configuration of the core.
69 * @param configuration
70 * The configuration of the core
71 * @return This core (for method chaining)
73 public Core configuration(Configuration configuration) {
74 this.configuration = configuration;
79 * Sets the Freenet interface to use.
81 * @param freenetInterface
82 * The Freenet interface to use
83 * @return This core (for method chaining)
85 public Core freenetInterface(FreenetInterface freenetInterface) {
86 this.freenetInterface = freenetInterface;
91 * Returns the local Sones.
93 * @return The local Sones
95 public Set<Sone> localSones() {
111 protected void serviceStart() {
120 * Loads the configuration.
122 private void loadConfiguration() {
123 logger.entering(Core.class.getName(), "loadConfiguration()");
125 /* get names of all local Sones. */
126 String allSoneNamesString = configuration.getStringValue("Sone/Names").getValue(null);
127 if (allSoneNamesString == null) {
128 allSoneNamesString = "";
130 List<String> allSoneNames;
132 allSoneNames = StringEscaper.parseLine(allSoneNamesString);
133 } catch (TextException te1) {
134 logger.log(Level.WARNING, "Could not parse Sone names: “" + allSoneNamesString + "”", te1);
135 allSoneNames = Collections.emptyList();
138 /* parse local Sones. */
139 for (String soneName : allSoneNames) {
140 String insertUri = configuration.getStringValue("Sone/Name." + soneName + "/InsertURI").getValue(null);
141 String requestUri = configuration.getStringValue("Sone/Name." + soneName + "/RequestURI").getValue(null);
143 localSones.add(new Sone(soneName, new FreenetURI(requestUri), new FreenetURI(insertUri)));
144 } catch (MalformedURLException mue1) {
145 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
149 logger.exiting(Core.class.getName(), "loadConfiguration()");