forked from I2P_Developers/i2p.i2p
* load clientApp.* lines from clients.config by default, falling back on
reading the router's props only if that file doesn't exist. * by default, only log CRIT messages to the screen (the rest are sent to the log file). this will be useful with the upcoming service controller * refactor a common Properties helper to DataHelper.loadProps
This commit is contained in:
@ -9,10 +9,15 @@ package net.i2p.data;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.math.BigInteger;
|
import java.math.BigInteger;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -129,6 +134,31 @@ public class DataHelper {
|
|||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A more efficient Properties.load
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public static void loadProps(Properties props, File file) throws IOException {
|
||||||
|
BufferedReader in = null;
|
||||||
|
try {
|
||||||
|
in = new BufferedReader(new InputStreamReader(new FileInputStream(file)), 16*1024);
|
||||||
|
String line = null;
|
||||||
|
while ( (line = in.readLine()) != null) {
|
||||||
|
if (line.trim().length() <= 0) continue;
|
||||||
|
if (line.charAt(0) == '#') continue;
|
||||||
|
int split = line.indexOf('=');
|
||||||
|
if (split <= 0) continue;
|
||||||
|
String key = line.substring(0, split);
|
||||||
|
String val = line.substring(split+1);
|
||||||
|
if ( (key.length() > 0) && (val.length() > 0) )
|
||||||
|
props.setProperty(key, val);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (in != null) try { in.close(); } catch (IOException ioe) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pretty print the collection
|
* Pretty print the collection
|
||||||
*
|
*
|
||||||
|
@ -59,7 +59,7 @@ public class LogManager {
|
|||||||
public final static int DEFAULT_CONSOLEBUFFERSIZE = 20;
|
public final static int DEFAULT_CONSOLEBUFFERSIZE = 20;
|
||||||
public final static String DEFAULT_ROTATIONLIMIT = "2";
|
public final static String DEFAULT_ROTATIONLIMIT = "2";
|
||||||
public final static String DEFAULT_DEFAULTLEVEL = Log.STR_ERROR;
|
public final static String DEFAULT_DEFAULTLEVEL = Log.STR_ERROR;
|
||||||
public final static String DEFAULT_ONSCREENLEVEL = Log.STR_ERROR;
|
public final static String DEFAULT_ONSCREENLEVEL = Log.STR_CRIT;
|
||||||
|
|
||||||
private I2PAppContext _context;
|
private I2PAppContext _context;
|
||||||
private Log _log;
|
private Log _log;
|
||||||
|
@ -183,10 +183,9 @@ public class Router {
|
|||||||
try {
|
try {
|
||||||
File f = new File(filename);
|
File f = new File(filename);
|
||||||
if (f.canRead()) {
|
if (f.canRead()) {
|
||||||
fis = new FileInputStream(f);
|
DataHelper.loadProps(props, f);
|
||||||
props.load(fis);
|
|
||||||
} else {
|
} else {
|
||||||
log.error("Configuration file " + filename + " does not exist");
|
log.warn("Configuration file " + filename + " does not exist");
|
||||||
}
|
}
|
||||||
} catch (Exception ioe) {
|
} catch (Exception ioe) {
|
||||||
log.error("Error loading the router configuration from " + filename, ioe);
|
log.error("Error loading the router configuration from " + filename, ioe);
|
||||||
|
@ -15,6 +15,7 @@ import java.util.Properties;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import net.i2p.data.DataFormatException;
|
import net.i2p.data.DataFormatException;
|
||||||
|
import net.i2p.data.DataHelper;
|
||||||
import net.i2p.data.Hash;
|
import net.i2p.data.Hash;
|
||||||
import net.i2p.router.RouterContext;
|
import net.i2p.router.RouterContext;
|
||||||
import net.i2p.util.Log;
|
import net.i2p.util.Log;
|
||||||
@ -212,26 +213,11 @@ class ProfilePersistenceHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void loadProps(Properties props, File file) {
|
private void loadProps(Properties props, File file) {
|
||||||
BufferedReader in = null;
|
|
||||||
try {
|
try {
|
||||||
in = new BufferedReader(new InputStreamReader(new FileInputStream(file)), 16*1024);
|
DataHelper.loadProps(props, file);
|
||||||
String line = null;
|
|
||||||
while ( (line = in.readLine()) != null) {
|
|
||||||
if (line.trim().length() <= 0) continue;
|
|
||||||
if (line.charAt(0) == '#') continue;
|
|
||||||
int split = line.indexOf('=');
|
|
||||||
if (split <= 0) continue;
|
|
||||||
String key = line.substring(0, split);
|
|
||||||
String val = line.substring(split+1);
|
|
||||||
if ( (key.length() > 0) && (val.length() > 0) )
|
|
||||||
props.setProperty(key, val);
|
|
||||||
}
|
|
||||||
} catch (IOException ioe) {
|
} catch (IOException ioe) {
|
||||||
_log.warn("Error loading properties from " + file.getName(), ioe);
|
_log.warn("Error loading properties from " + file.getName(), ioe);
|
||||||
} finally {
|
|
||||||
if (in != null) try { in.close(); } catch (IOException ioe) {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Hash getHash(String name) {
|
private Hash getHash(String name) {
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
package net.i2p.router.startup;
|
package net.i2p.router.startup;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import net.i2p.data.DataHelper;
|
||||||
import net.i2p.router.JobImpl;
|
import net.i2p.router.JobImpl;
|
||||||
import net.i2p.router.RouterContext;
|
import net.i2p.router.RouterContext;
|
||||||
import net.i2p.util.I2PThread;
|
import net.i2p.util.I2PThread;
|
||||||
@ -19,17 +23,22 @@ class LoadClientAppsJob extends JobImpl {
|
|||||||
private Log _log;
|
private Log _log;
|
||||||
/** wait 2 minutes before starting up client apps */
|
/** wait 2 minutes before starting up client apps */
|
||||||
private final static long STARTUP_DELAY = 2*60*1000;
|
private final static long STARTUP_DELAY = 2*60*1000;
|
||||||
|
|
||||||
|
private static final String PROP_CLIENT_CONFIG_FILENAME = "router.clientConfigFile";
|
||||||
|
private static final String DEFAULT_CLIENT_CONFIG_FILENAME = "clients.config";
|
||||||
|
|
||||||
public LoadClientAppsJob(RouterContext ctx) {
|
public LoadClientAppsJob(RouterContext ctx) {
|
||||||
super(ctx);
|
super(ctx);
|
||||||
_log = ctx.logManager().getLog(LoadClientAppsJob.class);
|
_log = ctx.logManager().getLog(LoadClientAppsJob.class);
|
||||||
}
|
}
|
||||||
public void runJob() {
|
public void runJob() {
|
||||||
|
Properties clientApps = getClientApps();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
String className = getContext().router().getConfigSetting("clientApp."+i+".main");
|
String className = clientApps.getProperty("clientApp."+i+".main");
|
||||||
String clientName = getContext().router().getConfigSetting("clientApp."+i+".name");
|
String clientName = clientApps.getProperty("clientApp."+i+".name");
|
||||||
String args = getContext().router().getConfigSetting("clientApp."+i+".args");
|
String args = clientApps.getProperty("clientApp."+i+".args");
|
||||||
String onBoot = getContext().router().getConfigSetting("clientApp." + i + ".onBoot");
|
String onBoot = clientApps.getProperty("clientApp." + i + ".onBoot");
|
||||||
boolean onStartup = false;
|
boolean onStartup = false;
|
||||||
if (onBoot != null)
|
if (onBoot != null)
|
||||||
onStartup = "true".equals(onBoot) || "yes".equals(onBoot);
|
onStartup = "true".equals(onBoot) || "yes".equals(onBoot);
|
||||||
@ -49,6 +58,24 @@ class LoadClientAppsJob extends JobImpl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Properties getClientApps() {
|
||||||
|
Properties rv = new Properties();
|
||||||
|
String clientConfigFile = getContext().getProperty(PROP_CLIENT_CONFIG_FILENAME, DEFAULT_CLIENT_CONFIG_FILENAME);
|
||||||
|
File cfgFile = new File(clientConfigFile);
|
||||||
|
|
||||||
|
// fall back to use router.config's clientApp.* lines
|
||||||
|
if (!cfgFile.exists())
|
||||||
|
return new Properties(getContext().router().getConfigMap());
|
||||||
|
|
||||||
|
try {
|
||||||
|
DataHelper.loadProps(rv, cfgFile);
|
||||||
|
} catch (IOException ioe) {
|
||||||
|
_log.warn("Error loading the client app properties from " + cfgFile.getName(), ioe);
|
||||||
|
}
|
||||||
|
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
private class DelayedRunClient extends JobImpl {
|
private class DelayedRunClient extends JobImpl {
|
||||||
private String _className;
|
private String _className;
|
||||||
private String _clientName;
|
private String _clientName;
|
||||||
|
Reference in New Issue
Block a user