* Router: Clean up config map methods and uses

This commit is contained in:
zzz
2011-11-09 18:36:32 +00:00
parent 3fbe8e70e6
commit af42b9e9a8
6 changed files with 58 additions and 47 deletions

View File

@ -1,11 +1,13 @@
package net.i2p.router.web; package net.i2p.router.web;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Map;
import java.util.Properties;
import java.util.Set;
import net.i2p.data.DataHelper;
/** /**
* Handler to deal with form submissions from the advanced config form and act * Handler to deal with form submissions from the advanced config form and act
@ -39,28 +41,26 @@ public class ConfigAdvancedHandler extends FormHandler {
* *
*/ */
private void saveChanges() { private void saveChanges() {
HashSet unsetKeys = new HashSet(_context.router().getConfigMap().keySet()); Set<String> unsetKeys = new HashSet(_context.router().getConfigSettings());
if (_config != null) { if (_config != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(_config.getBytes()))); Properties props = new Properties();
String line = null;
try { try {
while ( (line = reader.readLine()) != null) { DataHelper.loadProps(props, new ByteArrayInputStream(_config.getBytes()));
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);
unsetKeys.remove(key);
}
} catch (IOException ioe) { } catch (IOException ioe) {
_log.error("Config error", ioe);
addFormError(ioe.toString());
addFormError(_("Error updating the configuration - please see the error logs")); addFormError(_("Error updating the configuration - please see the error logs"));
return; return;
} }
Iterator cleaner = unsetKeys.iterator(); for (Map.Entry e : props.entrySet()) {
while (cleaner.hasNext()) { String key = (String) e.getKey();
String unsetKey = (String)cleaner.next(); String val = (String) e.getValue();
_context.router().setConfigSetting(key, val);
unsetKeys.remove(key);
}
for (String unsetKey : unsetKeys) {
_context.router().removeConfigSetting(unsetKey); _context.router().removeConfigSetting(unsetKey);
} }

View File

@ -1,8 +1,7 @@
package net.i2p.router.web; package net.i2p.router.web;
import java.util.Iterator; import java.util.Map;
import java.util.Set; import java.util.TreeMap;
import java.util.TreeSet;
public class ConfigAdvancedHelper extends HelperBase { public class ConfigAdvancedHelper extends HelperBase {
@ -10,11 +9,11 @@ public class ConfigAdvancedHelper extends HelperBase {
public String getSettings() { public String getSettings() {
StringBuilder buf = new StringBuilder(4*1024); StringBuilder buf = new StringBuilder(4*1024);
Set names = _context.router().getConfigSettings(); TreeMap<String, String> sorted = new TreeMap();
TreeSet sortedNames = new TreeSet(names); sorted.putAll(_context.router().getConfigMap());
for (Iterator iter = sortedNames.iterator(); iter.hasNext(); ) { for (Map.Entry<String, String> e : sorted.entrySet()) {
String name = (String)iter.next(); String name = e.getKey();
String val = _context.router().getConfigSetting(name); String val = e.getValue();
buf.append(name).append('=').append(val).append('\n'); buf.append(name).append('=').append(val).append('\n');
} }
return buf.toString(); return buf.toString();

View File

@ -14,6 +14,7 @@ import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.HashSet; import java.util.HashSet;
@ -59,8 +60,8 @@ import net.i2p.util.SimpleTimer;
* *
*/ */
public class Router implements RouterClock.ClockShiftListener { public class Router implements RouterClock.ClockShiftListener {
private Log _log; private final Log _log;
private RouterContext _context; private final RouterContext _context;
private final Map<String, String> _config; private final Map<String, String> _config;
/** full path */ /** full path */
private String _configFilename; private String _configFilename;
@ -320,7 +321,10 @@ public class Router implements RouterClock.ClockShiftListener {
/** @deprecated unused */ /** @deprecated unused */
public boolean getKillVMOnEnd() { return _killVMOnEnd; } public boolean getKillVMOnEnd() { return _killVMOnEnd; }
/** @return absolute path */
public String getConfigFilename() { return _configFilename; } public String getConfigFilename() { return _configFilename; }
/** @deprecated unused */
public void setConfigFilename(String filename) { _configFilename = filename; } public void setConfigFilename(String filename) { _configFilename = filename; }
public String getConfigSetting(String name) { public String getConfigSetting(String name) {
@ -332,13 +336,19 @@ public class Router implements RouterClock.ClockShiftListener {
public void removeConfigSetting(String name) { public void removeConfigSetting(String name) {
_config.remove(name); _config.remove(name);
} }
public Set getConfigSettings() {
return new HashSet(_config.keySet()); /**
* @return unmodifiable Set, unsorted
*/
public Set<String> getConfigSettings() {
return Collections.unmodifiableSet(_config.keySet());
} }
public Properties getConfigMap() {
Properties rv = new Properties(); /**
rv.putAll(_config); * @return unmodifiable Map, unsorted
return rv; */
public Map<String, String> getConfigMap() {
return Collections.unmodifiableMap(_config);
} }
public RouterInfo getRouterInfo() { return _routerInfo; } public RouterInfo getRouterInfo() { return _routerInfo; }

View File

@ -1,6 +1,6 @@
package net.i2p.router; package net.i2p.router;
import java.util.Iterator; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import net.i2p.data.Hash; import net.i2p.data.Hash;
@ -161,10 +161,10 @@ public class TunnelPoolSettings {
public Properties getUnknownOptions() { return _unknownOptions; } public Properties getUnknownOptions() { return _unknownOptions; }
public void readFromProperties(String prefix, Properties props) { public void readFromProperties(String prefix, Map<Object, Object> props) {
for (Iterator iter = props.keySet().iterator(); iter.hasNext(); ) { for (Map.Entry e : props.entrySet()) {
String name = (String)iter.next(); String name = (String) e.getKey();
String value = props.getProperty(name); String value = (String) e.getValue();
if (name.startsWith(prefix)) { if (name.startsWith(prefix)) {
if (name.equalsIgnoreCase(prefix + PROP_ALLOW_ZERO_HOP)) if (name.equalsIgnoreCase(prefix + PROP_ALLOW_ZERO_HOP))
_allowZeroHop = getBoolean(value, DEFAULT_ALLOW_ZERO_HOP); _allowZeroHop = getBoolean(value, DEFAULT_ALLOW_ZERO_HOP);
@ -202,9 +202,9 @@ public class TunnelPoolSettings {
props.setProperty(prefix + PROP_QUANTITY, ""+_quantity); props.setProperty(prefix + PROP_QUANTITY, ""+_quantity);
// props.setProperty(prefix + PROP_REBUILD_PERIOD, ""+_rebuildPeriod); // props.setProperty(prefix + PROP_REBUILD_PERIOD, ""+_rebuildPeriod);
props.setProperty(prefix + PROP_IP_RESTRICTION, ""+_IPRestriction); props.setProperty(prefix + PROP_IP_RESTRICTION, ""+_IPRestriction);
for (Iterator iter = _unknownOptions.keySet().iterator(); iter.hasNext(); ) { for (Map.Entry e : _unknownOptions.entrySet()) {
String name = (String)iter.next(); String name = (String) e.getKey();
String val = _unknownOptions.getProperty(name); String val = (String) e.getValue();
props.setProperty(prefix + name, val); props.setProperty(prefix + name, val);
} }
} }
@ -216,9 +216,9 @@ public class TunnelPoolSettings {
writeToProperties("", p); writeToProperties("", p);
buf.append("Tunnel pool settings:\n"); buf.append("Tunnel pool settings:\n");
buf.append("====================================\n"); buf.append("====================================\n");
for (Iterator iter = p.keySet().iterator(); iter.hasNext(); ) { for (Map.Entry e : p.entrySet()) {
String name = (String)iter.next(); String name = (String) e.getKey();
String val = p.getProperty(name); String val = (String) e.getValue();
buf.append(name).append(" = [").append(val).append("]\n"); buf.append(name).append(" = [").append(val).append("]\n");
} }
buf.append("is inbound? ").append(_isInbound).append("\n"); buf.append("is inbound? ").append(_isInbound).append("\n");

View File

@ -115,7 +115,8 @@ public class ClientAppConfig {
// fall back to use router.config's clientApp.* lines // fall back to use router.config's clientApp.* lines
if (!cfgFile.exists()) { if (!cfgFile.exists()) {
System.out.println("Warning - No client config file " + cfgFile.getAbsolutePath()); System.out.println("Warning - No client config file " + cfgFile.getAbsolutePath());
return ctx.router().getConfigMap(); rv.putAll(ctx.router().getConfigMap());
return rv;
} }
try { try {

View File

@ -6,6 +6,7 @@ import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.TreeSet; import java.util.TreeSet;
@ -121,7 +122,7 @@ public class TunnelPool {
return; // don't override client specified settings return; // don't override client specified settings
} else { } else {
if (_settings.isExploratory()) { if (_settings.isExploratory()) {
Properties props = _context.router().getConfigMap(); Map props = _context.router().getConfigMap();
if (_settings.isInbound()) if (_settings.isInbound())
_settings.readFromProperties(TunnelPoolSettings.PREFIX_INBOUND_EXPLORATORY, props); _settings.readFromProperties(TunnelPoolSettings.PREFIX_INBOUND_EXPLORATORY, props);
else else