From ce4b3424f5503697ee937efb71fb2e3bc86659bc Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 13 Oct 2010 14:07:05 +0200 Subject: [PATCH] Add method to store the configuration when the core is stopped. --- .../java/net/pterodactylus/sone/core/Core.java | 36 ++++++++++++++++++++++ 1 file changed, 36 insertions(+) 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); + } + } + } -- 2.7.4