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;
return this;
}
+ /**
+ * Returns the local Sones.
+ *
+ * @return The local Sones
+ */
+ public Set<Sone> localSones() {
+ return localSones;
+ }
+
//
// ACTIONS
//
loadConfiguration();
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ protected void serviceStop() {
+ saveConfiguration();
+ }
+
//
// PRIVATE METHODS
//
logger.entering(Core.class.getName(), "loadConfiguration()");
/* get names of all local Sones. */
- String allSoneNamesString = configuration.getStringValue("Sone/Names").getValue("");
+ String allSoneNamesString = configuration.getStringValue("Sone/Names").getValue(null);
+ if (allSoneNamesString == null) {
+ allSoneNamesString = "";
+ }
List<String> allSoneNames;
try {
allSoneNames = StringEscaper.parseLine(allSoneNamesString);
}
/* parse local Sones. */
+ logger.log(Level.INFO, "Loading %d Sones…", allSoneNames.size());
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)));
+ localSones.add(new Sone(soneName, 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()");
}
+ /**
+ * Saves the configuraiton.
+ */
+ private void saveConfiguration() {
+
+ /* get the names of all Sones. */
+ Set<String> soneNames = new HashSet<String>();
+ 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);
+ }
+ }
+
}