forked from I2P_Developers/i2p.i2p
* Router: Clean up config map methods and uses
This commit is contained in:
@ -1,11 +1,13 @@
|
||||
package net.i2p.router.web;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
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
|
||||
@ -39,28 +41,26 @@ public class ConfigAdvancedHandler extends FormHandler {
|
||||
*
|
||||
*/
|
||||
private void saveChanges() {
|
||||
HashSet unsetKeys = new HashSet(_context.router().getConfigMap().keySet());
|
||||
Set<String> unsetKeys = new HashSet(_context.router().getConfigSettings());
|
||||
if (_config != null) {
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(_config.getBytes())));
|
||||
String line = null;
|
||||
Properties props = new Properties();
|
||||
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);
|
||||
unsetKeys.remove(key);
|
||||
}
|
||||
DataHelper.loadProps(props, new ByteArrayInputStream(_config.getBytes()));
|
||||
} catch (IOException ioe) {
|
||||
_log.error("Config error", ioe);
|
||||
addFormError(ioe.toString());
|
||||
addFormError(_("Error updating the configuration - please see the error logs"));
|
||||
return;
|
||||
}
|
||||
|
||||
Iterator cleaner = unsetKeys.iterator();
|
||||
while (cleaner.hasNext()) {
|
||||
String unsetKey = (String)cleaner.next();
|
||||
for (Map.Entry e : props.entrySet()) {
|
||||
String key = (String) e.getKey();
|
||||
String val = (String) e.getValue();
|
||||
_context.router().setConfigSetting(key, val);
|
||||
unsetKeys.remove(key);
|
||||
}
|
||||
|
||||
for (String unsetKey : unsetKeys) {
|
||||
_context.router().removeConfigSetting(unsetKey);
|
||||
}
|
||||
|
||||
|
@ -1,8 +1,7 @@
|
||||
package net.i2p.router.web;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
|
||||
public class ConfigAdvancedHelper extends HelperBase {
|
||||
@ -10,11 +9,11 @@ public class ConfigAdvancedHelper extends HelperBase {
|
||||
|
||||
public String getSettings() {
|
||||
StringBuilder buf = new StringBuilder(4*1024);
|
||||
Set names = _context.router().getConfigSettings();
|
||||
TreeSet sortedNames = new TreeSet(names);
|
||||
for (Iterator iter = sortedNames.iterator(); iter.hasNext(); ) {
|
||||
String name = (String)iter.next();
|
||||
String val = _context.router().getConfigSetting(name);
|
||||
TreeMap<String, String> sorted = new TreeMap();
|
||||
sorted.putAll(_context.router().getConfigMap());
|
||||
for (Map.Entry<String, String> e : sorted.entrySet()) {
|
||||
String name = e.getKey();
|
||||
String val = e.getValue();
|
||||
buf.append(name).append('=').append(val).append('\n');
|
||||
}
|
||||
return buf.toString();
|
||||
|
@ -14,6 +14,7 @@ import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.HashSet;
|
||||
@ -59,8 +60,8 @@ import net.i2p.util.SimpleTimer;
|
||||
*
|
||||
*/
|
||||
public class Router implements RouterClock.ClockShiftListener {
|
||||
private Log _log;
|
||||
private RouterContext _context;
|
||||
private final Log _log;
|
||||
private final RouterContext _context;
|
||||
private final Map<String, String> _config;
|
||||
/** full path */
|
||||
private String _configFilename;
|
||||
@ -320,7 +321,10 @@ public class Router implements RouterClock.ClockShiftListener {
|
||||
/** @deprecated unused */
|
||||
public boolean getKillVMOnEnd() { return _killVMOnEnd; }
|
||||
|
||||
/** @return absolute path */
|
||||
public String getConfigFilename() { return _configFilename; }
|
||||
|
||||
/** @deprecated unused */
|
||||
public void setConfigFilename(String filename) { _configFilename = filename; }
|
||||
|
||||
public String getConfigSetting(String name) {
|
||||
@ -332,13 +336,19 @@ public class Router implements RouterClock.ClockShiftListener {
|
||||
public void removeConfigSetting(String 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 rv;
|
||||
|
||||
/**
|
||||
* @return unmodifiable Map, unsorted
|
||||
*/
|
||||
public Map<String, String> getConfigMap() {
|
||||
return Collections.unmodifiableMap(_config);
|
||||
}
|
||||
|
||||
public RouterInfo getRouterInfo() { return _routerInfo; }
|
||||
|
@ -1,6 +1,6 @@
|
||||
package net.i2p.router;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import net.i2p.data.Hash;
|
||||
@ -161,10 +161,10 @@ public class TunnelPoolSettings {
|
||||
|
||||
public Properties getUnknownOptions() { return _unknownOptions; }
|
||||
|
||||
public void readFromProperties(String prefix, Properties props) {
|
||||
for (Iterator iter = props.keySet().iterator(); iter.hasNext(); ) {
|
||||
String name = (String)iter.next();
|
||||
String value = props.getProperty(name);
|
||||
public void readFromProperties(String prefix, Map<Object, Object> props) {
|
||||
for (Map.Entry e : props.entrySet()) {
|
||||
String name = (String) e.getKey();
|
||||
String value = (String) e.getValue();
|
||||
if (name.startsWith(prefix)) {
|
||||
if (name.equalsIgnoreCase(prefix + PROP_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_REBUILD_PERIOD, ""+_rebuildPeriod);
|
||||
props.setProperty(prefix + PROP_IP_RESTRICTION, ""+_IPRestriction);
|
||||
for (Iterator iter = _unknownOptions.keySet().iterator(); iter.hasNext(); ) {
|
||||
String name = (String)iter.next();
|
||||
String val = _unknownOptions.getProperty(name);
|
||||
for (Map.Entry e : _unknownOptions.entrySet()) {
|
||||
String name = (String) e.getKey();
|
||||
String val = (String) e.getValue();
|
||||
props.setProperty(prefix + name, val);
|
||||
}
|
||||
}
|
||||
@ -216,9 +216,9 @@ public class TunnelPoolSettings {
|
||||
writeToProperties("", p);
|
||||
buf.append("Tunnel pool settings:\n");
|
||||
buf.append("====================================\n");
|
||||
for (Iterator iter = p.keySet().iterator(); iter.hasNext(); ) {
|
||||
String name = (String)iter.next();
|
||||
String val = p.getProperty(name);
|
||||
for (Map.Entry e : p.entrySet()) {
|
||||
String name = (String) e.getKey();
|
||||
String val = (String) e.getValue();
|
||||
buf.append(name).append(" = [").append(val).append("]\n");
|
||||
}
|
||||
buf.append("is inbound? ").append(_isInbound).append("\n");
|
||||
|
@ -115,7 +115,8 @@ public class ClientAppConfig {
|
||||
// fall back to use router.config's clientApp.* lines
|
||||
if (!cfgFile.exists()) {
|
||||
System.out.println("Warning - No client config file " + cfgFile.getAbsolutePath());
|
||||
return ctx.router().getConfigMap();
|
||||
rv.putAll(ctx.router().getConfigMap());
|
||||
return rv;
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -6,6 +6,7 @@ import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
import java.util.TreeSet;
|
||||
|
||||
@ -121,7 +122,7 @@ public class TunnelPool {
|
||||
return; // don't override client specified settings
|
||||
} else {
|
||||
if (_settings.isExploratory()) {
|
||||
Properties props = _context.router().getConfigMap();
|
||||
Map props = _context.router().getConfigMap();
|
||||
if (_settings.isInbound())
|
||||
_settings.readFromProperties(TunnelPoolSettings.PREFIX_INBOUND_EXPLORATORY, props);
|
||||
else
|
||||
|
Reference in New Issue
Block a user