Add loading of local sones from a configuration.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 12 Oct 2010 11:53:11 +0000 (13:53 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 12 Oct 2010 11:53:11 +0000 (13:53 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/main/SonePlugin.java

index a6245ce..84e813e 100644 (file)
 
 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<Sone> localSones = new HashSet<Sone>();
+
        /**
         * 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<String> 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()");
+       }
+
 }
index a9f4928..44208b8 100644 (file)
 
 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.<String, String> 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? */
        }
 
 }