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.
*/
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.
*/
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()");
+ }
+
}
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;
Logging.setup("sone");
}
+ /** The logger. */
+ private static final Logger logger = Logging.getLogger(SonePlugin.class);
+
+ /** The core. */
+ private Core core;
+
//
// FREDPLUGIN METHODS
//
*/
@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();
}
/**
*/
@Override
public void terminate() {
- /* TODO */
+ /* stop the core. */
+ core.stop();
+
+ /* TODO wait for core to stop? */
}
}