X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=f56c1cec2dffdf77e6b7c71e9c2996f7d8454d91;hb=ca0d9d2f705bc8304f4bcd1fea1f38dec3f661e9;hp=a6245cef479d84b997cd6de719e9b235066cf241;hpb=a6ef70ceaf8c750602a0f14c8f814554dd5979ea;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index a6245ce..f56c1ce 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -17,7 +17,21 @@ package net.pterodactylus.sone.core; +import java.net.MalformedURLException; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; + +import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.util.config.Configuration; +import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.service.AbstractService; +import net.pterodactylus.util.text.StringEscaper; +import net.pterodactylus.util.text.TextException; +import freenet.keys.FreenetURI; /** * The Sone core. @@ -26,6 +40,18 @@ import net.pterodactylus.util.service.AbstractService; */ public class Core extends AbstractService { + /** The logger. */ + private static final Logger logger = Logging.getLogger(Core.class); + + /** The configuration. */ + private Configuration configuration; + + /** Interface to freenet. */ + private FreenetInterface freenetInterface; + + /** The local Sones. */ + private final Set localSones = new HashSet(); + /** * Creates a new core. */ @@ -33,4 +59,82 @@ public class Core extends AbstractService { super("Sone Core"); } + // + // ACCESSORS + // + + /** + * Sets the configuration of the core. + * + * @param configuration + * The configuration of the core + * @return This core (for method chaining) + */ + public Core configuration(Configuration configuration) { + this.configuration = configuration; + return this; + } + + /** + * Sets the Freenet interface to use. + * + * @param freenetInterface + * The Freenet interface to use + * @return This core (for method chaining) + */ + public Core freenetInterface(FreenetInterface freenetInterface) { + this.freenetInterface = freenetInterface; + return this; + } + + // + // ACTIONS + // + + // + // SERVICE METHODS + // + + /** + * {@inheritDoc} + */ + @Override + protected void serviceStart() { + loadConfiguration(); + } + + // + // PRIVATE METHODS + // + + /** + * Loads the configuration. + */ + private void loadConfiguration() { + logger.entering(Core.class.getName(), "loadConfiguration()"); + + /* get names of all local Sones. */ + String allSoneNamesString = configuration.getStringValue("Sone/Names").getValue(""); + List allSoneNames; + try { + allSoneNames = StringEscaper.parseLine(allSoneNamesString); + } catch (TextException te1) { + logger.log(Level.WARNING, "Could not parse Sone names: “" + allSoneNamesString + "”", te1); + allSoneNames = Collections.emptyList(); + } + + /* parse local Sones. */ + for (String soneName : allSoneNames) { + String insertUri = configuration.getStringValue("Sone/Name." + soneName + "/InsertURI").getValue(null); + String requestUri = configuration.getStringValue("Sone/Name." + soneName + "/RequestURI").getValue(null); + try { + localSones.add(new Sone(new FreenetURI(requestUri), new FreenetURI(insertUri))); + } catch (MalformedURLException mue1) { + logger.log(Level.WARNING, "Could not create Sone from requestUri (“" + requestUri + "”) and insertUri (“" + insertUri + "”)!", mue1); + } + } + + logger.exiting(Core.class.getName(), "loadConfiguration()"); + } + }