added the handler component to deal with arbitrary changes

This commit is contained in:
jrandom
2004-07-31 04:10:33 +00:00
committed by zzz
parent 75652fc2c4
commit 8f46ead756
2 changed files with 85 additions and 1 deletions

View File

@ -0,0 +1,74 @@
package net.i2p.router.web;
import java.io.ByteArrayInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Handler to deal with form submissions from the advanced config form and act
* upon the values.
*
*/
public class ConfigAdvancedHandler extends FormHandler {
private boolean _forceRestart;
private boolean _shouldSave;
private String _config;
public void ConfigNetHandler() {
_shouldSave = false;
_forceRestart = false;
}
protected void processForm() {
if (_shouldSave) {
saveChanges();
} else {
// noop
}
}
public void setShouldsave(String moo) { _shouldSave = true; }
public void setRestart(String moo) { _forceRestart = true; }
public void setConfig(String val) {
_config = val;
}
/**
* The user made changes to the config and wants to save them, so
* lets go ahead and do so.
*
*/
private void saveChanges() {
if (_config != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(_config.getBytes())));
String line = null;
try {
while ( (line = reader.readLine()) != null) {
int eq = line.indexOf('=');
if (eq == -1) continue;
if (eq >= line.length() - 1) continue;
String key = line.substring(0, eq).trim();
String val = line.substring(eq + 1).trim();
_context.router().setConfigSetting(key, val);
}
} catch (IOException ioe) {
addFormError("Error updating the configuration (IOERROR) - please see the error logs");
return;
}
boolean saved = _context.router().saveConfig();
if (saved)
addFormNotice("Configuration saved successfully");
else
addFormNotice("Error saving the configuration (applied but not saved) - please see the error logs");
if (_forceRestart) {
addFormNotice("Performing a soft restart");
_context.router().restart();
addFormNotice("Soft restart complete");
}
}
}
}