From: David ‘Bombe’ Roden Date: Wed, 13 Oct 2010 12:07:05 +0000 (+0200) Subject: Add method to store the configuration when the core is stopped. X-Git-Tag: 0.1-RC1~483 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;ds=sidebyside;h=ce4b3424f5503697ee937efb71fb2e3bc86659bc;p=Sone.git Add method to store the configuration when the core is stopped. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 00bbed0..7333d68 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -27,6 +27,7 @@ import java.util.logging.Logger; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.util.config.Configuration; +import net.pterodactylus.util.config.ConfigurationException; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.service.AbstractService; import net.pterodactylus.util.text.StringEscaper; @@ -112,6 +113,14 @@ public class Core extends AbstractService { loadConfiguration(); } + /** + * {@inheritDoc} + */ + @Override + protected void serviceStop() { + saveConfiguration(); + } + // // PRIVATE METHODS // @@ -150,4 +159,31 @@ public class Core extends AbstractService { logger.exiting(Core.class.getName(), "loadConfiguration()"); } + /** + * Saves the configuraiton. + */ + private void saveConfiguration() { + + /* get the names of all Sones. */ + Set soneNames = new HashSet(); + for (Sone sone : localSones) { + soneNames.add(sone.getName()); + } + String soneNamesString = StringEscaper.escapeWords(soneNames); + + logger.log(Level.INFO, "Storing %d Sones…", soneNames.size()); + try { + /* store names of all Sones. */ + configuration.getStringValue("Sone/Names").setValue(soneNamesString); + + /* store all Sones. */ + for (Sone sone : localSones) { + configuration.getStringValue("Sone/Name." + sone.getName() + "/RequestURI").setValue(sone.getRequestUri().toString()); + configuration.getStringValue("Sone/Name." + sone.getName() + "/InsertURI").setValue(sone.getInsertUri().toString()); + } + } catch (ConfigurationException ce1) { + logger.log(Level.WARNING, "Could not store configuration!", ce1); + } + } + }