saveConfig for multi-configuration

This commit is contained in:
hankhill19580
2019-05-23 00:29:06 +00:00
parent 63b2f29633
commit 8466cdc675

View File

@@ -53,6 +53,7 @@ public class TunnelControllerGroup implements ClientApp {
// locking: this
private boolean _controllersLoaded;
private final String _configFile;
private final String _configDirectory;
private static final String REGISTERED_NAME = "i2ptunnel";
@@ -117,12 +118,18 @@ public class TunnelControllerGroup implements ClientApp {
_log = _context.logManager().getLog(TunnelControllerGroup.class);
_controllers = new ArrayList<TunnelController>();
_controllersLock = new ReentrantReadWriteLock(true);
if (args == null || args.length <= 0)
if (args == null || args.length <= 0){
_configFile = DEFAULT_CONFIG_FILE;
else if (args.length == 1)
_configDirectory = CONFIG_DIR;
}else if (args.length == 1){
_configFile = args[0];
else
throw new IllegalArgumentException("Usage: TunnelControllerGroup [filename]");
_configDirectory = CONFIG_DIR;
}else if (args.length == 2){
_configFile = args[0];
_configDirectory = args[1];
}else{
throw new IllegalArgumentException("Usage: TunnelControllerGroup [filename] [configdirectory] ");
}
_sessions = new HashMap<I2PSession, Set<TunnelController>>(4);
synchronized (TunnelControllerGroup.class) {
if (_instance == null)
@@ -155,25 +162,29 @@ public class TunnelControllerGroup implements ClientApp {
* @since 0.9.4
*/
public void startup() {
try {
loadControllers(_configFile);
} catch (IllegalArgumentException iae) {
if (DEFAULT_CONFIG_FILE.equals(_configFile) && !_context.isRouterContext()) {
// for i2ptunnel command line
synchronized (this) {
_controllersLoaded = true;
List<String> fileList = configFiles();
for (int i = 0; i < fileList.size(); i++) {
String configFile = fileList.get(i);
try {
loadControllers(configFile);
} catch (IllegalArgumentException iae) {
if (DEFAULT_CONFIG_FILE.equals(configFile) && !_context.isRouterContext()) {
// for i2ptunnel command line
synchronized (this) {
_controllersLoaded = true;
}
_log.logAlways(Log.WARN, "Not in router context and no preconfigured tunnels");
} else {
throw iae;
}
_log.logAlways(Log.WARN, "Not in router context and no preconfigured tunnels");
} else {
throw iae;
}
startControllers();
if (_mgr != null)
_mgr.register(this);
// RouterAppManager registers its own shutdown hook
else
_context.addShutdownTask(new Shutdown());
}
startControllers();
if (_mgr != null)
_mgr.register(this);
// RouterAppManager registers its own shutdown hook
else
_context.addShutdownTask(new Shutdown());
}
/**
@@ -440,9 +451,13 @@ public class TunnelControllerGroup implements ClientApp {
* @throws IllegalArgumentException if unable to reload config file
*/
public synchronized void reloadControllers() {
unloadControllers();
loadControllers(_configFile);
startControllers();
List<String> fileList = configFiles();
for (int i = 0; i < fileList.size(); i++) {
String configFile = fileList.get(i);
unloadControllers();
loadControllers(configFile);
startControllers();
}
}
/**
@@ -614,7 +629,11 @@ public class TunnelControllerGroup implements ClientApp {
*/
@Deprecated
public void saveConfig() throws IOException {
saveConfig(_configFile);
List<String> fileList = configFiles();
for (int i = 0; i < fileList.size(); i++) {
String configFile = fileList.get(i);
saveConfig(configFile);
}
}
/**
@@ -650,6 +669,65 @@ public class TunnelControllerGroup implements ClientApp {
* @since 0.9.34
*/
public synchronized void saveConfig(TunnelController tc) throws IOException {
List<String> fileList = configFiles();
boolean done = false;
Properties inputController = tc.getConfig("");
String inputName = inputController.getProperty("name");
Properties map = new OrderedProperties();
for (int i = 0; i < fileList.size(); i++) {
if (inConfig(tc, fileList.get(i)) != "") {
File cfgFile = new File(fileList.get(i));
if (!cfgFile.isAbsolute())
cfgFile = new File(_context.getConfigDir(), fileList.get(i));
File parent = cfgFile.getParentFile();
if ( (parent != null) && (!parent.exists()) )
parent.mkdirs();
_controllersLock.readLock().lock();
try {
for (int j = 0; j < _controllers.size(); j++) {
TunnelController controller = _controllers.get(j);
Properties controllerProperties = controller.getConfig(PREFIX + j + ".");
String curName = controllerProperties.getProperty("name");
if (curName != inputName) {
map.putAll(controllerProperties);
}
}
} finally {
map.putAll(inputController);
_controllersLock.readLock().unlock();
}
DataHelper.storeProps(map, cfgFile);
done = true;
}
}
if (! done) {
String newConfigFile = _configDirectory + "/" + inputName + ".config";
File cfgFile = new File(newConfigFile);
if (!cfgFile.isAbsolute())
cfgFile = new File(_context.getConfigDir(), newConfigFile);
File parent = cfgFile.getParentFile();
if ( (parent != null) && (!parent.exists()) )
parent.mkdirs();
_controllersLock.readLock().lock();
try {
for (int j = 0; j < _controllers.size(); j++) {
TunnelController controller = _controllers.get(j);
Properties controllerProperties = controller.getConfig(PREFIX + j + ".");
String curName = controllerProperties.getProperty("name");
if (curName != inputName) {
map.putAll(controllerProperties);
}
}
} finally {
map.putAll(inputController);
_controllersLock.readLock().unlock();
}
DataHelper.storeProps(map, cfgFile);
}
}
/**
@@ -659,6 +737,75 @@ public class TunnelControllerGroup implements ClientApp {
public synchronized void removeConfig(TunnelController tc) throws IOException {
}
/**
* Check if the tunnel is present in any config file
* @since 0.9.41
*/
public synchronized String inConfig(TunnelController tc) throws IOException {
List<String> fileList = configFiles();
for (int i = 0; i < fileList.size(); i++) {
String configFile = fileList.get(i);
return inConfig(tc, configFile);
}
return "";
}
/**
* Check if the tunnel is present in the specified config file
* @since 0.9.41
*/
public synchronized String inConfig(TunnelController tc, String configFile) throws IOException {
File cfgFile = new File(configFile);
if (!cfgFile.isAbsolute())
cfgFile = new File(_context.getConfigDir(), configFile);
File parent = cfgFile.getParentFile();
if ( (parent != null) && (!parent.exists()) )
parent.mkdirs();
Properties inputController = tc.getConfig("");
String inputName = inputController.getProperty("name");
String outputName = "";
Properties map = new OrderedProperties();
_controllersLock.readLock().lock();
try {
for (int i = 0; i < _controllers.size(); i++) {
TunnelController controller = _controllers.get(i);
Properties controllerProperties = controller.getConfig(PREFIX + i + ".");
String curName = controllerProperties.getProperty("name");
if (curName == inputName) {
outputName = curName;
}
}
} finally {
_controllersLock.readLock().unlock();
}
return outputName;
}
/**
* Return a list of config files in the config dir as strings
*
* @return non-null, properties loaded, one for each tunnel, or a list
* with one member, "i2ptunnel.config"
*/
private List<String> configFiles() {
File folder = new File(_configDirectory);
File[] listOfFiles = folder.listFiles();
List<String> files = new ArrayList<String>();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files.add(listOfFiles[i].getName());
}
}
files.add(_configFile);
return files;
}
/**
* Load up the config data from the file
*
@@ -699,10 +846,20 @@ public class TunnelControllerGroup implements ClientApp {
* @return list of TunnelController objects
* @throws IllegalArgumentException if unable to load config from file
*/
public List<TunnelController> getControllers() {
public List<TunnelController> getControllers() {
List<String> fileList = configFiles();
List<TunnelController> _tempControllers = new ArrayList<TunnelController>();
for (int i = 0; i < fileList.size(); i++) {
String configFile = fileList.get(i);
_tempControllers.addAll(getControllers(configFile));
}
return _tempControllers;
}
public List<TunnelController> getControllers(String configFile) {
synchronized (this) {
if (!_controllersLoaded)
loadControllers(_configFile);
loadControllers(configFile);
}
_controllersLock.readLock().lock();