unwrap some loops, get rid of configFiles(), fix loadConfig

This commit is contained in:
hankhill19580
2019-06-01 16:20:53 +00:00
parent 4fdcd25671
commit 0a1a2ed82a

View File

@ -169,10 +169,11 @@ public class TunnelControllerGroup implements ClientApp {
* @since 0.9.4 * @since 0.9.4
*/ */
public void startup() { public void startup() {
List<String> fileList = configFiles(); List<File> fileList = listFiles();
for (int i = 0; i < fileList.size(); i++) { for (File afile : fileList) {
String configFile = fileList.get(i); String configFile = afile.toString();
try { try {
_log.logAlways(Log.WARN, "Configuring a tunnel " + configFile);
loadControllers(configFile); loadControllers(configFile);
} catch (IllegalArgumentException iae) { } catch (IllegalArgumentException iae) {
if (DEFAULT_CONFIG_FILE.equals(configFile) && !_context.isRouterContext()) { if (DEFAULT_CONFIG_FILE.equals(configFile) && !_context.isRouterContext()) {
@ -353,7 +354,7 @@ public class TunnelControllerGroup implements ClientApp {
System.out.println("Error loading the client app properties from " + f); System.out.println("Error loading the client app properties from " + f);
} }
} catch (IOException ioe) { } catch (IOException ioe) {
_log.error("Error loading the client app properties from " + f, ioe); _log.error("Error loading the client app properties from " + f + ' '+ ioe);
System.out.println("Error loading the client app properties from " + f + ' ' + ioe); System.out.println("Error loading the client app properties from " + f + ' ' + ioe);
} }
} }
@ -464,9 +465,9 @@ public class TunnelControllerGroup implements ClientApp {
* @throws IllegalArgumentException if unable to reload config file * @throws IllegalArgumentException if unable to reload config file
*/ */
public synchronized void reloadControllers() { public synchronized void reloadControllers() {
List<String> fileList = configFiles(); List<File> fileList = listFiles();
for (int i = 0; i < fileList.size(); i++) { for (int i = 0; i < fileList.size(); i++) {
String configFile = fileList.get(i); String configFile = fileList.get(i).toString();
unloadControllers(); unloadControllers();
loadControllers(configFile); loadControllers(configFile);
startControllers(); startControllers();
@ -692,7 +693,7 @@ public class TunnelControllerGroup implements ClientApp {
Properties map = new OrderedProperties(); Properties map = new OrderedProperties();
String foundConfigFile = inConfig(tc); String foundConfigFile = inConfig(tc);
_log.logAlways(Log.DEBUG, "found " + foundConfigFile); _log.logAlways(Log.WARN, "found " + foundConfigFile);
File cfgFile = new File(foundConfigFile); File cfgFile = new File(foundConfigFile);
if (!cfgFile.isAbsolute()) if (!cfgFile.isAbsolute())
@ -750,10 +751,10 @@ public class TunnelControllerGroup implements ClientApp {
* @since 0.9.41 * @since 0.9.41
*/ */
public synchronized String inConfig(TunnelController tc) throws IOException { public synchronized String inConfig(TunnelController tc) throws IOException {
List<String> fileList = configFiles(); List<File> fileList = listFiles();
_log.logAlways(Log.DEBUG, "checking for tunnel controller presence"); _log.logAlways(Log.DEBUG, "checking for tunnel controller presence");
for (int i = 0; i < fileList.size(); i++) { for (File afile : fileList) {
String configFile = fileList.get(i); String configFile = afile.toString();
return inConfig(tc, configFile); return inConfig(tc, configFile);
} }
return ""; return "";
@ -790,34 +791,21 @@ public class TunnelControllerGroup implements ClientApp {
_controllersLock.readLock().unlock(); _controllersLock.readLock().unlock();
} }
if (outputName == "") { if (outputName == "") {
_log.debug("config " + inputName + " not found in " + configFile); _log.logAlways(Log.WARN, "config " + inputName + " not found in " + configFile);
System.out.println("config " + inputName + " not found in " + configFile); System.out.println("config " + inputName + " not found in " + configFile);
}else{ }else{
_log.debug("config " + inputName + " found in " + configFile); _log.logAlways(Log.WARN, "config " + inputName + " found in " + configFile);
System.out.println("config " + inputName + " found in " + configFile); System.out.println("config " + inputName + " found in " + configFile);
} }
return outputName; return outputName;
} }
/** /**
* Return a list of config files in the config dir as strings * List all the config files in the config directory, followed by the
* config file, if they exist
* *
* @return non-null, properties loaded, one for each tunnel, or a list * @return non-null list of files
* with one member, "i2ptunnel.config"
*/ */
private List<String> configFiles() {
List<File> listOfFiles = listFiles();
List<String> files = new ArrayList<String>();
boolean shouldMigrate = _context.isRouterContext() && !SystemVersion.isAndroid();
if (listOfFiles != null && listOfFiles.size() > 0) {
for (int i = 0; i < listOfFiles.size(); i++) {
files.add(listOfFiles.get(i).getName());
}
}
return files;
}
private List<File> listFiles() { private List<File> listFiles() {
File folder = new File(_configDirectory); File folder = new File(_configDirectory);
if (!folder.isAbsolute()) if (!folder.isAbsolute())
@ -829,7 +817,7 @@ public class TunnelControllerGroup implements ClientApp {
// //
if (listOfFiles != null && listOfFiles.length > 0) { if (listOfFiles != null && listOfFiles.length > 0) {
for (File afile : listOfFiles) { for (File afile : listOfFiles) {
if (afile.isFile() && afile.exists()) { if (afile.isFile() && afile.exists() && afile.getName().endsWith(".config")) {
files.add(afile); files.add(afile);
} }
} }
@ -837,7 +825,7 @@ public class TunnelControllerGroup implements ClientApp {
File file = new File(_configFile); File file = new File(_configFile);
if (!file.isAbsolute()) if (!file.isAbsolute())
file = new File(_context.getConfigDir(), _configFile); file = new File(_context.getConfigDir(), _configFile);
if (file.exists() && file.isFile()) if (file.exists() && file.isFile() && file.getName().endsWith(".config"))
files.add(file); files.add(file);
return files; return files;
@ -854,8 +842,9 @@ public class TunnelControllerGroup implements ClientApp {
DataHelper.loadProps(config, cfgFile); DataHelper.loadProps(config, cfgFile);
List<Properties> rv = new ArrayList<Properties>(); List<Properties> rv = new ArrayList<Properties>();
int i = 0; int i = 0;
int maxFiles = listFiles().size();
while (true) { while (true) {
String prefix = PREFIX + i + '.'; String prefix = PREFIX + i + ".";
Properties p = new Properties(); Properties p = new Properties();
for (Map.Entry<Object, Object> e : config.entrySet()) { for (Map.Entry<Object, Object> e : config.entrySet()) {
String key = (String) e.getKey(); String key = (String) e.getKey();
@ -865,7 +854,10 @@ public class TunnelControllerGroup implements ClientApp {
p.setProperty(key, val); p.setProperty(key, val);
} }
} }
if (p.isEmpty()) // OK so I don't really like this, but working around the numbered
// tunnel configurations requires either something like this, or
// altering the config file format which is probably even worse. idk
if (p.isEmpty() && i > maxFiles)
break; break;
p.setProperty("configFile", cfgFile.getAbsolutePath()); p.setProperty("configFile", cfgFile.getAbsolutePath());
rv.add(p); rv.add(p);
@ -884,10 +876,10 @@ public class TunnelControllerGroup implements ClientApp {
* @throws IllegalArgumentException if unable to load config from file * @throws IllegalArgumentException if unable to load config from file
*/ */
public List<TunnelController> getControllers() { public List<TunnelController> getControllers() {
List<String> fileList = configFiles(); List<File> fileList = listFiles();
List<TunnelController> _tempControllers = new ArrayList<TunnelController>(); List<TunnelController> _tempControllers = new ArrayList<TunnelController>();
for (int i = 0; i < fileList.size(); i++) { for (File afile : fileList) {
String configFile = fileList.get(i); String configFile = afile.toString();
_tempControllers.addAll(getControllers(configFile)); _tempControllers.addAll(getControllers(configFile));
} }
return _tempControllers; return _tempControllers;