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 /** The local Sones. */
50 private final Set<Sone> localSones = new HashSet<Sone>();
64 * Sets the configuration of the core.
66 * @param configuration
67 * The configuration of the core
68 * @return This core (for method chaining)
70 public Core configuration(Configuration configuration) {
71 this.configuration = configuration;
87 protected void serviceStart() {
96 * Loads the configuration.
98 private void loadConfiguration() {
99 logger.entering(Core.class.getName(), "loadConfiguration()");
101 /* get names of all local Sones. */
102 String allSoneNamesString = configuration.getStringValue("Sone/Names").getValue("");
103 List<String> allSoneNames;
105 allSoneNames = StringEscaper.parseLine(allSoneNamesString);
106 } catch (TextException te1) {
107 logger.log(Level.WARNING, "Could not parse Sone names: “" + allSoneNamesString + "”", te1);
108 allSoneNames = Collections.emptyList();
111 /* parse local Sones. */
112 for (String soneName : allSoneNames) {
113 String insertUri = configuration.getStringValue("Sone/Name." + soneName + "/InsertURI").getValue(null);
114 String requestUri = configuration.getStringValue("Sone/Name." + soneName + "/RequestURI").getValue(null);
116 localSones.add(new Sone(new FreenetURI(requestUri), new FreenetURI(insertUri)));
117 } catch (MalformedURLException mue1) {
118 logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1);
122 logger.exiting(Core.class.getName(), "loadConfiguration()");