From: David ‘Bombe’ Roden Date: Tue, 12 Oct 2010 11:53:11 +0000 (+0200) Subject: Add loading of local sones from a configuration. X-Git-Tag: 0.1-RC1~541 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=eed67a96e4545a032a4cb82ecc8901d041c42deb;p=Sone.git Add loading of local sones from a configuration. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index a6245ce..84e813e 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,15 @@ 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; + + /** The local Sones. */ + private final Set localSones = new HashSet(); + /** * Creates a new core. */ @@ -33,4 +56,70 @@ 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; + } + + // + // 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()"); + } + } diff --git a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java index a9f4928..44208b8 100644 --- a/src/main/java/net/pterodactylus/sone/main/SonePlugin.java +++ b/src/main/java/net/pterodactylus/sone/main/SonePlugin.java @@ -17,7 +17,19 @@ package net.pterodactylus.sone.main; +import java.io.File; +import java.util.Collections; +import java.util.logging.Level; +import java.util.logging.Logger; + +import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.freenet.PluginStoreConfigurationBackend; +import net.pterodactylus.util.config.Configuration; +import net.pterodactylus.util.config.ConfigurationException; +import net.pterodactylus.util.config.MapConfigurationBackend; +import net.pterodactylus.util.config.XMLConfigurationBackend; import net.pterodactylus.util.logging.Logging; +import freenet.client.async.DatabaseDisabledException; import freenet.pluginmanager.FredPlugin; import freenet.pluginmanager.PluginRespirator; @@ -34,6 +46,12 @@ public class SonePlugin implements FredPlugin { Logging.setup("sone"); } + /** The logger. */ + private static final Logger logger = Logging.getLogger(SonePlugin.class); + + /** The core. */ + private Core core; + // // FREDPLUGIN METHODS // @@ -43,7 +61,25 @@ public class SonePlugin implements FredPlugin { */ @Override public void runPlugin(PluginRespirator pluginRespirator) { - /* TODO */ + Configuration configuration; + try { + configuration = new Configuration(new PluginStoreConfigurationBackend(pluginRespirator.getStore())); + } catch (DatabaseDisabledException dde1) { + logger.log(Level.WARNING, "Could not load plugin store, using XML files."); + try { + configuration = new Configuration(new XMLConfigurationBackend(new File("sone.xml"), true)); + } catch (ConfigurationException ce1) { + logger.log(Level.SEVERE, "Could not load or create the “sone.xml” configuration file!"); + configuration = new Configuration(new MapConfigurationBackend(Collections. emptyMap())); + } + } + + /* create core. */ + core = new Core(); + core.configuration(configuration); + + /* start core! */ + core.start(); } /** @@ -51,7 +87,10 @@ public class SonePlugin implements FredPlugin { */ @Override public void terminate() { - /* TODO */ + /* stop the core. */ + core.stop(); + + /* TODO wait for core to stop? */ } }